blob: f75ea81d757a87c804c48dcaa4c3bdbfe5c63ce5 [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 {
846#ifdef FEAT_MBYTE
847 if (has_mbyte)
848 p += replace_push_mb(p);
849 else
850#endif
851 replace_push(*p++);
852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 saved_line[curwin->w_cursor.col] = NUL;
854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +0200856 if ((State & INSERT) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 {
858 p_extra = saved_line + curwin->w_cursor.col;
859#ifdef FEAT_SMARTINDENT
860 if (do_si) /* need first char after new line break */
861 {
862 p = skipwhite(p_extra);
863 first_char = *p;
864 }
865#endif
866#ifdef FEAT_COMMENTS
867 extra_len = (int)STRLEN(p_extra);
868#endif
869 saved_char = *p_extra;
870 *p_extra = NUL;
871 }
872
873 u_clearline(); /* cannot do "U" command when adding lines */
874#ifdef FEAT_SMARTINDENT
875 did_si = FALSE;
876#endif
877 ai_col = 0;
878
879 /*
880 * If we just did an auto-indent, then we didn't type anything on
881 * the prior line, and it should be truncated. Do this even if 'ai' is not
882 * set because automatically inserting a comment leader also sets did_ai.
883 */
884 if (dir == FORWARD && did_ai)
885 trunc_line = TRUE;
886
887 /*
888 * If 'autoindent' and/or 'smartindent' is set, try to figure out what
889 * indent to use for the new line.
890 */
891 if (curbuf->b_p_ai
892#ifdef FEAT_SMARTINDENT
893 || do_si
894#endif
895 )
896 {
897 /*
898 * count white space on current line
899 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200900#ifdef FEAT_VARTABS
901 newindent = get_indent_str_vtab(saved_line, curbuf->b_p_ts,
902 curbuf->b_p_vts_array, FALSE);
903#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200904 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200905#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200906 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
907 newindent = second_line_indent; /* for ^^D command in insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908
909#ifdef FEAT_SMARTINDENT
910 /*
911 * Do smart indenting.
912 * In insert/replace mode (only when dir == FORWARD)
913 * we may move some text to the next line. If it starts with '{'
914 * don't add an indent. Fixes inserting a NL before '{' in line
915 * "if (condition) {"
916 */
917 if (!trunc_line && do_si && *saved_line != NUL
918 && (p_extra == NULL || first_char != '{'))
919 {
920 char_u *ptr;
921 char_u last_char;
922
923 old_cursor = curwin->w_cursor;
924 ptr = saved_line;
925# ifdef FEAT_COMMENTS
926 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200927 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 else
929 lead_len = 0;
930# endif
931 if (dir == FORWARD)
932 {
933 /*
934 * Skip preprocessor directives, unless they are
935 * recognised as comments.
936 */
937 if (
938# ifdef FEAT_COMMENTS
939 lead_len == 0 &&
940# endif
941 ptr[0] == '#')
942 {
943 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
944 ptr = ml_get(--curwin->w_cursor.lnum);
945 newindent = get_indent();
946 }
947# ifdef FEAT_COMMENTS
948 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200949 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 else
951 lead_len = 0;
952 if (lead_len > 0)
953 {
954 /*
955 * This case gets the following right:
956 * \*
957 * * A comment (read '\' as '/').
958 * *\
959 * #define IN_THE_WAY
960 * This should line up here;
961 */
962 p = skipwhite(ptr);
963 if (p[0] == '/' && p[1] == '*')
964 p++;
965 if (p[0] == '*')
966 {
967 for (p++; *p; p++)
968 {
969 if (p[0] == '/' && p[-1] == '*')
970 {
971 /*
972 * End of C comment, indent should line up
973 * with the line containing the start of
974 * the comment
975 */
976 curwin->w_cursor.col = (colnr_T)(p - ptr);
977 if ((pos = findmatch(NULL, NUL)) != NULL)
978 {
979 curwin->w_cursor.lnum = pos->lnum;
980 newindent = get_indent();
981 }
982 }
983 }
984 }
985 }
986 else /* Not a comment line */
987# endif
988 {
989 /* Find last non-blank in line */
990 p = ptr + STRLEN(ptr) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100991 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 --p;
993 last_char = *p;
994
995 /*
996 * find the character just before the '{' or ';'
997 */
998 if (last_char == '{' || last_char == ';')
999 {
1000 if (p > ptr)
1001 --p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001002 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 --p;
1004 }
1005 /*
1006 * Try to catch lines that are split over multiple
1007 * lines. eg:
1008 * if (condition &&
1009 * condition) {
1010 * Should line up here!
1011 * }
1012 */
1013 if (*p == ')')
1014 {
1015 curwin->w_cursor.col = (colnr_T)(p - ptr);
1016 if ((pos = findmatch(NULL, '(')) != NULL)
1017 {
1018 curwin->w_cursor.lnum = pos->lnum;
1019 newindent = get_indent();
1020 ptr = ml_get_curline();
1021 }
1022 }
1023 /*
1024 * If last character is '{' do indent, without
1025 * checking for "if" and the like.
1026 */
1027 if (last_char == '{')
1028 {
1029 did_si = TRUE; /* do indent */
1030 no_si = TRUE; /* don't delete it when '{' typed */
1031 }
1032 /*
1033 * Look for "if" and the like, use 'cinwords'.
1034 * Don't do this if the previous line ended in ';' or
1035 * '}'.
1036 */
1037 else if (last_char != ';' && last_char != '}'
1038 && cin_is_cinword(ptr))
1039 did_si = TRUE;
1040 }
1041 }
1042 else /* dir == BACKWARD */
1043 {
1044 /*
1045 * Skip preprocessor directives, unless they are
1046 * recognised as comments.
1047 */
1048 if (
1049# ifdef FEAT_COMMENTS
1050 lead_len == 0 &&
1051# endif
1052 ptr[0] == '#')
1053 {
1054 int was_backslashed = FALSE;
1055
1056 while ((ptr[0] == '#' || was_backslashed) &&
1057 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
1058 {
1059 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
1060 was_backslashed = TRUE;
1061 else
1062 was_backslashed = FALSE;
1063 ptr = ml_get(++curwin->w_cursor.lnum);
1064 }
1065 if (was_backslashed)
1066 newindent = 0; /* Got to end of file */
1067 else
1068 newindent = get_indent();
1069 }
1070 p = skipwhite(ptr);
1071 if (*p == '}') /* if line starts with '}': do indent */
1072 did_si = TRUE;
1073 else /* can delete indent when '{' typed */
1074 can_si_back = TRUE;
1075 }
1076 curwin->w_cursor = old_cursor;
1077 }
1078 if (do_si)
1079 can_si = TRUE;
1080#endif /* FEAT_SMARTINDENT */
1081
1082 did_ai = TRUE;
1083 }
1084
1085#ifdef FEAT_COMMENTS
1086 /*
1087 * Find out if the current line starts with a comment leader.
1088 * This may then be inserted in front of the new line.
1089 */
1090 end_comment_pending = NUL;
1091 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +02001092 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 else
1094 lead_len = 0;
1095 if (lead_len > 0)
1096 {
1097 char_u *lead_repl = NULL; /* replaces comment leader */
1098 int lead_repl_len = 0; /* length of *lead_repl */
1099 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
1100 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
1101 char_u *comment_end = NULL; /* where lead_end has been found */
1102 int extra_space = FALSE; /* append extra space */
1103 int current_flag;
1104 int require_blank = FALSE; /* requires blank after middle */
1105 char_u *p2;
1106
1107 /*
1108 * If the comment leader has the start, middle or end flag, it may not
1109 * be used or may be replaced with the middle leader.
1110 */
1111 for (p = lead_flags; *p && *p != ':'; ++p)
1112 {
1113 if (*p == COM_BLANK)
1114 {
1115 require_blank = TRUE;
1116 continue;
1117 }
1118 if (*p == COM_START || *p == COM_MIDDLE)
1119 {
1120 current_flag = *p;
1121 if (*p == COM_START)
1122 {
1123 /*
1124 * Doing "O" on a start of comment does not insert leader.
1125 */
1126 if (dir == BACKWARD)
1127 {
1128 lead_len = 0;
1129 break;
1130 }
1131
1132 /* find start of middle part */
1133 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1134 require_blank = FALSE;
1135 }
1136
1137 /*
1138 * Isolate the strings of the middle and end leader.
1139 */
1140 while (*p && p[-1] != ':') /* find end of middle flags */
1141 {
1142 if (*p == COM_BLANK)
1143 require_blank = TRUE;
1144 ++p;
1145 }
1146 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1147
1148 while (*p && p[-1] != ':') /* find end of end flags */
1149 {
1150 /* Check whether we allow automatic ending of comments */
1151 if (*p == COM_AUTO_END)
1152 end_comment_pending = -1; /* means we want to set it */
1153 ++p;
1154 }
1155 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
1156
1157 if (end_comment_pending == -1) /* we can set it now */
1158 end_comment_pending = lead_end[n - 1];
1159
1160 /*
1161 * If the end of the comment is in the same line, don't use
1162 * the comment leader.
1163 */
1164 if (dir == FORWARD)
1165 {
1166 for (p = saved_line + lead_len; *p; ++p)
1167 if (STRNCMP(p, lead_end, n) == 0)
1168 {
1169 comment_end = p;
1170 lead_len = 0;
1171 break;
1172 }
1173 }
1174
1175 /*
1176 * Doing "o" on a start of comment inserts the middle leader.
1177 */
1178 if (lead_len > 0)
1179 {
1180 if (current_flag == COM_START)
1181 {
1182 lead_repl = lead_middle;
1183 lead_repl_len = (int)STRLEN(lead_middle);
1184 }
1185
1186 /*
1187 * If we have hit RETURN immediately after the start
1188 * comment leader, then put a space after the middle
1189 * comment leader on the next line.
1190 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001191 if (!VIM_ISWHITE(saved_line[lead_len - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 && ((p_extra != NULL
1193 && (int)curwin->w_cursor.col == lead_len)
1194 || (p_extra == NULL
1195 && saved_line[lead_len] == NUL)
1196 || require_blank))
1197 extra_space = TRUE;
1198 }
1199 break;
1200 }
1201 if (*p == COM_END)
1202 {
1203 /*
1204 * Doing "o" on the end of a comment does not insert leader.
1205 * Remember where the end is, might want to use it to find the
1206 * start (for C-comments).
1207 */
1208 if (dir == FORWARD)
1209 {
1210 comment_end = skipwhite(saved_line);
1211 lead_len = 0;
1212 break;
1213 }
1214
1215 /*
1216 * Doing "O" on the end of a comment inserts the middle leader.
1217 * Find the string for the middle leader, searching backwards.
1218 */
1219 while (p > curbuf->b_p_com && *p != ',')
1220 --p;
1221 for (lead_repl = p; lead_repl > curbuf->b_p_com
1222 && lead_repl[-1] != ':'; --lead_repl)
1223 ;
1224 lead_repl_len = (int)(p - lead_repl);
1225
1226 /* We can probably always add an extra space when doing "O" on
1227 * the comment-end */
1228 extra_space = TRUE;
1229
1230 /* Check whether we allow automatic ending of comments */
1231 for (p2 = p; *p2 && *p2 != ':'; p2++)
1232 {
1233 if (*p2 == COM_AUTO_END)
1234 end_comment_pending = -1; /* means we want to set it */
1235 }
1236 if (end_comment_pending == -1)
1237 {
1238 /* Find last character in end-comment string */
1239 while (*p2 && *p2 != ',')
1240 p2++;
1241 end_comment_pending = p2[-1];
1242 }
1243 break;
1244 }
1245 if (*p == COM_FIRST)
1246 {
1247 /*
1248 * Comment leader for first line only: Don't repeat leader
1249 * when using "O", blank out leader when using "o".
1250 */
1251 if (dir == BACKWARD)
1252 lead_len = 0;
1253 else
1254 {
1255 lead_repl = (char_u *)"";
1256 lead_repl_len = 0;
1257 }
1258 break;
1259 }
1260 }
1261 if (lead_len)
1262 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001263 /* allocate buffer (may concatenate p_extra later) */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001264 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001265 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 allocated = leader; /* remember to free it later */
1267
1268 if (leader == NULL)
1269 lead_len = 0;
1270 else
1271 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001272 vim_strncpy(leader, saved_line, lead_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273
1274 /*
1275 * Replace leader with lead_repl, right or left adjusted
1276 */
1277 if (lead_repl != NULL)
1278 {
1279 int c = 0;
1280 int off = 0;
1281
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001282 for (p = lead_flags; *p != NUL && *p != ':'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 {
1284 if (*p == COM_RIGHT || *p == COM_LEFT)
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001285 c = *p++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 else if (VIM_ISDIGIT(*p) || *p == '-')
1287 off = getdigits(&p);
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001288 else
1289 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 }
1291 if (c == COM_RIGHT) /* right adjusted leader */
1292 {
1293 /* find last non-white in the leader to line up with */
1294 for (p = leader + lead_len - 1; p > leader
Bram Moolenaar1c465442017-03-12 20:10:05 +01001295 && VIM_ISWHITE(*p); --p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 ++p;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001298
1299#ifdef FEAT_MBYTE
1300 /* Compute the length of the replaced characters in
1301 * screen characters, not bytes. */
1302 {
1303 int repl_size = vim_strnsize(lead_repl,
1304 lead_repl_len);
1305 int old_size = 0;
1306 char_u *endp = p;
1307 int l;
1308
1309 while (old_size < repl_size && p > leader)
1310 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001311 MB_PTR_BACK(leader, p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001312 old_size += ptr2cells(p);
1313 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001314 l = lead_repl_len - (int)(endp - p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001315 if (l != 0)
1316 mch_memmove(endp + l, endp,
1317 (size_t)((leader + lead_len) - endp));
1318 lead_len += l;
1319 }
1320#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 if (p < leader + lead_repl_len)
1322 p = leader;
1323 else
1324 p -= lead_repl_len;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1327 if (p + lead_repl_len > leader + lead_len)
1328 p[lead_repl_len] = NUL;
1329
1330 /* blank-out any other chars from the old leader. */
1331 while (--p >= leader)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001332 {
1333#ifdef FEAT_MBYTE
1334 int l = mb_head_off(leader, p);
1335
1336 if (l > 1)
1337 {
1338 p -= l;
1339 if (ptr2cells(p) > 1)
1340 {
1341 p[1] = ' ';
1342 --l;
1343 }
1344 mch_memmove(p + 1, p + l + 1,
1345 (size_t)((leader + lead_len) - (p + l + 1)));
1346 lead_len -= l;
1347 *p = ' ';
1348 }
1349 else
1350#endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01001351 if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 }
1355 else /* left adjusted leader */
1356 {
1357 p = skipwhite(leader);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001358#ifdef FEAT_MBYTE
1359 /* Compute the length of the replaced characters in
1360 * screen characters, not bytes. Move the part that is
1361 * not to be overwritten. */
1362 {
1363 int repl_size = vim_strnsize(lead_repl,
1364 lead_repl_len);
1365 int i;
1366 int l;
1367
Bram Moolenaardc633cf2016-04-23 14:33:19 +02001368 for (i = 0; i < lead_len && p[i] != NUL; i += l)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001369 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001370 l = (*mb_ptr2len)(p + i);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001371 if (vim_strnsize(p, i + l) > repl_size)
1372 break;
1373 }
1374 if (i != lead_repl_len)
1375 {
1376 mch_memmove(p + lead_repl_len, p + i,
Bram Moolenaar2d7ff052009-11-17 15:08:26 +00001377 (size_t)(lead_len - i - (p - leader)));
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001378 lead_len += lead_repl_len - i;
1379 }
1380 }
1381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1383
1384 /* Replace any remaining non-white chars in the old
1385 * leader by spaces. Keep Tabs, the indent must
1386 * remain the same. */
1387 for (p += lead_repl_len; p < leader + lead_len; ++p)
Bram Moolenaar1c465442017-03-12 20:10:05 +01001388 if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 {
1390 /* Don't put a space before a TAB. */
1391 if (p + 1 < leader + lead_len && p[1] == TAB)
1392 {
1393 --lead_len;
1394 mch_memmove(p, p + 1,
1395 (leader + lead_len) - p);
1396 }
1397 else
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001398 {
1399#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001400 int l = (*mb_ptr2len)(p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001401
1402 if (l > 1)
1403 {
1404 if (ptr2cells(p) > 1)
1405 {
1406 /* Replace a double-wide char with
1407 * two spaces */
1408 --l;
1409 *p++ = ' ';
1410 }
1411 mch_memmove(p + 1, p + l,
1412 (leader + lead_len) - p);
1413 lead_len -= l - 1;
1414 }
1415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 }
1419 *p = NUL;
1420 }
1421
1422 /* Recompute the indent, it may have changed. */
1423 if (curbuf->b_p_ai
1424#ifdef FEAT_SMARTINDENT
1425 || do_si
1426#endif
1427 )
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001428#ifdef FEAT_VARTABS
1429 newindent = get_indent_str_vtab(leader, curbuf->b_p_ts,
1430 curbuf->b_p_vts_array, FALSE);
1431#else
1432 newindent = get_indent_str(leader,
1433 (int)curbuf->b_p_ts, FALSE);
1434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435
1436 /* Add the indent offset */
1437 if (newindent + off < 0)
1438 {
1439 off = -newindent;
1440 newindent = 0;
1441 }
1442 else
1443 newindent += off;
1444
1445 /* Correct trailing spaces for the shift, so that
1446 * alignment remains equal. */
1447 while (off > 0 && lead_len > 0
1448 && leader[lead_len - 1] == ' ')
1449 {
1450 /* Don't do it when there is a tab before the space */
1451 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1452 break;
1453 --lead_len;
1454 --off;
1455 }
1456
1457 /* If the leader ends in white space, don't add an
1458 * extra space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001459 if (lead_len > 0 && VIM_ISWHITE(leader[lead_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 extra_space = FALSE;
1461 leader[lead_len] = NUL;
1462 }
1463
1464 if (extra_space)
1465 {
1466 leader[lead_len++] = ' ';
1467 leader[lead_len] = NUL;
1468 }
1469
1470 newcol = lead_len;
1471
1472 /*
1473 * if a new indent will be set below, remove the indent that
1474 * is in the comment leader
1475 */
1476 if (newindent
1477#ifdef FEAT_SMARTINDENT
1478 || did_si
1479#endif
1480 )
1481 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001482 while (lead_len && VIM_ISWHITE(*leader))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 {
1484 --lead_len;
1485 --newcol;
1486 ++leader;
1487 }
1488 }
1489
1490 }
1491#ifdef FEAT_SMARTINDENT
1492 did_si = can_si = FALSE;
1493#endif
1494 }
1495 else if (comment_end != NULL)
1496 {
1497 /*
1498 * We have finished a comment, so we don't use the leader.
1499 * If this was a C-comment and 'ai' or 'si' is set do a normal
1500 * indent to align with the line containing the start of the
1501 * comment.
1502 */
1503 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1504 (curbuf->b_p_ai
1505#ifdef FEAT_SMARTINDENT
1506 || do_si
1507#endif
1508 ))
1509 {
1510 old_cursor = curwin->w_cursor;
1511 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1512 if ((pos = findmatch(NULL, NUL)) != NULL)
1513 {
1514 curwin->w_cursor.lnum = pos->lnum;
1515 newindent = get_indent();
1516 }
1517 curwin->w_cursor = old_cursor;
1518 }
1519 }
1520 }
1521#endif
1522
1523 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */
1524 if (p_extra != NULL)
1525 {
1526 *p_extra = saved_char; /* restore char that NUL replaced */
1527
1528 /*
1529 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
1530 * non-blank.
1531 *
1532 * When in REPLACE mode, put the deleted blanks on the replace stack,
1533 * preceded by a NUL, so they can be put back when a BS is entered.
1534 */
1535 if (REPLACE_NORMAL(State))
1536 replace_push(NUL); /* end of extra blanks */
1537 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
1538 {
1539 while ((*p_extra == ' ' || *p_extra == '\t')
1540#ifdef FEAT_MBYTE
1541 && (!enc_utf8
1542 || !utf_iscomposing(utf_ptr2char(p_extra + 1)))
1543#endif
1544 )
1545 {
1546 if (REPLACE_NORMAL(State))
1547 replace_push(*p_extra);
1548 ++p_extra;
1549 ++less_cols_off;
1550 }
1551 }
1552 if (*p_extra != NUL)
1553 did_ai = FALSE; /* append some text, don't truncate now */
1554
1555 /* columns for marks adjusted for removed columns */
1556 less_cols = (int)(p_extra - saved_line);
1557 }
1558
1559 if (p_extra == NULL)
1560 p_extra = (char_u *)""; /* append empty line */
1561
1562#ifdef FEAT_COMMENTS
1563 /* concatenate leader and p_extra, if there is a leader */
1564 if (lead_len)
1565 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001566 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
1567 {
1568 int i;
Bram Moolenaar36105782012-06-14 20:59:25 +02001569 int padding = second_line_indent
1570 - (newindent + (int)STRLEN(leader));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001571
1572 /* Here whitespace is inserted after the comment char.
1573 * Below, set_indent(newindent, SIN_INSERT) will insert the
1574 * whitespace needed before the comment char. */
1575 for (i = 0; i < padding; i++)
1576 {
1577 STRCAT(leader, " ");
Bram Moolenaar5fb9ec52012-07-25 16:10:03 +02001578 less_cols--;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001579 newcol++;
1580 }
1581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 STRCAT(leader, p_extra);
1583 p_extra = leader;
1584 did_ai = TRUE; /* So truncating blanks works with comments */
1585 less_cols -= lead_len;
1586 }
1587 else
1588 end_comment_pending = NUL; /* turns out there was no leader */
1589#endif
1590
1591 old_cursor = curwin->w_cursor;
1592 if (dir == BACKWARD)
1593 --curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 {
1596 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
1597 == FAIL)
1598 goto theend;
1599 /* Postpone calling changed_lines(), because it would mess up folding
Bram Moolenaar82faa252016-06-04 20:14:07 +02001600 * with markers.
1601 * Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01001602 * be marks there. But still needed in diff mode. */
1603 if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count
1604#ifdef FEAT_DIFF
1605 || curwin->w_p_diff
1606#endif
1607 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02001608 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 did_append = TRUE;
1610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 else
1612 {
1613 /*
1614 * In VREPLACE mode we are starting to replace the next line.
1615 */
1616 curwin->w_cursor.lnum++;
1617 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1618 {
1619 /* In case we NL to a new line, BS to the previous one, and NL
1620 * again, we don't want to save the new line for undo twice.
1621 */
1622 (void)u_save_cursor(); /* errors are ignored! */
1623 vr_lines_changed++;
1624 }
1625 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1626 changed_bytes(curwin->w_cursor.lnum, 0);
1627 curwin->w_cursor.lnum--;
1628 did_append = FALSE;
1629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630
1631 if (newindent
1632#ifdef FEAT_SMARTINDENT
1633 || did_si
1634#endif
1635 )
1636 {
1637 ++curwin->w_cursor.lnum;
1638#ifdef FEAT_SMARTINDENT
1639 if (did_si)
1640 {
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001641 int sw = (int)get_sw_value(curbuf);
Bram Moolenaar14f24742012-08-08 18:01:05 +02001642
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001644 newindent -= newindent % sw;
1645 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 }
1647#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001648 /* Copy the indent */
1649 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 {
1651 (void)copy_indent(newindent, saved_line);
1652
1653 /*
1654 * Set the 'preserveindent' option so that any further screwing
1655 * with the line doesn't entirely destroy our efforts to preserve
1656 * it. It gets restored at the function end.
1657 */
1658 curbuf->b_p_pi = TRUE;
1659 }
1660 else
1661 (void)set_indent(newindent, SIN_INSERT);
1662 less_cols -= curwin->w_cursor.col;
1663
1664 ai_col = curwin->w_cursor.col;
1665
1666 /*
1667 * In REPLACE mode, for each character in the new indent, there must
1668 * be a NUL on the replace stack, for when it is deleted with BS
1669 */
1670 if (REPLACE_NORMAL(State))
1671 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1672 replace_push(NUL);
1673 newcol += curwin->w_cursor.col;
1674#ifdef FEAT_SMARTINDENT
1675 if (no_si)
1676 did_si = FALSE;
1677#endif
1678 }
1679
1680#ifdef FEAT_COMMENTS
1681 /*
1682 * In REPLACE mode, for each character in the extra leader, there must be
1683 * a NUL on the replace stack, for when it is deleted with BS.
1684 */
1685 if (REPLACE_NORMAL(State))
1686 while (lead_len-- > 0)
1687 replace_push(NUL);
1688#endif
1689
1690 curwin->w_cursor = old_cursor;
1691
1692 if (dir == FORWARD)
1693 {
1694 if (trunc_line || (State & INSERT))
1695 {
1696 /* truncate current line at cursor */
1697 saved_line[curwin->w_cursor.col] = NUL;
1698 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1699 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1700 truncate_spaces(saved_line);
1701 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1702 saved_line = NULL;
1703 if (did_append)
1704 {
1705 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1706 curwin->w_cursor.lnum + 1, 1L);
1707 did_append = FALSE;
1708
1709 /* Move marks after the line break to the new line. */
1710 if (flags & OPENLINE_MARKFIX)
1711 mark_col_adjust(curwin->w_cursor.lnum,
1712 curwin->w_cursor.col + less_cols_off,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01001713 1L, (long)-less_cols, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 }
1715 else
1716 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1717 }
1718
1719 /*
1720 * Put the cursor on the new line. Careful: the scrollup() above may
1721 * have moved w_cursor, we must use old_cursor.
1722 */
1723 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1724 }
1725 if (did_append)
1726 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1727
1728 curwin->w_cursor.col = newcol;
1729#ifdef FEAT_VIRTUALEDIT
1730 curwin->w_cursor.coladd = 0;
1731#endif
1732
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02001733#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 /*
1735 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1736 * fixthisline() from doing it (via change_indent()) by telling it we're in
1737 * normal INSERT mode.
1738 */
1739 if (State & VREPLACE_FLAG)
1740 {
1741 vreplace_mode = State; /* So we know to put things right later */
1742 State = INSERT;
1743 }
1744 else
1745 vreplace_mode = 0;
1746#endif
1747#ifdef FEAT_LISP
1748 /*
1749 * May do lisp indenting.
1750 */
1751 if (!p_paste
1752# ifdef FEAT_COMMENTS
1753 && leader == NULL
1754# endif
1755 && curbuf->b_p_lisp
1756 && curbuf->b_p_ai)
1757 {
1758 fixthisline(get_lisp_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
1762#ifdef FEAT_CINDENT
1763 /*
1764 * May do indenting after opening a new line.
1765 */
1766 if (!p_paste
1767 && (curbuf->b_p_cin
1768# ifdef FEAT_EVAL
1769 || *curbuf->b_p_inde != NUL
1770# endif
1771 )
1772 && in_cinkeys(dir == FORWARD
1773 ? KEY_OPEN_FORW
1774 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1775 {
1776 do_c_expr_indent();
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001777 ai_col = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 }
1779#endif
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02001780#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 if (vreplace_mode != 0)
1782 State = vreplace_mode;
1783#endif
1784
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 /*
1786 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1787 * original line, and inserts the new stuff char by char, pushing old stuff
1788 * onto the replace stack (via ins_char()).
1789 */
1790 if (State & VREPLACE_FLAG)
1791 {
1792 /* Put new line in p_extra */
1793 p_extra = vim_strsave(ml_get_curline());
1794 if (p_extra == NULL)
1795 goto theend;
1796
1797 /* Put back original line */
1798 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1799
1800 /* Insert new stuff into line again */
1801 curwin->w_cursor.col = 0;
1802#ifdef FEAT_VIRTUALEDIT
1803 curwin->w_cursor.coladd = 0;
1804#endif
1805 ins_bytes(p_extra); /* will call changed_bytes() */
1806 vim_free(p_extra);
1807 next_line = NULL;
1808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001810 retval = OK; /* success! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811theend:
1812 curbuf->b_p_pi = saved_pi;
1813 vim_free(saved_line);
1814 vim_free(next_line);
1815 vim_free(allocated);
1816 return retval;
1817}
1818
1819#if defined(FEAT_COMMENTS) || defined(PROTO)
1820/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +02001821 * get_leader_len() returns the length in bytes of the prefix of the given
1822 * string which introduces a comment. If this string is not a comment then
1823 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 * When "flags" is not NULL, it is set to point to the flags of the recognized
1825 * comment leader.
1826 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001827 * If "include_space" is set, include trailing whitespace while calculating the
1828 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 */
1830 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001831get_leader_len(
1832 char_u *line,
1833 char_u **flags,
1834 int backward,
1835 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836{
1837 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001838 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 int got_com = FALSE;
1840 int found_one;
1841 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1842 char_u *string; /* pointer to comment string */
1843 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001844 int middle_match_len = 0;
1845 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001846 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847
Bram Moolenaar81340392012-06-06 16:12:59 +02001848 result = i = 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001849 while (VIM_ISWHITE(line[i])) /* leading white space is ignored */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 ++i;
1851
1852 /*
1853 * Repeat to match several nested comment strings.
1854 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001855 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 {
1857 /*
1858 * scan through the 'comments' option for a match
1859 */
1860 found_one = FALSE;
1861 for (list = curbuf->b_p_com; *list; )
1862 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001863 /* Get one option part into part_buf[]. Advance "list" to next
1864 * one. Put "string" at start of string. */
1865 if (!got_com && flags != NULL)
1866 *flags = list; /* remember where flags started */
1867 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1869 string = vim_strchr(part_buf, ':');
1870 if (string == NULL) /* missing ':', ignore this part */
1871 continue;
1872 *string++ = NUL; /* isolate flags from string */
1873
Bram Moolenaara4271d52011-05-10 13:38:27 +02001874 /* If we found a middle match previously, use that match when this
1875 * is not a middle or end. */
1876 if (middle_match_len != 0
1877 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1878 && vim_strchr(part_buf, COM_END) == NULL)
1879 break;
1880
1881 /* When we already found a nested comment, only accept further
1882 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1884 continue;
1885
Bram Moolenaara4271d52011-05-10 13:38:27 +02001886 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1888 continue;
1889
Bram Moolenaara4271d52011-05-10 13:38:27 +02001890 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 * When string starts with white space, must have some white space
1892 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001893 * TABs and spaces). */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001894 if (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001896 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001897 continue; /* missing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001898 while (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 ++string;
1900 }
1901 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1902 ;
1903 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001904 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905
Bram Moolenaara4271d52011-05-10 13:38:27 +02001906 /* When 'b' flag used, there must be white space or an
1907 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001909 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 continue;
1911
Bram Moolenaara4271d52011-05-10 13:38:27 +02001912 /* We have found a match, stop searching unless this is a middle
1913 * comment. The middle comment can be a substring of the end
1914 * comment in which case it's better to return the length of the
1915 * end comment and its flags. Thus we keep searching with middle
1916 * and end matches and use an end match if it matches better. */
1917 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1918 {
1919 if (middle_match_len == 0)
1920 {
1921 middle_match_len = j;
1922 saved_flags = prev_list;
1923 }
1924 continue;
1925 }
1926 if (middle_match_len != 0 && j > middle_match_len)
1927 /* Use this match instead of the middle match, since it's a
1928 * longer thus better match. */
1929 middle_match_len = 0;
1930
1931 if (middle_match_len == 0)
1932 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 found_one = TRUE;
1934 break;
1935 }
1936
Bram Moolenaara4271d52011-05-10 13:38:27 +02001937 if (middle_match_len != 0)
1938 {
1939 /* Use the previously found middle match after failing to find a
1940 * match with an end. */
1941 if (!got_com && flags != NULL)
1942 *flags = saved_flags;
1943 i += middle_match_len;
1944 found_one = TRUE;
1945 }
1946
1947 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 if (!found_one)
1949 break;
1950
Bram Moolenaar81340392012-06-06 16:12:59 +02001951 result = i;
1952
Bram Moolenaara4271d52011-05-10 13:38:27 +02001953 /* Include any trailing white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001954 while (VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 ++i;
1956
Bram Moolenaar81340392012-06-06 16:12:59 +02001957 if (include_space)
1958 result = i;
1959
Bram Moolenaara4271d52011-05-10 13:38:27 +02001960 /* If this comment doesn't nest, stop here. */
1961 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 if (vim_strchr(part_buf, COM_NEST) == NULL)
1963 break;
1964 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001965 return result;
1966}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001967
Bram Moolenaar81340392012-06-06 16:12:59 +02001968/*
1969 * Return the offset at which the last comment in line starts. If there is no
1970 * comment in the whole line, -1 is returned.
1971 *
1972 * When "flags" is not null, it is set to point to the flags describing the
1973 * recognized comment leader.
1974 */
1975 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001976get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +02001977{
1978 int result = -1;
1979 int i, j;
1980 int lower_check_bound = 0;
1981 char_u *string;
1982 char_u *com_leader;
1983 char_u *com_flags;
1984 char_u *list;
1985 int found_one;
1986 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1987
1988 /*
1989 * Repeat to match several nested comment strings.
1990 */
1991 i = (int)STRLEN(line);
1992 while (--i >= lower_check_bound)
1993 {
1994 /*
1995 * scan through the 'comments' option for a match
1996 */
1997 found_one = FALSE;
1998 for (list = curbuf->b_p_com; *list; )
1999 {
2000 char_u *flags_save = list;
2001
2002 /*
2003 * Get one option part into part_buf[]. Advance list to next one.
2004 * put string at start of string.
2005 */
2006 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
2007 string = vim_strchr(part_buf, ':');
2008 if (string == NULL) /* If everything is fine, this cannot actually
2009 * happen. */
2010 {
2011 continue;
2012 }
2013 *string++ = NUL; /* Isolate flags from string. */
2014 com_leader = string;
2015
2016 /*
2017 * Line contents and string must match.
2018 * When string starts with white space, must have some white space
2019 * (but the amount does not need to match, there might be a mix of
2020 * TABs and spaces).
2021 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002022 if (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +02002023 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002024 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar81340392012-06-06 16:12:59 +02002025 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +01002026 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +02002027 ++string;
2028 }
2029 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
2030 /* do nothing */;
2031 if (string[j] != NUL)
2032 continue;
2033
2034 /*
2035 * When 'b' flag used, there must be white space or an
2036 * end-of-line after the string in the line.
2037 */
2038 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002039 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar81340392012-06-06 16:12:59 +02002040 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +01002041
Bram Moolenaar4af72592018-12-09 15:00:52 +01002042 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
Bram Moolenaar53932812018-12-07 21:08:49 +01002043 {
Bram Moolenaar4af72592018-12-09 15:00:52 +01002044 // For a middlepart comment, only consider it to match if
2045 // everything before the current position in the line is
2046 // whitespace. Otherwise we would think we are inside a
2047 // comment if the middle part appears somewhere in the middle
2048 // of the line. E.g. for C the "*" appears often.
Bram Moolenaar53932812018-12-07 21:08:49 +01002049 for (j = 0; VIM_ISWHITE(line[j]) && j <= i; j++)
2050 ;
2051 if (j < i)
2052 continue;
Bram Moolenaar81340392012-06-06 16:12:59 +02002053 }
2054
2055 /*
2056 * We have found a match, stop searching.
2057 */
2058 found_one = TRUE;
2059
2060 if (flags)
2061 *flags = flags_save;
2062 com_flags = flags_save;
2063
2064 break;
2065 }
2066
2067 if (found_one)
2068 {
2069 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
2070 int len1, len2, off;
2071
2072 result = i;
2073 /*
2074 * If this comment nests, continue searching.
2075 */
2076 if (vim_strchr(part_buf, COM_NEST) != NULL)
2077 continue;
2078
2079 lower_check_bound = i;
2080
2081 /* Let's verify whether the comment leader found is a substring
2082 * of other comment leaders. If it is, let's adjust the
2083 * lower_check_bound so that we make sure that we have determined
2084 * the comment leader correctly.
2085 */
2086
Bram Moolenaar1c465442017-03-12 20:10:05 +01002087 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +02002088 ++com_leader;
2089 len1 = (int)STRLEN(com_leader);
2090
2091 for (list = curbuf->b_p_com; *list; )
2092 {
2093 char_u *flags_save = list;
2094
2095 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
2096 if (flags_save == com_flags)
2097 continue;
2098 string = vim_strchr(part_buf2, ':');
2099 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002100 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +02002101 ++string;
2102 len2 = (int)STRLEN(string);
2103 if (len2 == 0)
2104 continue;
2105
2106 /* Now we have to verify whether string ends with a substring
2107 * beginning the com_leader. */
2108 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
2109 {
2110 --off;
2111 if (!STRNCMP(string + off, com_leader, len2 - off))
2112 {
2113 if (i - off < lower_check_bound)
2114 lower_check_bound = i - off;
2115 }
2116 }
2117 }
2118 }
2119 }
2120 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121}
2122#endif
2123
2124/*
2125 * Return the number of window lines occupied by buffer line "lnum".
2126 */
2127 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002128plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129{
2130 return plines_win(curwin, lnum, TRUE);
2131}
2132
2133 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002134plines_win(
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#if defined(FEAT_DIFF) || defined(PROTO)
2140 /* Check for filler lines above this buffer line. When folded the result
2141 * is one line anyway. */
2142 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
2143}
2144
2145 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002146plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147{
2148 return plines_win_nofill(curwin, lnum, TRUE);
2149}
2150
2151 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002152plines_win_nofill(
2153 win_T *wp,
2154 linenr_T lnum,
2155 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156{
2157#endif
2158 int lines;
2159
2160 if (!wp->w_p_wrap)
2161 return 1;
2162
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 if (wp->w_width == 0)
2164 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165
2166#ifdef FEAT_FOLDING
2167 /* A folded lines is handled just like an empty line. */
2168 /* NOTE: Caller must handle lines that are MAYBE folded. */
2169 if (lineFolded(wp, lnum) == TRUE)
2170 return 1;
2171#endif
2172
2173 lines = plines_win_nofold(wp, lnum);
2174 if (winheight > 0 && lines > wp->w_height)
2175 return (int)wp->w_height;
2176 return lines;
2177}
2178
2179/*
2180 * Return number of window lines physical line "lnum" will occupy in window
2181 * "wp". Does not care about folding, 'wrap' or 'diff'.
2182 */
2183 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002184plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185{
2186 char_u *s;
2187 long col;
2188 int width;
2189
2190 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2191 if (*s == NUL) /* empty line */
2192 return 1;
2193 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
2194
2195 /*
2196 * If list mode is on, then the '$' at the end of the line may take up one
2197 * extra column.
2198 */
2199 if (wp->w_p_list && lcs_eol != NUL)
2200 col += 1;
2201
2202 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002203 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002205 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 if (width <= 0)
2207 return 32000;
2208 if (col <= width)
2209 return 1;
2210 col -= width;
2211 width += win_col_off2(wp);
2212 return (col + (width - 1)) / width + 1;
2213}
2214
2215/*
2216 * Like plines_win(), but only reports the number of physical screen lines
2217 * used from the start of the line to the given column number.
2218 */
2219 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002220plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221{
2222 long col;
2223 char_u *s;
2224 int lines = 0;
2225 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002226 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227
2228#ifdef FEAT_DIFF
2229 /* Check for filler lines above this buffer line. When folded the result
2230 * is one line anyway. */
2231 lines = diff_check_fill(wp, lnum);
2232#endif
2233
2234 if (!wp->w_p_wrap)
2235 return lines + 1;
2236
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 if (wp->w_width == 0)
2238 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239
Bram Moolenaar597a4222014-06-25 14:39:50 +02002240 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241
2242 col = 0;
2243 while (*s != NUL && --column >= 0)
2244 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02002245 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002246 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 }
2248
2249 /*
2250 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2251 * INSERT mode, then col must be adjusted so that it represents the last
2252 * screen position of the TAB. This only fixes an error when the TAB wraps
2253 * from one screen line to the next (when 'columns' is not a multiple of
2254 * 'ts') -- webb.
2255 */
2256 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02002257 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258
2259 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002260 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002262 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002263 if (width <= 0)
2264 return 9999;
2265
2266 lines += 1;
2267 if (col > width)
2268 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2269 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270}
2271
2272 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002273plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274{
2275 int count = 0;
2276
2277 while (first <= last)
2278 {
2279#ifdef FEAT_FOLDING
2280 int x;
2281
2282 /* Check if there are any really folded lines, but also included lines
2283 * that are maybe folded. */
2284 x = foldedCount(wp, first, NULL);
2285 if (x > 0)
2286 {
2287 ++count; /* count 1 for "+-- folded" line */
2288 first += x;
2289 }
2290 else
2291#endif
2292 {
2293#ifdef FEAT_DIFF
2294 if (first == wp->w_topline)
2295 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2296 else
2297#endif
2298 count += plines_win(wp, first, TRUE);
2299 ++first;
2300 }
2301 }
2302 return (count);
2303}
2304
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305/*
2306 * Insert string "p" at the cursor position. Stops at a NUL byte.
2307 * Handles Replace mode and multi-byte characters.
2308 */
2309 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002310ins_bytes(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311{
2312 ins_bytes_len(p, (int)STRLEN(p));
2313}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315/*
2316 * Insert string "p" with length "len" at the cursor position.
2317 * Handles Replace mode and multi-byte characters.
2318 */
2319 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002320ins_bytes_len(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321{
2322 int i;
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02002323#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 int n;
2325
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002326 if (has_mbyte)
2327 for (i = 0; i < len; i += n)
2328 {
2329 if (enc_utf8)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002330 // avoid reading past p[len]
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002331 n = utfc_ptr2len_len(p + i, len - i);
2332 else
2333 n = (*mb_ptr2len)(p + i);
2334 ins_char_bytes(p + i, n);
2335 }
2336 else
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02002337#endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002338 for (i = 0; i < len; ++i)
2339 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341
2342/*
2343 * Insert or replace a single character at the cursor position.
2344 * When in REPLACE or VREPLACE mode, replace any existing character.
2345 * Caller must have prepared for undo.
2346 * For multi-byte characters we get the whole character, the caller must
2347 * convert bytes to a character.
2348 */
2349 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002350ins_char(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002352 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar560379d2017-01-21 22:50:00 +01002353 int n = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354
Bram Moolenaar6a8ede92017-01-22 19:49:12 +01002355#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 n = (*mb_char2bytes)(c, buf);
2357
2358 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2359 * Happens for CTRL-Vu9900. */
2360 if (buf[0] == 0)
2361 buf[0] = '\n';
Bram Moolenaar560379d2017-01-21 22:50:00 +01002362#else
2363 buf[0] = c;
2364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365
2366 ins_char_bytes(buf, n);
2367}
2368
2369 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002370ins_char_bytes(char_u *buf, int charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371{
2372 int c = buf[0];
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002373 int newlen; // nr of bytes inserted
2374 int oldlen; // nr of bytes deleted (0 when not replacing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 char_u *p;
2376 char_u *newp;
2377 char_u *oldp;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002378 int linelen; // length of old line including NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 colnr_T col;
2380 linenr_T lnum = curwin->w_cursor.lnum;
2381 int i;
2382
2383#ifdef FEAT_VIRTUALEDIT
2384 /* Break tabs if needed. */
2385 if (virtual_active() && curwin->w_cursor.coladd > 0)
2386 coladvance_force(getviscol());
2387#endif
2388
2389 col = curwin->w_cursor.col;
2390 oldp = ml_get(lnum);
2391 linelen = (int)STRLEN(oldp) + 1;
2392
2393 /* The lengths default to the values for when not replacing. */
2394 oldlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 newlen = charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396
2397 if (State & REPLACE_FLAG)
2398 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 if (State & VREPLACE_FLAG)
2400 {
2401 colnr_T new_vcol = 0; /* init for GCC */
2402 colnr_T vcol;
2403 int old_list;
2404#ifndef FEAT_MBYTE
Bram Moolenaar402385a2019-01-11 14:10:03 +01002405 char_u cbuf[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406#endif
2407
2408 /*
2409 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2410 * Returns the old value of list, so when finished,
2411 * curwin->w_p_list should be set back to this.
2412 */
2413 old_list = curwin->w_p_list;
2414 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2415 curwin->w_p_list = FALSE;
2416
2417 /*
2418 * In virtual replace mode each character may replace one or more
2419 * characters (zero if it's a TAB). Count the number of bytes to
2420 * be deleted to make room for the new character, counting screen
2421 * cells. May result in adding spaces to fill a gap.
2422 */
2423 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2424#ifndef FEAT_MBYTE
Bram Moolenaar402385a2019-01-11 14:10:03 +01002425 cbuf[0] = c;
2426 cbuf[1] = NUL;
2427 new_vcol = vcol + chartabsize(cbuf, vcol);
2428#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 new_vcol = vcol + chartabsize(buf, vcol);
Bram Moolenaar402385a2019-01-11 14:10:03 +01002430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2432 {
2433 vcol += chartabsize(oldp + col + oldlen, vcol);
2434 /* Don't need to remove a TAB that takes us to the right
2435 * position. */
2436 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2437 break;
2438#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002439 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440#else
2441 ++oldlen;
2442#endif
2443 /* Deleted a bit too much, insert spaces. */
2444 if (vcol > new_vcol)
2445 newlen += vcol - new_vcol;
2446 }
2447 curwin->w_p_list = old_list;
2448 }
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002449 else if (oldp[col] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 {
2451 /* normal replace */
2452#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002453 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454#else
2455 oldlen = 1;
2456#endif
2457 }
2458
2459
2460 /* Push the replaced bytes onto the replace stack, so that they can be
2461 * put back when BS is used. The bytes of a multi-byte character are
2462 * done the other way around, so that the first byte is popped off
2463 * first (it tells the byte length of the character). */
2464 replace_push(NUL);
2465 for (i = 0; i < oldlen; ++i)
2466 {
2467#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002468 if (has_mbyte)
2469 i += replace_push_mb(oldp + col + i) - 1;
2470 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002472 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 }
2474 }
2475
2476 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2477 if (newp == NULL)
2478 return;
2479
2480 /* Copy bytes before the cursor. */
2481 if (col > 0)
2482 mch_memmove(newp, oldp, (size_t)col);
2483
2484 /* Copy bytes after the changed character(s). */
2485 p = newp + col;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02002486 if (linelen > col + oldlen)
2487 mch_memmove(p + newlen, oldp + col + oldlen,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 (size_t)(linelen - col - oldlen));
2489
2490 /* Insert or overwrite the new character. */
2491#ifdef FEAT_MBYTE
2492 mch_memmove(p, buf, charlen);
2493 i = charlen;
2494#else
2495 *p = c;
2496 i = 1;
2497#endif
2498
2499 /* Fill with spaces when necessary. */
2500 while (i < newlen)
2501 p[i++] = ' ';
2502
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002503 // Replace the line in the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 ml_replace(lnum, newp, FALSE);
2505
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002506 // mark the buffer as changed and prepare for displaying
2507 inserted_bytes(lnum, col, newlen - oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508
2509 /*
2510 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2511 * show the match for right parens and braces.
2512 */
2513 if (p_sm && (State & INSERT)
2514 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002515#ifdef FEAT_INS_EXPAND
2516 && !ins_compl_active()
2517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002519 {
2520#ifdef FEAT_MBYTE
2521 if (has_mbyte)
2522 showmatch(mb_ptr2char(buf));
2523 else
2524#endif
2525 showmatch(c);
2526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527
2528#ifdef FEAT_RIGHTLEFT
2529 if (!p_ri || (State & REPLACE_FLAG))
2530#endif
2531 {
2532 /* Normal insert: move cursor right */
2533#ifdef FEAT_MBYTE
2534 curwin->w_cursor.col += charlen;
2535#else
2536 ++curwin->w_cursor.col;
2537#endif
2538 }
2539 /*
2540 * TODO: should try to update w_row here, to avoid recomputing it later.
2541 */
2542}
2543
2544/*
2545 * Insert a string at the cursor position.
2546 * Note: Does NOT handle Replace mode.
2547 * Caller must have prepared for undo.
2548 */
2549 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002550ins_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551{
2552 char_u *oldp, *newp;
2553 int newlen = (int)STRLEN(s);
2554 int oldlen;
2555 colnr_T col;
2556 linenr_T lnum = curwin->w_cursor.lnum;
2557
2558#ifdef FEAT_VIRTUALEDIT
2559 if (virtual_active() && curwin->w_cursor.coladd > 0)
2560 coladvance_force(getviscol());
2561#endif
2562
2563 col = curwin->w_cursor.col;
2564 oldp = ml_get(lnum);
2565 oldlen = (int)STRLEN(oldp);
2566
2567 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2568 if (newp == NULL)
2569 return;
2570 if (col > 0)
2571 mch_memmove(newp, oldp, (size_t)col);
2572 mch_memmove(newp + col, s, (size_t)newlen);
2573 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2574 ml_replace(lnum, newp, FALSE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002575 inserted_bytes(lnum, col, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 curwin->w_cursor.col += newlen;
2577}
2578
2579/*
2580 * Delete one character under the cursor.
2581 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2582 * Caller must have prepared for undo.
2583 *
2584 * return FAIL for failure, OK otherwise
2585 */
2586 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002587del_char(int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588{
2589#ifdef FEAT_MBYTE
2590 if (has_mbyte)
2591 {
2592 /* Make sure the cursor is at the start of a character. */
2593 mb_adjust_cursor();
2594 if (*ml_get_cursor() == NUL)
2595 return FAIL;
2596 return del_chars(1L, fixpos);
2597 }
2598#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002599 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600}
2601
2602#if defined(FEAT_MBYTE) || defined(PROTO)
2603/*
2604 * Like del_bytes(), but delete characters instead of bytes.
2605 */
2606 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002607del_chars(long count, int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608{
2609 long bytes = 0;
2610 long i;
2611 char_u *p;
2612 int l;
2613
2614 p = ml_get_cursor();
2615 for (i = 0; i < count && *p != NUL; ++i)
2616 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002617 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 bytes += l;
2619 p += l;
2620 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002621 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622}
2623#endif
2624
2625/*
2626 * Delete "count" bytes under the cursor.
2627 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2628 * Caller must have prepared for undo.
2629 *
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002630 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 */
2632 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002633del_bytes(
2634 long count,
2635 int fixpos_arg,
2636 int use_delcombine UNUSED) /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637{
2638 char_u *oldp, *newp;
2639 colnr_T oldlen;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002640 colnr_T newlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 linenr_T lnum = curwin->w_cursor.lnum;
2642 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002643 int alloc_newp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002645 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646
2647 oldp = ml_get(lnum);
2648 oldlen = (int)STRLEN(oldp);
2649
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002650 /* Can't do anything when the cursor is on the NUL after the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 if (col >= oldlen)
2652 return FAIL;
2653
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002654 /* If "count" is zero there is nothing to do. */
2655 if (count == 0)
2656 return OK;
2657
2658 /* If "count" is negative the caller must be doing something wrong. */
2659 if (count < 1)
2660 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002661 siemsg("E950: Invalid count for del_bytes(): %ld", count);
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002662 return FAIL;
2663 }
2664
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665#ifdef FEAT_MBYTE
2666 /* If 'delcombine' is set and deleting (less than) one character, only
2667 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002668 if (p_deco && use_delcombine && enc_utf8
2669 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002671 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 int n;
2673
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002674 (void)utfc_ptr2char(oldp + col, cc);
2675 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 {
2677 /* Find the last composing char, there can be several. */
2678 n = col;
2679 do
2680 {
2681 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002682 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 n += count;
2684 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2685 fixpos = 0;
2686 }
2687 }
2688#endif
2689
2690 /*
2691 * When count is too big, reduce it.
2692 */
2693 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2694 if (movelen <= 1)
2695 {
2696 /*
2697 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002698 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2699 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002701 if (col > 0 && fixpos && restart_edit == 0
2702#ifdef FEAT_VIRTUALEDIT
2703 && (ve_flags & VE_ONEMORE) == 0
2704#endif
2705 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 {
2707 --curwin->w_cursor.col;
2708#ifdef FEAT_VIRTUALEDIT
2709 curwin->w_cursor.coladd = 0;
2710#endif
2711#ifdef FEAT_MBYTE
2712 if (has_mbyte)
2713 curwin->w_cursor.col -=
2714 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2715#endif
2716 }
2717 count = oldlen - col;
2718 movelen = 1;
2719 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002720 newlen = oldlen - count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721
2722 /*
2723 * If the old line has been allocated the deletion can be done in the
2724 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002725 * Can't do this when using Netbeans, because we would need to invoke
2726 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002727 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002730 if (netbeans_active())
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002731 alloc_newp = TRUE;
Bram Moolenaare21877a2008-02-13 09:58:14 +00002732 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002734 alloc_newp = !ml_line_alloced(); // check if oldp was allocated
2735 if (!alloc_newp)
2736 newp = oldp; // use same allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 else
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002738 { // need to allocate a new line
2739 newp = alloc((unsigned)(newlen + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 if (newp == NULL)
2741 return FAIL;
2742 mch_memmove(newp, oldp, (size_t)col);
2743 }
2744 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002745 if (alloc_newp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 ml_replace(lnum, newp, FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002747#ifdef FEAT_TEXT_PROP
2748 else
2749 {
2750 // Also move any following text properties.
2751 if (oldlen + 1 < curbuf->b_ml.ml_line_len)
2752 mch_memmove(newp + newlen + 1, oldp + oldlen + 1,
2753 (size_t)curbuf->b_ml.ml_line_len - oldlen - 1);
2754 curbuf->b_ml.ml_line_len -= count;
2755 }
2756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002758 // mark the buffer as changed and prepare for displaying
Bram Moolenaar33c8ca92019-01-02 18:00:27 +01002759 inserted_bytes(lnum, curwin->w_cursor.col, -count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760
2761 return OK;
2762}
2763
2764/*
2765 * Delete from cursor to end of line.
2766 * Caller must have prepared for undo.
2767 *
2768 * return FAIL for failure, OK otherwise
2769 */
2770 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002771truncate_line(
2772 int fixpos) /* if TRUE fix the cursor position when done */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773{
2774 char_u *newp;
2775 linenr_T lnum = curwin->w_cursor.lnum;
2776 colnr_T col = curwin->w_cursor.col;
2777
2778 if (col == 0)
2779 newp = vim_strsave((char_u *)"");
2780 else
2781 newp = vim_strnsave(ml_get(lnum), col);
2782
2783 if (newp == NULL)
2784 return FAIL;
2785
2786 ml_replace(lnum, newp, FALSE);
2787
2788 /* mark the buffer as changed and prepare for displaying */
2789 changed_bytes(lnum, curwin->w_cursor.col);
2790
2791 /*
2792 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2793 */
2794 if (fixpos && curwin->w_cursor.col > 0)
2795 --curwin->w_cursor.col;
2796
2797 return OK;
2798}
2799
2800/*
2801 * Delete "nlines" lines at the cursor.
2802 * Saves the lines for undo first if "undo" is TRUE.
2803 */
2804 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002805del_lines(
2806 long nlines, /* number of lines to delete */
2807 int undo) /* if TRUE, prepare for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808{
2809 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002810 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811
2812 if (nlines <= 0)
2813 return;
2814
2815 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002816 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 return;
2818
2819 for (n = 0; n < nlines; )
2820 {
2821 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2822 break;
2823
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002824 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 ++n;
2826
2827 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002828 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 break;
2830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002832 /* Correct the cursor position before calling deleted_lines_mark(), it may
2833 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 curwin->w_cursor.col = 0;
2835 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002836
2837 /* adjust marks, mark the buffer as changed and prepare for displaying */
2838 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839}
2840
2841 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002842gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002844 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002846 /* When searching columns is sometimes put at the end of a line. */
2847 if (pos->col == MAXCOL)
2848 return NUL;
2849 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850#ifdef FEAT_MBYTE
2851 if (has_mbyte)
2852 return (*mb_ptr2char)(ptr);
2853#endif
2854 return (int)*ptr;
2855}
2856
2857 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002858gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859{
2860#ifdef FEAT_MBYTE
2861 if (has_mbyte)
2862 return (*mb_ptr2char)(ml_get_cursor());
2863#endif
2864 return (int)*ml_get_cursor();
2865}
2866
2867/*
2868 * Write a character at the current cursor position.
2869 * It is directly written into the block.
2870 */
2871 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002872pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873{
2874 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2875 + curwin->w_cursor.col) = c;
2876}
2877
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878/*
2879 * When extra == 0: Return TRUE if the cursor is before or on the first
2880 * non-blank in the line.
2881 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2882 * the line.
2883 */
2884 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002885inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886{
2887 char_u *ptr;
2888 colnr_T col;
2889
Bram Moolenaar1c465442017-03-12 20:10:05 +01002890 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 ++ptr;
2892 if (col >= curwin->w_cursor.col + extra)
2893 return TRUE;
2894 else
2895 return FALSE;
2896}
2897
2898/*
2899 * Skip to next part of an option argument: Skip space and comma.
2900 */
2901 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002902skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903{
2904 if (*p == ',')
2905 ++p;
2906 while (*p == ' ')
2907 ++p;
2908 return p;
2909}
2910
2911/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002912 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 *
2914 * Most often called through changed_bytes() and changed_lines(), which also
2915 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002916 *
2917 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 */
2919 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002920changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921{
2922#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02002923 if (p_imst == IM_ON_THE_SPOT)
2924 {
2925 /* The text of the preediting area is inserted, but this doesn't
2926 * mean a change of the buffer yet. That is delayed until the
2927 * text is committed. (this means preedit becomes empty) */
2928 if (im_is_preediting() && !xim_changed_while_preediting)
2929 return;
2930 xim_changed_while_preediting = FALSE;
2931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932#endif
2933
2934 if (!curbuf->b_changed)
2935 {
2936 int save_msg_scroll = msg_scroll;
2937
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002938 /* Give a warning about changing a read-only file. This may also
2939 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002941
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 /* Create a swap file if that is wanted.
2943 * Don't do this for "nofile" and "nowrite" buffer types. */
2944 if (curbuf->b_may_swap
2945#ifdef FEAT_QUICKFIX
2946 && !bt_dontwrite(curbuf)
2947#endif
2948 )
2949 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002950 int save_need_wait_return = need_wait_return;
2951
2952 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 ml_open_file(curbuf);
2954
2955 /* The ml_open_file() can cause an ATTENTION message.
2956 * Wait two seconds, to make sure the user reads this unexpected
2957 * message. Since we could be anywhere, call wait_return() now,
2958 * and don't let the emsg() set msg_scroll. */
2959 if (need_wait_return && emsg_silent == 0)
2960 {
2961 out_flush();
2962 ui_delay(2000L, TRUE);
2963 wait_return(TRUE);
2964 msg_scroll = save_msg_scroll;
2965 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002966 else
2967 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002969 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002971 ++CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972}
2973
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002974/*
2975 * Internal part of changed(), no user interaction.
2976 */
2977 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002978changed_int(void)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002979{
2980 curbuf->b_changed = TRUE;
2981 ml_setflags(curbuf);
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002982 check_status(curbuf);
2983 redraw_tabline = TRUE;
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002984#ifdef FEAT_TITLE
2985 need_maketitle = TRUE; /* set window title later */
2986#endif
2987}
2988
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002989static void changedOneline(buf_T *buf, linenr_T lnum);
2990static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
2991static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992
2993/*
2994 * Changed bytes within a single line for the current buffer.
2995 * - marks the windows on this buffer to be redisplayed
2996 * - marks the buffer changed by calling changed()
2997 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002998 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 */
3000 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003001changed_bytes(linenr_T lnum, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002{
Bram Moolenaardba8a912005-04-24 22:08:39 +00003003 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00003005
3006#ifdef FEAT_DIFF
3007 /* Diff highlighting in other diff windows may need to be updated too. */
3008 if (curwin->w_p_diff)
3009 {
3010 win_T *wp;
3011 linenr_T wlnum;
3012
Bram Moolenaar29323592016-07-24 22:04:11 +02003013 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00003014 if (wp->w_p_diff && wp != curwin)
3015 {
3016 redraw_win_later(wp, VALID);
3017 wlnum = diff_lnum_win(lnum, wp);
3018 if (wlnum > 0)
3019 changedOneline(wp->w_buffer, wlnum);
3020 }
3021 }
3022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023}
3024
Bram Moolenaar44746aa2019-01-02 00:02:11 +01003025/*
3026 * Like changed_bytes() but also adjust text properties for "added" bytes.
3027 * When "added" is negative text was deleted.
3028 */
3029 void
Bram Moolenaar402385a2019-01-11 14:10:03 +01003030inserted_bytes(linenr_T lnum, colnr_T col, int added UNUSED)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01003031{
3032 changed_bytes(lnum, col);
3033
3034#ifdef FEAT_TEXT_PROP
3035 if (curbuf->b_has_textprop && added != 0)
3036 adjust_prop_columns(lnum, col, added);
3037#endif
3038}
3039
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003041changedOneline(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042{
Bram Moolenaardba8a912005-04-24 22:08:39 +00003043 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 {
3045 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003046 if (lnum < buf->b_mod_top)
3047 buf->b_mod_top = lnum;
3048 else if (lnum >= buf->b_mod_bot)
3049 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 }
3051 else
3052 {
3053 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003054 buf->b_mod_set = TRUE;
3055 buf->b_mod_top = lnum;
3056 buf->b_mod_bot = lnum + 1;
3057 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 }
3059}
3060
3061/*
3062 * Appended "count" lines below line "lnum" in the current buffer.
3063 * Must be called AFTER the change and after mark_adjust().
3064 * Takes care of marking the buffer to be redrawn and sets the changed flag.
3065 */
3066 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003067appended_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068{
3069 changed_lines(lnum + 1, 0, lnum + 1, count);
3070}
3071
3072/*
3073 * Like appended_lines(), but adjust marks first.
3074 */
3075 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003076appended_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077{
Bram Moolenaar82faa252016-06-04 20:14:07 +02003078 /* Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003079 * be marks there. But it's still needed in diff mode. */
3080 if (lnum + count < curbuf->b_ml.ml_line_count
3081#ifdef FEAT_DIFF
3082 || curwin->w_p_diff
3083#endif
3084 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02003085 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 changed_lines(lnum + 1, 0, lnum + 1, count);
3087}
3088
3089/*
3090 * Deleted "count" lines at line "lnum" in the current buffer.
3091 * Must be called AFTER the change and after mark_adjust().
3092 * Takes care of marking the buffer to be redrawn and sets the changed flag.
3093 */
3094 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003095deleted_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096{
3097 changed_lines(lnum, 0, lnum + count, -count);
3098}
3099
3100/*
3101 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00003102 * Make sure the cursor is on a valid line before calling, a GUI callback may
3103 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 */
3105 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003106deleted_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107{
3108 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
3109 changed_lines(lnum, 0, lnum + count, -count);
3110}
3111
3112/*
3113 * Changed lines for the current buffer.
3114 * Must be called AFTER the change and after mark_adjust().
3115 * - mark the buffer changed by calling changed()
3116 * - mark the windows on this buffer to be redisplayed
3117 * - invalidate cached values
3118 * "lnum" is the first line that needs displaying, "lnume" the first line
3119 * below the changed lines (BEFORE the change).
3120 * When only inserting lines, "lnum" and "lnume" are equal.
3121 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003122 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 */
3124 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003125changed_lines(
3126 linenr_T lnum, /* first line with change */
3127 colnr_T col, /* column in first line with change */
3128 linenr_T lnume, /* line below last changed line */
3129 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130{
Bram Moolenaardba8a912005-04-24 22:08:39 +00003131 changed_lines_buf(curbuf, lnum, lnume, xtra);
3132
3133#ifdef FEAT_DIFF
Bram Moolenaare3521d92018-09-16 14:10:31 +02003134 if (xtra == 0 && curwin->w_p_diff && !diff_internal())
Bram Moolenaardba8a912005-04-24 22:08:39 +00003135 {
3136 /* When the number of lines doesn't change then mark_adjust() isn't
3137 * called and other diff buffers still need to be marked for
3138 * displaying. */
3139 win_T *wp;
3140 linenr_T wlnum;
3141
Bram Moolenaar29323592016-07-24 22:04:11 +02003142 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00003143 if (wp->w_p_diff && wp != curwin)
3144 {
3145 redraw_win_later(wp, VALID);
3146 wlnum = diff_lnum_win(lnum, wp);
3147 if (wlnum > 0)
3148 changed_lines_buf(wp->w_buffer, wlnum,
3149 lnume - lnum + wlnum, 0L);
3150 }
3151 }
3152#endif
3153
3154 changed_common(lnum, col, lnume, xtra);
3155}
3156
3157 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003158changed_lines_buf(
3159 buf_T *buf,
3160 linenr_T lnum, /* first line with change */
3161 linenr_T lnume, /* line below last changed line */
3162 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003163{
3164 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 {
3166 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003167 if (lnum < buf->b_mod_top)
3168 buf->b_mod_top = lnum;
3169 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 {
3171 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003172 buf->b_mod_bot += xtra;
3173 if (buf->b_mod_bot < lnum)
3174 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00003176 if (lnume + xtra > buf->b_mod_bot)
3177 buf->b_mod_bot = lnume + xtra;
3178 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 }
3180 else
3181 {
3182 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003183 buf->b_mod_set = TRUE;
3184 buf->b_mod_top = lnum;
3185 buf->b_mod_bot = lnume + xtra;
3186 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188}
3189
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003190/*
3191 * Common code for when a change is was made.
3192 * See changed_lines() for the arguments.
3193 * Careful: may trigger autocommands that reload the buffer.
3194 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003196changed_common(
3197 linenr_T lnum,
3198 colnr_T col,
3199 linenr_T lnume,
3200 long xtra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201{
3202 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003203 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 int i;
3205#ifdef FEAT_JUMPLIST
3206 int cols;
3207 pos_T *p;
3208 int add;
3209#endif
3210
3211 /* mark the buffer as modified */
3212 changed();
3213
Bram Moolenaare3521d92018-09-16 14:10:31 +02003214#ifdef FEAT_DIFF
3215 if (curwin->w_p_diff && diff_internal())
3216 curtab->tp_diff_update = TRUE;
3217#endif
3218
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 /* set the '. mark */
3220 if (!cmdmod.keepjumps)
3221 {
3222 curbuf->b_last_change.lnum = lnum;
3223 curbuf->b_last_change.col = col;
3224
3225#ifdef FEAT_JUMPLIST
3226 /* Create a new entry if a new undo-able change was started or we
3227 * don't have an entry yet. */
3228 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3229 {
3230 if (curbuf->b_changelistlen == 0)
3231 add = TRUE;
3232 else
3233 {
3234 /* Don't create a new entry when the line number is the same
3235 * as the last one and the column is not too far away. Avoids
3236 * creating many entries for typing "xxxxx". */
3237 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3238 if (p->lnum != lnum)
3239 add = TRUE;
3240 else
3241 {
3242 cols = comp_textwidth(FALSE);
3243 if (cols == 0)
3244 cols = 79;
3245 add = (p->col + cols < col || col + cols < p->col);
3246 }
3247 }
3248 if (add)
3249 {
3250 /* This is the first of a new sequence of undo-able changes
3251 * and it's at some distance of the last change. Use a new
3252 * position in the changelist. */
3253 curbuf->b_new_change = FALSE;
3254
3255 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3256 {
3257 /* changelist is full: remove oldest entry */
3258 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3259 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3260 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003261 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 {
3263 /* Correct position in changelist for other windows on
3264 * this buffer. */
3265 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3266 --wp->w_changelistidx;
3267 }
3268 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003269 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 {
3271 /* For other windows, if the position in the changelist is
3272 * at the end it stays at the end. */
3273 if (wp->w_buffer == curbuf
3274 && wp->w_changelistidx == curbuf->b_changelistlen)
3275 ++wp->w_changelistidx;
3276 }
3277 ++curbuf->b_changelistlen;
3278 }
3279 }
3280 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3281 curbuf->b_last_change;
3282 /* The current window is always after the last change, so that "g,"
3283 * takes you back to it. */
3284 curwin->w_changelistidx = curbuf->b_changelistlen;
3285#endif
3286 }
3287
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003288 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 {
3290 if (wp->w_buffer == curbuf)
3291 {
3292 /* Mark this window to be redrawn later. */
3293 if (wp->w_redr_type < VALID)
3294 wp->w_redr_type = VALID;
3295
3296 /* Check if a change in the buffer has invalidated the cached
3297 * values for the cursor. */
3298#ifdef FEAT_FOLDING
3299 /*
3300 * Update the folds for this window. Can't postpone this, because
3301 * a following operator might work on the whole fold: ">>dd".
3302 */
3303 foldUpdate(wp, lnum, lnume + xtra - 1);
3304
3305 /* The change may cause lines above or below the change to become
3306 * included in a fold. Set lnum/lnume to the first/last line that
3307 * might be displayed differently.
3308 * Set w_cline_folded here as an efficient way to update it when
3309 * inserting lines just above a closed fold. */
3310 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3311 if (wp->w_cursor.lnum == lnum)
3312 wp->w_cline_folded = i;
3313 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3314 if (wp->w_cursor.lnum == lnume)
3315 wp->w_cline_folded = i;
3316
3317 /* If the changed line is in a range of previously folded lines,
3318 * compare with the first line in that range. */
3319 if (wp->w_cursor.lnum <= lnum)
3320 {
3321 i = find_wl_entry(wp, lnum);
3322 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3323 changed_line_abv_curs_win(wp);
3324 }
3325#endif
3326
3327 if (wp->w_cursor.lnum > lnum)
3328 changed_line_abv_curs_win(wp);
3329 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3330 changed_cline_bef_curs_win(wp);
3331 if (wp->w_botline >= lnum)
3332 {
3333 /* Assume that botline doesn't change (inserted lines make
3334 * other lines scroll down below botline). */
3335 approximate_botline_win(wp);
3336 }
3337
3338 /* Check if any w_lines[] entries have become invalid.
3339 * For entries below the change: Correct the lnums for
3340 * inserted/deleted lines. Makes it possible to stop displaying
3341 * after the change. */
3342 for (i = 0; i < wp->w_lines_valid; ++i)
3343 if (wp->w_lines[i].wl_valid)
3344 {
3345 if (wp->w_lines[i].wl_lnum >= lnum)
3346 {
3347 if (wp->w_lines[i].wl_lnum < lnume)
3348 {
3349 /* line included in change */
3350 wp->w_lines[i].wl_valid = FALSE;
3351 }
3352 else if (xtra != 0)
3353 {
3354 /* line below change */
3355 wp->w_lines[i].wl_lnum += xtra;
3356#ifdef FEAT_FOLDING
3357 wp->w_lines[i].wl_lastlnum += xtra;
3358#endif
3359 }
3360 }
3361#ifdef FEAT_FOLDING
3362 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3363 {
3364 /* change somewhere inside this range of folded lines,
3365 * may need to be redrawn */
3366 wp->w_lines[i].wl_valid = FALSE;
3367 }
3368#endif
3369 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003370
3371#ifdef FEAT_FOLDING
3372 /* Take care of side effects for setting w_topline when folds have
3373 * changed. Esp. when the buffer was changed in another window. */
3374 if (hasAnyFolding(wp))
3375 set_topline(wp, wp->w_topline);
3376#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003377 /* relative numbering may require updating more */
3378 if (wp->w_p_rnu)
3379 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 }
3381 }
3382
3383 /* Call update_screen() later, which checks out what needs to be redrawn,
3384 * since it notices b_mod_set and then uses b_mod_*. */
3385 if (must_redraw < VALID)
3386 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003387
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003388 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003389 if (lnum <= curwin->w_cursor.lnum
3390 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003391 last_cursormoved.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392}
3393
3394/*
3395 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3396 */
3397 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003398unchanged(
3399 buf_T *buf,
3400 int ff) /* also reset 'fileformat' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003402 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 {
3404 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003405 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 if (ff)
3407 save_file_ff(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003409 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410#ifdef FEAT_TITLE
3411 need_maketitle = TRUE; /* set window title later */
3412#endif
3413 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003414 ++CHANGEDTICK(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415#ifdef FEAT_NETBEANS_INTG
3416 netbeans_unmodified(buf);
3417#endif
3418}
3419
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420/*
3421 * check_status: called when the status bars for the buffer 'buf'
3422 * need to be updated
3423 */
3424 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003425check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426{
3427 win_T *wp;
3428
Bram Moolenaar29323592016-07-24 22:04:11 +02003429 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 if (wp->w_buffer == buf && wp->w_status_height)
3431 {
3432 wp->w_redr_status = TRUE;
3433 if (must_redraw < VALID)
3434 must_redraw = VALID;
3435 }
3436}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437
3438/*
3439 * If the file is readonly, give a warning message with the first change.
3440 * Don't do this for autocommands.
3441 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003442 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003444 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 */
3446 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003447change_warning(
3448 int col) /* column for message; non-zero when in insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 mode and 'showmode' is on */
3450{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003451 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3452
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 if (curbuf->b_did_warn == FALSE
3454 && curbufIsChanged() == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 && !autocmd_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 && curbuf->b_p_ro)
3457 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003458 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003460 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 if (!curbuf->b_p_ro)
3462 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 /*
3464 * Do what msg() does, but with a column offset if the warning should
3465 * be after the mode message.
3466 */
3467 msg_start();
3468 if (msg_row == Rows - 1)
3469 msg_col = col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003470 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003471 msg_puts_attr(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST);
Bram Moolenaar496c5262009-03-18 14:42:00 +00003472#ifdef FEAT_EVAL
3473 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 msg_clr_eos();
3476 (void)msg_end();
Bram Moolenaare5f2a072017-02-01 22:31:49 +01003477 if (msg_silent == 0 && !silent_mode
3478#ifdef FEAT_EVAL
3479 && time_for_testing != 1
3480#endif
3481 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 {
3483 out_flush();
3484 ui_delay(1000L, TRUE); /* give the user time to think about it */
3485 }
3486 curbuf->b_did_warn = TRUE;
3487 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3488 if (msg_row < Rows - 1)
3489 showmode();
3490 }
3491}
3492
3493/*
3494 * Ask for a reply from the user, a 'y' or a 'n'.
3495 * No other characters are accepted, the message is repeated until a valid
3496 * reply is entered or CTRL-C is hit.
3497 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3498 * from any buffers but directly from the user.
3499 *
3500 * return the 'y' or 'n'
3501 */
3502 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003503ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504{
3505 int r = ' ';
3506 int save_State = State;
3507
3508 if (exiting) /* put terminal in raw mode for this question */
3509 settmode(TMODE_RAW);
3510 ++no_wait_return;
3511#ifdef USE_ON_FLY_SCROLL
3512 dont_scroll = TRUE; /* disallow scrolling here */
3513#endif
3514 State = CONFIRM; /* mouse behaves like with :confirm */
3515#ifdef FEAT_MOUSE
3516 setmouse(); /* disables mouse for xterm */
3517#endif
3518 ++no_mapping;
3519 ++allow_keys; /* no mapping here, but recognize keys */
3520
3521 while (r != 'y' && r != 'n')
3522 {
3523 /* same highlighting as for wait_return */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003524 smsg_attr(HL_ATTR(HLF_R), "%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 if (direct)
3526 r = get_keystroke();
3527 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003528 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 if (r == Ctrl_C || r == ESC)
3530 r = 'n';
3531 msg_putchar(r); /* show what you typed */
3532 out_flush();
3533 }
3534 --no_wait_return;
3535 State = save_State;
3536#ifdef FEAT_MOUSE
3537 setmouse();
3538#endif
3539 --no_mapping;
3540 --allow_keys;
3541
3542 return r;
3543}
3544
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003545#if defined(FEAT_MOUSE) || defined(PROTO)
3546/*
3547 * Return TRUE if "c" is a mouse key.
3548 */
3549 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003550is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003551{
3552 return c == K_LEFTMOUSE
3553 || c == K_LEFTMOUSE_NM
3554 || c == K_LEFTDRAG
3555 || c == K_LEFTRELEASE
3556 || c == K_LEFTRELEASE_NM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003557 || c == K_MOUSEMOVE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003558 || c == K_MIDDLEMOUSE
3559 || c == K_MIDDLEDRAG
3560 || c == K_MIDDLERELEASE
3561 || c == K_RIGHTMOUSE
3562 || c == K_RIGHTDRAG
3563 || c == K_RIGHTRELEASE
3564 || c == K_MOUSEDOWN
3565 || c == K_MOUSEUP
3566 || c == K_MOUSELEFT
3567 || c == K_MOUSERIGHT
3568 || c == K_X1MOUSE
3569 || c == K_X1DRAG
3570 || c == K_X1RELEASE
3571 || c == K_X2MOUSE
3572 || c == K_X2DRAG
3573 || c == K_X2RELEASE;
3574}
3575#endif
3576
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577/*
3578 * Get a key stroke directly from the user.
3579 * Ignores mouse clicks and scrollbar events, except a click for the left
3580 * button (used at the more prompt).
3581 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3582 * Disadvantage: typeahead is ignored.
3583 * Translates the interrupt character for unix to ESC.
3584 */
3585 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003586get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003588 char_u *buf = NULL;
3589 int buflen = 150;
3590 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 int len = 0;
3592 int n;
3593 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003594 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595
3596 mapped_ctrl_c = FALSE; /* mappings are not used here */
3597 for (;;)
3598 {
3599 cursor_on();
3600 out_flush();
3601
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003602 /* Leave some room for check_termcode() to insert a key code into (max
3603 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3604 * bytes. */
3605 maxlen = (buflen - 6 - len) / 3;
3606 if (buf == NULL)
3607 buf = alloc(buflen);
3608 else if (maxlen < 10)
3609 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003610 char_u *t_buf = buf;
3611
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003612 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003613 * escape sequence. */
3614 buflen += 100;
3615 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003616 if (buf == NULL)
3617 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003618 maxlen = (buflen - 6 - len) / 3;
3619 }
3620 if (buf == NULL)
3621 {
3622 do_outofmem_msg((long_u)buflen);
3623 return ESC; /* panic! */
3624 }
3625
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003627 * terminal code to complete. */
3628 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 if (n > 0)
3630 {
3631 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003632 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003634 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003636 else if (len > 0)
3637 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638
Bram Moolenaar4395a712006-09-05 18:57:57 +00003639 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003640 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003641 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003643
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003644 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003645 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003646 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003647 {
3648 /* Redrawing was postponed, do it now. */
3649 update_screen(0);
3650 setcursor(); /* put cursor back where it belongs */
3651 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003652 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003653 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003654 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003656 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 continue;
3658
3659 /* Handle modifier and/or special key code. */
3660 n = buf[0];
3661 if (n == K_SPECIAL)
3662 {
3663 n = TO_SPECIAL(buf[1], buf[2]);
3664 if (buf[1] == KS_MODIFIER
3665 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003666#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003667 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003668#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003669#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 || n == K_VER_SCROLLBAR
3671 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672#endif
3673 )
3674 {
3675 if (buf[1] == KS_MODIFIER)
3676 mod_mask = buf[2];
3677 len -= 3;
3678 if (len > 0)
3679 mch_memmove(buf, buf + 3, (size_t)len);
3680 continue;
3681 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003682 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 }
3684#ifdef FEAT_MBYTE
3685 if (has_mbyte)
3686 {
3687 if (MB_BYTE2LEN(n) > len)
3688 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003689 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 n = (*mb_ptr2char)(buf);
3691 }
3692#endif
3693#ifdef UNIX
3694 if (n == intr_char)
3695 n = ESC;
3696#endif
3697 break;
3698 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003699 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700
3701 mapped_ctrl_c = save_mapped_ctrl_c;
3702 return n;
3703}
3704
3705/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003706 * Get a number from the user.
3707 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 */
3709 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003710get_number(
3711 int colon, /* allow colon to abort */
3712 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713{
3714 int n = 0;
3715 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003716 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003718 if (mouse_used != NULL)
3719 *mouse_used = FALSE;
3720
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 /* When not printing messages, the user won't know what to type, return a
3722 * zero (as if CR was hit). */
3723 if (msg_silent != 0)
3724 return 0;
3725
3726#ifdef USE_ON_FLY_SCROLL
3727 dont_scroll = TRUE; /* disallow scrolling here */
3728#endif
3729 ++no_mapping;
3730 ++allow_keys; /* no mapping here, but recognize keys */
3731 for (;;)
3732 {
3733 windgoto(msg_row, msg_col);
3734 c = safe_vgetc();
3735 if (VIM_ISDIGIT(c))
3736 {
3737 n = n * 10 + c - '0';
3738 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003739 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 }
3741 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3742 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003743 if (typed > 0)
3744 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003745 msg_puts("\b \b");
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003746 --typed;
3747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003750#ifdef FEAT_MOUSE
3751 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3752 {
3753 *mouse_used = TRUE;
3754 n = mouse_row + 1;
3755 break;
3756 }
3757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 else if (n == 0 && c == ':' && colon)
3759 {
3760 stuffcharReadbuff(':');
3761 if (!exmode_active)
3762 cmdline_row = msg_row;
3763 skip_redraw = TRUE; /* skip redraw once */
3764 do_redraw = FALSE;
3765 break;
3766 }
3767 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3768 break;
3769 }
3770 --no_mapping;
3771 --allow_keys;
3772 return n;
3773}
3774
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003775/*
3776 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003777 * When "mouse_used" is not NULL allow using the mouse and in that case return
3778 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003779 */
3780 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003781prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003782{
3783 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003784 int save_cmdline_row;
3785 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003786
3787 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003788 if (mouse_used != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003789 msg_puts(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003790 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003791 msg_puts(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003792
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003793 // Set the state such that text can be selected/copied/pasted and we still
3794 // get mouse events. redraw_after_callback() will not redraw if cmdline_row
3795 // is zero.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003796 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003797 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003798 save_State = State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003799 State = CMDLINE;
Bram Moolenaar73658312018-04-24 17:41:57 +02003800#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003801 // May show different mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02003802 setmouse();
3803#endif
3804
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003805 i = get_number(TRUE, mouse_used);
3806 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003807 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003808 /* don't call wait_return() now */
3809 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003810 cmdline_row = msg_row - 1;
3811 need_wait_return = FALSE;
3812 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003813 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003814 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003815 else
3816 cmdline_row = save_cmdline_row;
3817 State = save_State;
Bram Moolenaar73658312018-04-24 17:41:57 +02003818#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003819 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02003820 setmouse();
3821#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003822
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003823 return i;
3824}
3825
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003827msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828{
3829 long pn;
3830
3831 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3833 return;
3834
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003835 /* We don't want to overwrite another important message, but do overwrite
3836 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3837 * then "put" reports the last action. */
3838 if (keep_msg != NULL && !keep_msg_more)
3839 return;
3840
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 if (n > 0)
3842 pn = n;
3843 else
3844 pn = -n;
3845
3846 if (pn > p_report)
3847 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003848 if (n > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003849 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003850 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003852 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003853 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003855 vim_strcat((char_u *)msg_buf, (char_u *)_(" (Interrupted)"),
3856 MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 if (msg(msg_buf))
3858 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003859 set_keep_msg((char_u *)msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003860 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 }
3862 }
3863}
3864
3865/*
3866 * flush map and typeahead buffers and give a warning for an error
3867 */
3868 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003869beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870{
3871 if (emsg_silent == 0)
3872 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003873 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003874 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 }
3876}
3877
3878/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003879 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 */
3881 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003882vim_beep(
3883 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003885#ifdef FEAT_EVAL
3886 called_vim_beep = TRUE;
3887#endif
3888
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 if (emsg_silent == 0)
3890 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003891 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3892 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003893#ifdef ELAPSED_FUNC
3894 static int did_init = FALSE;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01003895 static elapsed_T start_tv;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003896
3897 /* Only beep once per half a second, otherwise a sequence of beeps
3898 * would freeze Vim. */
3899 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
3900 {
3901 did_init = TRUE;
3902 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003904 if (p_vb
3905#ifdef FEAT_GUI
3906 /* While the GUI is starting up the termcap is set for
3907 * the GUI but the output still goes to a terminal. */
3908 && !(gui.in_use && gui.starting)
3909#endif
3910 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003911 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003912 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003913#ifdef FEAT_VTP
3914 /* No restore color information, refresh the screen. */
3915 if (has_vtp_working() != 0
3916# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003917 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003918# endif
3919 )
3920 {
3921 redraw_later(CLEAR);
3922 update_screen(0);
3923 redrawcmd();
3924 }
3925#endif
3926 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003927 else
3928 out_char(BELL);
3929#ifdef ELAPSED_FUNC
3930 }
3931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003933
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003934 /* When 'debug' contains "beep" produce a message. If we are sourcing
3935 * a script or executing a function give the user a hint where the beep
3936 * comes from. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003937 if (vim_strchr(p_debug, 'e') != NULL)
3938 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003939 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003940 msg_attr(_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 }
3943}
3944
3945/*
3946 * To get the "real" home directory:
3947 * - get value of $HOME
3948 * For Unix:
3949 * - go to that directory
3950 * - do mch_dirname() to get the real name of that directory.
3951 * This also works with mounts and links.
3952 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
Bram Moolenaar25a494c2018-11-16 19:39:50 +01003953 * For Windows:
3954 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 */
3956static char_u *homedir = NULL;
3957
3958 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003959init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960{
3961 char_u *var;
3962
Bram Moolenaar05159a02005-02-26 23:04:13 +00003963 /* In case we are called a second time (when 'encoding' changes). */
Bram Moolenaard23a8232018-02-10 18:45:26 +01003964 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003965
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966#ifdef VMS
3967 var = mch_getenv((char_u *)"SYS$LOGIN");
3968#else
3969 var = mch_getenv((char_u *)"HOME");
3970#endif
3971
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972#ifdef WIN3264
3973 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02003974 * Typically, $HOME is not defined on Windows, unless the user has
3975 * specifically defined it for Vim's sake. However, on Windows NT
3976 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3977 * each user. Try constructing $HOME from these.
3978 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02003979 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02003980 {
3981 char_u *homedrive, *homepath;
3982
3983 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3984 homepath = mch_getenv((char_u *)"HOMEPATH");
3985 if (homepath == NULL || *homepath == NUL)
3986 homepath = (char_u *)"\\";
3987 if (homedrive != NULL
3988 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3989 {
3990 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3991 if (NameBuff[0] != NUL)
3992 var = NameBuff;
3993 }
3994 }
3995
3996 if (var == NULL)
3997 var = mch_getenv((char_u *)"USERPROFILE");
3998
3999 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 * Weird but true: $HOME may contain an indirect reference to another
4001 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
4002 * when $HOME is being set.
4003 */
4004 if (var != NULL && *var == '%')
4005 {
4006 char_u *p;
4007 char_u *exp;
4008
4009 p = vim_strchr(var + 1, '%');
4010 if (p != NULL)
4011 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004012 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 exp = mch_getenv(NameBuff);
4014 if (exp != NULL && *exp != NUL
4015 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
4016 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00004017 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 }
4020 }
4021 }
4022
Bram Moolenaar48340b62017-08-29 22:08:53 +02004023 if (var != NULL && *var == NUL) /* empty is same as not set */
4024 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025
Bram Moolenaar48340b62017-08-29 22:08:53 +02004026# ifdef FEAT_MBYTE
Bram Moolenaar05159a02005-02-26 23:04:13 +00004027 if (enc_utf8 && var != NULL)
4028 {
4029 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004030 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004031
4032 /* Convert from active codepage to UTF-8. Other conversions are
4033 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004034 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004035 if (pp != NULL)
4036 {
4037 homedir = pp;
4038 return;
4039 }
4040 }
4041# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 /*
4044 * Default home dir is C:/
4045 * Best assumption we can make in such a situation.
4046 */
4047 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004048 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004050
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 if (var != NULL)
4052 {
4053#ifdef UNIX
4054 /*
4055 * Change to the directory and get the actual path. This resolves
4056 * links. Don't do it when we can't return.
4057 */
4058 if (mch_dirname(NameBuff, MAXPATHL) == OK
4059 && mch_chdir((char *)NameBuff) == 0)
4060 {
4061 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
4062 var = IObuff;
4063 if (mch_chdir((char *)NameBuff) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004064 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 }
4066#endif
4067 homedir = vim_strsave(var);
4068 }
4069}
4070
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004071#if defined(EXITFREE) || defined(PROTO)
4072 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004073free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004074{
4075 vim_free(homedir);
4076}
Bram Moolenaar24305862012-08-15 14:05:05 +02004077
4078# ifdef FEAT_CMDL_COMPL
4079 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004080free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02004081{
4082 ga_clear_strings(&ga_users);
4083}
4084# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004085#endif
4086
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004088 * Call expand_env() and store the result in an allocated string.
4089 * This is not very memory efficient, this expects the result to be freed
4090 * again soon.
4091 */
4092 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004093expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004094{
4095 return expand_env_save_opt(src, FALSE);
4096}
4097
4098/*
4099 * Idem, but when "one" is TRUE handle the string as one file name, only
4100 * expand "~" at the start.
4101 */
4102 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004103expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004104{
4105 char_u *p;
4106
4107 p = alloc(MAXPATHL);
4108 if (p != NULL)
4109 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
4110 return p;
4111}
4112
4113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 * Expand environment variable with path name.
4115 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004116 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 * If anything fails no expansion is done and dst equals src.
4118 */
4119 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004120expand_env(
4121 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
4122 char_u *dst, /* where to put the result */
4123 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004125 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126}
4127
4128 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004129expand_env_esc(
4130 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
4131 char_u *dst, /* where to put the result */
4132 int dstlen, /* maximum length of the result */
4133 int esc, /* escape spaces in expanded variables */
4134 int one, /* "srcp" is one file name */
4135 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004137 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 char_u *tail;
4139 int c;
4140 char_u *var;
4141 int copy_char;
4142 int mustfree; /* var was allocated, need to free it later */
4143 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004144 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004146 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004147 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004148
4149 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 --dstlen; /* leave one char space for "\," */
4151 while (*src && dstlen > 0)
4152 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004153#ifdef FEAT_EVAL
4154 /* Skip over `=expr`. */
4155 if (src[0] == '`' && src[1] == '=')
4156 {
4157 size_t len;
4158
4159 var = src;
4160 src += 2;
4161 (void)skip_expr(&src);
4162 if (*src == '`')
4163 ++src;
4164 len = src - var;
4165 if (len > (size_t)dstlen)
4166 len = dstlen;
4167 vim_strncpy(dst, var, len);
4168 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02004169 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004170 continue;
4171 }
4172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004174 if ((*src == '$'
4175#ifdef VMS
4176 && at_start
4177#endif
4178 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004179#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 || *src == '%'
4181#endif
4182 || (*src == '~' && at_start))
4183 {
4184 mustfree = FALSE;
4185
4186 /*
4187 * The variable name is copied into dst temporarily, because it may
4188 * be a string in read-only memory and a NUL needs to be appended.
4189 */
4190 if (*src != '~') /* environment var */
4191 {
4192 tail = src + 1;
4193 var = dst;
4194 c = dstlen - 1;
4195
4196#ifdef UNIX
4197 /* Unix has ${var-name} type environment vars */
4198 if (*tail == '{' && !vim_isIDc('{'))
4199 {
4200 tail++; /* ignore '{' */
4201 while (c-- > 0 && *tail && *tail != '}')
4202 *var++ = *tail++;
4203 }
4204 else
4205#endif
4206 {
4207 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004208#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 || (*src == '%' && *tail != '%')
4210#endif
4211 ))
4212 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 }
4215 }
4216
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004217#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218# ifdef UNIX
4219 if (src[1] == '{' && *tail != '}')
4220# else
4221 if (*src == '%' && *tail != '%')
4222# endif
4223 var = NULL;
4224 else
4225 {
4226# ifdef UNIX
4227 if (src[1] == '{')
4228# else
4229 if (*src == '%')
4230#endif
4231 ++tail;
4232#endif
4233 *var = NUL;
4234 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004235#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 }
4237#endif
4238 }
4239 /* home directory */
4240 else if ( src[1] == NUL
4241 || vim_ispathsep(src[1])
4242 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4243 {
4244 var = homedir;
4245 tail = src + 1;
4246 }
4247 else /* user directory */
4248 {
4249#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4250 /*
4251 * Copy ~user to dst[], so we can put a NUL after it.
4252 */
4253 tail = src;
4254 var = dst;
4255 c = dstlen - 1;
4256 while ( c-- > 0
4257 && *tail
4258 && vim_isfilec(*tail)
4259 && !vim_ispathsep(*tail))
4260 *var++ = *tail++;
4261 *var = NUL;
4262# ifdef UNIX
4263 /*
4264 * If the system supports getpwnam(), use it.
4265 * Otherwise, or if getpwnam() fails, the shell is used to
4266 * expand ~user. This is slower and may fail if the shell
4267 * does not support ~user (old versions of /bin/sh).
4268 */
4269# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4270 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004271 /* Note: memory allocated by getpwnam() is never freed.
4272 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01004273 struct passwd *pw = (*dst == NUL)
4274 ? NULL : getpwnam((char *)dst + 1);
4275
4276 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 }
4278 if (var == NULL)
4279# endif
4280 {
4281 expand_T xpc;
4282
4283 ExpandInit(&xpc);
4284 xpc.xp_context = EXPAND_FILES;
4285 var = ExpandOne(&xpc, dst, NULL,
4286 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 mustfree = TRUE;
4288 }
4289
4290# else /* !UNIX, thus VMS */
4291 /*
4292 * USER_HOME is a comma-separated list of
4293 * directories to search for the user account in.
4294 */
4295 {
4296 char_u test[MAXPATHL], paths[MAXPATHL];
4297 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004298 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299
4300 STRCPY(paths, USER_HOME);
4301 next_path = paths;
4302 while (*next_path)
4303 {
4304 for (path = next_path; *next_path && *next_path != ',';
4305 next_path++);
4306 if (*next_path)
4307 *next_path++ = NUL;
4308 STRCPY(test, path);
4309 STRCAT(test, "/");
4310 STRCAT(test, dst + 1);
4311 if (mch_stat(test, &st) == 0)
4312 {
4313 var = alloc(STRLEN(test) + 1);
4314 STRCPY(var, test);
4315 mustfree = TRUE;
4316 break;
4317 }
4318 }
4319 }
4320# endif /* UNIX */
4321#else
4322 /* cannot expand user's home directory, so don't try */
4323 var = NULL;
4324 tail = (char_u *)""; /* for gcc */
4325#endif /* UNIX || VMS */
4326 }
4327
4328#ifdef BACKSLASH_IN_FILENAME
4329 /* If 'shellslash' is set change backslashes to forward slashes.
4330 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4331 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4332 {
4333 char_u *p = vim_strsave(var);
4334
4335 if (p != NULL)
4336 {
4337 if (mustfree)
4338 vim_free(var);
4339 var = p;
4340 mustfree = TRUE;
4341 forward_slash(var);
4342 }
4343 }
4344#endif
4345
4346 /* If "var" contains white space, escape it with a backslash.
4347 * Required for ":e ~/tt" when $HOME includes a space. */
4348 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4349 {
4350 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4351
4352 if (p != NULL)
4353 {
4354 if (mustfree)
4355 vim_free(var);
4356 var = p;
4357 mustfree = TRUE;
4358 }
4359 }
4360
4361 if (var != NULL && *var != NUL
4362 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4363 {
4364 STRCPY(dst, var);
4365 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004366 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 /* if var[] ends in a path separator and tail[] starts
4368 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004369 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4371 && dst[-1] != ':'
4372#endif
4373 && vim_ispathsep(*tail))
4374 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004375 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 src = tail;
4377 copy_char = FALSE;
4378 }
4379 if (mustfree)
4380 vim_free(var);
4381 }
4382
4383 if (copy_char) /* copy at least one char */
4384 {
4385 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004386 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004387 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4388 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 */
4390 at_start = FALSE;
4391 if (src[0] == '\\' && src[1] != NUL)
4392 {
4393 *dst++ = *src++;
4394 --dstlen;
4395 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004396 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02004398 if (dstlen > 0)
4399 {
4400 *dst++ = *src++;
4401 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004402
Bram Moolenaar1c864092017-08-06 18:15:45 +02004403 if (startstr != NULL && src - startstr_len >= srcp
4404 && STRNCMP(src - startstr_len, startstr,
4405 startstr_len) == 0)
4406 at_start = TRUE;
4407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02004409
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 }
4411 *dst = NUL;
4412}
4413
4414/*
4415 * Vim's version of getenv().
4416 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004417 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004418 * "mustfree" is set to TRUE when returned is allocated, it must be
4419 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 */
4421 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004422vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423{
4424 char_u *p;
4425 char_u *pend;
4426 int vimruntime;
4427
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004428#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 /* use "C:/" when $HOME is not set */
4430 if (STRCMP(name, "HOME") == 0)
4431 return homedir;
4432#endif
4433
4434 p = mch_getenv(name);
4435 if (p != NULL && *p == NUL) /* empty is the same as not set */
4436 p = NULL;
4437
4438 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004439 {
4440#if defined(FEAT_MBYTE) && defined(WIN3264)
4441 if (enc_utf8)
4442 {
4443 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004444 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004445
4446 /* Convert from active codepage to UTF-8. Other conversions are
4447 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004448 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004449 if (pp != NULL)
4450 {
4451 p = pp;
4452 *mustfree = TRUE;
4453 }
4454 }
4455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458
4459 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4460 if (!vimruntime && STRCMP(name, "VIM") != 0)
4461 return NULL;
4462
4463 /*
4464 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4465 * Don't do this when default_vimruntime_dir is non-empty.
4466 */
4467 if (vimruntime
4468#ifdef HAVE_PATHDEF
4469 && *default_vimruntime_dir == NUL
4470#endif
4471 )
4472 {
4473 p = mch_getenv((char_u *)"VIM");
4474 if (p != NULL && *p == NUL) /* empty is the same as not set */
4475 p = NULL;
4476 if (p != NULL)
4477 {
4478 p = vim_version_dir(p);
4479 if (p != NULL)
4480 *mustfree = TRUE;
4481 else
4482 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004483
4484#if defined(FEAT_MBYTE) && defined(WIN3264)
4485 if (enc_utf8)
4486 {
4487 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004488 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004489
4490 /* Convert from active codepage to UTF-8. Other conversions
4491 * are not done, because they would fail for non-ASCII
4492 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004493 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004494 if (pp != NULL)
4495 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004496 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004497 vim_free(p);
4498 p = pp;
4499 *mustfree = TRUE;
4500 }
4501 }
4502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 }
4504 }
4505
4506 /*
4507 * When expanding $VIM or $VIMRUNTIME fails, try using:
4508 * - the directory name from 'helpfile' (unless it contains '$')
4509 * - the executable name from argv[0]
4510 */
4511 if (p == NULL)
4512 {
4513 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4514 p = p_hf;
4515#ifdef USE_EXE_NAME
4516 /*
4517 * Use the name of the executable, obtained from argv[0].
4518 */
4519 else
4520 p = exe_name;
4521#endif
4522 if (p != NULL)
4523 {
4524 /* remove the file name */
4525 pend = gettail(p);
4526
4527 /* remove "doc/" from 'helpfile', if present */
4528 if (p == p_hf)
4529 pend = remove_tail(p, pend, (char_u *)"doc");
4530
4531#ifdef USE_EXE_NAME
4532# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004533 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 if (p == exe_name)
4535 {
4536 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004537 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004539 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4540 if (pend1 != pend)
4541 {
4542 pnew = alloc((unsigned)(pend1 - p) + 15);
4543 if (pnew != NULL)
4544 {
4545 STRNCPY(pnew, p, (pend1 - p));
4546 STRCPY(pnew + (pend1 - p), "Resources/vim");
4547 p = pnew;
4548 pend = p + STRLEN(p);
4549 }
4550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 }
4552# endif
4553 /* remove "src/" from exe_name, if present */
4554 if (p == exe_name)
4555 pend = remove_tail(p, pend, (char_u *)"src");
4556#endif
4557
4558 /* for $VIM, remove "runtime/" or "vim54/", if present */
4559 if (!vimruntime)
4560 {
4561 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4562 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4563 }
4564
4565 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004566 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004569#ifdef MACOS_X
4570 if (p == exe_name || p == p_hf)
4571#endif
4572 /* check that the result is a directory name */
4573 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574
4575 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01004576 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 else
4578 {
4579#ifdef USE_EXE_NAME
4580 /* may add "/vim54" or "/runtime" if it exists */
4581 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4582 {
4583 vim_free(p);
4584 p = pend;
4585 }
4586#endif
4587 *mustfree = TRUE;
4588 }
4589 }
4590 }
4591
4592#ifdef HAVE_PATHDEF
4593 /* When there is a pathdef.c file we can use default_vim_dir and
4594 * default_vimruntime_dir */
4595 if (p == NULL)
4596 {
4597 /* Only use default_vimruntime_dir when it is not empty */
4598 if (vimruntime && *default_vimruntime_dir != NUL)
4599 {
4600 p = default_vimruntime_dir;
4601 *mustfree = FALSE;
4602 }
4603 else if (*default_vim_dir != NUL)
4604 {
4605 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4606 *mustfree = TRUE;
4607 else
4608 {
4609 p = default_vim_dir;
4610 *mustfree = FALSE;
4611 }
4612 }
4613 }
4614#endif
4615
4616 /*
4617 * Set the environment variable, so that the new value can be found fast
4618 * next time, and others can also use it (e.g. Perl).
4619 */
4620 if (p != NULL)
4621 {
4622 if (vimruntime)
4623 {
4624 vim_setenv((char_u *)"VIMRUNTIME", p);
4625 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 }
4627 else
4628 {
4629 vim_setenv((char_u *)"VIM", p);
4630 didset_vim = TRUE;
4631 }
4632 }
4633 return p;
4634}
4635
4636/*
4637 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4638 * Return NULL if not, return its name in allocated memory otherwise.
4639 */
4640 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004641vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642{
4643 char_u *p;
4644
4645 if (vimdir == NULL || *vimdir == NUL)
4646 return NULL;
4647 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4648 if (p != NULL && mch_isdir(p))
4649 return p;
4650 vim_free(p);
4651 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4652 if (p != NULL && mch_isdir(p))
4653 return p;
4654 vim_free(p);
4655 return NULL;
4656}
4657
4658/*
4659 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4660 * the length of "name/". Otherwise return "pend".
4661 */
4662 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004663remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664{
4665 int len = (int)STRLEN(name) + 1;
4666 char_u *newend = pend - len;
4667
4668 if (newend >= p
4669 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004670 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 return newend;
4672 return pend;
4673}
4674
Bram Moolenaar113e1072019-01-20 15:30:40 +01004675#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar137374f2018-05-13 15:59:50 +02004676 void
4677vim_unsetenv(char_u *var)
4678{
4679#ifdef HAVE_UNSETENV
4680 unsetenv((char *)var);
4681#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02004682 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02004683#endif
4684}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004685#endif
Bram Moolenaar137374f2018-05-13 15:59:50 +02004686
4687
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 * Our portable version of setenv.
4690 */
4691 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004692vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693{
4694#ifdef HAVE_SETENV
4695 mch_setenv((char *)name, (char *)val, 1);
4696#else
4697 char_u *envbuf;
4698
4699 /*
4700 * Putenv does not copy the string, it has to remain
4701 * valid. The allocated memory will never be freed.
4702 */
4703 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4704 if (envbuf != NULL)
4705 {
4706 sprintf((char *)envbuf, "%s=%s", name, val);
4707 putenv((char *)envbuf);
4708 }
4709#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004710#ifdef FEAT_GETTEXT
4711 /*
4712 * When setting $VIMRUNTIME adjust the directory to find message
4713 * translations to $VIMRUNTIME/lang.
4714 */
4715 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4716 {
4717 char_u *buf = concat_str(val, (char_u *)"/lang");
4718
4719 if (buf != NULL)
4720 {
4721 bindtextdomain(VIMPACKAGE, (char *)buf);
4722 vim_free(buf);
4723 }
4724 }
4725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726}
4727
4728#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4729/*
4730 * Function given to ExpandGeneric() to obtain an environment variable name.
4731 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004733get_env_name(
4734 expand_T *xp UNUSED,
4735 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736{
Bram Moolenaard0573012017-10-28 21:11:06 +02004737# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02004739 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 */
4741 return NULL;
4742# else
4743# ifndef __WIN32__
4744 /* Borland C++ 5.2 has this in a header file. */
4745 extern char **environ;
4746# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004747# define ENVNAMELEN 100
4748 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 char_u *str;
4750 int n;
4751
4752 str = (char_u *)environ[idx];
4753 if (str == NULL)
4754 return NULL;
4755
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004756 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 {
4758 if (str[n] == '=' || str[n] == NUL)
4759 break;
4760 name[n] = str[n];
4761 }
4762 name[n] = NUL;
4763 return name;
4764# endif
4765}
Bram Moolenaar24305862012-08-15 14:05:05 +02004766
4767/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004768 * Add a user name to the list of users in ga_users.
4769 * Do nothing if user name is NULL or empty.
4770 */
4771 static void
4772add_user(char_u *user, int need_copy)
4773{
4774 char_u *user_copy = (user != NULL && need_copy)
4775 ? vim_strsave(user) : user;
4776
4777 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
4778 {
4779 if (need_copy)
4780 vim_free(user);
4781 return;
4782 }
4783 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
4784}
4785
4786/*
Bram Moolenaar24305862012-08-15 14:05:05 +02004787 * Find all user names for user completion.
4788 * Done only once and then cached.
4789 */
4790 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004791init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004792{
Bram Moolenaar24305862012-08-15 14:05:05 +02004793 static int lazy_init_done = FALSE;
4794
4795 if (lazy_init_done)
4796 return;
4797
4798 lazy_init_done = TRUE;
4799 ga_init2(&ga_users, sizeof(char_u *), 20);
4800
4801# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4802 {
Bram Moolenaar24305862012-08-15 14:05:05 +02004803 struct passwd* pw;
4804
4805 setpwent();
4806 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004807 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02004808 endpwent();
4809 }
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004810# elif defined(WIN3264)
4811 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004812 DWORD nusers = 0, ntotal = 0, i;
4813 PUSER_INFO_0 uinfo;
4814
4815 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
4816 &nusers, &ntotal, NULL) == NERR_Success)
4817 {
4818 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004819 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004820
4821 NetApiBufferFree(uinfo);
4822 }
4823 }
Bram Moolenaar24305862012-08-15 14:05:05 +02004824# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004825# if defined(HAVE_GETPWNAM)
4826 {
4827 char_u *user_env = mch_getenv((char_u *)"USER");
4828
4829 // The $USER environment variable may be a valid remote user name (NIS,
4830 // LDAP) not already listed by getpwent(), as getpwent() only lists
4831 // local user names. If $USER is not already listed, check whether it
4832 // is a valid remote user name using getpwnam() and if it is, add it to
4833 // the list of user names.
4834
4835 if (user_env != NULL && *user_env != NUL)
4836 {
4837 int i;
4838
4839 for (i = 0; i < ga_users.ga_len; i++)
4840 {
4841 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
4842
4843 if (STRCMP(local_user, user_env) == 0)
4844 break;
4845 }
4846
4847 if (i == ga_users.ga_len)
4848 {
4849 struct passwd *pw = getpwnam((char *)user_env);
4850
4851 if (pw != NULL)
4852 add_user((char_u *)pw->pw_name, TRUE);
4853 }
4854 }
4855 }
4856# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02004857}
4858
4859/*
4860 * Function given to ExpandGeneric() to obtain an user names.
4861 */
4862 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004863get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004864{
4865 init_users();
4866 if (idx < ga_users.ga_len)
4867 return ((char_u **)ga_users.ga_data)[idx];
4868 return NULL;
4869}
4870
4871/*
4872 * Check whether name matches a user name. Return:
4873 * 0 if name does not match any user name.
4874 * 1 if name partially matches the beginning of a user name.
4875 * 2 is name fully matches a user name.
4876 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02004877 int
4878match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004879{
4880 int i;
4881 int n = (int)STRLEN(name);
4882 int result = 0;
4883
4884 init_users();
4885 for (i = 0; i < ga_users.ga_len; i++)
4886 {
4887 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4888 return 2; /* full match */
4889 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4890 result = 1; /* partial match */
4891 }
4892 return result;
4893}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894#endif
4895
4896/*
4897 * Replace home directory by "~" in each space or comma separated file name in
4898 * 'src'.
4899 * If anything fails (except when out of space) dst equals src.
4900 */
4901 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004902home_replace(
4903 buf_T *buf, /* when not NULL, check for help files */
4904 char_u *src, /* input file name */
4905 char_u *dst, /* where to put the result */
4906 int dstlen, /* maximum length of the result */
4907 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 spaces and commas in the file name. */
4909{
4910 size_t dirlen = 0, envlen = 0;
4911 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004912 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 char_u *p;
4914
4915 if (src == NULL)
4916 {
4917 *dst = NUL;
4918 return;
4919 }
4920
4921 /*
4922 * If the file is a help file, remove the path completely.
4923 */
4924 if (buf != NULL && buf->b_help)
4925 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02004926 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 return;
4928 }
4929
4930 /*
4931 * We check both the value of the $HOME environment variable and the
4932 * "real" home directory.
4933 */
4934 if (homedir != NULL)
4935 dirlen = STRLEN(homedir);
4936
4937#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004938 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004940 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4941#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004942#ifdef WIN3264
4943 if (homedir_env == NULL)
4944 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
4945#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004946 /* Empty is the same as not set. */
4947 if (homedir_env != NULL && *homedir_env == NUL)
4948 homedir_env = NULL;
4949
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004950#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaaref0a1d52018-12-30 11:38:57 +01004951 if (homedir_env != NULL && *homedir_env == '~')
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004952 {
4953 int usedlen = 0;
4954 int flen;
4955 char_u *fbuf = NULL;
4956
4957 flen = (int)STRLEN(homedir_env);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004958 (void)modify_fname((char_u *)":p", FALSE, &usedlen,
Bram Moolenaard12f8112012-06-20 17:56:09 +02004959 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004960 flen = (int)STRLEN(homedir_env);
4961 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4962 /* Remove the trailing / that is added to a directory. */
4963 homedir_env[flen - 1] = NUL;
4964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965#endif
4966
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 if (homedir_env != NULL)
4968 envlen = STRLEN(homedir_env);
4969
4970 if (!one)
4971 src = skipwhite(src);
4972 while (*src && dstlen > 0)
4973 {
4974 /*
4975 * Here we are at the beginning of a file name.
4976 * First, check to see if the beginning of the file name matches
4977 * $HOME or the "real" home directory. Check that there is a '/'
4978 * after the match (so that if e.g. the file is "/home/pieter/bla",
4979 * and the home directory is "/home/piet", the file does not end up
4980 * as "~er/bla" (which would seem to indicate the file "bla" in user
4981 * er's home directory)).
4982 */
4983 p = homedir;
4984 len = dirlen;
4985 for (;;)
4986 {
4987 if ( len
4988 && fnamencmp(src, p, len) == 0
4989 && (vim_ispathsep(src[len])
4990 || (!one && (src[len] == ',' || src[len] == ' '))
4991 || src[len] == NUL))
4992 {
4993 src += len;
4994 if (--dstlen > 0)
4995 *dst++ = '~';
4996
4997 /*
4998 * If it's just the home directory, add "/".
4999 */
5000 if (!vim_ispathsep(src[0]) && --dstlen > 0)
5001 *dst++ = '/';
5002 break;
5003 }
5004 if (p == homedir_env)
5005 break;
5006 p = homedir_env;
5007 len = envlen;
5008 }
5009
5010 /* if (!one) skip to separator: space or comma */
5011 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
5012 *dst++ = *src++;
5013 /* skip separator */
5014 while ((*src == ' ' || *src == ',') && --dstlen > 0)
5015 *dst++ = *src++;
5016 }
5017 /* if (dstlen == 0) out of space, what to do??? */
5018
5019 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02005020
5021 if (homedir_env != homedir_env_orig)
5022 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023}
5024
5025/*
5026 * Like home_replace, store the replaced string in allocated memory.
5027 * When something fails, NULL is returned.
5028 */
5029 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005030home_replace_save(
5031 buf_T *buf, /* when not NULL, check for help files */
5032 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033{
5034 char_u *dst;
5035 unsigned len;
5036
5037 len = 3; /* space for "~/" and trailing NUL */
5038 if (src != NULL) /* just in case */
5039 len += (unsigned)STRLEN(src);
5040 dst = alloc(len);
5041 if (dst != NULL)
5042 home_replace(buf, src, dst, len, TRUE);
5043 return dst;
5044}
5045
5046/*
5047 * Compare two file names and return:
5048 * FPC_SAME if they both exist and are the same file.
5049 * FPC_SAMEX if they both don't exist and have the same file name.
5050 * FPC_DIFF if they both exist and are different files.
5051 * FPC_NOTX if they both don't exist.
5052 * FPC_DIFFX if one of them doesn't exist.
5053 * For the first name environment variables are expanded
5054 */
5055 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005056fullpathcmp(
5057 char_u *s1,
5058 char_u *s2,
5059 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060{
5061#ifdef UNIX
5062 char_u exp1[MAXPATHL];
5063 char_u full1[MAXPATHL];
5064 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02005065 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 int r1, r2;
5067
5068 expand_env(s1, exp1, MAXPATHL);
5069 r1 = mch_stat((char *)exp1, &st1);
5070 r2 = mch_stat((char *)s2, &st2);
5071 if (r1 != 0 && r2 != 0)
5072 {
5073 /* if mch_stat() doesn't work, may compare the names */
5074 if (checkname)
5075 {
5076 if (fnamecmp(exp1, s2) == 0)
5077 return FPC_SAMEX;
5078 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
5079 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
5080 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
5081 return FPC_SAMEX;
5082 }
5083 return FPC_NOTX;
5084 }
5085 if (r1 != 0 || r2 != 0)
5086 return FPC_DIFFX;
5087 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
5088 return FPC_SAME;
5089 return FPC_DIFF;
5090#else
5091 char_u *exp1; /* expanded s1 */
5092 char_u *full1; /* full path of s1 */
5093 char_u *full2; /* full path of s2 */
5094 int retval = FPC_DIFF;
5095 int r1, r2;
5096
5097 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
5098 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
5099 {
5100 full1 = exp1 + MAXPATHL;
5101 full2 = full1 + MAXPATHL;
5102
5103 expand_env(s1, exp1, MAXPATHL);
5104 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
5105 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
5106
5107 /* If vim_FullName() fails, the file probably doesn't exist. */
5108 if (r1 != OK && r2 != OK)
5109 {
5110 if (checkname && fnamecmp(exp1, s2) == 0)
5111 retval = FPC_SAMEX;
5112 else
5113 retval = FPC_NOTX;
5114 }
5115 else if (r1 != OK || r2 != OK)
5116 retval = FPC_DIFFX;
5117 else if (fnamecmp(full1, full2))
5118 retval = FPC_DIFF;
5119 else
5120 retval = FPC_SAME;
5121 vim_free(exp1);
5122 }
5123 return retval;
5124#endif
5125}
5126
5127/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005128 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02005129 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005130 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 */
5132 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005133gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134{
5135 char_u *p1, *p2;
5136
5137 if (fname == NULL)
5138 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01005139 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01005141 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005143 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 }
5145 return p1;
5146}
5147
Bram Moolenaar31710262010-08-13 13:36:15 +02005148#if defined(FEAT_SEARCHPATH)
Bram Moolenaar31710262010-08-13 13:36:15 +02005149/*
5150 * Return the end of the directory name, on the first path
5151 * separator:
5152 * "/path/file", "/path/dir/", "/path//dir", "/file"
5153 * ^ ^ ^ ^
5154 */
5155 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005156gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02005157{
5158 char_u *dir_end = fname;
5159 char_u *next_dir_end = fname;
5160 int look_for_sep = TRUE;
5161 char_u *p;
5162
5163 for (p = fname; *p != NUL; )
5164 {
5165 if (vim_ispathsep(*p))
5166 {
5167 if (look_for_sep)
5168 {
5169 next_dir_end = p;
5170 look_for_sep = FALSE;
5171 }
5172 }
5173 else
5174 {
5175 if (!look_for_sep)
5176 dir_end = next_dir_end;
5177 look_for_sep = TRUE;
5178 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005179 MB_PTR_ADV(p);
Bram Moolenaar31710262010-08-13 13:36:15 +02005180 }
5181 return dir_end;
5182}
5183#endif
5184
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005186 * Get pointer to tail of "fname", including path separators. Putting a NUL
5187 * here leaves the directory name. Takes care of "c:/" and "//".
5188 * Always returns a valid pointer.
5189 */
5190 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005191gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005192{
5193 char_u *p;
5194 char_u *t;
5195
5196 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
5197 t = gettail(fname);
5198 while (t > p && after_pathsep(fname, t))
5199 --t;
5200#ifdef VMS
5201 /* path separator is part of the path */
5202 ++t;
5203#endif
5204 return t;
5205}
5206
5207/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 * get the next path component (just after the next path separator).
5209 */
5210 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005211getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212{
5213 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005214 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 if (*fname)
5216 ++fname;
5217 return fname;
5218}
5219
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220/*
5221 * Get a pointer to one character past the head of a path name.
5222 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
5223 * If there is no head, path is returned.
5224 */
5225 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005226get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227{
5228 char_u *retval;
5229
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005230#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 /* may skip "c:" */
5232 if (isalpha(path[0]) && path[1] == ':')
5233 retval = path + 2;
5234 else
5235 retval = path;
5236#else
5237# if defined(AMIGA)
5238 /* may skip "label:" */
5239 retval = vim_strchr(path, ':');
5240 if (retval == NULL)
5241 retval = path;
5242# else /* Unix */
5243 retval = path;
5244# endif
5245#endif
5246
5247 while (vim_ispathsep(*retval))
5248 ++retval;
5249
5250 return retval;
5251}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252
5253/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01005254 * Return TRUE if 'c' is a path separator.
5255 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 */
5257 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005258vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259{
Bram Moolenaare60acc12011-05-10 16:41:25 +02005260#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02005262#else
5263# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005265# else
5266# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
5268 return (c == ':' || c == '[' || c == ']' || c == '/'
5269 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02005270# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005272# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02005274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275}
5276
Bram Moolenaar69c35002013-11-04 02:54:12 +01005277/*
5278 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
5279 */
5280 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005281vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01005282{
5283 return vim_ispathsep(c)
5284#ifdef BACKSLASH_IN_FILENAME
5285 && c != ':'
5286#endif
5287 ;
5288}
5289
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5291/*
5292 * return TRUE if 'c' is a path list separator.
5293 */
5294 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005295vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296{
5297#ifdef UNIX
5298 return (c == ':');
5299#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005300 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301#endif
5302}
5303#endif
5304
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005305/*
5306 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5307 * It's done in-place.
5308 */
5309 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005310shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005311{
5312 char_u *tail, *s, *d;
5313 int skip = FALSE;
5314
5315 tail = gettail(str);
5316 d = str;
5317 for (s = str; ; ++s)
5318 {
5319 if (s >= tail) /* copy the whole tail */
5320 {
5321 *d++ = *s;
5322 if (*s == NUL)
5323 break;
5324 }
5325 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5326 {
5327 *d++ = *s;
5328 skip = FALSE;
5329 }
5330 else if (!skip)
5331 {
5332 *d++ = *s; /* copy next char */
5333 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5334 skip = TRUE;
5335# ifdef FEAT_MBYTE
5336 if (has_mbyte)
5337 {
5338 int l = mb_ptr2len(s);
5339
5340 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005341 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005342 }
5343# endif
5344 }
5345 }
5346}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005347
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005348/*
5349 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5350 * Also returns TRUE if there is no directory name.
5351 * "fname" must be writable!.
5352 */
5353 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005354dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005355{
5356 char_u *p;
5357 int c;
5358 int retval;
5359
5360 p = gettail_sep(fname);
5361 if (p == fname)
5362 return TRUE;
5363 c = *p;
5364 *p = NUL;
5365 retval = mch_isdir(fname);
5366 *p = c;
5367 return retval;
5368}
5369
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005371 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5372 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 */
5374 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005375vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005377#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005379#else
5380 if (p_fic)
5381 return MB_STRICMP(x, y);
5382 return STRCMP(x, y);
5383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384}
5385
5386 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005387vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005389#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005390 char_u *px = x;
5391 char_u *py = y;
5392 int cx = NUL;
5393 int cy = NUL;
5394
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005395 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005397 cx = PTR2CHAR(px);
5398 cy = PTR2CHAR(py);
5399 if (cx == NUL || cy == NUL
5400 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5401 && !(cx == '/' && cy == '\\')
5402 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005404 len -= MB_PTR2LEN(px);
5405 px += MB_PTR2LEN(px);
5406 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 }
5408 if (len == 0)
5409 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005410 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005411#else
5412 if (p_fic)
5413 return MB_STRNICMP(x, y, len);
5414 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005416}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417
5418/*
5419 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005420 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 */
5422 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005423concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424{
5425 char_u *dest;
5426
5427 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5428 if (dest != NULL)
5429 {
5430 STRCPY(dest, fname1);
5431 if (sep)
5432 add_pathsep(dest);
5433 STRCAT(dest, fname2);
5434 }
5435 return dest;
5436}
5437
Bram Moolenaard6754642005-01-17 22:18:45 +00005438/*
5439 * Concatenate two strings and return the result in allocated memory.
5440 * Returns NULL when out of memory.
5441 */
5442 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005443concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005444{
5445 char_u *dest;
5446 size_t l = STRLEN(str1);
5447
5448 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5449 if (dest != NULL)
5450 {
5451 STRCPY(dest, str1);
5452 STRCPY(dest + l, str2);
5453 }
5454 return dest;
5455}
Bram Moolenaard6754642005-01-17 22:18:45 +00005456
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457/*
5458 * Add a path separator to a file name, unless it already ends in a path
5459 * separator.
5460 */
5461 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005462add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005464 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 STRCAT(p, PATHSEPSTR);
5466}
5467
5468/*
5469 * FullName_save - Make an allocated copy of a full file name.
5470 * Returns NULL when out of memory.
5471 */
5472 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005473FullName_save(
5474 char_u *fname,
5475 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005476 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477{
5478 char_u *buf;
5479 char_u *new_fname = NULL;
5480
5481 if (fname == NULL)
5482 return NULL;
5483
5484 buf = alloc((unsigned)MAXPATHL);
5485 if (buf != NULL)
5486 {
5487 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5488 new_fname = vim_strsave(buf);
5489 else
5490 new_fname = vim_strsave(fname);
5491 vim_free(buf);
5492 }
5493 return new_fname;
5494}
5495
5496#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5497
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005498static char_u *skip_string(char_u *p);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005499static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500
5501/*
5502 * Find the start of a comment, not knowing if we are in a comment right now.
5503 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005504 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005506 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005507ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005508{
5509 return find_start_comment(curbuf->b_ind_maxcomment);
5510}
5511
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005513find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514{
5515 pos_T *pos;
5516 char_u *line;
5517 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005518 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005520 for (;;)
5521 {
5522 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5523 if (pos == NULL)
5524 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005526 /*
5527 * Check if the comment start we found is inside a string.
5528 * If it is then restrict the search to below this line and try again.
5529 */
5530 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005531 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005532 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005533 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005534 break;
5535 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5536 if (cur_maxcomment <= 0)
5537 {
5538 pos = NULL;
5539 break;
5540 }
5541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 return pos;
5543}
5544
5545/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005546 * Find the start of a comment or raw string, not knowing if we are in a
5547 * comment or raw string right now.
5548 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaardde81312017-08-26 17:49:01 +02005549 * If is_raw is given and returns start of raw_string, sets it to true.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005550 * Return NULL when not inside a comment or raw string.
5551 * "CORS" -> Comment Or Raw String
5552 */
5553 static pos_T *
Bram Moolenaardde81312017-08-26 17:49:01 +02005554ind_find_start_CORS(linenr_T *is_raw) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005555{
Bram Moolenaar089af182015-10-07 11:41:49 +02005556 static pos_T comment_pos_copy;
5557 pos_T *comment_pos;
5558 pos_T *rs_pos;
5559
5560 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5561 if (comment_pos != NULL)
5562 {
5563 /* Need to make a copy of the static pos in findmatchlimit(),
5564 * calling find_start_rawstring() may change it. */
5565 comment_pos_copy = *comment_pos;
5566 comment_pos = &comment_pos_copy;
5567 }
5568 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005569
5570 /* If comment_pos is before rs_pos the raw string is inside the comment.
5571 * If rs_pos is before comment_pos the comment is inside the raw string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005572 if (comment_pos == NULL || (rs_pos != NULL
5573 && LT_POS(*rs_pos, *comment_pos)))
Bram Moolenaardde81312017-08-26 17:49:01 +02005574 {
5575 if (is_raw != NULL && rs_pos != NULL)
5576 *is_raw = rs_pos->lnum;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005577 return rs_pos;
Bram Moolenaardde81312017-08-26 17:49:01 +02005578 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005579 return comment_pos;
5580}
5581
5582/*
5583 * Find the start of a raw string, not knowing if we are in one right now.
5584 * Search starts at w_cursor.lnum and goes backwards.
5585 * Return NULL when not inside a raw string.
5586 */
5587 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005588find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005589{
5590 pos_T *pos;
5591 char_u *line;
5592 char_u *p;
5593 int cur_maxcomment = ind_maxcomment;
5594
5595 for (;;)
5596 {
5597 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5598 if (pos == NULL)
5599 break;
5600
5601 /*
5602 * Check if the raw string start we found is inside a string.
5603 * If it is then restrict the search to below this line and try again.
5604 */
5605 line = ml_get(pos->lnum);
5606 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5607 p = skip_string(p);
5608 if ((colnr_T)(p - line) <= pos->col)
5609 break;
5610 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5611 if (cur_maxcomment <= 0)
5612 {
5613 pos = NULL;
5614 break;
5615 }
5616 }
5617 return pos;
5618}
5619
5620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 * Skip to the end of a "string" and a 'c' character.
5622 * If there is no string or character, return argument unmodified.
5623 */
5624 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005625skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626{
5627 int i;
5628
5629 /*
5630 * We loop, because strings may be concatenated: "date""time".
5631 */
5632 for ( ; ; ++p)
5633 {
5634 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5635 {
5636 if (!p[1]) /* ' at end of line */
5637 break;
5638 i = 2;
5639 if (p[1] == '\\') /* '\n' or '\000' */
5640 {
5641 ++i;
5642 while (vim_isdigit(p[i - 1])) /* '\000' */
5643 ++i;
5644 }
5645 if (p[i] == '\'') /* check for trailing ' */
5646 {
5647 p += i;
5648 continue;
5649 }
5650 }
5651 else if (p[0] == '"') /* start of string */
5652 {
5653 for (++p; p[0]; ++p)
5654 {
5655 if (p[0] == '\\' && p[1] != NUL)
5656 ++p;
5657 else if (p[0] == '"') /* end of string */
5658 break;
5659 }
5660 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005661 continue; /* continue for another string */
5662 }
5663 else if (p[0] == 'R' && p[1] == '"')
5664 {
5665 /* Raw string: R"[delim](...)[delim]" */
5666 char_u *delim = p + 2;
5667 char_u *paren = vim_strchr(delim, '(');
5668
5669 if (paren != NULL)
5670 {
5671 size_t delim_len = paren - delim;
5672
5673 for (p += 3; *p; ++p)
5674 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5675 && p[delim_len + 1] == '"')
5676 {
5677 p += delim_len + 1;
5678 break;
5679 }
5680 if (p[0] == '"')
5681 continue; /* continue for another string */
5682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 }
5684 break; /* no string found */
5685 }
5686 if (!*p)
5687 --p; /* backup from NUL */
5688 return p;
5689}
5690#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5691
5692#if defined(FEAT_CINDENT) || defined(PROTO)
5693
5694/*
5695 * Do C or expression indenting on the current line.
5696 */
5697 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005698do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699{
5700# ifdef FEAT_EVAL
5701 if (*curbuf->b_p_inde != NUL)
5702 fixthisline(get_expr_indent);
5703 else
5704# endif
5705 fixthisline(get_c_indent);
5706}
5707
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005708/* Find result cache for cpp_baseclass */
5709typedef struct {
5710 int found;
5711 lpos_T lpos;
5712} cpp_baseclass_cache_T;
5713
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714/*
5715 * Functions for C-indenting.
5716 * Most of this originally comes from Eric Fischer.
5717 */
5718/*
5719 * Below "XXX" means that this function may unlock the current line.
5720 */
5721
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005722static int cin_isdefault(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005723static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005724static int cin_iscomment(char_u *);
5725static int cin_islinecomment(char_u *);
5726static int cin_isterminated(char_u *, int, int);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005727static int cin_iselse(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005728static int cin_ends_in(char_u *, char_u *, char_u *);
5729static int cin_starts_with(char_u *s, char *word);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005730static pos_T *find_match_paren(int);
5731static pos_T *find_match_char(int c, int ind_maxparen);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005732static int find_last_paren(char_u *l, int start, int end);
5733static int find_match(int lookfor, linenr_T ourscope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734
5735/*
5736 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005737 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 */
5739 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005740cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741{
5742 while (*s)
5743 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005744 char_u *prev_s = s;
5745
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005747
5748 /* Perl/shell # comment comment continues until eol. Require a space
5749 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005750 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005751 {
5752 s += STRLEN(s);
5753 break;
5754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 if (*s != '/')
5756 break;
5757 ++s;
5758 if (*s == '/') /* slash-slash comment continues till eol */
5759 {
5760 s += STRLEN(s);
5761 break;
5762 }
5763 if (*s != '*')
5764 break;
5765 for (++s; *s; ++s) /* skip slash-star comment */
5766 if (s[0] == '*' && s[1] == '/')
5767 {
5768 s += 2;
5769 break;
5770 }
5771 }
5772 return s;
5773}
5774
5775/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005776 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 * not considered code.
5778 */
5779 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005780cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781{
5782 return *cin_skipcomment(s) == NUL;
5783}
5784
5785/*
5786 * Check previous lines for a "//" line comment, skipping over blank lines.
5787 */
5788 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005789find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790{
5791 static pos_T pos;
5792 char_u *line;
5793 char_u *p;
5794
5795 pos = curwin->w_cursor;
5796 while (--pos.lnum > 0)
5797 {
5798 line = ml_get(pos.lnum);
5799 p = skipwhite(line);
5800 if (cin_islinecomment(p))
5801 {
5802 pos.col = (int)(p - line);
5803 return &pos;
5804 }
5805 if (*p != NUL)
5806 break;
5807 }
5808 return NULL;
5809}
5810
5811/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005812 * Return TRUE if "text" starts with "key:".
5813 */
5814 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005815cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005816{
5817 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005818 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005819
5820 if (*s == '\'' || *s == '"')
5821 {
5822 /* can be 'key': or "key": */
5823 quote = *s;
5824 ++s;
5825 }
5826 if (!vim_isIDc(*s)) /* need at least one ID character */
5827 return FALSE;
5828
5829 while (vim_isIDc(*s))
5830 ++s;
5831 if (*s == quote)
5832 ++s;
5833
5834 s = cin_skipcomment(s);
5835
5836 /* "::" is not a label, it's C++ */
5837 return (*s == ':' && s[1] != ':');
5838}
5839
5840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005842 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 */
5844 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005845cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846{
5847 if (!vim_isIDc(**s)) /* need at least one ID character */
5848 return FALSE;
5849
5850 while (vim_isIDc(**s))
5851 (*s)++;
5852
5853 *s = cin_skipcomment(*s);
5854
5855 /* "::" is not a label, it's C++ */
5856 return (**s == ':' && *++*s != ':');
5857}
5858
5859/*
5860 * Recognize a label: "label:".
5861 * Note: curwin->w_cursor must be where we are looking for the label.
5862 */
5863 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005864cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865{
5866 char_u *s;
5867
5868 s = cin_skipcomment(ml_get_curline());
5869
5870 /*
5871 * Exclude "default" from labels, since it should be indented
5872 * like a switch label. Same for C++ scope declarations.
5873 */
5874 if (cin_isdefault(s))
5875 return FALSE;
5876 if (cin_isscopedecl(s))
5877 return FALSE;
5878
5879 if (cin_islabel_skip(&s))
5880 {
5881 /*
5882 * Only accept a label if the previous line is terminated or is a case
5883 * label.
5884 */
5885 pos_T cursor_save;
5886 pos_T *trypos;
5887 char_u *line;
5888
5889 cursor_save = curwin->w_cursor;
5890 while (curwin->w_cursor.lnum > 1)
5891 {
5892 --curwin->w_cursor.lnum;
5893
5894 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005895 * If we're in a comment or raw string now, skip to the start of
5896 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 */
5898 curwin->w_cursor.col = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02005899 if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 curwin->w_cursor = *trypos;
5901
5902 line = ml_get_curline();
5903 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5904 continue;
5905 if (*(line = cin_skipcomment(line)) == NUL)
5906 continue;
5907
5908 curwin->w_cursor = cursor_save;
5909 if (cin_isterminated(line, TRUE, FALSE)
5910 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005911 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 || (cin_islabel_skip(&line) && cin_nocode(line)))
5913 return TRUE;
5914 return FALSE;
5915 }
5916 curwin->w_cursor = cursor_save;
5917 return TRUE; /* label at start of file??? */
5918 }
5919 return FALSE;
5920}
5921
5922/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005923 * Recognize structure initialization and enumerations:
5924 * "[typedef] [static|public|protected|private] enum"
5925 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 */
5927 static int
5928cin_isinit(void)
5929{
5930 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005931 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932
5933 s = cin_skipcomment(ml_get_curline());
5934
Bram Moolenaar75342212013-03-07 13:13:52 +01005935 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 s = cin_skipcomment(s + 7);
5937
Bram Moolenaar75342212013-03-07 13:13:52 +01005938 for (;;)
5939 {
5940 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005941
Bram Moolenaar75342212013-03-07 13:13:52 +01005942 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5943 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005944 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005945 if (cin_starts_with(s, skip[i]))
5946 {
5947 s = cin_skipcomment(s + l);
5948 l = 0;
5949 break;
5950 }
5951 }
5952 if (l != 0)
5953 break;
5954 }
5955
5956 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 return TRUE;
5958
5959 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5960 return TRUE;
5961
5962 return FALSE;
5963}
5964
5965/*
5966 * Recognize a switch label: "case .*:" or "default:".
5967 */
5968 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005969cin_iscase(
5970 char_u *s,
5971 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972{
5973 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005974 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 {
5976 for (s += 4; *s; ++s)
5977 {
5978 s = cin_skipcomment(s);
5979 if (*s == ':')
5980 {
5981 if (s[1] == ':') /* skip over "::" for C++ */
5982 ++s;
5983 else
5984 return TRUE;
5985 }
5986 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005987 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5989 return FALSE; /* stop at comment */
5990 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005991 {
5992 /* JS etc. */
5993 if (strict)
5994 return FALSE; /* stop at string */
5995 else
5996 return TRUE;
5997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 }
5999 return FALSE;
6000 }
6001
6002 if (cin_isdefault(s))
6003 return TRUE;
6004 return FALSE;
6005}
6006
6007/*
6008 * Recognize a "default" switch label.
6009 */
6010 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006011cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012{
6013 return (STRNCMP(s, "default", 7) == 0
6014 && *(s = cin_skipcomment(s + 7)) == ':'
6015 && s[1] != ':');
6016}
6017
6018/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02006019 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 */
6021 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006022cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023{
6024 int i;
6025
6026 s = cin_skipcomment(s);
6027 if (STRNCMP(s, "public", 6) == 0)
6028 i = 6;
6029 else if (STRNCMP(s, "protected", 9) == 0)
6030 i = 9;
6031 else if (STRNCMP(s, "private", 7) == 0)
6032 i = 7;
6033 else
6034 return FALSE;
6035 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
6036}
6037
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006038/* Maximum number of lines to search back for a "namespace" line. */
6039#define FIND_NAMESPACE_LIM 20
6040
6041/*
6042 * Recognize a "namespace" scope declaration.
6043 */
6044 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006045cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006046{
6047 char_u *p;
6048 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006049 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006050
6051 s = cin_skipcomment(s);
6052 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
6053 {
6054 p = cin_skipcomment(skipwhite(s + 9));
6055 while (*p != NUL)
6056 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006057 if (VIM_ISWHITE(*p))
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006058 {
6059 has_name = TRUE; /* found end of a name */
6060 p = cin_skipcomment(skipwhite(p));
6061 }
6062 else if (*p == '{')
6063 {
6064 break;
6065 }
6066 else if (vim_iswordc(*p))
6067 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006068 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006069 if (has_name)
6070 return FALSE; /* word character after skipping past name */
6071 ++p;
6072 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006073 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
6074 {
6075 if (!has_name_start || has_name)
6076 return FALSE;
6077 /* C++ 17 nested namespace */
6078 p += 3;
6079 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006080 else
6081 {
6082 return FALSE;
6083 }
6084 }
6085 return TRUE;
6086 }
6087 return FALSE;
6088}
6089
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090/*
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006091 * Recognize a `extern "C"` or `extern "C++"` linkage specifications.
6092 */
6093 static int
6094cin_is_cpp_extern_c(char_u *s)
6095{
6096 char_u *p;
6097 int has_string_literal = FALSE;
6098
6099 s = cin_skipcomment(s);
6100 if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
6101 {
6102 p = cin_skipcomment(skipwhite(s + 6));
6103 while (*p != NUL)
6104 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006105 if (VIM_ISWHITE(*p))
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006106 {
6107 p = cin_skipcomment(skipwhite(p));
6108 }
6109 else if (*p == '{')
6110 {
6111 break;
6112 }
6113 else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
6114 {
6115 if (has_string_literal)
6116 return FALSE;
6117 has_string_literal = TRUE;
6118 p += 3;
6119 }
6120 else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
6121 && p[4] == '"')
6122 {
6123 if (has_string_literal)
6124 return FALSE;
6125 has_string_literal = TRUE;
6126 p += 5;
6127 }
6128 else
6129 {
6130 return FALSE;
6131 }
6132 }
6133 return has_string_literal ? TRUE : FALSE;
6134 }
6135 return FALSE;
6136}
6137
6138/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 * Return a pointer to the first non-empty non-comment character after a ':'.
6140 * Return NULL if not found.
6141 * case 234: a = b;
6142 * ^
6143 */
6144 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006145after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146{
6147 for ( ; *l; ++l)
6148 {
6149 if (*l == ':')
6150 {
6151 if (l[1] == ':') /* skip over "::" for C++ */
6152 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006153 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 break;
6155 }
6156 else if (*l == '\'' && l[1] && l[2] == '\'')
6157 l += 2; /* skip over 'x' */
6158 }
6159 if (*l == NUL)
6160 return NULL;
6161 l = cin_skipcomment(l + 1);
6162 if (*l == NUL)
6163 return NULL;
6164 return l;
6165}
6166
6167/*
6168 * Get indent of line "lnum", skipping a label.
6169 * Return 0 if there is nothing after the label.
6170 */
6171 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006172get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173{
6174 char_u *l;
6175 pos_T fp;
6176 colnr_T col;
6177 char_u *p;
6178
6179 l = ml_get(lnum);
6180 p = after_label(l);
6181 if (p == NULL)
6182 return 0;
6183
6184 fp.col = (colnr_T)(p - l);
6185 fp.lnum = lnum;
6186 getvcol(curwin, &fp, &col, NULL, NULL);
6187 return (int)col;
6188}
6189
6190/*
6191 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006192 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 * label: if (asdf && asdfasdf)
6194 * ^
6195 */
6196 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006197skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198{
6199 char_u *l;
6200 int amount;
6201 pos_T cursor_save;
6202
6203 cursor_save = curwin->w_cursor;
6204 curwin->w_cursor.lnum = lnum;
6205 l = ml_get_curline();
6206 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006207 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 {
6209 amount = get_indent_nolabel(lnum);
6210 l = after_label(ml_get_curline());
6211 if (l == NULL) /* just in case */
6212 l = ml_get_curline();
6213 }
6214 else
6215 {
6216 amount = get_indent();
6217 l = ml_get_curline();
6218 }
6219 *pp = l;
6220
6221 curwin->w_cursor = cursor_save;
6222 return amount;
6223}
6224
6225/*
6226 * Return the indent of the first variable name after a type in a declaration.
6227 * int a, indent of "a"
6228 * static struct foo b, indent of "b"
6229 * enum bla c, indent of "c"
6230 * Returns zero when it doesn't look like a declaration.
6231 */
6232 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006233cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234{
6235 char_u *line, *p, *s;
6236 int len;
6237 pos_T fp;
6238 colnr_T col;
6239
6240 line = ml_get_curline();
6241 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006242 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 if (len == 6 && STRNCMP(p, "static", 6) == 0)
6244 {
6245 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006246 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 }
6248 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
6249 p = skipwhite(p + 6);
6250 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
6251 p = skipwhite(p + 4);
6252 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
6253 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
6254 {
6255 s = skipwhite(p + len);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006256 if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3]))
6257 || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4]))
6258 || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5]))
6259 || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 p = s;
6261 }
6262 for (len = 0; vim_isIDc(p[len]); ++len)
6263 ;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006264 if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 return 0;
6266
6267 p = skipwhite(p + len);
6268 fp.lnum = curwin->w_cursor.lnum;
6269 fp.col = (colnr_T)(p - line);
6270 getvcol(curwin, &fp, &col, NULL, NULL);
6271 return (int)col;
6272}
6273
6274/*
6275 * Return the indent of the first non-blank after an equal sign.
6276 * char *foo = "here";
6277 * Return zero if no (useful) equal sign found.
6278 * Return -1 if the line above "lnum" ends in a backslash.
6279 * foo = "asdf\
6280 * asdf\
6281 * here";
6282 */
6283 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006284cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285{
6286 char_u *line;
6287 char_u *s;
6288 colnr_T col;
6289 pos_T fp;
6290
6291 if (lnum > 1)
6292 {
6293 line = ml_get(lnum - 1);
6294 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6295 return -1;
6296 }
6297
6298 line = s = ml_get(lnum);
6299 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
6300 {
6301 if (cin_iscomment(s)) /* ignore comments */
6302 s = cin_skipcomment(s);
6303 else
6304 ++s;
6305 }
6306 if (*s != '=')
6307 return 0;
6308
6309 s = skipwhite(s + 1);
6310 if (cin_nocode(s))
6311 return 0;
6312
6313 if (*s == '"') /* nice alignment for continued strings */
6314 ++s;
6315
6316 fp.lnum = lnum;
6317 fp.col = (colnr_T)(s - line);
6318 getvcol(curwin, &fp, &col, NULL, NULL);
6319 return (int)col;
6320}
6321
6322/*
6323 * Recognize a preprocessor statement: Any line that starts with '#'.
6324 */
6325 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006326cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006328 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 return TRUE;
6330 return FALSE;
6331}
6332
6333/*
6334 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6335 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6336 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006337 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 */
6339 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006340cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341{
6342 char_u *line = *pp;
6343 linenr_T lnum = *lnump;
6344 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006345 int candidate_amount = *amount;
6346
6347 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6348 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006350 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 {
6352 if (cin_ispreproc(line))
6353 {
6354 retval = TRUE;
6355 *lnump = lnum;
6356 break;
6357 }
6358 if (lnum == 1)
6359 break;
6360 line = ml_get(--lnum);
6361 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6362 break;
6363 }
6364
6365 if (lnum != *lnump)
6366 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006367 if (retval)
6368 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 return retval;
6370}
6371
6372/*
6373 * Recognize the start of a C or C++ comment.
6374 */
6375 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006376cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377{
6378 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6379}
6380
6381/*
6382 * Recognize the start of a "//" comment.
6383 */
6384 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006385cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386{
6387 return (p[0] == '/' && p[1] == '/');
6388}
6389
6390/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006391 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6392 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006394 * If a line begins with an "else", only consider it terminated if no unmatched
6395 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 * Return the character terminating the line (ending char's have precedence if
6397 * both apply in order to determine initializations).
6398 */
6399 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006400cin_isterminated(
6401 char_u *s,
6402 int incl_open, /* include '{' at the end as terminator */
6403 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006405 char_u found_start = 0;
6406 unsigned n_open = 0;
6407 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408
6409 s = cin_skipcomment(s);
6410
6411 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6412 found_start = *s;
6413
Bram Moolenaar496f9512011-05-19 16:35:09 +02006414 if (!found_start)
6415 is_else = cin_iselse(s);
6416
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 while (*s)
6418 {
6419 /* skip over comments, "" strings and 'c'haracters */
6420 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006421 if (*s == '}' && n_open > 0)
6422 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006423 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006424 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 && cin_nocode(s + 1))
6426 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006427 else if (*s == '{')
6428 {
6429 if (incl_open && cin_nocode(s + 1))
6430 return *s;
6431 else
6432 ++n_open;
6433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434
6435 if (*s)
6436 s++;
6437 }
6438 return found_start;
6439}
6440
6441/*
6442 * Recognize the basic picture of a function declaration -- it needs to
6443 * have an open paren somewhere and a close paren at the end of the line and
6444 * no semicolons anywhere.
6445 * When a line ends in a comma we continue looking in the next line.
6446 * "sp" points to a string with the line. When looking at other lines it must
6447 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006448 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006449 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 */
6451 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006452cin_isfuncdecl(
6453 char_u **sp,
6454 linenr_T first_lnum,
6455 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456{
6457 char_u *s;
6458 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006459 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006461 pos_T *trypos;
6462 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463
6464 if (sp == NULL)
6465 s = ml_get(lnum);
6466 else
6467 s = *sp;
6468
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006469 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006470 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006471 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006472 {
6473 lnum = trypos->lnum;
6474 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006475 {
6476 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006477 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006478 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006479
6480 s = ml_get(lnum);
6481 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006482 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006483
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006484 /* Ignore line starting with #. */
6485 if (cin_ispreproc(s))
6486 return FALSE;
6487
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6489 {
6490 if (cin_iscomment(s)) /* ignore comments */
6491 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006492 else if (*s == ':')
6493 {
6494 if (*(s + 1) == ':')
6495 s += 2;
6496 else
6497 /* To avoid a mistake in the following situation:
6498 * A::A(int a, int b)
6499 * : a(0) // <--not a function decl
6500 * , b(0)
6501 * {...
6502 */
6503 return FALSE;
6504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 else
6506 ++s;
6507 }
6508 if (*s != '(')
6509 return FALSE; /* ';', ' or " before any () or no '(' */
6510
6511 while (*s && *s != ';' && *s != '\'' && *s != '"')
6512 {
6513 if (*s == ')' && cin_nocode(s + 1))
6514 {
6515 /* ')' at the end: may have found a match
6516 * Check for he previous line not to end in a backslash:
6517 * #if defined(x) && \
6518 * defined(y)
6519 */
6520 lnum = first_lnum - 1;
6521 s = ml_get(lnum);
6522 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6523 retval = TRUE;
6524 goto done;
6525 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006526 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006528 int comma = (*s == ',');
6529
6530 /* ',' at the end: continue looking in the next line.
6531 * At the end: check for ',' in the next line, for this style:
6532 * func(arg1
6533 * , arg2) */
6534 for (;;)
6535 {
6536 if (lnum >= curbuf->b_ml.ml_line_count)
6537 break;
6538 s = ml_get(++lnum);
6539 if (!cin_ispreproc(s))
6540 break;
6541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 if (lnum >= curbuf->b_ml.ml_line_count)
6543 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006544 /* Require a comma at end of the line or a comma or ')' at the
6545 * start of next line. */
6546 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006547 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006548 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006549 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 }
6551 else if (cin_iscomment(s)) /* ignore comments */
6552 s = cin_skipcomment(s);
6553 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006554 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006556 just_started = FALSE;
6557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 }
6559
6560done:
6561 if (lnum != first_lnum && sp != NULL)
6562 *sp = ml_get(first_lnum);
6563
6564 return retval;
6565}
6566
6567 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006568cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006570 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571}
6572
6573 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006574cin_iselse(
6575 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576{
6577 if (*p == '}') /* accept "} else" */
6578 p = cin_skipcomment(p + 1);
6579 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6580}
6581
6582 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006583cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584{
6585 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6586}
6587
6588/*
6589 * Check if this is a "while" that should have a matching "do".
6590 * We only accept a "while (condition) ;", with only white space between the
6591 * ')' and ';'. The condition may be spread over several lines.
6592 */
6593 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006594cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595{
6596 pos_T cursor_save;
6597 pos_T *trypos;
6598 int retval = FALSE;
6599
6600 p = cin_skipcomment(p);
6601 if (*p == '}') /* accept "} while (cond);" */
6602 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006603 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 {
6605 cursor_save = curwin->w_cursor;
6606 curwin->w_cursor.lnum = lnum;
6607 curwin->w_cursor.col = 0;
6608 p = ml_get_curline();
6609 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6610 {
6611 ++p;
6612 ++curwin->w_cursor.col;
6613 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006614 if ((trypos = findmatchlimit(NULL, 0, 0,
6615 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6617 retval = TRUE;
6618 curwin->w_cursor = cursor_save;
6619 }
6620 return retval;
6621}
6622
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006623/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006624 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006625 * Return 0 if there is none.
6626 * Otherwise return !0 and update "*poffset" to point to the place where the
6627 * string was found.
6628 */
6629 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006630cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006631{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006632 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006633
6634 if (offset-- < 2)
6635 return 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006636 while (offset > 2 && VIM_ISWHITE(line[offset]))
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006637 --offset;
6638
6639 offset -= 1;
6640 if (!STRNCMP(line + offset, "if", 2))
6641 goto probablyFound;
6642
6643 if (offset >= 1)
6644 {
6645 offset -= 1;
6646 if (!STRNCMP(line + offset, "for", 3))
6647 goto probablyFound;
6648
6649 if (offset >= 2)
6650 {
6651 offset -= 2;
6652 if (!STRNCMP(line + offset, "while", 5))
6653 goto probablyFound;
6654 }
6655 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006656 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006657
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006658probablyFound:
6659 if (!offset || !vim_isIDc(line[offset - 1]))
6660 {
6661 *poffset = offset;
6662 return 1;
6663 }
6664 return 0;
6665}
6666
6667/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006668 * Return TRUE if we are at the end of a do-while.
6669 * do
6670 * nothing;
6671 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006672 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006673 * Adjust the cursor to the line with "while".
6674 */
6675 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006676cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006677{
6678 char_u *line;
6679 char_u *p;
6680 char_u *s;
6681 pos_T *trypos;
6682 int i;
6683
6684 if (terminated != ';') /* there must be a ';' at the end */
6685 return FALSE;
6686
6687 p = line = ml_get_curline();
6688 while (*p != NUL)
6689 {
6690 p = cin_skipcomment(p);
6691 if (*p == ')')
6692 {
6693 s = skipwhite(p + 1);
6694 if (*s == ';' && cin_nocode(s + 1))
6695 {
6696 /* Found ");" at end of the line, now check there is "while"
6697 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006698 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006699 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006700 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006701 if (trypos != NULL)
6702 {
6703 s = cin_skipcomment(ml_get(trypos->lnum));
6704 if (*s == '}') /* accept "} while (cond);" */
6705 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006706 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006707 {
6708 curwin->w_cursor.lnum = trypos->lnum;
6709 return TRUE;
6710 }
6711 }
6712
6713 /* Searching may have made "line" invalid, get it again. */
6714 line = ml_get_curline();
6715 p = line + i;
6716 }
6717 }
6718 if (*p != NUL)
6719 ++p;
6720 }
6721 return FALSE;
6722}
6723
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006725cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726{
6727 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6728}
6729
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006730/*
6731 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 * constructor-initialization. eg:
6733 *
6734 * class MyClass :
6735 * baseClass <-- here
6736 * class MyClass : public baseClass,
6737 * anotherBaseClass <-- here (should probably lineup ??)
6738 * MyClass::MyClass(...) :
6739 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006740 *
6741 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 */
6743 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006744cin_is_cpp_baseclass(
6745 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006747 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 char_u *s;
6749 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006750 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006751 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006753 if (pos->lnum <= lnum)
6754 return cached->found; /* Use the cached result */
6755
6756 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006758 s = skipwhite(line);
6759 if (*s == '#') /* skip #define FOO x ? (x) : x */
6760 return FALSE;
6761 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 if (*s == NUL)
6763 return FALSE;
6764
6765 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6766
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006767 /* Search for a line starting with '#', empty, ending in ';' or containing
6768 * '{' or '}' and start below it. This handles the following situations:
6769 * a = cond ?
6770 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006771 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006772 * func::foo()
6773 * : something
6774 * {}
6775 * Foo::Foo (int one, int two)
6776 * : something(4),
6777 * somethingelse(3)
6778 * {}
6779 */
6780 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006782 line = ml_get(lnum - 1);
6783 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006784 if (*s == '#' || *s == NUL)
6785 break;
6786 while (*s != NUL)
6787 {
6788 s = cin_skipcomment(s);
6789 if (*s == '{' || *s == '}'
6790 || (*s == ';' && cin_nocode(s + 1)))
6791 break;
6792 if (*s != NUL)
6793 ++s;
6794 }
6795 if (*s != NUL)
6796 break;
6797 --lnum;
6798 }
6799
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006800 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006801 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006802 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006803 for (;;)
6804 {
6805 if (*s == NUL)
6806 {
6807 if (lnum == curwin->w_cursor.lnum)
6808 break;
6809 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006810 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006811 s = line;
6812 }
6813 if (s == line)
6814 {
6815 /* don't recognize "case (foo):" as a baseclass */
6816 if (cin_iscase(s, FALSE))
6817 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006818 s = cin_skipcomment(line);
6819 if (*s == NUL)
6820 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006821 }
6822
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006823 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006824 s = skip_string(s) + 1;
6825 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 {
6827 if (s[1] == ':')
6828 {
6829 /* skip double colon. It can't be a constructor
6830 * initialization any more */
6831 lookfor_ctor_init = FALSE;
6832 s = cin_skipcomment(s + 2);
6833 }
6834 else if (lookfor_ctor_init || class_or_struct)
6835 {
6836 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006837 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 cpp_base_class = TRUE;
6839 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006840 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 s = cin_skipcomment(s + 1);
6842 }
6843 else
6844 s = cin_skipcomment(s + 1);
6845 }
6846 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6847 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6848 {
6849 class_or_struct = TRUE;
6850 lookfor_ctor_init = FALSE;
6851
6852 if (*s == 'c')
6853 s = cin_skipcomment(s + 5);
6854 else
6855 s = cin_skipcomment(s + 6);
6856 }
6857 else
6858 {
6859 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6860 {
6861 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6862 }
6863 else if (s[0] == ')')
6864 {
6865 /* Constructor-initialization is assumed if we come across
6866 * something like "):" */
6867 class_or_struct = FALSE;
6868 lookfor_ctor_init = TRUE;
6869 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006870 else if (s[0] == '?')
6871 {
6872 /* Avoid seeing '() :' after '?' as constructor init. */
6873 return FALSE;
6874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 else if (!vim_isIDc(s[0]))
6876 {
6877 /* if it is not an identifier, we are wrong */
6878 class_or_struct = FALSE;
6879 lookfor_ctor_init = FALSE;
6880 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006881 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 {
6883 /* it can't be a constructor-initialization any more */
6884 lookfor_ctor_init = FALSE;
6885
6886 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006887 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006888 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 }
6890
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006891 /* When the line ends in a comma don't align with it. */
6892 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006893 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006894
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 s = cin_skipcomment(s + 1);
6896 }
6897 }
6898
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006899 cached->found = cpp_base_class;
6900 if (cpp_base_class)
6901 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 return cpp_base_class;
6903}
6904
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006905 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006906get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006907{
6908 int amount;
6909 colnr_T vcol;
6910 pos_T *trypos;
6911
6912 if (col == 0)
6913 {
6914 amount = get_indent();
6915 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006916 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006917 amount = get_indent_lnum(trypos->lnum); /* XXX */
6918 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006919 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006920 }
6921 else
6922 {
6923 curwin->w_cursor.col = col;
6924 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6925 amount = (int)vcol;
6926 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006927 if (amount < curbuf->b_ind_cpp_baseclass)
6928 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006929 return amount;
6930}
6931
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932/*
6933 * Return TRUE if string "s" ends with the string "find", possibly followed by
6934 * white space and comments. Skip strings and comments.
6935 * Ignore "ignore" after "find" if it's not NULL.
6936 */
6937 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006938cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939{
6940 char_u *p = s;
6941 char_u *r;
6942 int len = (int)STRLEN(find);
6943
6944 while (*p != NUL)
6945 {
6946 p = cin_skipcomment(p);
6947 if (STRNCMP(p, find, len) == 0)
6948 {
6949 r = skipwhite(p + len);
6950 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6951 r = skipwhite(r + STRLEN(ignore));
6952 if (cin_nocode(r))
6953 return TRUE;
6954 }
6955 if (*p != NUL)
6956 ++p;
6957 }
6958 return FALSE;
6959}
6960
6961/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006962 * Return TRUE when "s" starts with "word" and then a non-ID character.
6963 */
6964 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006965cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006966{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006967 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006968
6969 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6970}
6971
6972/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 * Skip strings, chars and comments until at or past "trypos".
6974 * Return the column found.
6975 */
6976 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006977cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978{
6979 char_u *line;
6980 char_u *p;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006981 char_u *new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982
6983 p = line = ml_get(trypos->lnum);
6984 while (*p && (colnr_T)(p - line) < trypos->col)
6985 {
6986 if (cin_iscomment(p))
6987 p = cin_skipcomment(p);
6988 else
6989 {
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006990 new_p = skip_string(p);
6991 if (new_p == p)
6992 ++p;
6993 else
6994 p = new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 }
6996 }
6997 return (int)(p - line);
6998}
6999
7000/*
7001 * Find the '{' at the start of the block we are in.
7002 * Return NULL if no match found.
7003 * Ignore a '{' that is in a comment, makes indenting the next three lines
7004 * work. */
7005/* foo() */
7006/* { */
7007/* } */
7008
7009 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007010find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011{
7012 pos_T cursor_save;
7013 pos_T *trypos;
7014 pos_T *pos;
7015 static pos_T pos_copy;
7016
7017 cursor_save = curwin->w_cursor;
7018 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
7019 {
7020 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
7021 trypos = &pos_copy;
7022 curwin->w_cursor = *trypos;
7023 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007024 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaardde81312017-08-26 17:49:01 +02007026 && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 break;
7028 if (pos != NULL)
7029 curwin->w_cursor.lnum = pos->lnum;
7030 }
7031 curwin->w_cursor = cursor_save;
7032 return trypos;
7033}
7034
7035/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02007036 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007037 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 */
7039 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007040find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02007042 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007043}
7044
7045 static pos_T *
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02007046find_match_char(int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007047{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 pos_T cursor_save;
7049 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007050 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007051 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052
7053 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007054 ind_maxp_wk = ind_maxparen;
7055retry:
7056 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 {
7058 /* check if the ( is in a // comment */
7059 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01007060 {
7061 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
7062 if (ind_maxp_wk > 0)
7063 {
7064 curwin->w_cursor = *trypos;
7065 curwin->w_cursor.col = 0; /* XXX */
7066 goto retry;
7067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 else
7071 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01007072 pos_T *trypos_wk;
7073
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 pos_copy = *trypos; /* copy trypos, findmatch will change it */
7075 trypos = &pos_copy;
7076 curwin->w_cursor = *trypos;
Bram Moolenaardde81312017-08-26 17:49:01 +02007077 if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01007078 {
7079 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
7080 - trypos_wk->lnum);
7081 if (ind_maxp_wk > 0)
7082 {
7083 curwin->w_cursor = *trypos_wk;
7084 goto retry;
7085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 }
7089 }
7090 curwin->w_cursor = cursor_save;
7091 return trypos;
7092}
7093
7094/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02007095 * Find the matching '(', ignoring it if it is in a comment or before an
7096 * unmatched {.
7097 * Return NULL if no match found.
7098 */
7099 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007100find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02007101{
7102 pos_T *trypos = find_match_paren(ind_maxparen);
7103
7104 if (trypos != NULL)
7105 {
7106 pos_T *tryposBrace = find_start_brace();
7107
7108 /* If both an unmatched '(' and '{' is found. Ignore the '('
7109 * position if the '{' is further down. */
7110 if (tryposBrace != NULL
7111 && (trypos->lnum != tryposBrace->lnum
7112 ? trypos->lnum < tryposBrace->lnum
7113 : trypos->col < tryposBrace->col))
7114 trypos = NULL;
7115 }
7116 return trypos;
7117}
7118
7119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 * Return ind_maxparen corrected for the difference in line number between the
7121 * cursor position and "startpos". This makes sure that searching for a
7122 * matching paren above the cursor line doesn't find a match because of
7123 * looking a few lines further.
7124 */
7125 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007126corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127{
7128 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
7129
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007130 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
7131 return curbuf->b_ind_maxparen - (int)n;
7132 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133}
7134
7135/*
7136 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007137 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 */
7139 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007140find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141{
7142 int i;
7143 int retval = FALSE;
7144 int open_count = 0;
7145
7146 curwin->w_cursor.col = 0; /* default is start of line */
7147
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007148 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 {
7150 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
7151 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
7152 if (l[i] == start)
7153 ++open_count;
7154 else if (l[i] == end)
7155 {
7156 if (open_count > 0)
7157 --open_count;
7158 else
7159 {
7160 curwin->w_cursor.col = i;
7161 retval = TRUE;
7162 }
7163 }
7164 }
7165 return retval;
7166}
7167
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007168/*
7169 * Parse 'cinoptions' and set the values in "curbuf".
7170 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
7171 */
7172 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007173parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007174{
7175 char_u *p;
7176 char_u *l;
7177 char_u *digits;
7178 int n;
7179 int divider;
7180 int fraction = 0;
7181 int sw = (int)get_sw_value(buf);
7182
7183 /*
7184 * Set the default values.
7185 */
7186 /* Spaces from a block's opening brace the prevailing indent for that
7187 * block should be. */
7188 buf->b_ind_level = sw;
7189
7190 /* Spaces from the edge of the line an open brace that's at the end of a
7191 * line is imagined to be. */
7192 buf->b_ind_open_imag = 0;
7193
7194 /* Spaces from the prevailing indent for a line that is not preceded by
7195 * an opening brace. */
7196 buf->b_ind_no_brace = 0;
7197
7198 /* Column where the first { of a function should be located }. */
7199 buf->b_ind_first_open = 0;
7200
7201 /* Spaces from the prevailing indent a leftmost open brace should be
7202 * located. */
7203 buf->b_ind_open_extra = 0;
7204
7205 /* Spaces from the matching open brace (real location for one at the left
7206 * edge; imaginary location from one that ends a line) the matching close
7207 * brace should be located. */
7208 buf->b_ind_close_extra = 0;
7209
7210 /* Spaces from the edge of the line an open brace sitting in the leftmost
7211 * column is imagined to be. */
7212 buf->b_ind_open_left_imag = 0;
7213
7214 /* Spaces jump labels should be shifted to the left if N is non-negative,
7215 * otherwise the jump label will be put to column 1. */
7216 buf->b_ind_jump_label = -1;
7217
7218 /* Spaces from the switch() indent a "case xx" label should be located. */
7219 buf->b_ind_case = sw;
7220
7221 /* Spaces from the "case xx:" code after a switch() should be located. */
7222 buf->b_ind_case_code = sw;
7223
7224 /* Lineup break at end of case in switch() with case label. */
7225 buf->b_ind_case_break = 0;
7226
7227 /* Spaces from the class declaration indent a scope declaration label
7228 * should be located. */
7229 buf->b_ind_scopedecl = sw;
7230
7231 /* Spaces from the scope declaration label code should be located. */
7232 buf->b_ind_scopedecl_code = sw;
7233
7234 /* Amount K&R-style parameters should be indented. */
7235 buf->b_ind_param = sw;
7236
7237 /* Amount a function type spec should be indented. */
7238 buf->b_ind_func_type = sw;
7239
7240 /* Amount a cpp base class declaration or constructor initialization
7241 * should be indented. */
7242 buf->b_ind_cpp_baseclass = sw;
7243
7244 /* additional spaces beyond the prevailing indent a continuation line
7245 * should be located. */
7246 buf->b_ind_continuation = sw;
7247
7248 /* Spaces from the indent of the line with an unclosed parentheses. */
7249 buf->b_ind_unclosed = sw * 2;
7250
7251 /* Spaces from the indent of the line with an unclosed parentheses, which
7252 * itself is also unclosed. */
7253 buf->b_ind_unclosed2 = sw;
7254
7255 /* Suppress ignoring spaces from the indent of a line starting with an
7256 * unclosed parentheses. */
7257 buf->b_ind_unclosed_noignore = 0;
7258
7259 /* If the opening paren is the last nonwhite character on the line, and
7260 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
7261 * context (for very long lines). */
7262 buf->b_ind_unclosed_wrapped = 0;
7263
7264 /* Suppress ignoring white space when lining up with the character after
7265 * an unclosed parentheses. */
7266 buf->b_ind_unclosed_whiteok = 0;
7267
7268 /* Indent a closing parentheses under the line start of the matching
7269 * opening parentheses. */
7270 buf->b_ind_matching_paren = 0;
7271
7272 /* Indent a closing parentheses under the previous line. */
7273 buf->b_ind_paren_prev = 0;
7274
7275 /* Extra indent for comments. */
7276 buf->b_ind_comment = 0;
7277
7278 /* Spaces from the comment opener when there is nothing after it. */
7279 buf->b_ind_in_comment = 3;
7280
7281 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
7282 * after the comment opener. */
7283 buf->b_ind_in_comment2 = 0;
7284
7285 /* Max lines to search for an open paren. */
7286 buf->b_ind_maxparen = 20;
7287
7288 /* Max lines to search for an open comment. */
7289 buf->b_ind_maxcomment = 70;
7290
7291 /* Handle braces for java code. */
7292 buf->b_ind_java = 0;
7293
7294 /* Not to confuse JS object properties with labels. */
7295 buf->b_ind_js = 0;
7296
7297 /* Handle blocked cases correctly. */
7298 buf->b_ind_keep_case_label = 0;
7299
7300 /* Handle C++ namespace. */
7301 buf->b_ind_cpp_namespace = 0;
7302
7303 /* Handle continuation lines containing conditions of if(), for() and
7304 * while(). */
7305 buf->b_ind_if_for_while = 0;
7306
Bram Moolenaar6b643942017-03-05 19:44:06 +01007307 /* indentation for # comments */
7308 buf->b_ind_hash_comment = 0;
7309
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007310 /* Handle C++ extern "C" or "C++" */
7311 buf->b_ind_cpp_extern_c = 0;
7312
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007313 for (p = buf->b_p_cino; *p; )
7314 {
7315 l = p++;
7316 if (*p == '-')
7317 ++p;
7318 digits = p; /* remember where the digits start */
7319 n = getdigits(&p);
7320 divider = 0;
7321 if (*p == '.') /* ".5s" means a fraction */
7322 {
7323 fraction = atol((char *)++p);
7324 while (VIM_ISDIGIT(*p))
7325 {
7326 ++p;
7327 if (divider)
7328 divider *= 10;
7329 else
7330 divider = 10;
7331 }
7332 }
7333 if (*p == 's') /* "2s" means two times 'shiftwidth' */
7334 {
7335 if (p == digits)
7336 n = sw; /* just "s" is one 'shiftwidth' */
7337 else
7338 {
7339 n *= sw;
7340 if (divider)
7341 n += (sw * fraction + divider / 2) / divider;
7342 }
7343 ++p;
7344 }
7345 if (l[1] == '-')
7346 n = -n;
7347
7348 /* When adding an entry here, also update the default 'cinoptions' in
7349 * doc/indent.txt, and add explanation for it! */
7350 switch (*l)
7351 {
7352 case '>': buf->b_ind_level = n; break;
7353 case 'e': buf->b_ind_open_imag = n; break;
7354 case 'n': buf->b_ind_no_brace = n; break;
7355 case 'f': buf->b_ind_first_open = n; break;
7356 case '{': buf->b_ind_open_extra = n; break;
7357 case '}': buf->b_ind_close_extra = n; break;
7358 case '^': buf->b_ind_open_left_imag = n; break;
7359 case 'L': buf->b_ind_jump_label = n; break;
7360 case ':': buf->b_ind_case = n; break;
7361 case '=': buf->b_ind_case_code = n; break;
7362 case 'b': buf->b_ind_case_break = n; break;
7363 case 'p': buf->b_ind_param = n; break;
7364 case 't': buf->b_ind_func_type = n; break;
7365 case '/': buf->b_ind_comment = n; break;
7366 case 'c': buf->b_ind_in_comment = n; break;
7367 case 'C': buf->b_ind_in_comment2 = n; break;
7368 case 'i': buf->b_ind_cpp_baseclass = n; break;
7369 case '+': buf->b_ind_continuation = n; break;
7370 case '(': buf->b_ind_unclosed = n; break;
7371 case 'u': buf->b_ind_unclosed2 = n; break;
7372 case 'U': buf->b_ind_unclosed_noignore = n; break;
7373 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7374 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7375 case 'm': buf->b_ind_matching_paren = n; break;
7376 case 'M': buf->b_ind_paren_prev = n; break;
7377 case ')': buf->b_ind_maxparen = n; break;
7378 case '*': buf->b_ind_maxcomment = n; break;
7379 case 'g': buf->b_ind_scopedecl = n; break;
7380 case 'h': buf->b_ind_scopedecl_code = n; break;
7381 case 'j': buf->b_ind_java = n; break;
7382 case 'J': buf->b_ind_js = n; break;
7383 case 'l': buf->b_ind_keep_case_label = n; break;
7384 case '#': buf->b_ind_hash_comment = n; break;
7385 case 'N': buf->b_ind_cpp_namespace = n; break;
7386 case 'k': buf->b_ind_if_for_while = n; break;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007387 case 'E': buf->b_ind_cpp_extern_c = n; break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007388 }
7389 if (*p == ',')
7390 ++p;
7391 }
7392}
7393
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007394/*
7395 * Return the desired indent for C code.
7396 * Return -1 if the indent should be left alone (inside a raw string).
7397 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007399get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 pos_T cur_curpos;
7402 int amount;
7403 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007404 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 colnr_T col;
7406 char_u *theline;
7407 char_u *linecopy;
7408 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007409 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007411 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 pos_T our_paren_pos;
7413 char_u *start;
7414 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007415#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416#define BRACE_AT_START 2 /* '{' is at start of line */
7417#define BRACE_AT_END 3 /* '{' is at end of line */
7418 linenr_T ourscope;
7419 char_u *l;
7420 char_u *look;
7421 char_u terminated;
7422 int lookfor;
7423#define LOOKFOR_INITIAL 0
7424#define LOOKFOR_IF 1
7425#define LOOKFOR_DO 2
7426#define LOOKFOR_CASE 3
7427#define LOOKFOR_ANY 4
7428#define LOOKFOR_TERM 5
7429#define LOOKFOR_UNTERM 6
7430#define LOOKFOR_SCOPEDECL 7
7431#define LOOKFOR_NOBREAK 8
7432#define LOOKFOR_CPP_BASECLASS 9
7433#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007434#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007435#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436
7437 int whilelevel;
7438 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 int n;
7440 int iscase;
7441 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007442 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007444 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007445 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007446 int js_cur_has_key = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02007447 linenr_T raw_string_start = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007448 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007450 /* make a copy, value is changed below */
7451 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452
7453 /* remember where the cursor was when we started */
7454 cur_curpos = curwin->w_cursor;
7455
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007456 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007457 if (cur_curpos.lnum == 1)
7458 return 0;
7459
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 /* Get a copy of the current contents of the line.
7461 * This is required, because only the most recent line obtained with
7462 * ml_get is valid! */
7463 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7464 if (linecopy == NULL)
7465 return 0;
7466
7467 /*
7468 * In insert mode and the cursor is on a ')' truncate the line at the
7469 * cursor position. We don't want to line up with the matching '(' when
7470 * inserting new stuff.
7471 * For unknown reasons the cursor might be past the end of the line, thus
7472 * check for that.
7473 */
7474 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007475 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 && linecopy[curwin->w_cursor.col] == ')')
7477 linecopy[curwin->w_cursor.col] = NUL;
7478
7479 theline = skipwhite(linecopy);
7480
7481 /* move the cursor to the start of the line */
7482
7483 curwin->w_cursor.col = 0;
7484
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007485 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007486
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007488 * If we are inside a raw string don't change the indent.
7489 * Ignore a raw string inside a comment.
7490 */
7491 comment_pos = ind_find_start_comment();
7492 if (comment_pos != NULL)
7493 {
7494 /* findmatchlimit() static pos is overwritten, make a copy */
7495 tryposCopy = *comment_pos;
7496 comment_pos = &tryposCopy;
7497 }
7498 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007499 if (trypos != NULL && (comment_pos == NULL
7500 || LT_POS(*trypos, *comment_pos)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007501 {
7502 amount = -1;
7503 goto laterend;
7504 }
7505
7506 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 * #defines and so on always go at the left when included in 'cinkeys'.
7508 */
7509 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007510 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007511 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007512 goto theend;
7513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514
7515 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007516 * Is it a non-case label? Then that goes at the left margin too unless:
7517 * - JS flag is set.
7518 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007520 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007521 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 {
7523 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007524 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 }
7526
7527 /*
7528 * If we're inside a "//" comment and there is a "//" comment in a
7529 * previous line, lineup with that one.
7530 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007531 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 && (trypos = find_line_comment()) != NULL) /* XXX */
7533 {
7534 /* find how indented the line beginning the comment is */
7535 getvcol(curwin, trypos, &col, NULL, NULL);
7536 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007537 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 }
7539
7540 /*
7541 * If we're inside a comment and not looking at the start of the
7542 * comment, try using the 'comments' option.
7543 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007544 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 {
7546 int lead_start_len = 2;
7547 int lead_middle_len = 1;
7548 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7549 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7550 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7551 char_u *p;
7552 int start_align = 0;
7553 int start_off = 0;
7554 int done = FALSE;
7555
7556 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007557 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007559 *lead_start = NUL;
7560 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561
7562 p = curbuf->b_p_com;
7563 while (*p != NUL)
7564 {
7565 int align = 0;
7566 int off = 0;
7567 int what = 0;
7568
7569 while (*p != NUL && *p != ':')
7570 {
7571 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7572 what = *p++;
7573 else if (*p == COM_LEFT || *p == COM_RIGHT)
7574 align = *p++;
7575 else if (VIM_ISDIGIT(*p) || *p == '-')
7576 off = getdigits(&p);
7577 else
7578 ++p;
7579 }
7580
7581 if (*p == ':')
7582 ++p;
7583 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7584 if (what == COM_START)
7585 {
7586 STRCPY(lead_start, lead_end);
7587 lead_start_len = (int)STRLEN(lead_start);
7588 start_off = off;
7589 start_align = align;
7590 }
7591 else if (what == COM_MIDDLE)
7592 {
7593 STRCPY(lead_middle, lead_end);
7594 lead_middle_len = (int)STRLEN(lead_middle);
7595 }
7596 else if (what == COM_END)
7597 {
7598 /* If our line starts with the middle comment string, line it
7599 * up with the comment opener per the 'comments' option. */
7600 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7601 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7602 {
7603 done = TRUE;
7604 if (curwin->w_cursor.lnum > 1)
7605 {
7606 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007607 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 * the middle comment string matches in the previous
7609 * line, use the indent of that line. XXX */
7610 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7611 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7612 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7613 else if (STRNCMP(look, lead_middle,
7614 lead_middle_len) == 0)
7615 {
7616 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7617 break;
7618 }
7619 /* If the start comment string doesn't match with the
7620 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007621 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 lead_start, lead_start_len) != 0)
7623 continue;
7624 }
7625 if (start_off != 0)
7626 amount += start_off;
7627 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007628 amount += vim_strsize(lead_start)
7629 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 break;
7631 }
7632
7633 /* If our line starts with the end comment string, line it up
7634 * with the middle comment */
7635 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7636 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7637 {
7638 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7639 /* XXX */
7640 if (off != 0)
7641 amount += off;
7642 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007643 amount += vim_strsize(lead_start)
7644 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 done = TRUE;
7646 break;
7647 }
7648 }
7649 }
7650
7651 /* If our line starts with an asterisk, line up with the
7652 * asterisk in the comment opener; otherwise, line up
7653 * with the first character of the comment text.
7654 */
7655 if (done)
7656 ;
7657 else if (theline[0] == '*')
7658 amount += 1;
7659 else
7660 {
7661 /*
7662 * If we are more than one line away from the comment opener, take
7663 * the indent of the previous non-empty line. If 'cino' has "CO"
7664 * and we are just below the comment opener and there are any
7665 * white characters after it line up with the text after it;
7666 * otherwise, add the amount specified by "c" in 'cino'
7667 */
7668 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007669 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 {
7671 if (linewhite(lnum)) /* skip blank lines */
7672 continue;
7673 amount = get_indent_lnum(lnum); /* XXX */
7674 break;
7675 }
7676 if (amount == -1) /* use the comment opener */
7677 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007678 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007680 start = ml_get(comment_pos->lnum);
7681 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007683 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007685 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007687 if (curbuf->b_ind_in_comment2 || *look == NUL)
7688 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 }
7690 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007691 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 }
7693
7694 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007695 * Are we looking at a ']' that has a match?
7696 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007697 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007698 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7699 {
7700 /* align with the line containing the '['. */
7701 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007702 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007703 }
7704
7705 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 * Are we inside parentheses or braces?
7707 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007708 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007709 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007710 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 || trypos != NULL)
7712 {
7713 if (trypos != NULL && tryposBrace != NULL)
7714 {
7715 /* Both an unmatched '(' and '{' is found. Use the one which is
7716 * closer to the current cursor position, set the other to NULL. */
7717 if (trypos->lnum != tryposBrace->lnum
7718 ? trypos->lnum < tryposBrace->lnum
7719 : trypos->col < tryposBrace->col)
7720 trypos = NULL;
7721 else
7722 tryposBrace = NULL;
7723 }
7724
7725 if (trypos != NULL)
7726 {
7727 /*
7728 * If the matching paren is more than one line away, use the indent of
7729 * a previous non-empty line that matches the same paren.
7730 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007731 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007733 /* Line up with the start of the matching paren line. */
7734 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7735 }
7736 else
7737 {
7738 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007739 our_paren_pos = *trypos;
7740 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007742 l = skipwhite(ml_get(lnum));
7743 if (cin_nocode(l)) /* skip comment lines */
7744 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007745 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007746 continue; /* ignore #define, #if, etc. */
7747 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007749 /* Skip a comment or raw string. XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02007750 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007751 {
7752 lnum = trypos->lnum + 1;
7753 continue;
7754 }
7755
7756 /* XXX */
7757 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007758 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007759 && trypos->lnum == our_paren_pos.lnum
7760 && trypos->col == our_paren_pos.col)
7761 {
7762 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007764 if (theline[0] == ')')
7765 {
7766 if (our_paren_pos.lnum != lnum
7767 && cur_amount > amount)
7768 cur_amount = amount;
7769 amount = -1;
7770 }
7771 break;
7772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 }
7774 }
7775
7776 /*
7777 * Line up with line where the matching paren is. XXX
7778 * If the line starts with a '(' or the indent for unclosed
7779 * parentheses is zero, line up with the unclosed parentheses.
7780 */
7781 if (amount == -1)
7782 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007783 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007784 int is_if_for_while = 0;
7785
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007786 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007787 {
7788 /* Look for the outermost opening parenthesis on this line
7789 * and check whether it belongs to an "if", "for" or "while". */
7790
7791 pos_T cursor_save = curwin->w_cursor;
7792 pos_T outermost;
7793 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007794
7795 trypos = &our_paren_pos;
7796 do {
7797 outermost = *trypos;
7798 curwin->w_cursor.lnum = outermost.lnum;
7799 curwin->w_cursor.col = outermost.col;
7800
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007801 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007802 } while (trypos && trypos->lnum == outermost.lnum);
7803
7804 curwin->w_cursor = cursor_save;
7805
7806 line = ml_get(outermost.lnum);
7807
7808 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007809 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007810 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007811
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007812 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007813 look = skipwhite(look);
7814 if (*look == '(')
7815 {
7816 linenr_T save_lnum = curwin->w_cursor.lnum;
7817 char_u *line;
7818 int look_col;
7819
7820 /* Ignore a '(' in front of the line that has a match before
7821 * our matching '('. */
7822 curwin->w_cursor.lnum = our_paren_pos.lnum;
7823 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007824 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007825 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007826 if ((trypos = findmatchlimit(NULL, ')', 0,
7827 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007828 != NULL
7829 && trypos->lnum == our_paren_pos.lnum
7830 && trypos->col < our_paren_pos.col)
7831 ignore_paren_col = trypos->col + 1;
7832
7833 curwin->w_cursor.lnum = save_lnum;
7834 look = ml_get(our_paren_pos.lnum) + look_col;
7835 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007836 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7837 && is_if_for_while == 0)
7838 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007839 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 {
7841 /*
7842 * If we're looking at a close paren, line up right there;
7843 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007844 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 * the last nonwhite character of the line, use either the
7846 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007847 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 * lines).
7849 */
7850 if (theline[0] != ')')
7851 {
7852 cur_amount = MAXCOL;
7853 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007854 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 && cin_ends_in(l, (char_u *)"(", NULL))
7856 {
7857 /* look for opening unmatched paren, indent one level
7858 * for each additional level */
7859 n = 1;
7860 for (col = 0; col < our_paren_pos.col; ++col)
7861 {
7862 switch (l[col])
7863 {
7864 case '(':
7865 case '{': ++n;
7866 break;
7867
7868 case ')':
7869 case '}': if (n > 1)
7870 --n;
7871 break;
7872 }
7873 }
7874
7875 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007876 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007878 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 our_paren_pos.col++;
7880 else
7881 {
7882 col = our_paren_pos.col + 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01007883 while (VIM_ISWHITE(l[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 col++;
7885 if (l[col] != NUL) /* In case of trailing space */
7886 our_paren_pos.col = col;
7887 else
7888 our_paren_pos.col++;
7889 }
7890 }
7891
7892 /*
7893 * Find how indented the paren is, or the character after it
7894 * if we did the above "if".
7895 */
7896 if (our_paren_pos.col > 0)
7897 {
7898 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7899 if (cur_amount > (int)col)
7900 cur_amount = col;
7901 }
7902 }
7903
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007904 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 {
7906 /* Line up with the start of the matching paren line. */
7907 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007908 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7909 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007910 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 {
7912 if (cur_amount != MAXCOL)
7913 amount = cur_amount;
7914 }
7915 else
7916 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007917 /* Add b_ind_unclosed2 for each '(' before our matching one,
7918 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007920 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 {
7922 --our_paren_pos.col;
7923 switch (*ml_get_pos(&our_paren_pos))
7924 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007925 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 col = our_paren_pos.col;
7927 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007928 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 col = MAXCOL;
7930 break;
7931 }
7932 }
7933
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007934 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 * braces */
7936 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007937 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 else
7939 {
7940 curwin->w_cursor.lnum = our_paren_pos.lnum;
7941 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007942 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7943 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007944 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007946 {
7947 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007948 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007949 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007950 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 }
7953 /*
7954 * For a line starting with ')' use the minimum of the two
7955 * positions, to avoid giving it more indent than the previous
7956 * lines:
7957 * func_long_name( if (x
7958 * arg && yy
7959 * ) ^ not here ) ^ not here
7960 */
7961 if (cur_amount < amount)
7962 amount = cur_amount;
7963 }
7964 }
7965
7966 /* add extra indent for a comment */
7967 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007968 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 else
7971 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007972 /*
7973 * We are inside braces, there is a { before this line at the position
7974 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007975 * Make a copy of tryposBrace, it may point to pos_copy inside
7976 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007977 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007978 tryposCopy = *tryposBrace;
7979 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 ourscope = trypos->lnum;
7982 start = ml_get(ourscope);
7983
7984 /*
7985 * Now figure out how indented the line is in general.
7986 * If the brace was at the start of the line, we use that;
7987 * otherwise, check out the indentation of the line as
7988 * a whole and then add the "imaginary indent" to that.
7989 */
7990 look = skipwhite(start);
7991 if (*look == '{')
7992 {
7993 getvcol(curwin, trypos, &col, NULL, NULL);
7994 amount = col;
7995 if (*start == '{')
7996 start_brace = BRACE_IN_COL0;
7997 else
7998 start_brace = BRACE_AT_START;
7999 }
8000 else
8001 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008002 /* That opening brace might have been on a continuation
8003 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 curwin->w_cursor.lnum = ourscope;
8005
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008006 /* Position the cursor over the rightmost paren, so that
8007 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 lnum = ourscope;
8009 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008010 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
8011 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 lnum = trypos->lnum;
8013
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008014 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 * case 1: if (asdf &&
8016 * ldfd) {
8017 * }
8018 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02008019 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
8020 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02008022 else if (curbuf->b_ind_js)
8023 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008025 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026
8027 start_brace = BRACE_AT_END;
8028 }
8029
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008030 /* For Javascript check if the line starts with "key:". */
8031 if (curbuf->b_ind_js)
8032 js_cur_has_key = cin_has_js_key(theline);
8033
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008035 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 * we want to be. otherwise, add the amount of room
8037 * that an indent is supposed to be.
8038 */
8039 if (theline[0] == '}')
8040 {
8041 /*
8042 * they may want closing braces to line up with something
8043 * other than the open brace. indulge them, if so.
8044 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008045 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 }
8047 else
8048 {
8049 /*
8050 * If we're looking at an "else", try to find an "if"
8051 * to match it with.
8052 * If we're looking at a "while", try to find a "do"
8053 * to match it with.
8054 */
8055 lookfor = LOOKFOR_INITIAL;
8056 if (cin_iselse(theline))
8057 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008058 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 lookfor = LOOKFOR_DO;
8060 if (lookfor != LOOKFOR_INITIAL)
8061 {
8062 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008063 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 {
8065 amount = get_indent(); /* XXX */
8066 goto theend;
8067 }
8068 }
8069
8070 /*
8071 * We get here if we are not on an "while-of-do" or "else" (or
8072 * failed to find a matching "if").
8073 * Search backwards for something to line up with.
8074 * First set amount for when we don't find anything.
8075 */
8076
8077 /*
8078 * if the '{' is _really_ at the left margin, use the imaginary
8079 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008080 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 */
8082
8083 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
8084 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008085 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008086 lookfor_cpp_namespace = TRUE;
8087 }
8088 else if (start_brace == BRACE_AT_START &&
8089 lookfor_cpp_namespace) /* '{' is at start */
8090 {
8091
8092 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 }
8094 else
8095 {
8096 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008097 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008098 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008099
8100 l = skipwhite(ml_get_curline());
8101 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008102 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008103 else if (cin_is_cpp_extern_c(l))
8104 amount += curbuf->b_ind_cpp_extern_c;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 else
8107 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008108 /* Compensate for adding b_ind_open_extra later. */
8109 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 if (amount < 0)
8111 amount = 0;
8112 }
8113 }
8114
8115 lookfor_break = FALSE;
8116
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008117 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 {
8119 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008120 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 }
8122 else if (cin_isscopedecl(theline)) /* private:, ... */
8123 {
8124 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008125 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 }
8127 else
8128 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008129 if (curbuf->b_ind_case_break && cin_isbreak(theline))
8130 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 lookfor_break = TRUE;
8132
8133 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008134 /* b_ind_level from start of block */
8135 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 }
8137 scope_amount = amount;
8138 whilelevel = 0;
8139
8140 /*
8141 * Search backwards. If we find something we recognize, line up
8142 * with that.
8143 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008144 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 * the usual amount relative to the conditional
8146 * that opens the block.
8147 */
8148 curwin->w_cursor = cur_curpos;
8149 for (;;)
8150 {
8151 curwin->w_cursor.lnum--;
8152 curwin->w_cursor.col = 0;
8153
8154 /*
8155 * If we went all the way back to the start of our scope, line
8156 * up with it.
8157 */
8158 if (curwin->w_cursor.lnum <= ourscope)
8159 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008160 /* We reached end of scope:
8161 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008163 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 * don't add ind_continuation, otherwise it is a variable
8165 * declaration:
8166 * int x,
8167 * here; <-- add ind_continuation
8168 */
8169 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8170 {
8171 if (curwin->w_cursor.lnum == 0
8172 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008173 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008175 /* nothing found (abuse curbuf->b_ind_maxparen as
8176 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 * initialization) */
8178 if (cont_amount > 0)
8179 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008180 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 amount += ind_continuation;
8182 break;
8183 }
8184
8185 l = ml_get_curline();
8186
8187 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008188 * If we're in a comment or raw string now, skip to
8189 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 */
Bram Moolenaardde81312017-08-26 17:49:01 +02008191 trypos = ind_find_start_CORS(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 if (trypos != NULL)
8193 {
8194 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008195 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 continue;
8197 }
8198
8199 /*
8200 * Skip preprocessor directives and blank lines.
8201 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008202 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8203 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 continue;
8205
8206 if (cin_nocode(l))
8207 continue;
8208
8209 terminated = cin_isterminated(l, FALSE, TRUE);
8210
8211 /*
8212 * If we are at top level and the line looks like a
8213 * function declaration, we are done
8214 * (it's a variable declaration).
8215 */
8216 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008217 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 {
8219 /* if the line is terminated with another ','
8220 * it is a continued variable initialization.
8221 * don't add extra indent.
8222 * TODO: does not work, if a function
8223 * declaration is split over multiple lines:
8224 * cin_isfuncdecl returns FALSE then.
8225 */
8226 if (terminated == ',')
8227 break;
8228
8229 /* if it es a enum declaration or an assignment,
8230 * we are done.
8231 */
8232 if (terminated != ';' && cin_isinit())
8233 break;
8234
8235 /* nothing useful found */
8236 if (terminated == 0 || terminated == '{')
8237 continue;
8238 }
8239
8240 if (terminated != ';')
8241 {
8242 /* Skip parens and braces. Position the cursor
8243 * over the rightmost paren, so that matching it
8244 * will take us back to the start of the line.
8245 */ /* XXX */
8246 trypos = NULL;
8247 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008248 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008249 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250
8251 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008252 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253
8254 if (trypos != NULL)
8255 {
8256 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008257 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 continue;
8259 }
8260 }
8261
8262 /* it's a variable declaration, add indentation
8263 * like in
8264 * int a,
8265 * b;
8266 */
8267 if (cont_amount > 0)
8268 amount = cont_amount;
8269 else
8270 amount += ind_continuation;
8271 }
8272 else if (lookfor == LOOKFOR_UNTERM)
8273 {
8274 if (cont_amount > 0)
8275 amount = cont_amount;
8276 else
8277 amount += ind_continuation;
8278 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008279 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008280 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02008281 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008282 && lookfor != LOOKFOR_CPP_BASECLASS
8283 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02008284 {
8285 amount = scope_amount;
8286 if (theline[0] == '{')
8287 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008288 amount += curbuf->b_ind_open_extra;
8289 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008290 }
8291 }
8292
8293 if (lookfor_cpp_namespace)
8294 {
8295 /*
8296 * Looking for C++ namespace, need to look further
8297 * back.
8298 */
8299 if (curwin->w_cursor.lnum == ourscope)
8300 continue;
8301
8302 if (curwin->w_cursor.lnum == 0
8303 || curwin->w_cursor.lnum
8304 < ourscope - FIND_NAMESPACE_LIM)
8305 break;
8306
8307 l = ml_get_curline();
8308
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008309 /* If we're in a comment or raw string now, skip
8310 * to the start of it. */
Bram Moolenaardde81312017-08-26 17:49:01 +02008311 trypos = ind_find_start_CORS(NULL);
Bram Moolenaare79d1532011-10-04 18:03:47 +02008312 if (trypos != NULL)
8313 {
8314 curwin->w_cursor.lnum = trypos->lnum + 1;
8315 curwin->w_cursor.col = 0;
8316 continue;
8317 }
8318
8319 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008320 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8321 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02008322 continue;
8323
8324 /* Finally the actual check for "namespace". */
8325 if (cin_is_cpp_namespace(l))
8326 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008327 amount += curbuf->b_ind_cpp_namespace
8328 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008329 break;
8330 }
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008331 else if (cin_is_cpp_extern_c(l))
8332 {
8333 amount += curbuf->b_ind_cpp_extern_c
8334 - added_to_amount;
8335 break;
8336 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008337
8338 if (cin_nocode(l))
8339 continue;
8340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 }
8342 break;
8343 }
8344
8345 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008346 * If we're in a comment or raw string now, skip to the start
8347 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008349 if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 {
8351 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008352 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 continue;
8354 }
8355
8356 l = ml_get_curline();
8357
8358 /*
8359 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008360 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008362 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 if (iscase || cin_isscopedecl(l))
8364 {
8365 /* we are only looking for cpp base class
8366 * declaration/initialization any longer */
8367 if (lookfor == LOOKFOR_CPP_BASECLASS)
8368 break;
8369
8370 /* When looking for a "do" we are not interested in
8371 * labels. */
8372 if (whilelevel > 0)
8373 continue;
8374
8375 /*
8376 * case xx:
8377 * c = 99 + <- this indent plus continuation
8378 *-> here;
8379 */
8380 if (lookfor == LOOKFOR_UNTERM
8381 || lookfor == LOOKFOR_ENUM_OR_INIT)
8382 {
8383 if (cont_amount > 0)
8384 amount = cont_amount;
8385 else
8386 amount += ind_continuation;
8387 break;
8388 }
8389
8390 /*
8391 * case xx: <- line up with this case
8392 * x = 333;
8393 * case yy:
8394 */
8395 if ( (iscase && lookfor == LOOKFOR_CASE)
8396 || (iscase && lookfor_break)
8397 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8398 {
8399 /*
8400 * Check that this case label is not for another
8401 * switch()
8402 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008403 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008404 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 {
8406 amount = get_indent(); /* XXX */
8407 break;
8408 }
8409 continue;
8410 }
8411
8412 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8413
8414 /*
8415 * case xx: if (cond) <- line up with this if
8416 * y = y + 1;
8417 * -> s = 99;
8418 *
8419 * case xx:
8420 * if (cond) <- line up with this line
8421 * y = y + 1;
8422 * -> s = 99;
8423 */
8424 if (lookfor == LOOKFOR_TERM)
8425 {
8426 if (n)
8427 amount = n;
8428
8429 if (!lookfor_break)
8430 break;
8431 }
8432
8433 /*
8434 * case xx: x = x + 1; <- line up with this x
8435 * -> y = y + 1;
8436 *
8437 * case xx: if (cond) <- line up with this if
8438 * -> y = y + 1;
8439 */
8440 if (n)
8441 {
8442 amount = n;
8443 l = after_label(ml_get_curline());
8444 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008445 {
8446 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008447 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008448 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008449 amount += curbuf->b_ind_level
8450 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 break;
8453 }
8454
8455 /*
8456 * Try to get the indent of a statement before the switch
8457 * label. If nothing is found, line up relative to the
8458 * switch label.
8459 * break; <- may line up with this line
8460 * case xx:
8461 * -> y = 1;
8462 */
8463 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008464 ? curbuf->b_ind_case_code
8465 : curbuf->b_ind_scopedecl_code);
8466 lookfor = curbuf->b_ind_case_break
8467 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 continue;
8469 }
8470
8471 /*
8472 * Looking for a switch() label or C++ scope declaration,
8473 * ignore other lines, skip {}-blocks.
8474 */
8475 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8476 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008477 if (find_last_paren(l, '{', '}')
8478 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008479 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008481 curwin->w_cursor.col = 0;
8482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 continue;
8484 }
8485
8486 /*
8487 * Ignore jump labels with nothing after them.
8488 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008489 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 {
8491 l = after_label(ml_get_curline());
8492 if (l == NULL || cin_nocode(l))
8493 continue;
8494 }
8495
8496 /*
8497 * Ignore #defines, #if, etc.
8498 * Ignore comment and empty lines.
8499 * (need to get the line again, cin_islabel() may have
8500 * unlocked it)
8501 */
8502 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008503 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 || cin_nocode(l))
8505 continue;
8506
8507 /*
8508 * Are we at the start of a cpp base class declaration or
8509 * constructor initialization?
8510 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008511 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008512 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008513 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008514 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008515 l = ml_get_curline();
8516 }
8517 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 {
8519 if (lookfor == LOOKFOR_UNTERM)
8520 {
8521 if (cont_amount > 0)
8522 amount = cont_amount;
8523 else
8524 amount += ind_continuation;
8525 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008526 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008528 /* Need to find start of the declaration. */
8529 lookfor = LOOKFOR_UNTERM;
8530 ind_continuation = 0;
8531 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 }
8533 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008534 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008535 amount = get_baseclass_amount(
8536 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 break;
8538 }
8539 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8540 {
8541 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008542 * declaration or initialization before the opening brace.
8543 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 if (cin_isterminated(l, TRUE, FALSE))
8545 break;
8546 else
8547 continue;
8548 }
8549
8550 /*
8551 * What happens next depends on the line being terminated.
8552 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008553 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 * 123,
8555 * sizeof
8556 * here
8557 * Otherwise check whether it is a enumeration or structure
8558 * initialisation (not indented) or a variable declaration
8559 * (indented).
8560 */
8561 terminated = cin_isterminated(l, FALSE, TRUE);
8562
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008563 if (js_cur_has_key)
8564 {
8565 js_cur_has_key = 0; /* only check the first line */
8566 if (curbuf->b_ind_js && terminated == ',')
8567 {
8568 /* For Javascript we might be inside an object:
8569 * key: something, <- align with this
8570 * key: something
8571 * or:
8572 * key: something + <- align with this
8573 * something,
8574 * key: something
8575 */
8576 lookfor = LOOKFOR_JS_KEY;
8577 }
8578 }
8579 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8580 {
8581 amount = get_indent();
8582 break;
8583 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008584 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008585 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008586 if (tryposBrace != NULL && tryposBrace->lnum
8587 >= curwin->w_cursor.lnum)
8588 break;
8589 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008590 /* line below current line is the one that starts a
8591 * (possibly broken) line ending in a comma. */
8592 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008593 else
8594 {
8595 amount = get_indent();
8596 if (curwin->w_cursor.lnum - 1 == ourscope)
8597 /* line above is start of the scope, thus current
8598 * line is the one that stars a (possibly broken)
8599 * line ending in a comma. */
8600 break;
8601 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008602 }
8603
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8605 && terminated == ','))
8606 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008607 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8608 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008609 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 /*
8611 * if we're in the middle of a paren thing,
8612 * go back to the line that starts it so
8613 * we can get the right prevailing indent
8614 * if ( foo &&
8615 * bar )
8616 */
8617 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008618 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008620 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 */
8622 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008623 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008624 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8625 || (trypos->lnum == tryposBrace->lnum
8626 && trypos->col < tryposBrace->col)))
8627 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628
8629 /*
8630 * If we are looking for ',', we also look for matching
8631 * braces.
8632 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008633 if (trypos == NULL && terminated == ','
8634 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008635 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636
8637 if (trypos != NULL)
8638 {
8639 /*
8640 * Check if we are on a case label now. This is
8641 * handled above.
8642 * case xx: if ( asdf &&
8643 * asdf)
8644 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008645 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008647 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 {
8649 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008650 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 continue;
8652 }
8653 }
8654
8655 /*
8656 * Skip over continuation lines to find the one to get the
8657 * indent from
8658 * char *usethis = "bla\
8659 * bla",
8660 * here;
8661 */
8662 if (terminated == ',')
8663 {
8664 while (curwin->w_cursor.lnum > 1)
8665 {
8666 l = ml_get(curwin->w_cursor.lnum - 1);
8667 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8668 break;
8669 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008670 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 }
8672 }
8673
8674 /*
8675 * Get indent and pointer to text for current line,
8676 * ignoring any jump label. XXX
8677 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008678 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008679 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008680 else
8681 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 /*
8683 * If this is just above the line we are indenting, and it
8684 * starts with a '{', line it up with this line.
8685 * while (not)
8686 * -> {
8687 * }
8688 */
8689 if (terminated != ',' && lookfor != LOOKFOR_TERM
8690 && theline[0] == '{')
8691 {
8692 amount = cur_amount;
8693 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008694 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 * doesn't start with a '{', which must have a match
8696 * in the same line (scope is the same). Probably:
8697 * { 1, 2 },
8698 * -> { 3, 4 }
8699 */
8700 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008701 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008703 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 {
8705 /* have to look back, whether it is a cpp base
8706 * class declaration or initialization */
8707 lookfor = LOOKFOR_CPP_BASECLASS;
8708 continue;
8709 }
8710 break;
8711 }
8712
8713 /*
8714 * Check if we are after an "if", "while", etc.
8715 * Also allow " } else".
8716 */
8717 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8718 {
8719 /*
8720 * Found an unterminated line after an if (), line up
8721 * with the last one.
8722 * if (cond)
8723 * 100 +
8724 * -> here;
8725 */
8726 if (lookfor == LOOKFOR_UNTERM
8727 || lookfor == LOOKFOR_ENUM_OR_INIT)
8728 {
8729 if (cont_amount > 0)
8730 amount = cont_amount;
8731 else
8732 amount += ind_continuation;
8733 break;
8734 }
8735
8736 /*
8737 * If this is just above the line we are indenting, we
8738 * are finished.
8739 * while (not)
8740 * -> here;
8741 * Otherwise this indent can be used when the line
8742 * before this is terminated.
8743 * yyy;
8744 * if (stat)
8745 * while (not)
8746 * xxx;
8747 * -> here;
8748 */
8749 amount = cur_amount;
8750 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008751 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 if (lookfor != LOOKFOR_TERM)
8753 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008754 amount += curbuf->b_ind_level
8755 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 break;
8757 }
8758
8759 /*
8760 * Special trick: when expecting the while () after a
8761 * do, line up with the while()
8762 * do
8763 * x = 1;
8764 * -> here
8765 */
8766 l = skipwhite(ml_get_curline());
8767 if (cin_isdo(l))
8768 {
8769 if (whilelevel == 0)
8770 break;
8771 --whilelevel;
8772 }
8773
8774 /*
8775 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008776 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 * Need to use the scope of this "else". XXX
8778 * If whilelevel != 0 continue looking for a "do {".
8779 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008780 if (cin_iselse(l) && whilelevel == 0)
8781 {
8782 /* If we're looking at "} else", let's make sure we
8783 * find the opening brace of the enclosing scope,
8784 * not the one from "if () {". */
8785 if (*l == '}')
8786 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008787 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008788
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008789 if ((trypos = find_start_brace()) == NULL
8790 || find_match(LOOKFOR_IF, trypos->lnum)
8791 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008792 break;
8793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 }
8795
8796 /*
8797 * If we're below an unterminated line that is not an
8798 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008799 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 * the line before this one.
8801 */
8802 else
8803 {
8804 /*
8805 * Found two unterminated lines on a row, line up with
8806 * the last one.
8807 * c = 99 +
8808 * 100 +
8809 * -> here;
8810 */
8811 if (lookfor == LOOKFOR_UNTERM)
8812 {
8813 /* When line ends in a comma add extra indent */
8814 if (terminated == ',')
8815 amount += ind_continuation;
8816 break;
8817 }
8818
8819 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8820 {
8821 /* Found two lines ending in ',', lineup with the
8822 * lowest one, but check for cpp base class
8823 * declaration/initialization, if it is an
8824 * opening brace or we are looking just for
8825 * enumerations/initializations. */
8826 if (terminated == ',')
8827 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008828 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 break;
8830
8831 lookfor = LOOKFOR_CPP_BASECLASS;
8832 continue;
8833 }
8834
8835 /* Ignore unterminated lines in between, but
8836 * reduce indent. */
8837 if (amount > cur_amount)
8838 amount = cur_amount;
8839 }
8840 else
8841 {
8842 /*
8843 * Found first unterminated line on a row, may
8844 * line up with this line, remember its indent
8845 * 100 +
8846 * -> here;
8847 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008848 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008850
8851 n = (int)STRLEN(l);
8852 if (terminated == ',' && (*skipwhite(l) == ']'
8853 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008854 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855
8856 /*
8857 * If previous line ends in ',', check whether we
8858 * are in an initialization or enum
8859 * struct xxx =
8860 * {
8861 * sizeof a,
8862 * 124 };
8863 * or a normal possible continuation line.
8864 * but only, of no other statement has been found
8865 * yet.
8866 */
8867 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8868 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008869 if (curbuf->b_ind_js)
8870 {
8871 /* Search for a line ending in a comma
8872 * and line up with the line below it
8873 * (could be the current line).
8874 * some = [
8875 * 1, <- line up here
8876 * 2,
8877 * some = [
8878 * 3 + <- line up here
8879 * 4 *
8880 * 5,
8881 * 6,
8882 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008883 if (cin_iscomment(skipwhite(l)))
8884 break;
8885 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008886 trypos = find_match_char('[',
8887 curbuf->b_ind_maxparen);
8888 if (trypos != NULL)
8889 {
8890 if (trypos->lnum
8891 == curwin->w_cursor.lnum - 1)
8892 {
8893 /* Current line is first inside
8894 * [], line up with it. */
8895 break;
8896 }
8897 ourscope = trypos->lnum;
8898 }
8899 }
8900 else
8901 {
8902 lookfor = LOOKFOR_ENUM_OR_INIT;
8903 cont_amount = cin_first_id_amount();
8904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 }
8906 else
8907 {
8908 if (lookfor == LOOKFOR_INITIAL
8909 && *l != NUL
8910 && l[STRLEN(l) - 1] == '\\')
8911 /* XXX */
8912 cont_amount = cin_get_equal_amount(
8913 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008914 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008915 && lookfor != LOOKFOR_JS_KEY
Bram Moolenaardde81312017-08-26 17:49:01 +02008916 && lookfor != LOOKFOR_COMMA
8917 && raw_string_start != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 lookfor = LOOKFOR_UNTERM;
8919 }
8920 }
8921 }
8922 }
8923
8924 /*
8925 * Check if we are after a while (cond);
8926 * If so: Ignore until the matching "do".
8927 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008928 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 {
8930 /*
8931 * Found an unterminated line after a while ();, line up
8932 * with the last one.
8933 * while (cond);
8934 * 100 + <- line up with this one
8935 * -> here;
8936 */
8937 if (lookfor == LOOKFOR_UNTERM
8938 || lookfor == LOOKFOR_ENUM_OR_INIT)
8939 {
8940 if (cont_amount > 0)
8941 amount = cont_amount;
8942 else
8943 amount += ind_continuation;
8944 break;
8945 }
8946
8947 if (whilelevel == 0)
8948 {
8949 lookfor = LOOKFOR_TERM;
8950 amount = get_indent(); /* XXX */
8951 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008952 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 }
8954 ++whilelevel;
8955 }
8956
8957 /*
8958 * We are after a "normal" statement.
8959 * If we had another statement we can stop now and use the
8960 * indent of that other statement.
8961 * Otherwise the indent of the current statement may be used,
8962 * search backwards for the next "normal" statement.
8963 */
8964 else
8965 {
8966 /*
8967 * Skip single break line, if before a switch label. It
8968 * may be lined up with the case label.
8969 */
8970 if (lookfor == LOOKFOR_NOBREAK
8971 && cin_isbreak(skipwhite(ml_get_curline())))
8972 {
8973 lookfor = LOOKFOR_ANY;
8974 continue;
8975 }
8976
8977 /*
8978 * Handle "do {" line.
8979 */
8980 if (whilelevel > 0)
8981 {
8982 l = cin_skipcomment(ml_get_curline());
8983 if (cin_isdo(l))
8984 {
8985 amount = get_indent(); /* XXX */
8986 --whilelevel;
8987 continue;
8988 }
8989 }
8990
8991 /*
8992 * Found a terminated line above an unterminated line. Add
8993 * the amount for a continuation line.
8994 * x = 1;
8995 * y = foo +
8996 * -> here;
8997 * or
8998 * int x = 1;
8999 * int foo,
9000 * -> here;
9001 */
9002 if (lookfor == LOOKFOR_UNTERM
9003 || lookfor == LOOKFOR_ENUM_OR_INIT)
9004 {
9005 if (cont_amount > 0)
9006 amount = cont_amount;
9007 else
9008 amount += ind_continuation;
9009 break;
9010 }
9011
9012 /*
9013 * Found a terminated line above a terminated line or "if"
9014 * etc. line. Use the amount of the line below us.
9015 * x = 1; x = 1;
9016 * if (asdf) y = 2;
9017 * while (asdf) ->here;
9018 * here;
9019 * ->foo;
9020 */
9021 if (lookfor == LOOKFOR_TERM)
9022 {
9023 if (!lookfor_break && whilelevel == 0)
9024 break;
9025 }
9026
9027 /*
9028 * First line above the one we're indenting is terminated.
9029 * To know what needs to be done look further backward for
9030 * a terminated line.
9031 */
9032 else
9033 {
9034 /*
9035 * position the cursor over the rightmost paren, so
9036 * that matching it will take us back to the start of
9037 * the line. Helps for:
9038 * func(asdr,
9039 * asdfasdf);
9040 * here;
9041 */
9042term_again:
9043 l = ml_get_curline();
9044 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009045 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009046 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 {
9048 /*
9049 * Check if we are on a case label now. This is
9050 * handled above.
9051 * case xx: if ( asdf &&
9052 * asdf)
9053 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009054 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02009056 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 {
9058 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009059 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 continue;
9061 }
9062 }
9063
9064 /* When aligning with the case statement, don't align
9065 * with a statement after it.
9066 * case 1: { <-- don't use this { position
9067 * stat;
9068 * }
9069 * case 2:
9070 * stat;
9071 * }
9072 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009073 iscase = (curbuf->b_ind_keep_case_label
9074 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075
9076 /*
9077 * Get indent and pointer to text for current line,
9078 * ignoring any jump label.
9079 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009080 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081
9082 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009083 amount += curbuf->b_ind_open_extra;
9084 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00009085 l = skipwhite(l);
9086 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009087 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
9089
9090 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009091 * When a terminated line starts with "else" skip to
9092 * the matching "if":
9093 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009094 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009095 * Need to use the scope of this "else". XXX
9096 * If whilelevel != 0 continue looking for a "do {".
9097 */
9098 if (lookfor == LOOKFOR_TERM
9099 && *l != '}'
9100 && cin_iselse(l)
9101 && whilelevel == 0)
9102 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009103 if ((trypos = find_start_brace()) == NULL
9104 || find_match(LOOKFOR_IF, trypos->lnum)
9105 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009106 break;
9107 continue;
9108 }
9109
9110 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 * If we're at the end of a block, skip to the start of
9112 * that block.
9113 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01009114 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009115 if (find_last_paren(l, '{', '}') /* XXX */
9116 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009118 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 /* if not "else {" check for terminated again */
9120 /* but skip block for "} else {" */
9121 l = cin_skipcomment(ml_get_curline());
9122 if (*l == '}' || !cin_iselse(l))
9123 goto term_again;
9124 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009125 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 }
9127 }
9128 }
9129 }
9130 }
9131 }
9132
9133 /* add extra indent for a comment */
9134 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009135 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02009136
9137 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009138 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
9139 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009140
9141 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009143
9144 /*
9145 * ok -- we're not inside any sort of structure at all!
9146 *
9147 * This means we're at the top level, and everything should
9148 * basically just match where the previous line is, except
9149 * for the lines immediately following a function declaration,
9150 * which are K&R-style parameters and need to be indented.
9151 *
9152 * if our line starts with an open brace, forget about any
9153 * prevailing indent and make sure it looks like the start
9154 * of a function
9155 */
9156
9157 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009159 amount = curbuf->b_ind_first_open;
9160 goto theend;
9161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009163 /*
9164 * If the NEXT line is a function declaration, the current
9165 * line needs to be indented as a function type spec.
9166 * Don't do this if the current line looks like a comment or if the
9167 * current line is terminated, ie. ends in ';', or if the current line
9168 * contains { or }: "void f() {\n if (1)"
9169 */
9170 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
9171 && !cin_nocode(theline)
9172 && vim_strchr(theline, '{') == NULL
9173 && vim_strchr(theline, '}') == NULL
9174 && !cin_ends_in(theline, (char_u *)":", NULL)
9175 && !cin_ends_in(theline, (char_u *)",", NULL)
9176 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
9177 cur_curpos.lnum + 1)
9178 && !cin_isterminated(theline, FALSE, TRUE))
9179 {
9180 amount = curbuf->b_ind_func_type;
9181 goto theend;
9182 }
9183
9184 /* search backwards until we find something we recognize */
9185 amount = 0;
9186 curwin->w_cursor = cur_curpos;
9187 while (curwin->w_cursor.lnum > 1)
9188 {
9189 curwin->w_cursor.lnum--;
9190 curwin->w_cursor.col = 0;
9191
9192 l = ml_get_curline();
9193
9194 /*
9195 * If we're in a comment or raw string now, skip to the start
9196 * of it.
9197 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02009198 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009200 curwin->w_cursor.lnum = trypos->lnum + 1;
9201 curwin->w_cursor.col = 0;
9202 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 }
9204
9205 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009206 * Are we at the start of a cpp base class declaration or
9207 * constructor initialization?
9208 */ /* XXX */
9209 n = FALSE;
9210 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009212 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
9213 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009215 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009217 /* XXX */
9218 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
9219 break;
9220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009222 /*
9223 * Skip preprocessor directives and blank lines.
9224 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009225 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009226 continue;
9227
9228 if (cin_nocode(l))
9229 continue;
9230
9231 /*
9232 * If the previous line ends in ',', use one level of
9233 * indentation:
9234 * int foo,
9235 * bar;
9236 * do this before checking for '}' in case of eg.
9237 * enum foobar
9238 * {
9239 * ...
9240 * } foo,
9241 * bar;
9242 */
9243 n = 0;
9244 if (cin_ends_in(l, (char_u *)",", NULL)
9245 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
9246 {
9247 /* take us back to opening paren */
9248 if (find_last_paren(l, '(', ')')
9249 && (trypos = find_match_paren(
9250 curbuf->b_ind_maxparen)) != NULL)
9251 curwin->w_cursor = *trypos;
9252
9253 /* For a line ending in ',' that is a continuation line go
9254 * back to the first line with a backslash:
9255 * char *foo = "bla\
9256 * bla",
9257 * here;
9258 */
9259 while (n == 0 && curwin->w_cursor.lnum > 1)
9260 {
9261 l = ml_get(curwin->w_cursor.lnum - 1);
9262 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
9263 break;
9264 --curwin->w_cursor.lnum;
9265 curwin->w_cursor.col = 0;
9266 }
9267
9268 amount = get_indent(); /* XXX */
9269
9270 if (amount == 0)
9271 amount = cin_first_id_amount();
9272 if (amount == 0)
9273 amount = ind_continuation;
9274 break;
9275 }
9276
9277 /*
9278 * If the line looks like a function declaration, and we're
9279 * not in a comment, put it the left margin.
9280 */
9281 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
9282 break;
9283 l = ml_get_curline();
9284
9285 /*
9286 * Finding the closing '}' of a previous function. Put
9287 * current line at the left margin. For when 'cino' has "fs".
9288 */
9289 if (*skipwhite(l) == '}')
9290 break;
9291
9292 /* (matching {)
9293 * If the previous line ends on '};' (maybe followed by
9294 * comments) align at column 0. For example:
9295 * char *string_array[] = { "foo",
9296 * / * x * / "b};ar" }; / * foobar * /
9297 */
9298 if (cin_ends_in(l, (char_u *)"};", NULL))
9299 break;
9300
9301 /*
9302 * If the previous line ends on '[' we are probably in an
9303 * array constant:
9304 * something = [
9305 * 234, <- extra indent
9306 */
9307 if (cin_ends_in(l, (char_u *)"[", NULL))
9308 {
9309 amount = get_indent() + ind_continuation;
9310 break;
9311 }
9312
9313 /*
9314 * Find a line only has a semicolon that belongs to a previous
9315 * line ending in '}', e.g. before an #endif. Don't increase
9316 * indent then.
9317 */
9318 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
9319 {
9320 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321
9322 while (curwin->w_cursor.lnum > 1)
9323 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009324 look = ml_get(--curwin->w_cursor.lnum);
9325 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009326 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009328 }
9329 if (curwin->w_cursor.lnum > 0
9330 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009333 curwin->w_cursor = curpos_save;
9334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009336 /*
9337 * If the PREVIOUS line is a function declaration, the current
9338 * line (and the ones that follow) needs to be indented as
9339 * parameters.
9340 */
9341 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
9342 {
9343 amount = curbuf->b_ind_param;
9344 break;
9345 }
9346
9347 /*
9348 * If the previous line ends in ';' and the line before the
9349 * previous line ends in ',' or '\', ident to column zero:
9350 * int foo,
9351 * bar;
9352 * indent_to_0 here;
9353 */
9354 if (cin_ends_in(l, (char_u *)";", NULL))
9355 {
9356 l = ml_get(curwin->w_cursor.lnum - 1);
9357 if (cin_ends_in(l, (char_u *)",", NULL)
9358 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9359 break;
9360 l = ml_get_curline();
9361 }
9362
9363 /*
9364 * Doesn't look like anything interesting -- so just
9365 * use the indent of this line.
9366 *
9367 * Position the cursor over the rightmost paren, so that
9368 * matching it will take us back to the start of the line.
9369 */
9370 find_last_paren(l, '(', ')');
9371
9372 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9373 curwin->w_cursor = *trypos;
9374 amount = get_indent(); /* XXX */
9375 break;
9376 }
9377
9378 /* add extra indent for a comment */
9379 if (cin_iscomment(theline))
9380 amount += curbuf->b_ind_comment;
9381
9382 /* add extra indent if the previous line ended in a backslash:
9383 * "asdfasdf\
9384 * here";
9385 * char *foo = "asdf\
9386 * here";
9387 */
9388 if (cur_curpos.lnum > 1)
9389 {
9390 l = ml_get(cur_curpos.lnum - 1);
9391 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9392 {
9393 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9394 if (cur_amount > 0)
9395 amount = cur_amount;
9396 else if (cur_amount == 0)
9397 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 }
9399 }
9400
9401theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009402 if (amount < 0)
9403 amount = 0;
9404
9405laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 /* put the cursor back where it belongs */
9407 curwin->w_cursor = cur_curpos;
9408
9409 vim_free(linecopy);
9410
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 return amount;
9412}
9413
9414 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009415find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416{
9417 char_u *look;
9418 pos_T *theirscope;
9419 char_u *mightbeif;
9420 int elselevel;
9421 int whilelevel;
9422
9423 if (lookfor == LOOKFOR_IF)
9424 {
9425 elselevel = 1;
9426 whilelevel = 0;
9427 }
9428 else
9429 {
9430 elselevel = 0;
9431 whilelevel = 1;
9432 }
9433
9434 curwin->w_cursor.col = 0;
9435
9436 while (curwin->w_cursor.lnum > ourscope + 1)
9437 {
9438 curwin->w_cursor.lnum--;
9439 curwin->w_cursor.col = 0;
9440
9441 look = cin_skipcomment(ml_get_curline());
9442 if (cin_iselse(look)
9443 || cin_isif(look)
9444 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009445 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446 {
9447 /*
9448 * if we've gone outside the braces entirely,
9449 * we must be out of scope...
9450 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009451 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 if (theirscope == NULL)
9453 break;
9454
9455 /*
9456 * and if the brace enclosing this is further
9457 * back than the one enclosing the else, we're
9458 * out of luck too.
9459 */
9460 if (theirscope->lnum < ourscope)
9461 break;
9462
9463 /*
9464 * and if they're enclosed in a *deeper* brace,
9465 * then we can ignore it because it's in a
9466 * different scope...
9467 */
9468 if (theirscope->lnum > ourscope)
9469 continue;
9470
9471 /*
9472 * if it was an "else" (that's not an "else if")
9473 * then we need to go back to another if, so
9474 * increment elselevel
9475 */
9476 look = cin_skipcomment(ml_get_curline());
9477 if (cin_iselse(look))
9478 {
9479 mightbeif = cin_skipcomment(look + 4);
9480 if (!cin_isif(mightbeif))
9481 ++elselevel;
9482 continue;
9483 }
9484
9485 /*
9486 * if it was a "while" then we need to go back to
9487 * another "do", so increment whilelevel. XXX
9488 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009489 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 {
9491 ++whilelevel;
9492 continue;
9493 }
9494
9495 /* If it's an "if" decrement elselevel */
9496 look = cin_skipcomment(ml_get_curline());
9497 if (cin_isif(look))
9498 {
9499 elselevel--;
9500 /*
9501 * When looking for an "if" ignore "while"s that
9502 * get in the way.
9503 */
9504 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9505 whilelevel = 0;
9506 }
9507
9508 /* If it's a "do" decrement whilelevel */
9509 if (cin_isdo(look))
9510 whilelevel--;
9511
9512 /*
9513 * if we've used up all the elses, then
9514 * this must be the if that we want!
9515 * match the indent level of that if.
9516 */
9517 if (elselevel <= 0 && whilelevel <= 0)
9518 {
9519 return OK;
9520 }
9521 }
9522 }
9523 return FAIL;
9524}
9525
9526# if defined(FEAT_EVAL) || defined(PROTO)
9527/*
9528 * Get indent level from 'indentexpr'.
9529 */
9530 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009531get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532{
Bram Moolenaar97db5542017-04-21 23:18:26 +02009533 int indent = -1;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009534 char_u *inde_copy;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009535 pos_T save_pos;
9536 colnr_T save_curswant;
9537 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009539 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9540 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009542 /* Save and restore cursor position and curswant, in case it was changed
9543 * via :normal commands */
9544 save_pos = curwin->w_cursor;
9545 save_curswant = curwin->w_curswant;
9546 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009548 if (use_sandbox)
9549 ++sandbox;
9550 ++textlock;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009551
9552 /* Need to make a copy, the 'indentexpr' option could be changed while
9553 * evaluating it. */
9554 inde_copy = vim_strsave(curbuf->b_p_inde);
9555 if (inde_copy != NULL)
9556 {
9557 indent = (int)eval_to_number(inde_copy);
9558 vim_free(inde_copy);
9559 }
9560
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009561 if (use_sandbox)
9562 --sandbox;
9563 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564
9565 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9566 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9567 * command. */
9568 save_State = State;
9569 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009570 curwin->w_cursor = save_pos;
9571 curwin->w_curswant = save_curswant;
9572 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 check_cursor();
9574 State = save_State;
9575
9576 /* If there is an error, just keep the current indent. */
9577 if (indent < 0)
9578 indent = get_indent();
9579
9580 return indent;
9581}
9582# endif
9583
9584#endif /* FEAT_CINDENT */
9585
9586#if defined(FEAT_LISP) || defined(PROTO)
9587
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009589lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590{
9591 char_u buf[LSIZE];
9592 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009593 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594
9595 while (*word != NUL)
9596 {
9597 (void)copy_option_part(&word, buf, LSIZE, ",");
9598 len = (int)STRLEN(buf);
9599 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9600 return TRUE;
9601 }
9602 return FALSE;
9603}
9604
9605/*
9606 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9607 * The incompatible newer method is quite a bit better at indenting
9608 * code in lisp-like languages than the traditional one; it's still
9609 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9610 *
9611 * TODO:
9612 * Findmatch() should be adapted for lisp, also to make showmatch
9613 * work correctly: now (v5.3) it seems all C/C++ oriented:
9614 * - it does not recognize the #\( and #\) notations as character literals
9615 * - it doesn't know about comments starting with a semicolon
9616 * - it incorrectly interprets '(' as a character literal
9617 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009618 * Update from Sergey Khorev:
9619 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 */
9621 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009622get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009624 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 int amount;
9626 char_u *that;
9627 colnr_T col;
9628 colnr_T firsttry;
9629 int parencount, quotecount;
9630 int vi_lisp;
9631
9632 /* Set vi_lisp to use the vi-compatible method */
9633 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9634
9635 realpos = curwin->w_cursor;
9636 curwin->w_cursor.col = 0;
9637
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009638 if ((pos = findmatch(NULL, '(')) == NULL)
9639 pos = findmatch(NULL, '[');
9640 else
9641 {
9642 paren = *pos;
9643 pos = findmatch(NULL, '[');
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009644 if (pos == NULL || LT_POSP(pos, &paren))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009645 pos = &paren;
9646 }
9647 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 {
9649 /* Extra trick: Take the indent of the first previous non-white
9650 * line that is at the same () level. */
9651 amount = -1;
9652 parencount = 0;
9653
9654 while (--curwin->w_cursor.lnum >= pos->lnum)
9655 {
9656 if (linewhite(curwin->w_cursor.lnum))
9657 continue;
9658 for (that = ml_get_curline(); *that != NUL; ++that)
9659 {
9660 if (*that == ';')
9661 {
9662 while (*(that + 1) != NUL)
9663 ++that;
9664 continue;
9665 }
9666 if (*that == '\\')
9667 {
9668 if (*(that + 1) != NUL)
9669 ++that;
9670 continue;
9671 }
9672 if (*that == '"' && *(that + 1) != NUL)
9673 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009674 while (*++that && *that != '"')
9675 {
9676 /* skipping escaped characters in the string */
9677 if (*that == '\\')
9678 {
9679 if (*++that == NUL)
9680 break;
9681 if (that[1] == NUL)
9682 {
9683 ++that;
9684 break;
9685 }
9686 }
9687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009689 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009691 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692 --parencount;
9693 }
9694 if (parencount == 0)
9695 {
9696 amount = get_indent();
9697 break;
9698 }
9699 }
9700
9701 if (amount == -1)
9702 {
9703 curwin->w_cursor.lnum = pos->lnum;
9704 curwin->w_cursor.col = pos->col;
9705 col = pos->col;
9706
9707 that = ml_get_curline();
9708
9709 if (vi_lisp && get_indent() == 0)
9710 amount = 2;
9711 else
9712 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009713 char_u *line = that;
9714
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 amount = 0;
9716 while (*that && col)
9717 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009718 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 col--;
9720 }
9721
9722 /*
9723 * Some keywords require "body" indenting rules (the
9724 * non-standard-lisp ones are Scheme special forms):
9725 *
9726 * (let ((a 1)) instead (let ((a 1))
9727 * (...)) of (...))
9728 */
9729
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009730 if (!vi_lisp && (*that == '(' || *that == '[')
9731 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 amount += 2;
9733 else
9734 {
9735 that++;
9736 amount++;
9737 firsttry = amount;
9738
Bram Moolenaar1c465442017-03-12 20:10:05 +01009739 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009741 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742 ++that;
9743 }
9744
9745 if (*that && *that != ';') /* not a comment line */
9746 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009747 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009749 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 firsttry++;
9751
9752 parencount = 0;
9753 quotecount = 0;
9754
9755 if (vi_lisp
9756 || (*that != '"'
9757 && *that != '\''
9758 && *that != '#'
9759 && (*that < '0' || *that > '9')))
9760 {
9761 while (*that
Bram Moolenaar1c465442017-03-12 20:10:05 +01009762 && (!VIM_ISWHITE(*that)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 || quotecount
9764 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009765 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 && !quotecount
9767 && !parencount
9768 && vi_lisp)))
9769 {
9770 if (*that == '"')
9771 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009772 if ((*that == '(' || *that == '[')
9773 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009775 if ((*that == ')' || *that == ']')
9776 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 --parencount;
9778 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009779 amount += lbr_chartabsize_adv(
9780 line, &that, (colnr_T)amount);
9781 amount += lbr_chartabsize_adv(
9782 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 }
9784 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01009785 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009787 amount += lbr_chartabsize(
9788 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 that++;
9790 }
9791 if (!*that || *that == ';')
9792 amount = firsttry;
9793 }
9794 }
9795 }
9796 }
9797 }
9798 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009799 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800
9801 curwin->w_cursor = realpos;
9802
9803 return amount;
9804}
9805#endif /* FEAT_LISP */
9806
9807 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009808prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009810#if defined(SIGHUP) && defined(SIG_IGN)
9811 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9812 * makes Vim exit and then handling SIGHUP causes various reentrance
9813 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009814 signal(SIGHUP, SIG_IGN);
9815#endif
9816
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817#ifdef FEAT_GUI
9818 if (gui.in_use)
9819 {
9820 gui.dying = TRUE;
9821 out_trash(); /* trash any pending output */
9822 }
9823 else
9824#endif
9825 {
9826 windgoto((int)Rows - 1, 0);
9827
9828 /*
9829 * Switch terminal mode back now, so messages end up on the "normal"
9830 * screen (if there are two screens).
9831 */
9832 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009833 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834 out_flush();
9835 }
9836}
9837
9838/*
9839 * Preserve files and exit.
9840 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009841 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9842 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 */
9844 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009845preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846{
9847 buf_T *buf;
9848
9849 prepare_to_exit();
9850
Bram Moolenaar4770d092006-01-12 23:22:24 +00009851 /* Setting this will prevent free() calls. That avoids calling free()
9852 * recursively when free() was invoked with a bad pointer. */
9853 really_exiting = TRUE;
9854
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 out_str(IObuff);
9856 screen_start(); /* don't know where cursor is now */
9857 out_flush();
9858
9859 ml_close_notmod(); /* close all not-modified buffers */
9860
Bram Moolenaar29323592016-07-24 22:04:11 +02009861 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 {
9863 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9864 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009865 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 screen_start(); /* don't know where cursor is now */
9867 out_flush();
9868 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9869 break;
9870 }
9871 }
9872
9873 ml_close_all(FALSE); /* close all memfiles, without deleting */
9874
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009875 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876
9877 getout(1);
9878}
9879
9880/*
9881 * return TRUE if "fname" exists.
9882 */
9883 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009884vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009886 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887
9888 if (mch_stat((char *)fname, &st))
9889 return FALSE;
9890 return TRUE;
9891}
9892
9893/*
9894 * Check for CTRL-C pressed, but only once in a while.
9895 * Should be used instead of ui_breakcheck() for functions that check for
9896 * each line in the file. Calling ui_breakcheck() each time takes too much
9897 * time, because it can be a system call.
9898 */
9899
9900#ifndef BREAKCHECK_SKIP
9901# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9902# define BREAKCHECK_SKIP 200
9903# else
9904# define BREAKCHECK_SKIP 32
9905# endif
9906#endif
9907
9908static int breakcheck_count = 0;
9909
9910 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009911line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912{
9913 if (++breakcheck_count >= BREAKCHECK_SKIP)
9914 {
9915 breakcheck_count = 0;
9916 ui_breakcheck();
9917 }
9918}
9919
9920/*
9921 * Like line_breakcheck() but check 10 times less often.
9922 */
9923 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009924fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925{
9926 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9927 {
9928 breakcheck_count = 0;
9929 ui_breakcheck();
9930 }
9931}
9932
9933/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009934 * Invoke expand_wildcards() for one pattern.
9935 * Expand items like "%:h" before the expansion.
9936 * Returns OK or FAIL.
9937 */
9938 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009939expand_wildcards_eval(
9940 char_u **pat, /* pointer to input pattern */
9941 int *num_file, /* resulting number of files */
9942 char_u ***file, /* array of resulting files */
9943 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009944{
9945 int ret = FAIL;
9946 char_u *eval_pat = NULL;
9947 char_u *exp_pat = *pat;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009948 char *ignored_msg;
Bram Moolenaard7834d32009-12-02 16:14:36 +00009949 int usedlen;
9950
9951 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9952 {
9953 ++emsg_off;
9954 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9955 NULL, &ignored_msg, NULL);
9956 --emsg_off;
9957 if (eval_pat != NULL)
9958 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9959 }
9960
9961 if (exp_pat != NULL)
9962 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9963
9964 if (eval_pat != NULL)
9965 {
9966 vim_free(exp_pat);
9967 vim_free(eval_pat);
9968 }
9969
9970 return ret;
9971}
9972
9973/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9975 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009976 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977 */
9978 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009979expand_wildcards(
9980 int num_pat, /* number of input patterns */
9981 char_u **pat, /* array of input patterns */
9982 int *num_files, /* resulting number of files */
9983 char_u ***files, /* array of resulting files */
9984 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985{
9986 int retval;
9987 int i, j;
9988 char_u *p;
9989 int non_suf_match; /* number without matching suffix */
9990
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009991 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992
9993 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009994 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995 return retval;
9996
9997#ifdef FEAT_WILDIGN
9998 /*
9999 * Remove names that match 'wildignore'.
10000 */
10001 if (*p_wig)
10002 {
10003 char_u *ffname;
10004
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010005 /* check all files in (*files)[] */
10006 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010008 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009 if (ffname == NULL) /* out of memory */
10010 break;
10011# ifdef VMS
10012 vms_remove_version(ffname);
10013# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010014 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010016 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010017 vim_free((*files)[i]);
10018 for (j = i; j + 1 < *num_files; ++j)
10019 (*files)[j] = (*files)[j + 1];
10020 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021 --i;
10022 }
10023 vim_free(ffname);
10024 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010025
10026 /* If the number of matches is now zero, we fail. */
10027 if (*num_files == 0)
10028 {
Bram Moolenaard23a8232018-02-10 18:45:26 +010010029 VIM_CLEAR(*files);
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010030 return FAIL;
10031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 }
10033#endif
10034
10035 /*
10036 * Move the names where 'suffixes' match to the end.
10037 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010038 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 {
10040 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010041 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010043 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 {
10045 /*
10046 * Move the name without matching suffix to the front
10047 * of the list.
10048 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010049 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010051 (*files)[j] = (*files)[j - 1];
10052 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 }
10054 }
10055 }
10056
10057 return retval;
10058}
10059
10060/*
10061 * Return TRUE if "fname" matches with an entry in 'suffixes'.
10062 */
10063 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010064match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065{
10066 int fnamelen, setsuflen;
10067 char_u *setsuf;
10068#define MAXSUFLEN 30 /* maximum length of a file suffix */
10069 char_u suf_buf[MAXSUFLEN];
10070
10071 fnamelen = (int)STRLEN(fname);
10072 setsuflen = 0;
10073 for (setsuf = p_su; *setsuf; )
10074 {
10075 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +000010076 if (setsuflen == 0)
10077 {
10078 char_u *tail = gettail(fname);
10079
10080 /* empty entry: match name without a '.' */
10081 if (vim_strchr(tail, '.') == NULL)
10082 {
10083 setsuflen = 1;
10084 break;
10085 }
10086 }
10087 else
10088 {
10089 if (fnamelen >= setsuflen
10090 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
10091 (size_t)setsuflen) == 0)
10092 break;
10093 setsuflen = 0;
10094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 }
10096 return (setsuflen != 0);
10097}
10098
10099#if !defined(NO_EXPANDPATH) || defined(PROTO)
10100
10101# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010102static int vim_backtick(char_u *p);
10103static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104# endif
10105
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010106# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107/*
10108 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
10109 * it's shared between these systems.
10110 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010111# if defined(PROTO)
10112# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113# else
10114# ifdef __BORLANDC__
10115# define _cdecl _RTLENTRYF
10116# endif
10117# endif
10118
10119/*
10120 * comparison function for qsort in dos_expandpath()
10121 */
10122 static int _cdecl
10123pstrcmp(const void *a, const void *b)
10124{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010125 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126}
10127
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128/*
Bram Moolenaar231334e2005-07-25 20:46:57 +000010129 * Recursively expand one path component into all matching files and/or
10130 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131 * Return the number of matches found.
10132 * "path" has backslashes before chars that are not to be expanded, starting
10133 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +000010134 * Return the number of matches found.
10135 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 */
10137 static int
10138dos_expandpath(
10139 garray_T *gap,
10140 char_u *path,
10141 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +000010142 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +000010143 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010145 char_u *buf;
10146 char_u *path_end;
10147 char_u *p, *s, *e;
10148 int start_len = gap->ga_len;
10149 char_u *pat;
10150 regmatch_T regmatch;
10151 int starts_with_dot;
10152 int matches;
10153 int len;
10154 int starstar = FALSE;
10155 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156 WIN32_FIND_DATA fb;
10157 HANDLE hFind = (HANDLE)0;
10158# ifdef FEAT_MBYTE
10159 WIN32_FIND_DATAW wfb;
10160 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
10161# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010163 int ok;
10164
10165 /* Expanding "**" may take a long time, check for CTRL-C. */
10166 if (stardepth > 0)
10167 {
10168 ui_breakcheck();
10169 if (got_int)
10170 return 0;
10171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172
Bram Moolenaar7314efd2015-10-31 15:32:52 +010010173 /* Make room for file name. When doing encoding conversion the actual
10174 * length may be quite a bit longer, thus use the maximum possible length. */
10175 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176 if (buf == NULL)
10177 return 0;
10178
10179 /*
10180 * Find the first part in the path name that contains a wildcard or a ~1.
10181 * Copy it into buf, including the preceding characters.
10182 */
10183 p = buf;
10184 s = buf;
10185 e = NULL;
10186 path_end = path;
10187 while (*path_end != NUL)
10188 {
10189 /* May ignore a wildcard that has a backslash before it; it will
10190 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10191 if (path_end >= path + wildoff && rem_backslash(path_end))
10192 *p++ = *path_end++;
10193 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
10194 {
10195 if (e != NULL)
10196 break;
10197 s = p + 1;
10198 }
10199 else if (path_end >= path + wildoff
10200 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
10201 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +010010202# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 if (has_mbyte)
10204 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010205 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 STRNCPY(p, path_end, len);
10207 p += len;
10208 path_end += len;
10209 }
10210 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +010010211# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 *p++ = *path_end++;
10213 }
10214 e = p;
10215 *e = NUL;
10216
10217 /* now we have one wildcard component between s and e */
10218 /* Remove backslashes between "wildoff" and the start of the wildcard
10219 * component. */
10220 for (p = buf + wildoff; p < s; ++p)
10221 if (rem_backslash(p))
10222 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010223 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 --e;
10225 --s;
10226 }
10227
Bram Moolenaar231334e2005-07-25 20:46:57 +000010228 /* Check for "**" between "s" and "e". */
10229 for (p = s; p < e; ++p)
10230 if (p[0] == '*' && p[1] == '*')
10231 starstar = TRUE;
10232
Bram Moolenaard82103e2016-01-17 17:04:05 +010010233 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10235 if (pat == NULL)
10236 {
10237 vim_free(buf);
10238 return 0;
10239 }
10240
10241 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010242 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010243 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 regmatch.rm_ic = TRUE; /* Always ignore case */
10245 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010246 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010247 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248 vim_free(pat);
10249
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010250 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251 {
10252 vim_free(buf);
10253 return 0;
10254 }
10255
10256 /* remember the pattern or file name being looked for */
10257 matchname = vim_strsave(s);
10258
Bram Moolenaar231334e2005-07-25 20:46:57 +000010259 /* If "**" is by itself, this is the first time we encounter it and more
10260 * is following then find matches without any directory. */
10261 if (!didstar && stardepth < 100 && starstar && e - s == 2
10262 && *path_end == '/')
10263 {
10264 STRCPY(s, path_end + 1);
10265 ++stardepth;
10266 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10267 --stardepth;
10268 }
10269
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270 /* Scan all files in the directory with "dir/ *.*" */
10271 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272# ifdef FEAT_MBYTE
10273 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
10274 {
10275 /* The active codepage differs from 'encoding'. Attempt using the
10276 * wide function. If it fails because it is not implemented fall back
10277 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010278 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279 if (wn != NULL)
10280 {
10281 hFind = FindFirstFileW(wn, &wfb);
10282 if (hFind == INVALID_HANDLE_VALUE
10283 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
Bram Moolenaard23a8232018-02-10 18:45:26 +010010284 VIM_CLEAR(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285 }
10286 }
10287
10288 if (wn == NULL)
10289# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010290 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292
10293 while (ok)
10294 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295# ifdef FEAT_MBYTE
10296 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010297 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298 else
10299# endif
10300 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301 /* Ignore entries starting with a dot, unless when asked for. Accept
10302 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010303 if ((p[0] != '.' || starts_with_dot
10304 || ((flags & EW_DODOT)
10305 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010307 || (regmatch.regprog != NULL
10308 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010309 || ((flags & EW_NOTWILD)
10310 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010314
10315 if (starstar && stardepth < 100)
10316 {
10317 /* For "**" in the pattern first go deeper in the tree to
10318 * find matches. */
10319 STRCPY(buf + len, "/**");
10320 STRCPY(buf + len + 3, path_end);
10321 ++stardepth;
10322 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
10323 --stardepth;
10324 }
10325
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326 STRCPY(buf + len, path_end);
10327 if (mch_has_exp_wildcard(path_end))
10328 {
10329 /* need to expand another component of the path */
10330 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010331 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 }
10333 else
10334 {
10335 /* no more wildcards, check if there is a match */
10336 /* remove backslashes for the remaining components only */
10337 if (*path_end != 0)
10338 backslash_halve(buf + len + 1);
10339 if (mch_getperm(buf) >= 0) /* add existing file */
10340 addfile(gap, buf, flags);
10341 }
10342 }
10343
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344# ifdef FEAT_MBYTE
10345 if (wn != NULL)
10346 {
10347 vim_free(p);
10348 ok = FindNextFileW(hFind, &wfb);
10349 }
10350 else
10351# endif
10352 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353
10354 /* If no more matches and no match was used, try expanding the name
10355 * itself. Finds the long name of a short filename. */
10356 if (!ok && matchname != NULL && gap->ga_len == start_len)
10357 {
10358 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 FindClose(hFind);
10360# ifdef FEAT_MBYTE
10361 if (wn != NULL)
10362 {
10363 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010364 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365 if (wn != NULL)
10366 hFind = FindFirstFileW(wn, &wfb);
10367 }
10368 if (wn == NULL)
10369# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010370 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaard23a8232018-02-10 18:45:26 +010010372 VIM_CLEAR(matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373 }
10374 }
10375
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376 FindClose(hFind);
10377# ifdef FEAT_MBYTE
10378 vim_free(wn);
10379# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010381 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 vim_free(matchname);
10383
10384 matches = gap->ga_len - start_len;
10385 if (matches > 0)
10386 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10387 sizeof(char_u *), pstrcmp);
10388 return matches;
10389}
10390
10391 int
10392mch_expandpath(
10393 garray_T *gap,
10394 char_u *path,
10395 int flags) /* EW_* flags */
10396{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010397 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010399# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400
Bram Moolenaar231334e2005-07-25 20:46:57 +000010401#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10402 || defined(PROTO)
10403/*
10404 * Unix style wildcard expansion code.
10405 * It's here because it's used both for Unix and Mac.
10406 */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010407 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010408pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010409{
10410 return (pathcmp(*(char **)a, *(char **)b, -1));
10411}
10412
10413/*
10414 * Recursively expand one path component into all matching files and/or
10415 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10416 * "path" has backslashes before chars that are not to be expanded, starting
10417 * at "path + wildoff".
10418 * Return the number of matches found.
10419 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10420 */
10421 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010422unix_expandpath(
10423 garray_T *gap,
10424 char_u *path,
10425 int wildoff,
10426 int flags, /* EW_* flags */
10427 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010428{
10429 char_u *buf;
10430 char_u *path_end;
10431 char_u *p, *s, *e;
10432 int start_len = gap->ga_len;
10433 char_u *pat;
10434 regmatch_T regmatch;
10435 int starts_with_dot;
10436 int matches;
10437 int len;
10438 int starstar = FALSE;
10439 static int stardepth = 0; /* depth for "**" expansion */
10440
10441 DIR *dirp;
10442 struct dirent *dp;
10443
10444 /* Expanding "**" may take a long time, check for CTRL-C. */
10445 if (stardepth > 0)
10446 {
10447 ui_breakcheck();
10448 if (got_int)
10449 return 0;
10450 }
10451
10452 /* make room for file name */
10453 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10454 if (buf == NULL)
10455 return 0;
10456
10457 /*
10458 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010459 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010460 * Copy it into "buf", including the preceding characters.
10461 */
10462 p = buf;
10463 s = buf;
10464 e = NULL;
10465 path_end = path;
10466 while (*path_end != NUL)
10467 {
10468 /* May ignore a wildcard that has a backslash before it; it will
10469 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10470 if (path_end >= path + wildoff && rem_backslash(path_end))
10471 *p++ = *path_end++;
10472 else if (*path_end == '/')
10473 {
10474 if (e != NULL)
10475 break;
10476 s = p + 1;
10477 }
10478 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010479 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010480 || (!p_fic && (flags & EW_ICASE)
10481 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010482 e = p;
10483#ifdef FEAT_MBYTE
10484 if (has_mbyte)
10485 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010486 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010487 STRNCPY(p, path_end, len);
10488 p += len;
10489 path_end += len;
10490 }
10491 else
10492#endif
10493 *p++ = *path_end++;
10494 }
10495 e = p;
10496 *e = NUL;
10497
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010498 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010499 /* Remove backslashes between "wildoff" and the start of the wildcard
10500 * component. */
10501 for (p = buf + wildoff; p < s; ++p)
10502 if (rem_backslash(p))
10503 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010504 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010505 --e;
10506 --s;
10507 }
10508
10509 /* Check for "**" between "s" and "e". */
10510 for (p = s; p < e; ++p)
10511 if (p[0] == '*' && p[1] == '*')
10512 starstar = TRUE;
10513
10514 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010515 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010516 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10517 if (pat == NULL)
10518 {
10519 vim_free(buf);
10520 return 0;
10521 }
10522
10523 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010524 if (flags & EW_ICASE)
10525 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10526 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010527 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010528 if (flags & (EW_NOERROR | EW_NOTWILD))
10529 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010530 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010531 if (flags & (EW_NOERROR | EW_NOTWILD))
10532 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010533 vim_free(pat);
10534
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010535 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010536 {
10537 vim_free(buf);
10538 return 0;
10539 }
10540
10541 /* If "**" is by itself, this is the first time we encounter it and more
10542 * is following then find matches without any directory. */
10543 if (!didstar && stardepth < 100 && starstar && e - s == 2
10544 && *path_end == '/')
10545 {
10546 STRCPY(s, path_end + 1);
10547 ++stardepth;
10548 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10549 --stardepth;
10550 }
10551
10552 /* open the directory for scanning */
10553 *s = NUL;
10554 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10555
10556 /* Find all matching entries */
10557 if (dirp != NULL)
10558 {
10559 for (;;)
10560 {
10561 dp = readdir(dirp);
10562 if (dp == NULL)
10563 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010564 if ((dp->d_name[0] != '.' || starts_with_dot
10565 || ((flags & EW_DODOT)
10566 && dp->d_name[1] != NUL
10567 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010568 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10569 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010570 || ((flags & EW_NOTWILD)
10571 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010572 {
10573 STRCPY(s, dp->d_name);
10574 len = STRLEN(buf);
10575
10576 if (starstar && stardepth < 100)
10577 {
10578 /* For "**" in the pattern first go deeper in the tree to
10579 * find matches. */
10580 STRCPY(buf + len, "/**");
10581 STRCPY(buf + len + 3, path_end);
10582 ++stardepth;
10583 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10584 --stardepth;
10585 }
10586
10587 STRCPY(buf + len, path_end);
10588 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10589 {
10590 /* need to expand another component of the path */
10591 /* remove backslashes for the remaining components only */
10592 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10593 }
10594 else
10595 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010596 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010597
Bram Moolenaar231334e2005-07-25 20:46:57 +000010598 /* no more wildcards, check if there is a match */
10599 /* remove backslashes for the remaining components only */
10600 if (*path_end != NUL)
10601 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010602 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010603 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010604 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010605 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010606#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010607 size_t precomp_len = STRLEN(buf)+1;
10608 char_u *precomp_buf =
10609 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010610
Bram Moolenaar231334e2005-07-25 20:46:57 +000010611 if (precomp_buf)
10612 {
10613 mch_memmove(buf, precomp_buf, precomp_len);
10614 vim_free(precomp_buf);
10615 }
10616#endif
10617 addfile(gap, buf, flags);
10618 }
10619 }
10620 }
10621 }
10622
10623 closedir(dirp);
10624 }
10625
10626 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010627 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010628
10629 matches = gap->ga_len - start_len;
10630 if (matches > 0)
10631 qsort(((char_u **)gap->ga_data) + start_len, matches,
10632 sizeof(char_u *), pstrcmp);
10633 return matches;
10634}
10635#endif
10636
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010637#if defined(FEAT_SEARCHPATH)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010638/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010639 * Moves "*psep" back to the previous path separator in "path".
10640 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010641 */
10642 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010643find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010644{
10645 /* skip the current separator */
10646 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010647 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010648
10649 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010650 while (*psep > path)
10651 {
10652 if (vim_ispathsep(**psep))
10653 return OK;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010654 MB_PTR_BACK(path, *psep);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010655 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010656
10657 return FAIL;
10658}
10659
10660/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010661 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10662 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010663 */
10664 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010665is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010666{
10667 int j;
10668 int candidate_len;
10669 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010670 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010671 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010672
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010673 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010674 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010675 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010676 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010677
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010678 candidate_len = (int)STRLEN(maybe_unique);
10679 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010680 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010681 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010682
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010683 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010684 if (fnamecmp(maybe_unique, rival) == 0
10685 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010686 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010687 }
10688
Bram Moolenaar162bd912010-07-28 22:29:10 +020010689 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010690}
10691
10692/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010693 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010694 * paths are expanded to their equivalent fullpath. This includes the "."
10695 * (relative to current buffer directory) and empty path (relative to current
10696 * directory) notations.
10697 *
10698 * TODO: handle upward search (;) and path limiter (**N) notations by
10699 * expanding each into their equivalent path(s).
10700 */
10701 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010702expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010703{
10704 char_u *path_option = *curbuf->b_p_path == NUL
10705 ? p_path : curbuf->b_p_path;
10706 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010707 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010708 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010709
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010710 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010711 return;
10712
10713 while (*path_option != NUL)
10714 {
10715 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10716
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010717 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010718 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010719 /* Relative to current buffer:
10720 * "/path/file" + "." -> "/path/"
10721 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010722 if (curbuf->b_ffname == NULL)
10723 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010724 p = gettail(curbuf->b_ffname);
10725 len = (int)(p - curbuf->b_ffname);
10726 if (len + (int)STRLEN(buf) >= MAXPATHL)
10727 continue;
10728 if (buf[1] == NUL)
10729 buf[len] = NUL;
10730 else
10731 STRMOVE(buf + len, buf + 2);
10732 mch_memmove(buf, curbuf->b_ffname, len);
10733 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010734 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010735 else if (buf[0] == NUL)
10736 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010737 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010738 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010739 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010740 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010741 else if (!mch_isFullName(buf))
10742 {
10743 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010744 len = (int)STRLEN(curdir);
10745 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010746 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010747 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010748 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010749 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010750 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010751 }
10752
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010753 if (ga_grow(gap, 1) == FAIL)
10754 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010755
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010756# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010757 /* Avoid the path ending in a backslash, it fails when a comma is
10758 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010759 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010760 if (buf[len - 1] == '\\')
10761 buf[len - 1] = '/';
10762# endif
10763
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010764 p = vim_strsave(buf);
10765 if (p == NULL)
10766 break;
10767 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010768 }
10769
10770 vim_free(buf);
10771}
10772
10773/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010774 * Returns a pointer to the file or directory name in "fname" that matches the
10775 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010776 *
10777 * path: /foo/bar/baz
10778 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010779 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010780 */
10781 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010782get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010783{
10784 int i;
10785 int maxlen = 0;
10786 char_u **path_part = (char_u **)gap->ga_data;
10787 char_u *cutoff = NULL;
10788
10789 for (i = 0; i < gap->ga_len; i++)
10790 {
10791 int j = 0;
10792
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010793 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010794# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010795 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10796#endif
10797 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010798 j++;
10799 if (j > maxlen)
10800 {
10801 maxlen = j;
10802 cutoff = &fname[j];
10803 }
10804 }
10805
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010806 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010807 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010808 while (vim_ispathsep(*cutoff))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010809 MB_PTR_ADV(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010810
10811 return cutoff;
10812}
10813
10814/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010815 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10816 * that they are unique with respect to each other while conserving the part
10817 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010818 */
10819 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010820uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010821{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010822 int i;
10823 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010824 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010825 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010826 char_u *pat;
10827 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010828 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010829 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010830 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010831 char_u **in_curdir = NULL;
10832 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010833
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010834 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010835 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010836
10837 /*
10838 * We need to prepend a '*' at the beginning of file_pattern so that the
10839 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010840 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010841 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010842 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010843 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010844 if (file_pattern == NULL)
10845 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010846 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010847 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010848 STRCAT(file_pattern, pattern);
10849 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10850 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010851 if (pat == NULL)
10852 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010853
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010854 regmatch.rm_ic = TRUE; /* always ignore case */
10855 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10856 vim_free(pat);
10857 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010858 return;
10859
Bram Moolenaar162bd912010-07-28 22:29:10 +020010860 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010861 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010862 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010863 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010864
10865 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010866 if (in_curdir == NULL)
10867 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010868
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010869 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010870 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010871 char_u *path = fnames[i];
10872 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010873 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010874 char_u *pathsep_p;
10875 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010876
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010877 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010878 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010879 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010880 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010881 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010882
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010883 /* Shorten the filename while maintaining its uniqueness */
10884 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010885
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010886 /* Don't assume all files can be reached without path when search
10887 * pattern starts with star star slash, so only remove path_cutoff
10888 * when possible. */
10889 if (pattern[0] == '*' && pattern[1] == '*'
10890 && vim_ispathsep_nocolon(pattern[2])
10891 && path_cutoff != NULL
10892 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10893 && is_unique(path_cutoff, gap, i))
10894 {
10895 sort_again = TRUE;
10896 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10897 }
10898 else
10899 {
10900 /* Here all files can be reached without path, so get shortest
10901 * unique path. We start at the end of the path. */
10902 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010903
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010904 while (find_previous_pathsep(path, &pathsep_p))
10905 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10906 && is_unique(pathsep_p + 1, gap, i)
10907 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10908 {
10909 sort_again = TRUE;
10910 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10911 break;
10912 }
10913 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010914
10915 if (mch_isFullName(path))
10916 {
10917 /*
10918 * Last resort: shorten relative to curdir if possible.
10919 * 'possible' means:
10920 * 1. It is under the current directory.
10921 * 2. The result is actually shorter than the original.
10922 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010923 * Before curdir After
10924 * /foo/bar/file.txt /foo/bar ./file.txt
10925 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10926 * /file.txt / /file.txt
10927 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010928 */
10929 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010930 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010931#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010932 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010933 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010934 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010935 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010936 && !vim_ispathsep(*short_name)
10937#endif
10938 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010939 {
10940 STRCPY(path, ".");
10941 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010942 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010943 }
10944 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010945 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010946 }
10947
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010948 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010949 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010950 {
10951 char_u *rel_path;
10952 char_u *path = in_curdir[i];
10953
10954 if (path == NULL)
10955 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010956
10957 /* If the {filename} is not unique, change it to ./{filename}.
10958 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010959 short_name = shorten_fname(path, curdir);
10960 if (short_name == NULL)
10961 short_name = path;
10962 if (is_unique(short_name, gap, i))
10963 {
10964 STRCPY(fnames[i], short_name);
10965 continue;
10966 }
10967
10968 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10969 if (rel_path == NULL)
10970 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010971 STRCPY(rel_path, ".");
10972 add_pathsep(rel_path);
10973 STRCAT(rel_path, short_name);
10974
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010975 vim_free(fnames[i]);
10976 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010977 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010978 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010979 }
10980
Bram Moolenaar162bd912010-07-28 22:29:10 +020010981theend:
10982 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010983 if (in_curdir != NULL)
10984 {
10985 for (i = 0; i < gap->ga_len; i++)
10986 vim_free(in_curdir[i]);
10987 vim_free(in_curdir);
10988 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010989 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010990 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010991
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010992 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010993 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010994}
10995
10996/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010997 * Calls globpath() with 'path' values for the given pattern and stores the
10998 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010999 * Returns the total number of matches.
11000 */
11001 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011002expand_in_path(
11003 garray_T *gap,
11004 char_u *pattern,
11005 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011006{
Bram Moolenaar162bd912010-07-28 22:29:10 +020011007 char_u *curdir;
11008 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020011009 char_u *paths = NULL;
Bram Moolenaar8a37b032018-02-03 20:43:08 +010011010 int glob_flags = 0;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011011
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020011012 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020011013 return 0;
11014 mch_dirname(curdir, MAXPATHL);
11015
Bram Moolenaar0be992e2010-08-12 21:50:51 +020011016 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020011017 expand_path_option(curdir, &path_ga);
11018 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020011019 if (path_ga.ga_len == 0)
11020 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011021
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020011022 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011023 ga_clear_strings(&path_ga);
11024 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020011025 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020011026
Bram Moolenaar8a37b032018-02-03 20:43:08 +010011027 if (flags & EW_ICASE)
11028 glob_flags |= WILD_ICASE;
11029 if (flags & EW_ADDSLASH)
11030 glob_flags |= WILD_ADD_SLASH;
11031 globpath(paths, pattern, gap, glob_flags);
Bram Moolenaar162bd912010-07-28 22:29:10 +020011032 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011033
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020011034 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011035}
11036#endif
11037
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011038#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
11039/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020011040 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
11041 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011042 */
11043 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011044remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011045{
11046 int i;
11047 int j;
11048 char_u **fnames = (char_u **)gap->ga_data;
11049
Bram Moolenaardc685ab2010-08-13 21:16:49 +020011050 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011051 for (i = gap->ga_len - 1; i > 0; --i)
11052 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
11053 {
11054 vim_free(fnames[i]);
11055 for (j = i + 1; j < gap->ga_len; ++j)
11056 fnames[j - 1] = fnames[j];
11057 --gap->ga_len;
11058 }
11059}
11060#endif
11061
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011062/*
11063 * Return TRUE if "p" contains what looks like an environment variable.
11064 * Allowing for escaping.
11065 */
11066 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011067has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011068{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011069 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011070 {
11071 if (*p == '\\' && p[1] != NUL)
11072 ++p;
11073 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010011074#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011075 "$%"
11076#else
11077 "$"
11078#endif
11079 , *p) != NULL)
11080 return TRUE;
11081 }
11082 return FALSE;
11083}
11084
11085#ifdef SPECIAL_WILDCHAR
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011086/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +010011087 * Return TRUE if "p" contains a special wildcard character, one that Vim
11088 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011089 */
11090 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011091has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011092{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011093 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011094 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010011095 /* Allow for escaping. */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011096 if (*p == '\\' && p[1] != NUL)
11097 ++p;
11098 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
11099 return TRUE;
11100 }
11101 return FALSE;
11102}
11103#endif
11104
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105/*
11106 * Generic wildcard expansion code.
11107 *
11108 * Characters in "pat" that should not be expanded must be preceded with a
11109 * backslash. E.g., "/path\ with\ spaces/my\*star*"
11110 *
11111 * Return FAIL when no single file was found. In this case "num_file" is not
11112 * set, and "file" may contain an error message.
11113 * Return OK when some files found. "num_file" is set to the number of
11114 * matches, "file" to the array of matches. Call FreeWild() later.
11115 */
11116 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011117gen_expand_wildcards(
11118 int num_pat, /* number of input patterns */
11119 char_u **pat, /* array of input patterns */
11120 int *num_file, /* resulting number of files */
11121 char_u ***file, /* array of resulting files */
11122 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123{
11124 int i;
11125 garray_T ga;
11126 char_u *p;
11127 static int recursive = FALSE;
11128 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020011129 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011130#if defined(FEAT_SEARCHPATH)
11131 int did_expand_in_path = FALSE;
11132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133
11134 /*
11135 * expand_env() is called to expand things like "~user". If this fails,
11136 * it calls ExpandOne(), which brings us back here. In this case, always
11137 * call the machine specific expansion function, if possible. Otherwise,
11138 * return FAIL.
11139 */
11140 if (recursive)
11141#ifdef SPECIAL_WILDCHAR
11142 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11143#else
11144 return FAIL;
11145#endif
11146
11147#ifdef SPECIAL_WILDCHAR
11148 /*
11149 * If there are any special wildcard characters which we cannot handle
11150 * here, call machine specific function for all the expansion. This
11151 * avoids starting the shell for each argument separately.
11152 * For `=expr` do use the internal function.
11153 */
11154 for (i = 0; i < num_pat; i++)
11155 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011156 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157# ifdef VIM_BACKTICK
11158 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
11159# endif
11160 )
11161 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11162 }
11163#endif
11164
11165 recursive = TRUE;
11166
11167 /*
11168 * The matching file names are stored in a growarray. Init it empty.
11169 */
11170 ga_init2(&ga, (int)sizeof(char_u *), 30);
11171
11172 for (i = 0; i < num_pat; ++i)
11173 {
11174 add_pat = -1;
11175 p = pat[i];
11176
11177#ifdef VIM_BACKTICK
11178 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020011179 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020011181 if (add_pat == -1)
11182 retval = FAIL;
11183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184 else
11185#endif
11186 {
11187 /*
11188 * First expand environment variables, "~/" and "~user/".
11189 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011190 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011191 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000011192 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193 if (p == NULL)
11194 p = pat[i];
11195#ifdef UNIX
11196 /*
11197 * On Unix, if expand_env() can't expand an environment
11198 * variable, use the shell to do that. Discard previously
11199 * found file names and start all over again.
11200 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011201 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202 {
11203 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000011204 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020011206 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207 recursive = FALSE;
11208 return i;
11209 }
11210#endif
11211 }
11212
11213 /*
11214 * If there are wildcards: Expand file names and add each match to
11215 * the list. If there is no match, and EW_NOTFOUND is given, add
11216 * the pattern.
11217 * If there are no wildcards: Add the file name if it exists or
11218 * when EW_NOTFOUND is given.
11219 */
11220 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011221 {
11222#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011223 if ((flags & EW_PATH)
11224 && !mch_isFullName(p)
11225 && !(p[0] == '.'
11226 && (vim_ispathsep(p[1])
11227 || (p[1] == '.' && vim_ispathsep(p[2]))))
11228 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011229 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011230 /* :find completion where 'path' is used.
11231 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011232 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011233 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011234 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011235 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011236 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011237 else
11238#endif
11239 add_pat = mch_expandpath(&ga, p, flags);
11240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241 }
11242
11243 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
11244 {
11245 char_u *t = backslash_halve_save(p);
11246
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247 /* When EW_NOTFOUND is used, always add files and dirs. Makes
11248 * "vim c:/" work. */
11249 if (flags & EW_NOTFOUND)
11250 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020011251 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252 addfile(&ga, t, flags);
11253 vim_free(t);
11254 }
11255
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011256#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011257 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011258 uniquefy_paths(&ga, p);
11259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260 if (p != pat[i])
11261 vim_free(p);
11262 }
11263
11264 *num_file = ga.ga_len;
11265 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
11266
11267 recursive = FALSE;
11268
Bram Moolenaar336bd622016-01-17 18:23:58 +010011269 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270}
11271
11272# ifdef VIM_BACKTICK
11273
11274/*
11275 * Return TRUE if we can expand this backtick thing here.
11276 */
11277 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011278vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011279{
11280 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
11281}
11282
11283/*
11284 * Expand an item in `backticks` by executing it as a command.
11285 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020011286 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287 */
11288 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011289expand_backtick(
11290 garray_T *gap,
11291 char_u *pat,
11292 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293{
11294 char_u *p;
11295 char_u *cmd;
11296 char_u *buffer;
11297 int cnt = 0;
11298 int i;
11299
11300 /* Create the command: lop off the backticks. */
11301 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
11302 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011303 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304
11305#ifdef FEAT_EVAL
11306 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011307 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308 else
11309#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011310 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011311 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312 vim_free(cmd);
11313 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011314 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315
11316 cmd = buffer;
11317 while (*cmd != NUL)
11318 {
11319 cmd = skipwhite(cmd); /* skip over white space */
11320 p = cmd;
11321 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
11322 ++p;
11323 /* add an entry if it is not empty */
11324 if (p > cmd)
11325 {
11326 i = *p;
11327 *p = NUL;
11328 addfile(gap, cmd, flags);
11329 *p = i;
11330 ++cnt;
11331 }
11332 cmd = p;
11333 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
11334 ++cmd;
11335 }
11336
11337 vim_free(buffer);
11338 return cnt;
11339}
11340# endif /* VIM_BACKTICK */
11341
11342/*
11343 * Add a file to a file list. Accepted flags:
11344 * EW_DIR add directories
11345 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011346 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347 * EW_NOTFOUND add even when it doesn't exist
11348 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011349 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350 */
11351 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011352addfile(
11353 garray_T *gap,
11354 char_u *f, /* filename */
11355 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356{
11357 char_u *p;
11358 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011359 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011361 /* if the file/dir/link doesn't exist, may not add it */
11362 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011363 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364 return;
11365
11366#ifdef FNAME_ILLEGAL
11367 /* if the file/dir contains illegal characters, don't add it */
11368 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11369 return;
11370#endif
11371
11372 isdir = mch_isdir(f);
11373 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11374 return;
11375
Bram Moolenaarb5971142015-03-21 17:32:19 +010011376 /* If the file isn't executable, may not add it. Do accept directories.
11377 * When invoked from expand_shellcmd() do not use $PATH. */
11378 if (!isdir && (flags & EW_EXEC)
11379 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011380 return;
11381
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382 /* Make room for another item in the file list. */
11383 if (ga_grow(gap, 1) == FAIL)
11384 return;
11385
11386 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11387 if (p == NULL)
11388 return;
11389
11390 STRCPY(p, f);
11391#ifdef BACKSLASH_IN_FILENAME
11392 slash_adjust(p);
11393#endif
11394 /*
11395 * Append a slash or backslash after directory names if none is present.
11396 */
11397#ifndef DONT_ADD_PATHSEP_TO_DIR
11398 if (isdir && (flags & EW_ADDSLASH))
11399 add_pathsep(p);
11400#endif
11401 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402}
11403#endif /* !NO_EXPANDPATH */
11404
11405#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11406
11407#ifndef SEEK_SET
11408# define SEEK_SET 0
11409#endif
11410#ifndef SEEK_END
11411# define SEEK_END 2
11412#endif
11413
11414/*
11415 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011416 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11417 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418 * Returns an allocated string, or NULL for error.
11419 */
11420 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011421get_cmd_output(
11422 char_u *cmd,
11423 char_u *infile, /* optional input file name */
11424 int flags, /* can be SHELL_SILENT */
11425 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426{
11427 char_u *tempname;
11428 char_u *command;
11429 char_u *buffer = NULL;
11430 int len;
11431 int i = 0;
11432 FILE *fd;
11433
11434 if (check_restricted() || check_secure())
11435 return NULL;
11436
11437 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011438 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011440 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011441 return NULL;
11442 }
11443
11444 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011445 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446 if (command == NULL)
11447 goto done;
11448
11449 /*
11450 * Call the shell to execute the command (errors are ignored).
11451 * Don't check timestamps here.
11452 */
11453 ++no_check_timestamps;
11454 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11455 --no_check_timestamps;
11456
11457 vim_free(command);
11458
11459 /*
11460 * read the names from the file into memory
11461 */
11462# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011463 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011464 fd = mch_fopen((char *)tempname, "r");
11465# else
11466 fd = mch_fopen((char *)tempname, READBIN);
11467# endif
11468
11469 if (fd == NULL)
11470 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011471 semsg(_(e_notopen), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472 goto done;
11473 }
11474
11475 fseek(fd, 0L, SEEK_END);
11476 len = ftell(fd); /* get size of temp file */
11477 fseek(fd, 0L, SEEK_SET);
11478
11479 buffer = alloc(len + 1);
11480 if (buffer != NULL)
11481 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11482 fclose(fd);
11483 mch_remove(tempname);
11484 if (buffer == NULL)
11485 goto done;
11486#ifdef VMS
11487 len = i; /* VMS doesn't give us what we asked for... */
11488#endif
11489 if (i != len)
11490 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011491 semsg(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +010011492 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011494 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011495 {
11496 /* Change NUL into SOH, otherwise the string is truncated. */
11497 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011498 if (buffer[i] == NUL)
11499 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011500
Bram Moolenaar162bd912010-07-28 22:29:10 +020011501 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011502 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011503 else
11504 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505
11506done:
11507 vim_free(tempname);
11508 return buffer;
11509}
11510#endif
11511
11512/*
11513 * Free the list of files returned by expand_wildcards() or other expansion
11514 * functions.
11515 */
11516 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011517FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011519 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521 while (count--)
11522 vim_free(files[count]);
11523 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524}
11525
11526/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011527 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528 * Don't do this when still processing a command or a mapping.
11529 * Don't do this when inside a ":normal" command.
11530 */
11531 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011532goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011533{
11534 return (p_im && stuff_empty() && typebuf_typed());
11535}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011536
11537/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011538 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011539 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11540 * - Remove any argument. E.g., "csh -f" -> "csh".
11541 * But don't allow a space in the path, so that this works:
11542 * "/usr/bin/csh --rcfile ~/.cshrc"
11543 * But don't do that for Windows, it's common to have a space in the path.
11544 */
11545 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011546get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011547{
11548 char_u *p;
11549
11550#ifdef WIN3264
11551 p = gettail(p_sh);
11552 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11553#else
11554 p = skiptowhite(p_sh);
11555 if (*p == NUL)
11556 {
11557 /* No white space, use the tail. */
11558 p = vim_strsave(gettail(p_sh));
11559 }
11560 else
11561 {
11562 char_u *p1, *p2;
11563
11564 /* Find the last path separator before the space. */
11565 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011566 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011567 if (vim_ispathsep(*p2))
11568 p1 = p2 + 1;
11569 p = vim_strnsave(p1, (int)(p - p1));
11570 }
11571#endif
11572 return p;
11573}