blob: a4b56286aaacbafd487e2766656be0a88736df10 [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));
3471 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 {
3745 MSG_PUTS("\b \b");
3746 --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 Moolenaard812df62008-11-09 12:46:09 +00003789 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003790 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003791 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)
3849 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3850 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003852 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3853 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003855 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 if (msg(msg_buf))
3857 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003858 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003859 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 }
3861 }
3862}
3863
3864/*
3865 * flush map and typeahead buffers and give a warning for an error
3866 */
3867 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003868beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869{
3870 if (emsg_silent == 0)
3871 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003872 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003873 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 }
3875}
3876
3877/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003878 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 */
3880 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003881vim_beep(
3882 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003884#ifdef FEAT_EVAL
3885 called_vim_beep = TRUE;
3886#endif
3887
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 if (emsg_silent == 0)
3889 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003890 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3891 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003892#ifdef ELAPSED_FUNC
3893 static int did_init = FALSE;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01003894 static elapsed_T start_tv;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003895
3896 /* Only beep once per half a second, otherwise a sequence of beeps
3897 * would freeze Vim. */
3898 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
3899 {
3900 did_init = TRUE;
3901 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003903 if (p_vb
3904#ifdef FEAT_GUI
3905 /* While the GUI is starting up the termcap is set for
3906 * the GUI but the output still goes to a terminal. */
3907 && !(gui.in_use && gui.starting)
3908#endif
3909 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003910 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003911 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003912#ifdef FEAT_VTP
3913 /* No restore color information, refresh the screen. */
3914 if (has_vtp_working() != 0
3915# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003916 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003917# endif
3918 )
3919 {
3920 redraw_later(CLEAR);
3921 update_screen(0);
3922 redrawcmd();
3923 }
3924#endif
3925 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003926 else
3927 out_char(BELL);
3928#ifdef ELAPSED_FUNC
3929 }
3930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003932
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003933 /* When 'debug' contains "beep" produce a message. If we are sourcing
3934 * a script or executing a function give the user a hint where the beep
3935 * comes from. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003936 if (vim_strchr(p_debug, 'e') != NULL)
3937 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003938 msg_source(HL_ATTR(HLF_W));
3939 msg_attr((char_u *)_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 }
3942}
3943
3944/*
3945 * To get the "real" home directory:
3946 * - get value of $HOME
3947 * For Unix:
3948 * - go to that directory
3949 * - do mch_dirname() to get the real name of that directory.
3950 * This also works with mounts and links.
3951 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
Bram Moolenaar25a494c2018-11-16 19:39:50 +01003952 * For Windows:
3953 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 */
3955static char_u *homedir = NULL;
3956
3957 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003958init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959{
3960 char_u *var;
3961
Bram Moolenaar05159a02005-02-26 23:04:13 +00003962 /* In case we are called a second time (when 'encoding' changes). */
Bram Moolenaard23a8232018-02-10 18:45:26 +01003963 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003964
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965#ifdef VMS
3966 var = mch_getenv((char_u *)"SYS$LOGIN");
3967#else
3968 var = mch_getenv((char_u *)"HOME");
3969#endif
3970
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971#ifdef WIN3264
3972 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02003973 * Typically, $HOME is not defined on Windows, unless the user has
3974 * specifically defined it for Vim's sake. However, on Windows NT
3975 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3976 * each user. Try constructing $HOME from these.
3977 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02003978 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02003979 {
3980 char_u *homedrive, *homepath;
3981
3982 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3983 homepath = mch_getenv((char_u *)"HOMEPATH");
3984 if (homepath == NULL || *homepath == NUL)
3985 homepath = (char_u *)"\\";
3986 if (homedrive != NULL
3987 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3988 {
3989 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3990 if (NameBuff[0] != NUL)
3991 var = NameBuff;
3992 }
3993 }
3994
3995 if (var == NULL)
3996 var = mch_getenv((char_u *)"USERPROFILE");
3997
3998 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 * Weird but true: $HOME may contain an indirect reference to another
4000 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
4001 * when $HOME is being set.
4002 */
4003 if (var != NULL && *var == '%')
4004 {
4005 char_u *p;
4006 char_u *exp;
4007
4008 p = vim_strchr(var + 1, '%');
4009 if (p != NULL)
4010 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004011 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 exp = mch_getenv(NameBuff);
4013 if (exp != NULL && *exp != NUL
4014 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
4015 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00004016 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 }
4019 }
4020 }
4021
Bram Moolenaar48340b62017-08-29 22:08:53 +02004022 if (var != NULL && *var == NUL) /* empty is same as not set */
4023 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024
Bram Moolenaar48340b62017-08-29 22:08:53 +02004025# ifdef FEAT_MBYTE
Bram Moolenaar05159a02005-02-26 23:04:13 +00004026 if (enc_utf8 && var != NULL)
4027 {
4028 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004029 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004030
4031 /* Convert from active codepage to UTF-8. Other conversions are
4032 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004033 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004034 if (pp != NULL)
4035 {
4036 homedir = pp;
4037 return;
4038 }
4039 }
4040# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 /*
4043 * Default home dir is C:/
4044 * Best assumption we can make in such a situation.
4045 */
4046 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004047 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004049
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 if (var != NULL)
4051 {
4052#ifdef UNIX
4053 /*
4054 * Change to the directory and get the actual path. This resolves
4055 * links. Don't do it when we can't return.
4056 */
4057 if (mch_dirname(NameBuff, MAXPATHL) == OK
4058 && mch_chdir((char *)NameBuff) == 0)
4059 {
4060 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
4061 var = IObuff;
4062 if (mch_chdir((char *)NameBuff) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004063 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 }
4065#endif
4066 homedir = vim_strsave(var);
4067 }
4068}
4069
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004070#if defined(EXITFREE) || defined(PROTO)
4071 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004072free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004073{
4074 vim_free(homedir);
4075}
Bram Moolenaar24305862012-08-15 14:05:05 +02004076
4077# ifdef FEAT_CMDL_COMPL
4078 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004079free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02004080{
4081 ga_clear_strings(&ga_users);
4082}
4083# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004084#endif
4085
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004087 * Call expand_env() and store the result in an allocated string.
4088 * This is not very memory efficient, this expects the result to be freed
4089 * again soon.
4090 */
4091 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004092expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004093{
4094 return expand_env_save_opt(src, FALSE);
4095}
4096
4097/*
4098 * Idem, but when "one" is TRUE handle the string as one file name, only
4099 * expand "~" at the start.
4100 */
4101 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004102expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004103{
4104 char_u *p;
4105
4106 p = alloc(MAXPATHL);
4107 if (p != NULL)
4108 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
4109 return p;
4110}
4111
4112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 * Expand environment variable with path name.
4114 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004115 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 * If anything fails no expansion is done and dst equals src.
4117 */
4118 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004119expand_env(
4120 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
4121 char_u *dst, /* where to put the result */
4122 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004124 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125}
4126
4127 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004128expand_env_esc(
4129 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
4130 char_u *dst, /* where to put the result */
4131 int dstlen, /* maximum length of the result */
4132 int esc, /* escape spaces in expanded variables */
4133 int one, /* "srcp" is one file name */
4134 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004136 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 char_u *tail;
4138 int c;
4139 char_u *var;
4140 int copy_char;
4141 int mustfree; /* var was allocated, need to free it later */
4142 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004143 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004145 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004146 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004147
4148 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 --dstlen; /* leave one char space for "\," */
4150 while (*src && dstlen > 0)
4151 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004152#ifdef FEAT_EVAL
4153 /* Skip over `=expr`. */
4154 if (src[0] == '`' && src[1] == '=')
4155 {
4156 size_t len;
4157
4158 var = src;
4159 src += 2;
4160 (void)skip_expr(&src);
4161 if (*src == '`')
4162 ++src;
4163 len = src - var;
4164 if (len > (size_t)dstlen)
4165 len = dstlen;
4166 vim_strncpy(dst, var, len);
4167 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02004168 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004169 continue;
4170 }
4171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004173 if ((*src == '$'
4174#ifdef VMS
4175 && at_start
4176#endif
4177 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004178#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 || *src == '%'
4180#endif
4181 || (*src == '~' && at_start))
4182 {
4183 mustfree = FALSE;
4184
4185 /*
4186 * The variable name is copied into dst temporarily, because it may
4187 * be a string in read-only memory and a NUL needs to be appended.
4188 */
4189 if (*src != '~') /* environment var */
4190 {
4191 tail = src + 1;
4192 var = dst;
4193 c = dstlen - 1;
4194
4195#ifdef UNIX
4196 /* Unix has ${var-name} type environment vars */
4197 if (*tail == '{' && !vim_isIDc('{'))
4198 {
4199 tail++; /* ignore '{' */
4200 while (c-- > 0 && *tail && *tail != '}')
4201 *var++ = *tail++;
4202 }
4203 else
4204#endif
4205 {
4206 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004207#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 || (*src == '%' && *tail != '%')
4209#endif
4210 ))
4211 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 }
4214 }
4215
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004216#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217# ifdef UNIX
4218 if (src[1] == '{' && *tail != '}')
4219# else
4220 if (*src == '%' && *tail != '%')
4221# endif
4222 var = NULL;
4223 else
4224 {
4225# ifdef UNIX
4226 if (src[1] == '{')
4227# else
4228 if (*src == '%')
4229#endif
4230 ++tail;
4231#endif
4232 *var = NUL;
4233 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004234#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 }
4236#endif
4237 }
4238 /* home directory */
4239 else if ( src[1] == NUL
4240 || vim_ispathsep(src[1])
4241 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4242 {
4243 var = homedir;
4244 tail = src + 1;
4245 }
4246 else /* user directory */
4247 {
4248#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4249 /*
4250 * Copy ~user to dst[], so we can put a NUL after it.
4251 */
4252 tail = src;
4253 var = dst;
4254 c = dstlen - 1;
4255 while ( c-- > 0
4256 && *tail
4257 && vim_isfilec(*tail)
4258 && !vim_ispathsep(*tail))
4259 *var++ = *tail++;
4260 *var = NUL;
4261# ifdef UNIX
4262 /*
4263 * If the system supports getpwnam(), use it.
4264 * Otherwise, or if getpwnam() fails, the shell is used to
4265 * expand ~user. This is slower and may fail if the shell
4266 * does not support ~user (old versions of /bin/sh).
4267 */
4268# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4269 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004270 /* Note: memory allocated by getpwnam() is never freed.
4271 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01004272 struct passwd *pw = (*dst == NUL)
4273 ? NULL : getpwnam((char *)dst + 1);
4274
4275 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 }
4277 if (var == NULL)
4278# endif
4279 {
4280 expand_T xpc;
4281
4282 ExpandInit(&xpc);
4283 xpc.xp_context = EXPAND_FILES;
4284 var = ExpandOne(&xpc, dst, NULL,
4285 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 mustfree = TRUE;
4287 }
4288
4289# else /* !UNIX, thus VMS */
4290 /*
4291 * USER_HOME is a comma-separated list of
4292 * directories to search for the user account in.
4293 */
4294 {
4295 char_u test[MAXPATHL], paths[MAXPATHL];
4296 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004297 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298
4299 STRCPY(paths, USER_HOME);
4300 next_path = paths;
4301 while (*next_path)
4302 {
4303 for (path = next_path; *next_path && *next_path != ',';
4304 next_path++);
4305 if (*next_path)
4306 *next_path++ = NUL;
4307 STRCPY(test, path);
4308 STRCAT(test, "/");
4309 STRCAT(test, dst + 1);
4310 if (mch_stat(test, &st) == 0)
4311 {
4312 var = alloc(STRLEN(test) + 1);
4313 STRCPY(var, test);
4314 mustfree = TRUE;
4315 break;
4316 }
4317 }
4318 }
4319# endif /* UNIX */
4320#else
4321 /* cannot expand user's home directory, so don't try */
4322 var = NULL;
4323 tail = (char_u *)""; /* for gcc */
4324#endif /* UNIX || VMS */
4325 }
4326
4327#ifdef BACKSLASH_IN_FILENAME
4328 /* If 'shellslash' is set change backslashes to forward slashes.
4329 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4330 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4331 {
4332 char_u *p = vim_strsave(var);
4333
4334 if (p != NULL)
4335 {
4336 if (mustfree)
4337 vim_free(var);
4338 var = p;
4339 mustfree = TRUE;
4340 forward_slash(var);
4341 }
4342 }
4343#endif
4344
4345 /* If "var" contains white space, escape it with a backslash.
4346 * Required for ":e ~/tt" when $HOME includes a space. */
4347 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4348 {
4349 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4350
4351 if (p != NULL)
4352 {
4353 if (mustfree)
4354 vim_free(var);
4355 var = p;
4356 mustfree = TRUE;
4357 }
4358 }
4359
4360 if (var != NULL && *var != NUL
4361 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4362 {
4363 STRCPY(dst, var);
4364 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004365 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 /* if var[] ends in a path separator and tail[] starts
4367 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004368 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4370 && dst[-1] != ':'
4371#endif
4372 && vim_ispathsep(*tail))
4373 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004374 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 src = tail;
4376 copy_char = FALSE;
4377 }
4378 if (mustfree)
4379 vim_free(var);
4380 }
4381
4382 if (copy_char) /* copy at least one char */
4383 {
4384 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004385 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004386 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4387 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 */
4389 at_start = FALSE;
4390 if (src[0] == '\\' && src[1] != NUL)
4391 {
4392 *dst++ = *src++;
4393 --dstlen;
4394 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004395 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02004397 if (dstlen > 0)
4398 {
4399 *dst++ = *src++;
4400 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004401
Bram Moolenaar1c864092017-08-06 18:15:45 +02004402 if (startstr != NULL && src - startstr_len >= srcp
4403 && STRNCMP(src - startstr_len, startstr,
4404 startstr_len) == 0)
4405 at_start = TRUE;
4406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02004408
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 }
4410 *dst = NUL;
4411}
4412
4413/*
4414 * Vim's version of getenv().
4415 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004416 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004417 * "mustfree" is set to TRUE when returned is allocated, it must be
4418 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 */
4420 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004421vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422{
4423 char_u *p;
4424 char_u *pend;
4425 int vimruntime;
4426
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004427#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 /* use "C:/" when $HOME is not set */
4429 if (STRCMP(name, "HOME") == 0)
4430 return homedir;
4431#endif
4432
4433 p = mch_getenv(name);
4434 if (p != NULL && *p == NUL) /* empty is the same as not set */
4435 p = NULL;
4436
4437 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004438 {
4439#if defined(FEAT_MBYTE) && defined(WIN3264)
4440 if (enc_utf8)
4441 {
4442 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004443 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004444
4445 /* Convert from active codepage to UTF-8. Other conversions are
4446 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004447 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004448 if (pp != NULL)
4449 {
4450 p = pp;
4451 *mustfree = TRUE;
4452 }
4453 }
4454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457
4458 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4459 if (!vimruntime && STRCMP(name, "VIM") != 0)
4460 return NULL;
4461
4462 /*
4463 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4464 * Don't do this when default_vimruntime_dir is non-empty.
4465 */
4466 if (vimruntime
4467#ifdef HAVE_PATHDEF
4468 && *default_vimruntime_dir == NUL
4469#endif
4470 )
4471 {
4472 p = mch_getenv((char_u *)"VIM");
4473 if (p != NULL && *p == NUL) /* empty is the same as not set */
4474 p = NULL;
4475 if (p != NULL)
4476 {
4477 p = vim_version_dir(p);
4478 if (p != NULL)
4479 *mustfree = TRUE;
4480 else
4481 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004482
4483#if defined(FEAT_MBYTE) && defined(WIN3264)
4484 if (enc_utf8)
4485 {
4486 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004487 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004488
4489 /* Convert from active codepage to UTF-8. Other conversions
4490 * are not done, because they would fail for non-ASCII
4491 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004492 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004493 if (pp != NULL)
4494 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004495 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004496 vim_free(p);
4497 p = pp;
4498 *mustfree = TRUE;
4499 }
4500 }
4501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 }
4503 }
4504
4505 /*
4506 * When expanding $VIM or $VIMRUNTIME fails, try using:
4507 * - the directory name from 'helpfile' (unless it contains '$')
4508 * - the executable name from argv[0]
4509 */
4510 if (p == NULL)
4511 {
4512 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4513 p = p_hf;
4514#ifdef USE_EXE_NAME
4515 /*
4516 * Use the name of the executable, obtained from argv[0].
4517 */
4518 else
4519 p = exe_name;
4520#endif
4521 if (p != NULL)
4522 {
4523 /* remove the file name */
4524 pend = gettail(p);
4525
4526 /* remove "doc/" from 'helpfile', if present */
4527 if (p == p_hf)
4528 pend = remove_tail(p, pend, (char_u *)"doc");
4529
4530#ifdef USE_EXE_NAME
4531# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004532 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 if (p == exe_name)
4534 {
4535 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004536 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004538 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4539 if (pend1 != pend)
4540 {
4541 pnew = alloc((unsigned)(pend1 - p) + 15);
4542 if (pnew != NULL)
4543 {
4544 STRNCPY(pnew, p, (pend1 - p));
4545 STRCPY(pnew + (pend1 - p), "Resources/vim");
4546 p = pnew;
4547 pend = p + STRLEN(p);
4548 }
4549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 }
4551# endif
4552 /* remove "src/" from exe_name, if present */
4553 if (p == exe_name)
4554 pend = remove_tail(p, pend, (char_u *)"src");
4555#endif
4556
4557 /* for $VIM, remove "runtime/" or "vim54/", if present */
4558 if (!vimruntime)
4559 {
4560 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4561 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4562 }
4563
4564 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004565 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004568#ifdef MACOS_X
4569 if (p == exe_name || p == p_hf)
4570#endif
4571 /* check that the result is a directory name */
4572 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573
4574 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01004575 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 else
4577 {
4578#ifdef USE_EXE_NAME
4579 /* may add "/vim54" or "/runtime" if it exists */
4580 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4581 {
4582 vim_free(p);
4583 p = pend;
4584 }
4585#endif
4586 *mustfree = TRUE;
4587 }
4588 }
4589 }
4590
4591#ifdef HAVE_PATHDEF
4592 /* When there is a pathdef.c file we can use default_vim_dir and
4593 * default_vimruntime_dir */
4594 if (p == NULL)
4595 {
4596 /* Only use default_vimruntime_dir when it is not empty */
4597 if (vimruntime && *default_vimruntime_dir != NUL)
4598 {
4599 p = default_vimruntime_dir;
4600 *mustfree = FALSE;
4601 }
4602 else if (*default_vim_dir != NUL)
4603 {
4604 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4605 *mustfree = TRUE;
4606 else
4607 {
4608 p = default_vim_dir;
4609 *mustfree = FALSE;
4610 }
4611 }
4612 }
4613#endif
4614
4615 /*
4616 * Set the environment variable, so that the new value can be found fast
4617 * next time, and others can also use it (e.g. Perl).
4618 */
4619 if (p != NULL)
4620 {
4621 if (vimruntime)
4622 {
4623 vim_setenv((char_u *)"VIMRUNTIME", p);
4624 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 }
4626 else
4627 {
4628 vim_setenv((char_u *)"VIM", p);
4629 didset_vim = TRUE;
4630 }
4631 }
4632 return p;
4633}
4634
4635/*
4636 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4637 * Return NULL if not, return its name in allocated memory otherwise.
4638 */
4639 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004640vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641{
4642 char_u *p;
4643
4644 if (vimdir == NULL || *vimdir == NUL)
4645 return NULL;
4646 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4647 if (p != NULL && mch_isdir(p))
4648 return p;
4649 vim_free(p);
4650 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4651 if (p != NULL && mch_isdir(p))
4652 return p;
4653 vim_free(p);
4654 return NULL;
4655}
4656
4657/*
4658 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4659 * the length of "name/". Otherwise return "pend".
4660 */
4661 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004662remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663{
4664 int len = (int)STRLEN(name) + 1;
4665 char_u *newend = pend - len;
4666
4667 if (newend >= p
4668 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004669 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 return newend;
4671 return pend;
4672}
4673
Bram Moolenaar137374f2018-05-13 15:59:50 +02004674 void
4675vim_unsetenv(char_u *var)
4676{
4677#ifdef HAVE_UNSETENV
4678 unsetenv((char *)var);
4679#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02004680 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02004681#endif
4682}
4683
4684
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 * Our portable version of setenv.
4687 */
4688 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004689vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690{
4691#ifdef HAVE_SETENV
4692 mch_setenv((char *)name, (char *)val, 1);
4693#else
4694 char_u *envbuf;
4695
4696 /*
4697 * Putenv does not copy the string, it has to remain
4698 * valid. The allocated memory will never be freed.
4699 */
4700 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4701 if (envbuf != NULL)
4702 {
4703 sprintf((char *)envbuf, "%s=%s", name, val);
4704 putenv((char *)envbuf);
4705 }
4706#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004707#ifdef FEAT_GETTEXT
4708 /*
4709 * When setting $VIMRUNTIME adjust the directory to find message
4710 * translations to $VIMRUNTIME/lang.
4711 */
4712 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4713 {
4714 char_u *buf = concat_str(val, (char_u *)"/lang");
4715
4716 if (buf != NULL)
4717 {
4718 bindtextdomain(VIMPACKAGE, (char *)buf);
4719 vim_free(buf);
4720 }
4721 }
4722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723}
4724
4725#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4726/*
4727 * Function given to ExpandGeneric() to obtain an environment variable name.
4728 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004730get_env_name(
4731 expand_T *xp UNUSED,
4732 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733{
Bram Moolenaard0573012017-10-28 21:11:06 +02004734# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02004736 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 */
4738 return NULL;
4739# else
4740# ifndef __WIN32__
4741 /* Borland C++ 5.2 has this in a header file. */
4742 extern char **environ;
4743# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004744# define ENVNAMELEN 100
4745 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 char_u *str;
4747 int n;
4748
4749 str = (char_u *)environ[idx];
4750 if (str == NULL)
4751 return NULL;
4752
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004753 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 {
4755 if (str[n] == '=' || str[n] == NUL)
4756 break;
4757 name[n] = str[n];
4758 }
4759 name[n] = NUL;
4760 return name;
4761# endif
4762}
Bram Moolenaar24305862012-08-15 14:05:05 +02004763
4764/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004765 * Add a user name to the list of users in ga_users.
4766 * Do nothing if user name is NULL or empty.
4767 */
4768 static void
4769add_user(char_u *user, int need_copy)
4770{
4771 char_u *user_copy = (user != NULL && need_copy)
4772 ? vim_strsave(user) : user;
4773
4774 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
4775 {
4776 if (need_copy)
4777 vim_free(user);
4778 return;
4779 }
4780 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
4781}
4782
4783/*
Bram Moolenaar24305862012-08-15 14:05:05 +02004784 * Find all user names for user completion.
4785 * Done only once and then cached.
4786 */
4787 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004788init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004789{
Bram Moolenaar24305862012-08-15 14:05:05 +02004790 static int lazy_init_done = FALSE;
4791
4792 if (lazy_init_done)
4793 return;
4794
4795 lazy_init_done = TRUE;
4796 ga_init2(&ga_users, sizeof(char_u *), 20);
4797
4798# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4799 {
Bram Moolenaar24305862012-08-15 14:05:05 +02004800 struct passwd* pw;
4801
4802 setpwent();
4803 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004804 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02004805 endpwent();
4806 }
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004807# elif defined(WIN3264)
4808 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004809 DWORD nusers = 0, ntotal = 0, i;
4810 PUSER_INFO_0 uinfo;
4811
4812 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
4813 &nusers, &ntotal, NULL) == NERR_Success)
4814 {
4815 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004816 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004817
4818 NetApiBufferFree(uinfo);
4819 }
4820 }
Bram Moolenaar24305862012-08-15 14:05:05 +02004821# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004822# if defined(HAVE_GETPWNAM)
4823 {
4824 char_u *user_env = mch_getenv((char_u *)"USER");
4825
4826 // The $USER environment variable may be a valid remote user name (NIS,
4827 // LDAP) not already listed by getpwent(), as getpwent() only lists
4828 // local user names. If $USER is not already listed, check whether it
4829 // is a valid remote user name using getpwnam() and if it is, add it to
4830 // the list of user names.
4831
4832 if (user_env != NULL && *user_env != NUL)
4833 {
4834 int i;
4835
4836 for (i = 0; i < ga_users.ga_len; i++)
4837 {
4838 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
4839
4840 if (STRCMP(local_user, user_env) == 0)
4841 break;
4842 }
4843
4844 if (i == ga_users.ga_len)
4845 {
4846 struct passwd *pw = getpwnam((char *)user_env);
4847
4848 if (pw != NULL)
4849 add_user((char_u *)pw->pw_name, TRUE);
4850 }
4851 }
4852 }
4853# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02004854}
4855
4856/*
4857 * Function given to ExpandGeneric() to obtain an user names.
4858 */
4859 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004860get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004861{
4862 init_users();
4863 if (idx < ga_users.ga_len)
4864 return ((char_u **)ga_users.ga_data)[idx];
4865 return NULL;
4866}
4867
4868/*
4869 * Check whether name matches a user name. Return:
4870 * 0 if name does not match any user name.
4871 * 1 if name partially matches the beginning of a user name.
4872 * 2 is name fully matches a user name.
4873 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02004874 int
4875match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004876{
4877 int i;
4878 int n = (int)STRLEN(name);
4879 int result = 0;
4880
4881 init_users();
4882 for (i = 0; i < ga_users.ga_len; i++)
4883 {
4884 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4885 return 2; /* full match */
4886 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4887 result = 1; /* partial match */
4888 }
4889 return result;
4890}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891#endif
4892
4893/*
4894 * Replace home directory by "~" in each space or comma separated file name in
4895 * 'src'.
4896 * If anything fails (except when out of space) dst equals src.
4897 */
4898 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004899home_replace(
4900 buf_T *buf, /* when not NULL, check for help files */
4901 char_u *src, /* input file name */
4902 char_u *dst, /* where to put the result */
4903 int dstlen, /* maximum length of the result */
4904 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 spaces and commas in the file name. */
4906{
4907 size_t dirlen = 0, envlen = 0;
4908 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004909 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 char_u *p;
4911
4912 if (src == NULL)
4913 {
4914 *dst = NUL;
4915 return;
4916 }
4917
4918 /*
4919 * If the file is a help file, remove the path completely.
4920 */
4921 if (buf != NULL && buf->b_help)
4922 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02004923 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 return;
4925 }
4926
4927 /*
4928 * We check both the value of the $HOME environment variable and the
4929 * "real" home directory.
4930 */
4931 if (homedir != NULL)
4932 dirlen = STRLEN(homedir);
4933
4934#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004935 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004937 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4938#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004939#ifdef WIN3264
4940 if (homedir_env == NULL)
4941 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
4942#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004943 /* Empty is the same as not set. */
4944 if (homedir_env != NULL && *homedir_env == NUL)
4945 homedir_env = NULL;
4946
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004947#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaaref0a1d52018-12-30 11:38:57 +01004948 if (homedir_env != NULL && *homedir_env == '~')
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004949 {
4950 int usedlen = 0;
4951 int flen;
4952 char_u *fbuf = NULL;
4953
4954 flen = (int)STRLEN(homedir_env);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004955 (void)modify_fname((char_u *)":p", FALSE, &usedlen,
Bram Moolenaard12f8112012-06-20 17:56:09 +02004956 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004957 flen = (int)STRLEN(homedir_env);
4958 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4959 /* Remove the trailing / that is added to a directory. */
4960 homedir_env[flen - 1] = NUL;
4961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962#endif
4963
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 if (homedir_env != NULL)
4965 envlen = STRLEN(homedir_env);
4966
4967 if (!one)
4968 src = skipwhite(src);
4969 while (*src && dstlen > 0)
4970 {
4971 /*
4972 * Here we are at the beginning of a file name.
4973 * First, check to see if the beginning of the file name matches
4974 * $HOME or the "real" home directory. Check that there is a '/'
4975 * after the match (so that if e.g. the file is "/home/pieter/bla",
4976 * and the home directory is "/home/piet", the file does not end up
4977 * as "~er/bla" (which would seem to indicate the file "bla" in user
4978 * er's home directory)).
4979 */
4980 p = homedir;
4981 len = dirlen;
4982 for (;;)
4983 {
4984 if ( len
4985 && fnamencmp(src, p, len) == 0
4986 && (vim_ispathsep(src[len])
4987 || (!one && (src[len] == ',' || src[len] == ' '))
4988 || src[len] == NUL))
4989 {
4990 src += len;
4991 if (--dstlen > 0)
4992 *dst++ = '~';
4993
4994 /*
4995 * If it's just the home directory, add "/".
4996 */
4997 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4998 *dst++ = '/';
4999 break;
5000 }
5001 if (p == homedir_env)
5002 break;
5003 p = homedir_env;
5004 len = envlen;
5005 }
5006
5007 /* if (!one) skip to separator: space or comma */
5008 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
5009 *dst++ = *src++;
5010 /* skip separator */
5011 while ((*src == ' ' || *src == ',') && --dstlen > 0)
5012 *dst++ = *src++;
5013 }
5014 /* if (dstlen == 0) out of space, what to do??? */
5015
5016 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02005017
5018 if (homedir_env != homedir_env_orig)
5019 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020}
5021
5022/*
5023 * Like home_replace, store the replaced string in allocated memory.
5024 * When something fails, NULL is returned.
5025 */
5026 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005027home_replace_save(
5028 buf_T *buf, /* when not NULL, check for help files */
5029 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030{
5031 char_u *dst;
5032 unsigned len;
5033
5034 len = 3; /* space for "~/" and trailing NUL */
5035 if (src != NULL) /* just in case */
5036 len += (unsigned)STRLEN(src);
5037 dst = alloc(len);
5038 if (dst != NULL)
5039 home_replace(buf, src, dst, len, TRUE);
5040 return dst;
5041}
5042
5043/*
5044 * Compare two file names and return:
5045 * FPC_SAME if they both exist and are the same file.
5046 * FPC_SAMEX if they both don't exist and have the same file name.
5047 * FPC_DIFF if they both exist and are different files.
5048 * FPC_NOTX if they both don't exist.
5049 * FPC_DIFFX if one of them doesn't exist.
5050 * For the first name environment variables are expanded
5051 */
5052 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005053fullpathcmp(
5054 char_u *s1,
5055 char_u *s2,
5056 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057{
5058#ifdef UNIX
5059 char_u exp1[MAXPATHL];
5060 char_u full1[MAXPATHL];
5061 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02005062 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 int r1, r2;
5064
5065 expand_env(s1, exp1, MAXPATHL);
5066 r1 = mch_stat((char *)exp1, &st1);
5067 r2 = mch_stat((char *)s2, &st2);
5068 if (r1 != 0 && r2 != 0)
5069 {
5070 /* if mch_stat() doesn't work, may compare the names */
5071 if (checkname)
5072 {
5073 if (fnamecmp(exp1, s2) == 0)
5074 return FPC_SAMEX;
5075 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
5076 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
5077 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
5078 return FPC_SAMEX;
5079 }
5080 return FPC_NOTX;
5081 }
5082 if (r1 != 0 || r2 != 0)
5083 return FPC_DIFFX;
5084 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
5085 return FPC_SAME;
5086 return FPC_DIFF;
5087#else
5088 char_u *exp1; /* expanded s1 */
5089 char_u *full1; /* full path of s1 */
5090 char_u *full2; /* full path of s2 */
5091 int retval = FPC_DIFF;
5092 int r1, r2;
5093
5094 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
5095 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
5096 {
5097 full1 = exp1 + MAXPATHL;
5098 full2 = full1 + MAXPATHL;
5099
5100 expand_env(s1, exp1, MAXPATHL);
5101 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
5102 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
5103
5104 /* If vim_FullName() fails, the file probably doesn't exist. */
5105 if (r1 != OK && r2 != OK)
5106 {
5107 if (checkname && fnamecmp(exp1, s2) == 0)
5108 retval = FPC_SAMEX;
5109 else
5110 retval = FPC_NOTX;
5111 }
5112 else if (r1 != OK || r2 != OK)
5113 retval = FPC_DIFFX;
5114 else if (fnamecmp(full1, full2))
5115 retval = FPC_DIFF;
5116 else
5117 retval = FPC_SAME;
5118 vim_free(exp1);
5119 }
5120 return retval;
5121#endif
5122}
5123
5124/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005125 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02005126 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005127 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 */
5129 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005130gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131{
5132 char_u *p1, *p2;
5133
5134 if (fname == NULL)
5135 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01005136 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01005138 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005140 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 }
5142 return p1;
5143}
5144
Bram Moolenaar31710262010-08-13 13:36:15 +02005145#if defined(FEAT_SEARCHPATH)
Bram Moolenaar31710262010-08-13 13:36:15 +02005146/*
5147 * Return the end of the directory name, on the first path
5148 * separator:
5149 * "/path/file", "/path/dir/", "/path//dir", "/file"
5150 * ^ ^ ^ ^
5151 */
5152 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005153gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02005154{
5155 char_u *dir_end = fname;
5156 char_u *next_dir_end = fname;
5157 int look_for_sep = TRUE;
5158 char_u *p;
5159
5160 for (p = fname; *p != NUL; )
5161 {
5162 if (vim_ispathsep(*p))
5163 {
5164 if (look_for_sep)
5165 {
5166 next_dir_end = p;
5167 look_for_sep = FALSE;
5168 }
5169 }
5170 else
5171 {
5172 if (!look_for_sep)
5173 dir_end = next_dir_end;
5174 look_for_sep = TRUE;
5175 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005176 MB_PTR_ADV(p);
Bram Moolenaar31710262010-08-13 13:36:15 +02005177 }
5178 return dir_end;
5179}
5180#endif
5181
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005183 * Get pointer to tail of "fname", including path separators. Putting a NUL
5184 * here leaves the directory name. Takes care of "c:/" and "//".
5185 * Always returns a valid pointer.
5186 */
5187 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005188gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005189{
5190 char_u *p;
5191 char_u *t;
5192
5193 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
5194 t = gettail(fname);
5195 while (t > p && after_pathsep(fname, t))
5196 --t;
5197#ifdef VMS
5198 /* path separator is part of the path */
5199 ++t;
5200#endif
5201 return t;
5202}
5203
5204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 * get the next path component (just after the next path separator).
5206 */
5207 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005208getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209{
5210 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005211 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 if (*fname)
5213 ++fname;
5214 return fname;
5215}
5216
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217/*
5218 * Get a pointer to one character past the head of a path name.
5219 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
5220 * If there is no head, path is returned.
5221 */
5222 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005223get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224{
5225 char_u *retval;
5226
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005227#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 /* may skip "c:" */
5229 if (isalpha(path[0]) && path[1] == ':')
5230 retval = path + 2;
5231 else
5232 retval = path;
5233#else
5234# if defined(AMIGA)
5235 /* may skip "label:" */
5236 retval = vim_strchr(path, ':');
5237 if (retval == NULL)
5238 retval = path;
5239# else /* Unix */
5240 retval = path;
5241# endif
5242#endif
5243
5244 while (vim_ispathsep(*retval))
5245 ++retval;
5246
5247 return retval;
5248}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249
5250/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01005251 * Return TRUE if 'c' is a path separator.
5252 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 */
5254 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005255vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256{
Bram Moolenaare60acc12011-05-10 16:41:25 +02005257#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02005259#else
5260# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005262# else
5263# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
5265 return (c == ':' || c == '[' || c == ']' || c == '/'
5266 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02005267# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005269# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02005271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272}
5273
Bram Moolenaar69c35002013-11-04 02:54:12 +01005274/*
5275 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
5276 */
5277 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005278vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01005279{
5280 return vim_ispathsep(c)
5281#ifdef BACKSLASH_IN_FILENAME
5282 && c != ':'
5283#endif
5284 ;
5285}
5286
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5288/*
5289 * return TRUE if 'c' is a path list separator.
5290 */
5291 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005292vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293{
5294#ifdef UNIX
5295 return (c == ':');
5296#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005297 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298#endif
5299}
5300#endif
5301
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005302/*
5303 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5304 * It's done in-place.
5305 */
5306 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005307shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005308{
5309 char_u *tail, *s, *d;
5310 int skip = FALSE;
5311
5312 tail = gettail(str);
5313 d = str;
5314 for (s = str; ; ++s)
5315 {
5316 if (s >= tail) /* copy the whole tail */
5317 {
5318 *d++ = *s;
5319 if (*s == NUL)
5320 break;
5321 }
5322 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5323 {
5324 *d++ = *s;
5325 skip = FALSE;
5326 }
5327 else if (!skip)
5328 {
5329 *d++ = *s; /* copy next char */
5330 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5331 skip = TRUE;
5332# ifdef FEAT_MBYTE
5333 if (has_mbyte)
5334 {
5335 int l = mb_ptr2len(s);
5336
5337 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005338 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005339 }
5340# endif
5341 }
5342 }
5343}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005344
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005345/*
5346 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5347 * Also returns TRUE if there is no directory name.
5348 * "fname" must be writable!.
5349 */
5350 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005351dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005352{
5353 char_u *p;
5354 int c;
5355 int retval;
5356
5357 p = gettail_sep(fname);
5358 if (p == fname)
5359 return TRUE;
5360 c = *p;
5361 *p = NUL;
5362 retval = mch_isdir(fname);
5363 *p = c;
5364 return retval;
5365}
5366
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005368 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5369 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 */
5371 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005372vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005374#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005376#else
5377 if (p_fic)
5378 return MB_STRICMP(x, y);
5379 return STRCMP(x, y);
5380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381}
5382
5383 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005384vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005386#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005387 char_u *px = x;
5388 char_u *py = y;
5389 int cx = NUL;
5390 int cy = NUL;
5391
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005392 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005394 cx = PTR2CHAR(px);
5395 cy = PTR2CHAR(py);
5396 if (cx == NUL || cy == NUL
5397 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5398 && !(cx == '/' && cy == '\\')
5399 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005401 len -= MB_PTR2LEN(px);
5402 px += MB_PTR2LEN(px);
5403 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 }
5405 if (len == 0)
5406 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005407 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005408#else
5409 if (p_fic)
5410 return MB_STRNICMP(x, y, len);
5411 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005413}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414
5415/*
5416 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005417 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 */
5419 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005420concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421{
5422 char_u *dest;
5423
5424 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5425 if (dest != NULL)
5426 {
5427 STRCPY(dest, fname1);
5428 if (sep)
5429 add_pathsep(dest);
5430 STRCAT(dest, fname2);
5431 }
5432 return dest;
5433}
5434
Bram Moolenaard6754642005-01-17 22:18:45 +00005435/*
5436 * Concatenate two strings and return the result in allocated memory.
5437 * Returns NULL when out of memory.
5438 */
5439 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005440concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005441{
5442 char_u *dest;
5443 size_t l = STRLEN(str1);
5444
5445 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5446 if (dest != NULL)
5447 {
5448 STRCPY(dest, str1);
5449 STRCPY(dest + l, str2);
5450 }
5451 return dest;
5452}
Bram Moolenaard6754642005-01-17 22:18:45 +00005453
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454/*
5455 * Add a path separator to a file name, unless it already ends in a path
5456 * separator.
5457 */
5458 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005459add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005461 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 STRCAT(p, PATHSEPSTR);
5463}
5464
5465/*
5466 * FullName_save - Make an allocated copy of a full file name.
5467 * Returns NULL when out of memory.
5468 */
5469 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005470FullName_save(
5471 char_u *fname,
5472 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005473 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474{
5475 char_u *buf;
5476 char_u *new_fname = NULL;
5477
5478 if (fname == NULL)
5479 return NULL;
5480
5481 buf = alloc((unsigned)MAXPATHL);
5482 if (buf != NULL)
5483 {
5484 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5485 new_fname = vim_strsave(buf);
5486 else
5487 new_fname = vim_strsave(fname);
5488 vim_free(buf);
5489 }
5490 return new_fname;
5491}
5492
5493#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5494
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005495static char_u *skip_string(char_u *p);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005496static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497
5498/*
5499 * Find the start of a comment, not knowing if we are in a comment right now.
5500 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005501 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005503 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005504ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005505{
5506 return find_start_comment(curbuf->b_ind_maxcomment);
5507}
5508
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005510find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511{
5512 pos_T *pos;
5513 char_u *line;
5514 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005515 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005517 for (;;)
5518 {
5519 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5520 if (pos == NULL)
5521 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005523 /*
5524 * Check if the comment start we found is inside a string.
5525 * If it is then restrict the search to below this line and try again.
5526 */
5527 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005528 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005529 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005530 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005531 break;
5532 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5533 if (cur_maxcomment <= 0)
5534 {
5535 pos = NULL;
5536 break;
5537 }
5538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 return pos;
5540}
5541
5542/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005543 * Find the start of a comment or raw string, not knowing if we are in a
5544 * comment or raw string right now.
5545 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaardde81312017-08-26 17:49:01 +02005546 * If is_raw is given and returns start of raw_string, sets it to true.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005547 * Return NULL when not inside a comment or raw string.
5548 * "CORS" -> Comment Or Raw String
5549 */
5550 static pos_T *
Bram Moolenaardde81312017-08-26 17:49:01 +02005551ind_find_start_CORS(linenr_T *is_raw) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005552{
Bram Moolenaar089af182015-10-07 11:41:49 +02005553 static pos_T comment_pos_copy;
5554 pos_T *comment_pos;
5555 pos_T *rs_pos;
5556
5557 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5558 if (comment_pos != NULL)
5559 {
5560 /* Need to make a copy of the static pos in findmatchlimit(),
5561 * calling find_start_rawstring() may change it. */
5562 comment_pos_copy = *comment_pos;
5563 comment_pos = &comment_pos_copy;
5564 }
5565 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005566
5567 /* If comment_pos is before rs_pos the raw string is inside the comment.
5568 * If rs_pos is before comment_pos the comment is inside the raw string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005569 if (comment_pos == NULL || (rs_pos != NULL
5570 && LT_POS(*rs_pos, *comment_pos)))
Bram Moolenaardde81312017-08-26 17:49:01 +02005571 {
5572 if (is_raw != NULL && rs_pos != NULL)
5573 *is_raw = rs_pos->lnum;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005574 return rs_pos;
Bram Moolenaardde81312017-08-26 17:49:01 +02005575 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005576 return comment_pos;
5577}
5578
5579/*
5580 * Find the start of a raw string, not knowing if we are in one right now.
5581 * Search starts at w_cursor.lnum and goes backwards.
5582 * Return NULL when not inside a raw string.
5583 */
5584 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005585find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005586{
5587 pos_T *pos;
5588 char_u *line;
5589 char_u *p;
5590 int cur_maxcomment = ind_maxcomment;
5591
5592 for (;;)
5593 {
5594 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5595 if (pos == NULL)
5596 break;
5597
5598 /*
5599 * Check if the raw string start we found is inside a string.
5600 * If it is then restrict the search to below this line and try again.
5601 */
5602 line = ml_get(pos->lnum);
5603 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5604 p = skip_string(p);
5605 if ((colnr_T)(p - line) <= pos->col)
5606 break;
5607 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5608 if (cur_maxcomment <= 0)
5609 {
5610 pos = NULL;
5611 break;
5612 }
5613 }
5614 return pos;
5615}
5616
5617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 * Skip to the end of a "string" and a 'c' character.
5619 * If there is no string or character, return argument unmodified.
5620 */
5621 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005622skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623{
5624 int i;
5625
5626 /*
5627 * We loop, because strings may be concatenated: "date""time".
5628 */
5629 for ( ; ; ++p)
5630 {
5631 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5632 {
5633 if (!p[1]) /* ' at end of line */
5634 break;
5635 i = 2;
5636 if (p[1] == '\\') /* '\n' or '\000' */
5637 {
5638 ++i;
5639 while (vim_isdigit(p[i - 1])) /* '\000' */
5640 ++i;
5641 }
5642 if (p[i] == '\'') /* check for trailing ' */
5643 {
5644 p += i;
5645 continue;
5646 }
5647 }
5648 else if (p[0] == '"') /* start of string */
5649 {
5650 for (++p; p[0]; ++p)
5651 {
5652 if (p[0] == '\\' && p[1] != NUL)
5653 ++p;
5654 else if (p[0] == '"') /* end of string */
5655 break;
5656 }
5657 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005658 continue; /* continue for another string */
5659 }
5660 else if (p[0] == 'R' && p[1] == '"')
5661 {
5662 /* Raw string: R"[delim](...)[delim]" */
5663 char_u *delim = p + 2;
5664 char_u *paren = vim_strchr(delim, '(');
5665
5666 if (paren != NULL)
5667 {
5668 size_t delim_len = paren - delim;
5669
5670 for (p += 3; *p; ++p)
5671 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5672 && p[delim_len + 1] == '"')
5673 {
5674 p += delim_len + 1;
5675 break;
5676 }
5677 if (p[0] == '"')
5678 continue; /* continue for another string */
5679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 }
5681 break; /* no string found */
5682 }
5683 if (!*p)
5684 --p; /* backup from NUL */
5685 return p;
5686}
5687#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5688
5689#if defined(FEAT_CINDENT) || defined(PROTO)
5690
5691/*
5692 * Do C or expression indenting on the current line.
5693 */
5694 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005695do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696{
5697# ifdef FEAT_EVAL
5698 if (*curbuf->b_p_inde != NUL)
5699 fixthisline(get_expr_indent);
5700 else
5701# endif
5702 fixthisline(get_c_indent);
5703}
5704
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005705/* Find result cache for cpp_baseclass */
5706typedef struct {
5707 int found;
5708 lpos_T lpos;
5709} cpp_baseclass_cache_T;
5710
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711/*
5712 * Functions for C-indenting.
5713 * Most of this originally comes from Eric Fischer.
5714 */
5715/*
5716 * Below "XXX" means that this function may unlock the current line.
5717 */
5718
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005719static int cin_isdefault(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005720static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005721static int cin_iscomment(char_u *);
5722static int cin_islinecomment(char_u *);
5723static int cin_isterminated(char_u *, int, int);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005724static int cin_iselse(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005725static int cin_ends_in(char_u *, char_u *, char_u *);
5726static int cin_starts_with(char_u *s, char *word);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005727static pos_T *find_match_paren(int);
5728static pos_T *find_match_char(int c, int ind_maxparen);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005729static int find_last_paren(char_u *l, int start, int end);
5730static int find_match(int lookfor, linenr_T ourscope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731
5732/*
5733 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005734 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 */
5736 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005737cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738{
5739 while (*s)
5740 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005741 char_u *prev_s = s;
5742
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005744
5745 /* Perl/shell # comment comment continues until eol. Require a space
5746 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005747 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005748 {
5749 s += STRLEN(s);
5750 break;
5751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 if (*s != '/')
5753 break;
5754 ++s;
5755 if (*s == '/') /* slash-slash comment continues till eol */
5756 {
5757 s += STRLEN(s);
5758 break;
5759 }
5760 if (*s != '*')
5761 break;
5762 for (++s; *s; ++s) /* skip slash-star comment */
5763 if (s[0] == '*' && s[1] == '/')
5764 {
5765 s += 2;
5766 break;
5767 }
5768 }
5769 return s;
5770}
5771
5772/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005773 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 * not considered code.
5775 */
5776 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005777cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778{
5779 return *cin_skipcomment(s) == NUL;
5780}
5781
5782/*
5783 * Check previous lines for a "//" line comment, skipping over blank lines.
5784 */
5785 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005786find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787{
5788 static pos_T pos;
5789 char_u *line;
5790 char_u *p;
5791
5792 pos = curwin->w_cursor;
5793 while (--pos.lnum > 0)
5794 {
5795 line = ml_get(pos.lnum);
5796 p = skipwhite(line);
5797 if (cin_islinecomment(p))
5798 {
5799 pos.col = (int)(p - line);
5800 return &pos;
5801 }
5802 if (*p != NUL)
5803 break;
5804 }
5805 return NULL;
5806}
5807
5808/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005809 * Return TRUE if "text" starts with "key:".
5810 */
5811 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005812cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005813{
5814 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005815 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005816
5817 if (*s == '\'' || *s == '"')
5818 {
5819 /* can be 'key': or "key": */
5820 quote = *s;
5821 ++s;
5822 }
5823 if (!vim_isIDc(*s)) /* need at least one ID character */
5824 return FALSE;
5825
5826 while (vim_isIDc(*s))
5827 ++s;
5828 if (*s == quote)
5829 ++s;
5830
5831 s = cin_skipcomment(s);
5832
5833 /* "::" is not a label, it's C++ */
5834 return (*s == ':' && s[1] != ':');
5835}
5836
5837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005839 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 */
5841 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005842cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843{
5844 if (!vim_isIDc(**s)) /* need at least one ID character */
5845 return FALSE;
5846
5847 while (vim_isIDc(**s))
5848 (*s)++;
5849
5850 *s = cin_skipcomment(*s);
5851
5852 /* "::" is not a label, it's C++ */
5853 return (**s == ':' && *++*s != ':');
5854}
5855
5856/*
5857 * Recognize a label: "label:".
5858 * Note: curwin->w_cursor must be where we are looking for the label.
5859 */
5860 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005861cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862{
5863 char_u *s;
5864
5865 s = cin_skipcomment(ml_get_curline());
5866
5867 /*
5868 * Exclude "default" from labels, since it should be indented
5869 * like a switch label. Same for C++ scope declarations.
5870 */
5871 if (cin_isdefault(s))
5872 return FALSE;
5873 if (cin_isscopedecl(s))
5874 return FALSE;
5875
5876 if (cin_islabel_skip(&s))
5877 {
5878 /*
5879 * Only accept a label if the previous line is terminated or is a case
5880 * label.
5881 */
5882 pos_T cursor_save;
5883 pos_T *trypos;
5884 char_u *line;
5885
5886 cursor_save = curwin->w_cursor;
5887 while (curwin->w_cursor.lnum > 1)
5888 {
5889 --curwin->w_cursor.lnum;
5890
5891 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005892 * If we're in a comment or raw string now, skip to the start of
5893 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 */
5895 curwin->w_cursor.col = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02005896 if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 curwin->w_cursor = *trypos;
5898
5899 line = ml_get_curline();
5900 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5901 continue;
5902 if (*(line = cin_skipcomment(line)) == NUL)
5903 continue;
5904
5905 curwin->w_cursor = cursor_save;
5906 if (cin_isterminated(line, TRUE, FALSE)
5907 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005908 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 || (cin_islabel_skip(&line) && cin_nocode(line)))
5910 return TRUE;
5911 return FALSE;
5912 }
5913 curwin->w_cursor = cursor_save;
5914 return TRUE; /* label at start of file??? */
5915 }
5916 return FALSE;
5917}
5918
5919/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005920 * Recognize structure initialization and enumerations:
5921 * "[typedef] [static|public|protected|private] enum"
5922 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 */
5924 static int
5925cin_isinit(void)
5926{
5927 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005928 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929
5930 s = cin_skipcomment(ml_get_curline());
5931
Bram Moolenaar75342212013-03-07 13:13:52 +01005932 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 s = cin_skipcomment(s + 7);
5934
Bram Moolenaar75342212013-03-07 13:13:52 +01005935 for (;;)
5936 {
5937 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005938
Bram Moolenaar75342212013-03-07 13:13:52 +01005939 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5940 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005941 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005942 if (cin_starts_with(s, skip[i]))
5943 {
5944 s = cin_skipcomment(s + l);
5945 l = 0;
5946 break;
5947 }
5948 }
5949 if (l != 0)
5950 break;
5951 }
5952
5953 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 return TRUE;
5955
5956 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5957 return TRUE;
5958
5959 return FALSE;
5960}
5961
5962/*
5963 * Recognize a switch label: "case .*:" or "default:".
5964 */
5965 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005966cin_iscase(
5967 char_u *s,
5968 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969{
5970 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005971 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 {
5973 for (s += 4; *s; ++s)
5974 {
5975 s = cin_skipcomment(s);
5976 if (*s == ':')
5977 {
5978 if (s[1] == ':') /* skip over "::" for C++ */
5979 ++s;
5980 else
5981 return TRUE;
5982 }
5983 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005984 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5986 return FALSE; /* stop at comment */
5987 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005988 {
5989 /* JS etc. */
5990 if (strict)
5991 return FALSE; /* stop at string */
5992 else
5993 return TRUE;
5994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 }
5996 return FALSE;
5997 }
5998
5999 if (cin_isdefault(s))
6000 return TRUE;
6001 return FALSE;
6002}
6003
6004/*
6005 * Recognize a "default" switch label.
6006 */
6007 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006008cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009{
6010 return (STRNCMP(s, "default", 7) == 0
6011 && *(s = cin_skipcomment(s + 7)) == ':'
6012 && s[1] != ':');
6013}
6014
6015/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02006016 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 */
6018 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006019cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020{
6021 int i;
6022
6023 s = cin_skipcomment(s);
6024 if (STRNCMP(s, "public", 6) == 0)
6025 i = 6;
6026 else if (STRNCMP(s, "protected", 9) == 0)
6027 i = 9;
6028 else if (STRNCMP(s, "private", 7) == 0)
6029 i = 7;
6030 else
6031 return FALSE;
6032 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
6033}
6034
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006035/* Maximum number of lines to search back for a "namespace" line. */
6036#define FIND_NAMESPACE_LIM 20
6037
6038/*
6039 * Recognize a "namespace" scope declaration.
6040 */
6041 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006042cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006043{
6044 char_u *p;
6045 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006046 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006047
6048 s = cin_skipcomment(s);
6049 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
6050 {
6051 p = cin_skipcomment(skipwhite(s + 9));
6052 while (*p != NUL)
6053 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006054 if (VIM_ISWHITE(*p))
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006055 {
6056 has_name = TRUE; /* found end of a name */
6057 p = cin_skipcomment(skipwhite(p));
6058 }
6059 else if (*p == '{')
6060 {
6061 break;
6062 }
6063 else if (vim_iswordc(*p))
6064 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006065 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006066 if (has_name)
6067 return FALSE; /* word character after skipping past name */
6068 ++p;
6069 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006070 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
6071 {
6072 if (!has_name_start || has_name)
6073 return FALSE;
6074 /* C++ 17 nested namespace */
6075 p += 3;
6076 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006077 else
6078 {
6079 return FALSE;
6080 }
6081 }
6082 return TRUE;
6083 }
6084 return FALSE;
6085}
6086
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087/*
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006088 * Recognize a `extern "C"` or `extern "C++"` linkage specifications.
6089 */
6090 static int
6091cin_is_cpp_extern_c(char_u *s)
6092{
6093 char_u *p;
6094 int has_string_literal = FALSE;
6095
6096 s = cin_skipcomment(s);
6097 if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
6098 {
6099 p = cin_skipcomment(skipwhite(s + 6));
6100 while (*p != NUL)
6101 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006102 if (VIM_ISWHITE(*p))
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006103 {
6104 p = cin_skipcomment(skipwhite(p));
6105 }
6106 else if (*p == '{')
6107 {
6108 break;
6109 }
6110 else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
6111 {
6112 if (has_string_literal)
6113 return FALSE;
6114 has_string_literal = TRUE;
6115 p += 3;
6116 }
6117 else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
6118 && p[4] == '"')
6119 {
6120 if (has_string_literal)
6121 return FALSE;
6122 has_string_literal = TRUE;
6123 p += 5;
6124 }
6125 else
6126 {
6127 return FALSE;
6128 }
6129 }
6130 return has_string_literal ? TRUE : FALSE;
6131 }
6132 return FALSE;
6133}
6134
6135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 * Return a pointer to the first non-empty non-comment character after a ':'.
6137 * Return NULL if not found.
6138 * case 234: a = b;
6139 * ^
6140 */
6141 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006142after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143{
6144 for ( ; *l; ++l)
6145 {
6146 if (*l == ':')
6147 {
6148 if (l[1] == ':') /* skip over "::" for C++ */
6149 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006150 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 break;
6152 }
6153 else if (*l == '\'' && l[1] && l[2] == '\'')
6154 l += 2; /* skip over 'x' */
6155 }
6156 if (*l == NUL)
6157 return NULL;
6158 l = cin_skipcomment(l + 1);
6159 if (*l == NUL)
6160 return NULL;
6161 return l;
6162}
6163
6164/*
6165 * Get indent of line "lnum", skipping a label.
6166 * Return 0 if there is nothing after the label.
6167 */
6168 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006169get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170{
6171 char_u *l;
6172 pos_T fp;
6173 colnr_T col;
6174 char_u *p;
6175
6176 l = ml_get(lnum);
6177 p = after_label(l);
6178 if (p == NULL)
6179 return 0;
6180
6181 fp.col = (colnr_T)(p - l);
6182 fp.lnum = lnum;
6183 getvcol(curwin, &fp, &col, NULL, NULL);
6184 return (int)col;
6185}
6186
6187/*
6188 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006189 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 * label: if (asdf && asdfasdf)
6191 * ^
6192 */
6193 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006194skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195{
6196 char_u *l;
6197 int amount;
6198 pos_T cursor_save;
6199
6200 cursor_save = curwin->w_cursor;
6201 curwin->w_cursor.lnum = lnum;
6202 l = ml_get_curline();
6203 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006204 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 {
6206 amount = get_indent_nolabel(lnum);
6207 l = after_label(ml_get_curline());
6208 if (l == NULL) /* just in case */
6209 l = ml_get_curline();
6210 }
6211 else
6212 {
6213 amount = get_indent();
6214 l = ml_get_curline();
6215 }
6216 *pp = l;
6217
6218 curwin->w_cursor = cursor_save;
6219 return amount;
6220}
6221
6222/*
6223 * Return the indent of the first variable name after a type in a declaration.
6224 * int a, indent of "a"
6225 * static struct foo b, indent of "b"
6226 * enum bla c, indent of "c"
6227 * Returns zero when it doesn't look like a declaration.
6228 */
6229 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006230cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231{
6232 char_u *line, *p, *s;
6233 int len;
6234 pos_T fp;
6235 colnr_T col;
6236
6237 line = ml_get_curline();
6238 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006239 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 if (len == 6 && STRNCMP(p, "static", 6) == 0)
6241 {
6242 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006243 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 }
6245 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
6246 p = skipwhite(p + 6);
6247 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
6248 p = skipwhite(p + 4);
6249 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
6250 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
6251 {
6252 s = skipwhite(p + len);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006253 if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3]))
6254 || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4]))
6255 || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5]))
6256 || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 p = s;
6258 }
6259 for (len = 0; vim_isIDc(p[len]); ++len)
6260 ;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006261 if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 return 0;
6263
6264 p = skipwhite(p + len);
6265 fp.lnum = curwin->w_cursor.lnum;
6266 fp.col = (colnr_T)(p - line);
6267 getvcol(curwin, &fp, &col, NULL, NULL);
6268 return (int)col;
6269}
6270
6271/*
6272 * Return the indent of the first non-blank after an equal sign.
6273 * char *foo = "here";
6274 * Return zero if no (useful) equal sign found.
6275 * Return -1 if the line above "lnum" ends in a backslash.
6276 * foo = "asdf\
6277 * asdf\
6278 * here";
6279 */
6280 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006281cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282{
6283 char_u *line;
6284 char_u *s;
6285 colnr_T col;
6286 pos_T fp;
6287
6288 if (lnum > 1)
6289 {
6290 line = ml_get(lnum - 1);
6291 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6292 return -1;
6293 }
6294
6295 line = s = ml_get(lnum);
6296 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
6297 {
6298 if (cin_iscomment(s)) /* ignore comments */
6299 s = cin_skipcomment(s);
6300 else
6301 ++s;
6302 }
6303 if (*s != '=')
6304 return 0;
6305
6306 s = skipwhite(s + 1);
6307 if (cin_nocode(s))
6308 return 0;
6309
6310 if (*s == '"') /* nice alignment for continued strings */
6311 ++s;
6312
6313 fp.lnum = lnum;
6314 fp.col = (colnr_T)(s - line);
6315 getvcol(curwin, &fp, &col, NULL, NULL);
6316 return (int)col;
6317}
6318
6319/*
6320 * Recognize a preprocessor statement: Any line that starts with '#'.
6321 */
6322 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006323cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006325 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 return TRUE;
6327 return FALSE;
6328}
6329
6330/*
6331 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6332 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6333 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006334 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 */
6336 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006337cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338{
6339 char_u *line = *pp;
6340 linenr_T lnum = *lnump;
6341 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006342 int candidate_amount = *amount;
6343
6344 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6345 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006347 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 {
6349 if (cin_ispreproc(line))
6350 {
6351 retval = TRUE;
6352 *lnump = lnum;
6353 break;
6354 }
6355 if (lnum == 1)
6356 break;
6357 line = ml_get(--lnum);
6358 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6359 break;
6360 }
6361
6362 if (lnum != *lnump)
6363 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006364 if (retval)
6365 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 return retval;
6367}
6368
6369/*
6370 * Recognize the start of a C or C++ comment.
6371 */
6372 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006373cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374{
6375 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6376}
6377
6378/*
6379 * Recognize the start of a "//" comment.
6380 */
6381 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006382cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383{
6384 return (p[0] == '/' && p[1] == '/');
6385}
6386
6387/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006388 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6389 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006391 * If a line begins with an "else", only consider it terminated if no unmatched
6392 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 * Return the character terminating the line (ending char's have precedence if
6394 * both apply in order to determine initializations).
6395 */
6396 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006397cin_isterminated(
6398 char_u *s,
6399 int incl_open, /* include '{' at the end as terminator */
6400 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006402 char_u found_start = 0;
6403 unsigned n_open = 0;
6404 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405
6406 s = cin_skipcomment(s);
6407
6408 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6409 found_start = *s;
6410
Bram Moolenaar496f9512011-05-19 16:35:09 +02006411 if (!found_start)
6412 is_else = cin_iselse(s);
6413
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 while (*s)
6415 {
6416 /* skip over comments, "" strings and 'c'haracters */
6417 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006418 if (*s == '}' && n_open > 0)
6419 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006420 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006421 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 && cin_nocode(s + 1))
6423 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006424 else if (*s == '{')
6425 {
6426 if (incl_open && cin_nocode(s + 1))
6427 return *s;
6428 else
6429 ++n_open;
6430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431
6432 if (*s)
6433 s++;
6434 }
6435 return found_start;
6436}
6437
6438/*
6439 * Recognize the basic picture of a function declaration -- it needs to
6440 * have an open paren somewhere and a close paren at the end of the line and
6441 * no semicolons anywhere.
6442 * When a line ends in a comma we continue looking in the next line.
6443 * "sp" points to a string with the line. When looking at other lines it must
6444 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006445 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006446 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 */
6448 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006449cin_isfuncdecl(
6450 char_u **sp,
6451 linenr_T first_lnum,
6452 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453{
6454 char_u *s;
6455 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006456 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006458 pos_T *trypos;
6459 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460
6461 if (sp == NULL)
6462 s = ml_get(lnum);
6463 else
6464 s = *sp;
6465
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006466 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006467 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006468 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006469 {
6470 lnum = trypos->lnum;
6471 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006472 {
6473 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006474 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006475 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006476
6477 s = ml_get(lnum);
6478 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006479 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006480
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006481 /* Ignore line starting with #. */
6482 if (cin_ispreproc(s))
6483 return FALSE;
6484
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6486 {
6487 if (cin_iscomment(s)) /* ignore comments */
6488 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006489 else if (*s == ':')
6490 {
6491 if (*(s + 1) == ':')
6492 s += 2;
6493 else
6494 /* To avoid a mistake in the following situation:
6495 * A::A(int a, int b)
6496 * : a(0) // <--not a function decl
6497 * , b(0)
6498 * {...
6499 */
6500 return FALSE;
6501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 else
6503 ++s;
6504 }
6505 if (*s != '(')
6506 return FALSE; /* ';', ' or " before any () or no '(' */
6507
6508 while (*s && *s != ';' && *s != '\'' && *s != '"')
6509 {
6510 if (*s == ')' && cin_nocode(s + 1))
6511 {
6512 /* ')' at the end: may have found a match
6513 * Check for he previous line not to end in a backslash:
6514 * #if defined(x) && \
6515 * defined(y)
6516 */
6517 lnum = first_lnum - 1;
6518 s = ml_get(lnum);
6519 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6520 retval = TRUE;
6521 goto done;
6522 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006523 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006525 int comma = (*s == ',');
6526
6527 /* ',' at the end: continue looking in the next line.
6528 * At the end: check for ',' in the next line, for this style:
6529 * func(arg1
6530 * , arg2) */
6531 for (;;)
6532 {
6533 if (lnum >= curbuf->b_ml.ml_line_count)
6534 break;
6535 s = ml_get(++lnum);
6536 if (!cin_ispreproc(s))
6537 break;
6538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 if (lnum >= curbuf->b_ml.ml_line_count)
6540 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006541 /* Require a comma at end of the line or a comma or ')' at the
6542 * start of next line. */
6543 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006544 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006545 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006546 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 }
6548 else if (cin_iscomment(s)) /* ignore comments */
6549 s = cin_skipcomment(s);
6550 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006551 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006553 just_started = FALSE;
6554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 }
6556
6557done:
6558 if (lnum != first_lnum && sp != NULL)
6559 *sp = ml_get(first_lnum);
6560
6561 return retval;
6562}
6563
6564 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006565cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006567 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568}
6569
6570 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006571cin_iselse(
6572 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573{
6574 if (*p == '}') /* accept "} else" */
6575 p = cin_skipcomment(p + 1);
6576 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6577}
6578
6579 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006580cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581{
6582 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6583}
6584
6585/*
6586 * Check if this is a "while" that should have a matching "do".
6587 * We only accept a "while (condition) ;", with only white space between the
6588 * ')' and ';'. The condition may be spread over several lines.
6589 */
6590 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006591cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592{
6593 pos_T cursor_save;
6594 pos_T *trypos;
6595 int retval = FALSE;
6596
6597 p = cin_skipcomment(p);
6598 if (*p == '}') /* accept "} while (cond);" */
6599 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006600 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 {
6602 cursor_save = curwin->w_cursor;
6603 curwin->w_cursor.lnum = lnum;
6604 curwin->w_cursor.col = 0;
6605 p = ml_get_curline();
6606 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6607 {
6608 ++p;
6609 ++curwin->w_cursor.col;
6610 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006611 if ((trypos = findmatchlimit(NULL, 0, 0,
6612 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6614 retval = TRUE;
6615 curwin->w_cursor = cursor_save;
6616 }
6617 return retval;
6618}
6619
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006620/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006621 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006622 * Return 0 if there is none.
6623 * Otherwise return !0 and update "*poffset" to point to the place where the
6624 * string was found.
6625 */
6626 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006627cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006628{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006629 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006630
6631 if (offset-- < 2)
6632 return 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006633 while (offset > 2 && VIM_ISWHITE(line[offset]))
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006634 --offset;
6635
6636 offset -= 1;
6637 if (!STRNCMP(line + offset, "if", 2))
6638 goto probablyFound;
6639
6640 if (offset >= 1)
6641 {
6642 offset -= 1;
6643 if (!STRNCMP(line + offset, "for", 3))
6644 goto probablyFound;
6645
6646 if (offset >= 2)
6647 {
6648 offset -= 2;
6649 if (!STRNCMP(line + offset, "while", 5))
6650 goto probablyFound;
6651 }
6652 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006653 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006654
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006655probablyFound:
6656 if (!offset || !vim_isIDc(line[offset - 1]))
6657 {
6658 *poffset = offset;
6659 return 1;
6660 }
6661 return 0;
6662}
6663
6664/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006665 * Return TRUE if we are at the end of a do-while.
6666 * do
6667 * nothing;
6668 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006669 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006670 * Adjust the cursor to the line with "while".
6671 */
6672 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006673cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006674{
6675 char_u *line;
6676 char_u *p;
6677 char_u *s;
6678 pos_T *trypos;
6679 int i;
6680
6681 if (terminated != ';') /* there must be a ';' at the end */
6682 return FALSE;
6683
6684 p = line = ml_get_curline();
6685 while (*p != NUL)
6686 {
6687 p = cin_skipcomment(p);
6688 if (*p == ')')
6689 {
6690 s = skipwhite(p + 1);
6691 if (*s == ';' && cin_nocode(s + 1))
6692 {
6693 /* Found ");" at end of the line, now check there is "while"
6694 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006695 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006696 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006697 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006698 if (trypos != NULL)
6699 {
6700 s = cin_skipcomment(ml_get(trypos->lnum));
6701 if (*s == '}') /* accept "} while (cond);" */
6702 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006703 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006704 {
6705 curwin->w_cursor.lnum = trypos->lnum;
6706 return TRUE;
6707 }
6708 }
6709
6710 /* Searching may have made "line" invalid, get it again. */
6711 line = ml_get_curline();
6712 p = line + i;
6713 }
6714 }
6715 if (*p != NUL)
6716 ++p;
6717 }
6718 return FALSE;
6719}
6720
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006722cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723{
6724 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6725}
6726
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006727/*
6728 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 * constructor-initialization. eg:
6730 *
6731 * class MyClass :
6732 * baseClass <-- here
6733 * class MyClass : public baseClass,
6734 * anotherBaseClass <-- here (should probably lineup ??)
6735 * MyClass::MyClass(...) :
6736 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006737 *
6738 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 */
6740 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006741cin_is_cpp_baseclass(
6742 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006744 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 char_u *s;
6746 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006747 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006748 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006750 if (pos->lnum <= lnum)
6751 return cached->found; /* Use the cached result */
6752
6753 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006755 s = skipwhite(line);
6756 if (*s == '#') /* skip #define FOO x ? (x) : x */
6757 return FALSE;
6758 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 if (*s == NUL)
6760 return FALSE;
6761
6762 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6763
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006764 /* Search for a line starting with '#', empty, ending in ';' or containing
6765 * '{' or '}' and start below it. This handles the following situations:
6766 * a = cond ?
6767 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006768 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006769 * func::foo()
6770 * : something
6771 * {}
6772 * Foo::Foo (int one, int two)
6773 * : something(4),
6774 * somethingelse(3)
6775 * {}
6776 */
6777 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006779 line = ml_get(lnum - 1);
6780 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006781 if (*s == '#' || *s == NUL)
6782 break;
6783 while (*s != NUL)
6784 {
6785 s = cin_skipcomment(s);
6786 if (*s == '{' || *s == '}'
6787 || (*s == ';' && cin_nocode(s + 1)))
6788 break;
6789 if (*s != NUL)
6790 ++s;
6791 }
6792 if (*s != NUL)
6793 break;
6794 --lnum;
6795 }
6796
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006797 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006798 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006799 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006800 for (;;)
6801 {
6802 if (*s == NUL)
6803 {
6804 if (lnum == curwin->w_cursor.lnum)
6805 break;
6806 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006807 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006808 s = line;
6809 }
6810 if (s == line)
6811 {
6812 /* don't recognize "case (foo):" as a baseclass */
6813 if (cin_iscase(s, FALSE))
6814 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006815 s = cin_skipcomment(line);
6816 if (*s == NUL)
6817 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006818 }
6819
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006820 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006821 s = skip_string(s) + 1;
6822 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 {
6824 if (s[1] == ':')
6825 {
6826 /* skip double colon. It can't be a constructor
6827 * initialization any more */
6828 lookfor_ctor_init = FALSE;
6829 s = cin_skipcomment(s + 2);
6830 }
6831 else if (lookfor_ctor_init || class_or_struct)
6832 {
6833 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006834 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 cpp_base_class = TRUE;
6836 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006837 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 s = cin_skipcomment(s + 1);
6839 }
6840 else
6841 s = cin_skipcomment(s + 1);
6842 }
6843 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6844 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6845 {
6846 class_or_struct = TRUE;
6847 lookfor_ctor_init = FALSE;
6848
6849 if (*s == 'c')
6850 s = cin_skipcomment(s + 5);
6851 else
6852 s = cin_skipcomment(s + 6);
6853 }
6854 else
6855 {
6856 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6857 {
6858 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6859 }
6860 else if (s[0] == ')')
6861 {
6862 /* Constructor-initialization is assumed if we come across
6863 * something like "):" */
6864 class_or_struct = FALSE;
6865 lookfor_ctor_init = TRUE;
6866 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006867 else if (s[0] == '?')
6868 {
6869 /* Avoid seeing '() :' after '?' as constructor init. */
6870 return FALSE;
6871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 else if (!vim_isIDc(s[0]))
6873 {
6874 /* if it is not an identifier, we are wrong */
6875 class_or_struct = FALSE;
6876 lookfor_ctor_init = FALSE;
6877 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006878 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 {
6880 /* it can't be a constructor-initialization any more */
6881 lookfor_ctor_init = FALSE;
6882
6883 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006884 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006885 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 }
6887
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006888 /* When the line ends in a comma don't align with it. */
6889 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006890 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006891
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 s = cin_skipcomment(s + 1);
6893 }
6894 }
6895
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006896 cached->found = cpp_base_class;
6897 if (cpp_base_class)
6898 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 return cpp_base_class;
6900}
6901
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006902 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006903get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006904{
6905 int amount;
6906 colnr_T vcol;
6907 pos_T *trypos;
6908
6909 if (col == 0)
6910 {
6911 amount = get_indent();
6912 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006913 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006914 amount = get_indent_lnum(trypos->lnum); /* XXX */
6915 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006916 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006917 }
6918 else
6919 {
6920 curwin->w_cursor.col = col;
6921 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6922 amount = (int)vcol;
6923 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006924 if (amount < curbuf->b_ind_cpp_baseclass)
6925 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006926 return amount;
6927}
6928
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929/*
6930 * Return TRUE if string "s" ends with the string "find", possibly followed by
6931 * white space and comments. Skip strings and comments.
6932 * Ignore "ignore" after "find" if it's not NULL.
6933 */
6934 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006935cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936{
6937 char_u *p = s;
6938 char_u *r;
6939 int len = (int)STRLEN(find);
6940
6941 while (*p != NUL)
6942 {
6943 p = cin_skipcomment(p);
6944 if (STRNCMP(p, find, len) == 0)
6945 {
6946 r = skipwhite(p + len);
6947 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6948 r = skipwhite(r + STRLEN(ignore));
6949 if (cin_nocode(r))
6950 return TRUE;
6951 }
6952 if (*p != NUL)
6953 ++p;
6954 }
6955 return FALSE;
6956}
6957
6958/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006959 * Return TRUE when "s" starts with "word" and then a non-ID character.
6960 */
6961 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006962cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006963{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006964 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006965
6966 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6967}
6968
6969/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 * Skip strings, chars and comments until at or past "trypos".
6971 * Return the column found.
6972 */
6973 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006974cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975{
6976 char_u *line;
6977 char_u *p;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006978 char_u *new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979
6980 p = line = ml_get(trypos->lnum);
6981 while (*p && (colnr_T)(p - line) < trypos->col)
6982 {
6983 if (cin_iscomment(p))
6984 p = cin_skipcomment(p);
6985 else
6986 {
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006987 new_p = skip_string(p);
6988 if (new_p == p)
6989 ++p;
6990 else
6991 p = new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 }
6993 }
6994 return (int)(p - line);
6995}
6996
6997/*
6998 * Find the '{' at the start of the block we are in.
6999 * Return NULL if no match found.
7000 * Ignore a '{' that is in a comment, makes indenting the next three lines
7001 * work. */
7002/* foo() */
7003/* { */
7004/* } */
7005
7006 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007007find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008{
7009 pos_T cursor_save;
7010 pos_T *trypos;
7011 pos_T *pos;
7012 static pos_T pos_copy;
7013
7014 cursor_save = curwin->w_cursor;
7015 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
7016 {
7017 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
7018 trypos = &pos_copy;
7019 curwin->w_cursor = *trypos;
7020 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007021 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaardde81312017-08-26 17:49:01 +02007023 && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 break;
7025 if (pos != NULL)
7026 curwin->w_cursor.lnum = pos->lnum;
7027 }
7028 curwin->w_cursor = cursor_save;
7029 return trypos;
7030}
7031
7032/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02007033 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007034 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 */
7036 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007037find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02007039 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007040}
7041
7042 static pos_T *
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02007043find_match_char(int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007044{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 pos_T cursor_save;
7046 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007047 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007048 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049
7050 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007051 ind_maxp_wk = ind_maxparen;
7052retry:
7053 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 {
7055 /* check if the ( is in a // comment */
7056 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01007057 {
7058 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
7059 if (ind_maxp_wk > 0)
7060 {
7061 curwin->w_cursor = *trypos;
7062 curwin->w_cursor.col = 0; /* XXX */
7063 goto retry;
7064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 else
7068 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01007069 pos_T *trypos_wk;
7070
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 pos_copy = *trypos; /* copy trypos, findmatch will change it */
7072 trypos = &pos_copy;
7073 curwin->w_cursor = *trypos;
Bram Moolenaardde81312017-08-26 17:49:01 +02007074 if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01007075 {
7076 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
7077 - trypos_wk->lnum);
7078 if (ind_maxp_wk > 0)
7079 {
7080 curwin->w_cursor = *trypos_wk;
7081 goto retry;
7082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 }
7086 }
7087 curwin->w_cursor = cursor_save;
7088 return trypos;
7089}
7090
7091/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02007092 * Find the matching '(', ignoring it if it is in a comment or before an
7093 * unmatched {.
7094 * Return NULL if no match found.
7095 */
7096 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007097find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02007098{
7099 pos_T *trypos = find_match_paren(ind_maxparen);
7100
7101 if (trypos != NULL)
7102 {
7103 pos_T *tryposBrace = find_start_brace();
7104
7105 /* If both an unmatched '(' and '{' is found. Ignore the '('
7106 * position if the '{' is further down. */
7107 if (tryposBrace != NULL
7108 && (trypos->lnum != tryposBrace->lnum
7109 ? trypos->lnum < tryposBrace->lnum
7110 : trypos->col < tryposBrace->col))
7111 trypos = NULL;
7112 }
7113 return trypos;
7114}
7115
7116/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 * Return ind_maxparen corrected for the difference in line number between the
7118 * cursor position and "startpos". This makes sure that searching for a
7119 * matching paren above the cursor line doesn't find a match because of
7120 * looking a few lines further.
7121 */
7122 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007123corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124{
7125 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
7126
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007127 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
7128 return curbuf->b_ind_maxparen - (int)n;
7129 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130}
7131
7132/*
7133 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007134 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 */
7136 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007137find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138{
7139 int i;
7140 int retval = FALSE;
7141 int open_count = 0;
7142
7143 curwin->w_cursor.col = 0; /* default is start of line */
7144
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007145 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 {
7147 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
7148 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
7149 if (l[i] == start)
7150 ++open_count;
7151 else if (l[i] == end)
7152 {
7153 if (open_count > 0)
7154 --open_count;
7155 else
7156 {
7157 curwin->w_cursor.col = i;
7158 retval = TRUE;
7159 }
7160 }
7161 }
7162 return retval;
7163}
7164
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007165/*
7166 * Parse 'cinoptions' and set the values in "curbuf".
7167 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
7168 */
7169 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007170parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007171{
7172 char_u *p;
7173 char_u *l;
7174 char_u *digits;
7175 int n;
7176 int divider;
7177 int fraction = 0;
7178 int sw = (int)get_sw_value(buf);
7179
7180 /*
7181 * Set the default values.
7182 */
7183 /* Spaces from a block's opening brace the prevailing indent for that
7184 * block should be. */
7185 buf->b_ind_level = sw;
7186
7187 /* Spaces from the edge of the line an open brace that's at the end of a
7188 * line is imagined to be. */
7189 buf->b_ind_open_imag = 0;
7190
7191 /* Spaces from the prevailing indent for a line that is not preceded by
7192 * an opening brace. */
7193 buf->b_ind_no_brace = 0;
7194
7195 /* Column where the first { of a function should be located }. */
7196 buf->b_ind_first_open = 0;
7197
7198 /* Spaces from the prevailing indent a leftmost open brace should be
7199 * located. */
7200 buf->b_ind_open_extra = 0;
7201
7202 /* Spaces from the matching open brace (real location for one at the left
7203 * edge; imaginary location from one that ends a line) the matching close
7204 * brace should be located. */
7205 buf->b_ind_close_extra = 0;
7206
7207 /* Spaces from the edge of the line an open brace sitting in the leftmost
7208 * column is imagined to be. */
7209 buf->b_ind_open_left_imag = 0;
7210
7211 /* Spaces jump labels should be shifted to the left if N is non-negative,
7212 * otherwise the jump label will be put to column 1. */
7213 buf->b_ind_jump_label = -1;
7214
7215 /* Spaces from the switch() indent a "case xx" label should be located. */
7216 buf->b_ind_case = sw;
7217
7218 /* Spaces from the "case xx:" code after a switch() should be located. */
7219 buf->b_ind_case_code = sw;
7220
7221 /* Lineup break at end of case in switch() with case label. */
7222 buf->b_ind_case_break = 0;
7223
7224 /* Spaces from the class declaration indent a scope declaration label
7225 * should be located. */
7226 buf->b_ind_scopedecl = sw;
7227
7228 /* Spaces from the scope declaration label code should be located. */
7229 buf->b_ind_scopedecl_code = sw;
7230
7231 /* Amount K&R-style parameters should be indented. */
7232 buf->b_ind_param = sw;
7233
7234 /* Amount a function type spec should be indented. */
7235 buf->b_ind_func_type = sw;
7236
7237 /* Amount a cpp base class declaration or constructor initialization
7238 * should be indented. */
7239 buf->b_ind_cpp_baseclass = sw;
7240
7241 /* additional spaces beyond the prevailing indent a continuation line
7242 * should be located. */
7243 buf->b_ind_continuation = sw;
7244
7245 /* Spaces from the indent of the line with an unclosed parentheses. */
7246 buf->b_ind_unclosed = sw * 2;
7247
7248 /* Spaces from the indent of the line with an unclosed parentheses, which
7249 * itself is also unclosed. */
7250 buf->b_ind_unclosed2 = sw;
7251
7252 /* Suppress ignoring spaces from the indent of a line starting with an
7253 * unclosed parentheses. */
7254 buf->b_ind_unclosed_noignore = 0;
7255
7256 /* If the opening paren is the last nonwhite character on the line, and
7257 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
7258 * context (for very long lines). */
7259 buf->b_ind_unclosed_wrapped = 0;
7260
7261 /* Suppress ignoring white space when lining up with the character after
7262 * an unclosed parentheses. */
7263 buf->b_ind_unclosed_whiteok = 0;
7264
7265 /* Indent a closing parentheses under the line start of the matching
7266 * opening parentheses. */
7267 buf->b_ind_matching_paren = 0;
7268
7269 /* Indent a closing parentheses under the previous line. */
7270 buf->b_ind_paren_prev = 0;
7271
7272 /* Extra indent for comments. */
7273 buf->b_ind_comment = 0;
7274
7275 /* Spaces from the comment opener when there is nothing after it. */
7276 buf->b_ind_in_comment = 3;
7277
7278 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
7279 * after the comment opener. */
7280 buf->b_ind_in_comment2 = 0;
7281
7282 /* Max lines to search for an open paren. */
7283 buf->b_ind_maxparen = 20;
7284
7285 /* Max lines to search for an open comment. */
7286 buf->b_ind_maxcomment = 70;
7287
7288 /* Handle braces for java code. */
7289 buf->b_ind_java = 0;
7290
7291 /* Not to confuse JS object properties with labels. */
7292 buf->b_ind_js = 0;
7293
7294 /* Handle blocked cases correctly. */
7295 buf->b_ind_keep_case_label = 0;
7296
7297 /* Handle C++ namespace. */
7298 buf->b_ind_cpp_namespace = 0;
7299
7300 /* Handle continuation lines containing conditions of if(), for() and
7301 * while(). */
7302 buf->b_ind_if_for_while = 0;
7303
Bram Moolenaar6b643942017-03-05 19:44:06 +01007304 /* indentation for # comments */
7305 buf->b_ind_hash_comment = 0;
7306
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007307 /* Handle C++ extern "C" or "C++" */
7308 buf->b_ind_cpp_extern_c = 0;
7309
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007310 for (p = buf->b_p_cino; *p; )
7311 {
7312 l = p++;
7313 if (*p == '-')
7314 ++p;
7315 digits = p; /* remember where the digits start */
7316 n = getdigits(&p);
7317 divider = 0;
7318 if (*p == '.') /* ".5s" means a fraction */
7319 {
7320 fraction = atol((char *)++p);
7321 while (VIM_ISDIGIT(*p))
7322 {
7323 ++p;
7324 if (divider)
7325 divider *= 10;
7326 else
7327 divider = 10;
7328 }
7329 }
7330 if (*p == 's') /* "2s" means two times 'shiftwidth' */
7331 {
7332 if (p == digits)
7333 n = sw; /* just "s" is one 'shiftwidth' */
7334 else
7335 {
7336 n *= sw;
7337 if (divider)
7338 n += (sw * fraction + divider / 2) / divider;
7339 }
7340 ++p;
7341 }
7342 if (l[1] == '-')
7343 n = -n;
7344
7345 /* When adding an entry here, also update the default 'cinoptions' in
7346 * doc/indent.txt, and add explanation for it! */
7347 switch (*l)
7348 {
7349 case '>': buf->b_ind_level = n; break;
7350 case 'e': buf->b_ind_open_imag = n; break;
7351 case 'n': buf->b_ind_no_brace = n; break;
7352 case 'f': buf->b_ind_first_open = n; break;
7353 case '{': buf->b_ind_open_extra = n; break;
7354 case '}': buf->b_ind_close_extra = n; break;
7355 case '^': buf->b_ind_open_left_imag = n; break;
7356 case 'L': buf->b_ind_jump_label = n; break;
7357 case ':': buf->b_ind_case = n; break;
7358 case '=': buf->b_ind_case_code = n; break;
7359 case 'b': buf->b_ind_case_break = n; break;
7360 case 'p': buf->b_ind_param = n; break;
7361 case 't': buf->b_ind_func_type = n; break;
7362 case '/': buf->b_ind_comment = n; break;
7363 case 'c': buf->b_ind_in_comment = n; break;
7364 case 'C': buf->b_ind_in_comment2 = n; break;
7365 case 'i': buf->b_ind_cpp_baseclass = n; break;
7366 case '+': buf->b_ind_continuation = n; break;
7367 case '(': buf->b_ind_unclosed = n; break;
7368 case 'u': buf->b_ind_unclosed2 = n; break;
7369 case 'U': buf->b_ind_unclosed_noignore = n; break;
7370 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7371 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7372 case 'm': buf->b_ind_matching_paren = n; break;
7373 case 'M': buf->b_ind_paren_prev = n; break;
7374 case ')': buf->b_ind_maxparen = n; break;
7375 case '*': buf->b_ind_maxcomment = n; break;
7376 case 'g': buf->b_ind_scopedecl = n; break;
7377 case 'h': buf->b_ind_scopedecl_code = n; break;
7378 case 'j': buf->b_ind_java = n; break;
7379 case 'J': buf->b_ind_js = n; break;
7380 case 'l': buf->b_ind_keep_case_label = n; break;
7381 case '#': buf->b_ind_hash_comment = n; break;
7382 case 'N': buf->b_ind_cpp_namespace = n; break;
7383 case 'k': buf->b_ind_if_for_while = n; break;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007384 case 'E': buf->b_ind_cpp_extern_c = n; break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007385 }
7386 if (*p == ',')
7387 ++p;
7388 }
7389}
7390
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007391/*
7392 * Return the desired indent for C code.
7393 * Return -1 if the indent should be left alone (inside a raw string).
7394 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007396get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 pos_T cur_curpos;
7399 int amount;
7400 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007401 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 colnr_T col;
7403 char_u *theline;
7404 char_u *linecopy;
7405 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007406 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007408 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 pos_T our_paren_pos;
7410 char_u *start;
7411 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007412#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413#define BRACE_AT_START 2 /* '{' is at start of line */
7414#define BRACE_AT_END 3 /* '{' is at end of line */
7415 linenr_T ourscope;
7416 char_u *l;
7417 char_u *look;
7418 char_u terminated;
7419 int lookfor;
7420#define LOOKFOR_INITIAL 0
7421#define LOOKFOR_IF 1
7422#define LOOKFOR_DO 2
7423#define LOOKFOR_CASE 3
7424#define LOOKFOR_ANY 4
7425#define LOOKFOR_TERM 5
7426#define LOOKFOR_UNTERM 6
7427#define LOOKFOR_SCOPEDECL 7
7428#define LOOKFOR_NOBREAK 8
7429#define LOOKFOR_CPP_BASECLASS 9
7430#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007431#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007432#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433
7434 int whilelevel;
7435 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 int n;
7437 int iscase;
7438 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007439 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007441 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007442 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007443 int js_cur_has_key = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02007444 linenr_T raw_string_start = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007445 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007447 /* make a copy, value is changed below */
7448 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449
7450 /* remember where the cursor was when we started */
7451 cur_curpos = curwin->w_cursor;
7452
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007453 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007454 if (cur_curpos.lnum == 1)
7455 return 0;
7456
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 /* Get a copy of the current contents of the line.
7458 * This is required, because only the most recent line obtained with
7459 * ml_get is valid! */
7460 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7461 if (linecopy == NULL)
7462 return 0;
7463
7464 /*
7465 * In insert mode and the cursor is on a ')' truncate the line at the
7466 * cursor position. We don't want to line up with the matching '(' when
7467 * inserting new stuff.
7468 * For unknown reasons the cursor might be past the end of the line, thus
7469 * check for that.
7470 */
7471 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007472 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 && linecopy[curwin->w_cursor.col] == ')')
7474 linecopy[curwin->w_cursor.col] = NUL;
7475
7476 theline = skipwhite(linecopy);
7477
7478 /* move the cursor to the start of the line */
7479
7480 curwin->w_cursor.col = 0;
7481
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007482 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007483
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007485 * If we are inside a raw string don't change the indent.
7486 * Ignore a raw string inside a comment.
7487 */
7488 comment_pos = ind_find_start_comment();
7489 if (comment_pos != NULL)
7490 {
7491 /* findmatchlimit() static pos is overwritten, make a copy */
7492 tryposCopy = *comment_pos;
7493 comment_pos = &tryposCopy;
7494 }
7495 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007496 if (trypos != NULL && (comment_pos == NULL
7497 || LT_POS(*trypos, *comment_pos)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007498 {
7499 amount = -1;
7500 goto laterend;
7501 }
7502
7503 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 * #defines and so on always go at the left when included in 'cinkeys'.
7505 */
7506 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007507 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007508 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007509 goto theend;
7510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511
7512 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007513 * Is it a non-case label? Then that goes at the left margin too unless:
7514 * - JS flag is set.
7515 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007517 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007518 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 {
7520 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007521 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 }
7523
7524 /*
7525 * If we're inside a "//" comment and there is a "//" comment in a
7526 * previous line, lineup with that one.
7527 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007528 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 && (trypos = find_line_comment()) != NULL) /* XXX */
7530 {
7531 /* find how indented the line beginning the comment is */
7532 getvcol(curwin, trypos, &col, NULL, NULL);
7533 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007534 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 }
7536
7537 /*
7538 * If we're inside a comment and not looking at the start of the
7539 * comment, try using the 'comments' option.
7540 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007541 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 {
7543 int lead_start_len = 2;
7544 int lead_middle_len = 1;
7545 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7546 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7547 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7548 char_u *p;
7549 int start_align = 0;
7550 int start_off = 0;
7551 int done = FALSE;
7552
7553 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007554 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007556 *lead_start = NUL;
7557 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558
7559 p = curbuf->b_p_com;
7560 while (*p != NUL)
7561 {
7562 int align = 0;
7563 int off = 0;
7564 int what = 0;
7565
7566 while (*p != NUL && *p != ':')
7567 {
7568 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7569 what = *p++;
7570 else if (*p == COM_LEFT || *p == COM_RIGHT)
7571 align = *p++;
7572 else if (VIM_ISDIGIT(*p) || *p == '-')
7573 off = getdigits(&p);
7574 else
7575 ++p;
7576 }
7577
7578 if (*p == ':')
7579 ++p;
7580 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7581 if (what == COM_START)
7582 {
7583 STRCPY(lead_start, lead_end);
7584 lead_start_len = (int)STRLEN(lead_start);
7585 start_off = off;
7586 start_align = align;
7587 }
7588 else if (what == COM_MIDDLE)
7589 {
7590 STRCPY(lead_middle, lead_end);
7591 lead_middle_len = (int)STRLEN(lead_middle);
7592 }
7593 else if (what == COM_END)
7594 {
7595 /* If our line starts with the middle comment string, line it
7596 * up with the comment opener per the 'comments' option. */
7597 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7598 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7599 {
7600 done = TRUE;
7601 if (curwin->w_cursor.lnum > 1)
7602 {
7603 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007604 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 * the middle comment string matches in the previous
7606 * line, use the indent of that line. XXX */
7607 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7608 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7609 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7610 else if (STRNCMP(look, lead_middle,
7611 lead_middle_len) == 0)
7612 {
7613 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7614 break;
7615 }
7616 /* If the start comment string doesn't match with the
7617 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007618 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 lead_start, lead_start_len) != 0)
7620 continue;
7621 }
7622 if (start_off != 0)
7623 amount += start_off;
7624 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007625 amount += vim_strsize(lead_start)
7626 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 break;
7628 }
7629
7630 /* If our line starts with the end comment string, line it up
7631 * with the middle comment */
7632 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7633 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7634 {
7635 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7636 /* XXX */
7637 if (off != 0)
7638 amount += off;
7639 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007640 amount += vim_strsize(lead_start)
7641 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 done = TRUE;
7643 break;
7644 }
7645 }
7646 }
7647
7648 /* If our line starts with an asterisk, line up with the
7649 * asterisk in the comment opener; otherwise, line up
7650 * with the first character of the comment text.
7651 */
7652 if (done)
7653 ;
7654 else if (theline[0] == '*')
7655 amount += 1;
7656 else
7657 {
7658 /*
7659 * If we are more than one line away from the comment opener, take
7660 * the indent of the previous non-empty line. If 'cino' has "CO"
7661 * and we are just below the comment opener and there are any
7662 * white characters after it line up with the text after it;
7663 * otherwise, add the amount specified by "c" in 'cino'
7664 */
7665 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007666 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 {
7668 if (linewhite(lnum)) /* skip blank lines */
7669 continue;
7670 amount = get_indent_lnum(lnum); /* XXX */
7671 break;
7672 }
7673 if (amount == -1) /* use the comment opener */
7674 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007675 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007677 start = ml_get(comment_pos->lnum);
7678 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007680 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007682 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007684 if (curbuf->b_ind_in_comment2 || *look == NUL)
7685 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 }
7687 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007688 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 }
7690
7691 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007692 * Are we looking at a ']' that has a match?
7693 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007694 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007695 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7696 {
7697 /* align with the line containing the '['. */
7698 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007699 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007700 }
7701
7702 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 * Are we inside parentheses or braces?
7704 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007705 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007706 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007707 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 || trypos != NULL)
7709 {
7710 if (trypos != NULL && tryposBrace != NULL)
7711 {
7712 /* Both an unmatched '(' and '{' is found. Use the one which is
7713 * closer to the current cursor position, set the other to NULL. */
7714 if (trypos->lnum != tryposBrace->lnum
7715 ? trypos->lnum < tryposBrace->lnum
7716 : trypos->col < tryposBrace->col)
7717 trypos = NULL;
7718 else
7719 tryposBrace = NULL;
7720 }
7721
7722 if (trypos != NULL)
7723 {
7724 /*
7725 * If the matching paren is more than one line away, use the indent of
7726 * a previous non-empty line that matches the same paren.
7727 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007728 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007730 /* Line up with the start of the matching paren line. */
7731 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7732 }
7733 else
7734 {
7735 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007736 our_paren_pos = *trypos;
7737 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007739 l = skipwhite(ml_get(lnum));
7740 if (cin_nocode(l)) /* skip comment lines */
7741 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007742 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007743 continue; /* ignore #define, #if, etc. */
7744 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007746 /* Skip a comment or raw string. XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02007747 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007748 {
7749 lnum = trypos->lnum + 1;
7750 continue;
7751 }
7752
7753 /* XXX */
7754 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007755 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007756 && trypos->lnum == our_paren_pos.lnum
7757 && trypos->col == our_paren_pos.col)
7758 {
7759 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007761 if (theline[0] == ')')
7762 {
7763 if (our_paren_pos.lnum != lnum
7764 && cur_amount > amount)
7765 cur_amount = amount;
7766 amount = -1;
7767 }
7768 break;
7769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 }
7771 }
7772
7773 /*
7774 * Line up with line where the matching paren is. XXX
7775 * If the line starts with a '(' or the indent for unclosed
7776 * parentheses is zero, line up with the unclosed parentheses.
7777 */
7778 if (amount == -1)
7779 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007780 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007781 int is_if_for_while = 0;
7782
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007783 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007784 {
7785 /* Look for the outermost opening parenthesis on this line
7786 * and check whether it belongs to an "if", "for" or "while". */
7787
7788 pos_T cursor_save = curwin->w_cursor;
7789 pos_T outermost;
7790 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007791
7792 trypos = &our_paren_pos;
7793 do {
7794 outermost = *trypos;
7795 curwin->w_cursor.lnum = outermost.lnum;
7796 curwin->w_cursor.col = outermost.col;
7797
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007798 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007799 } while (trypos && trypos->lnum == outermost.lnum);
7800
7801 curwin->w_cursor = cursor_save;
7802
7803 line = ml_get(outermost.lnum);
7804
7805 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007806 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007807 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007808
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007809 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007810 look = skipwhite(look);
7811 if (*look == '(')
7812 {
7813 linenr_T save_lnum = curwin->w_cursor.lnum;
7814 char_u *line;
7815 int look_col;
7816
7817 /* Ignore a '(' in front of the line that has a match before
7818 * our matching '('. */
7819 curwin->w_cursor.lnum = our_paren_pos.lnum;
7820 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007821 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007822 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007823 if ((trypos = findmatchlimit(NULL, ')', 0,
7824 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007825 != NULL
7826 && trypos->lnum == our_paren_pos.lnum
7827 && trypos->col < our_paren_pos.col)
7828 ignore_paren_col = trypos->col + 1;
7829
7830 curwin->w_cursor.lnum = save_lnum;
7831 look = ml_get(our_paren_pos.lnum) + look_col;
7832 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007833 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7834 && is_if_for_while == 0)
7835 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007836 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 {
7838 /*
7839 * If we're looking at a close paren, line up right there;
7840 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007841 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 * the last nonwhite character of the line, use either the
7843 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007844 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 * lines).
7846 */
7847 if (theline[0] != ')')
7848 {
7849 cur_amount = MAXCOL;
7850 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007851 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 && cin_ends_in(l, (char_u *)"(", NULL))
7853 {
7854 /* look for opening unmatched paren, indent one level
7855 * for each additional level */
7856 n = 1;
7857 for (col = 0; col < our_paren_pos.col; ++col)
7858 {
7859 switch (l[col])
7860 {
7861 case '(':
7862 case '{': ++n;
7863 break;
7864
7865 case ')':
7866 case '}': if (n > 1)
7867 --n;
7868 break;
7869 }
7870 }
7871
7872 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007873 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007875 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 our_paren_pos.col++;
7877 else
7878 {
7879 col = our_paren_pos.col + 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01007880 while (VIM_ISWHITE(l[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 col++;
7882 if (l[col] != NUL) /* In case of trailing space */
7883 our_paren_pos.col = col;
7884 else
7885 our_paren_pos.col++;
7886 }
7887 }
7888
7889 /*
7890 * Find how indented the paren is, or the character after it
7891 * if we did the above "if".
7892 */
7893 if (our_paren_pos.col > 0)
7894 {
7895 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7896 if (cur_amount > (int)col)
7897 cur_amount = col;
7898 }
7899 }
7900
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007901 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 {
7903 /* Line up with the start of the matching paren line. */
7904 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007905 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7906 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007907 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 {
7909 if (cur_amount != MAXCOL)
7910 amount = cur_amount;
7911 }
7912 else
7913 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007914 /* Add b_ind_unclosed2 for each '(' before our matching one,
7915 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007917 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 {
7919 --our_paren_pos.col;
7920 switch (*ml_get_pos(&our_paren_pos))
7921 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007922 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 col = our_paren_pos.col;
7924 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007925 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 col = MAXCOL;
7927 break;
7928 }
7929 }
7930
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007931 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 * braces */
7933 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007934 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 else
7936 {
7937 curwin->w_cursor.lnum = our_paren_pos.lnum;
7938 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007939 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7940 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007941 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007943 {
7944 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007945 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007946 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007947 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 }
7950 /*
7951 * For a line starting with ')' use the minimum of the two
7952 * positions, to avoid giving it more indent than the previous
7953 * lines:
7954 * func_long_name( if (x
7955 * arg && yy
7956 * ) ^ not here ) ^ not here
7957 */
7958 if (cur_amount < amount)
7959 amount = cur_amount;
7960 }
7961 }
7962
7963 /* add extra indent for a comment */
7964 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007965 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 else
7968 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007969 /*
7970 * We are inside braces, there is a { before this line at the position
7971 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007972 * Make a copy of tryposBrace, it may point to pos_copy inside
7973 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007974 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007975 tryposCopy = *tryposBrace;
7976 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 ourscope = trypos->lnum;
7979 start = ml_get(ourscope);
7980
7981 /*
7982 * Now figure out how indented the line is in general.
7983 * If the brace was at the start of the line, we use that;
7984 * otherwise, check out the indentation of the line as
7985 * a whole and then add the "imaginary indent" to that.
7986 */
7987 look = skipwhite(start);
7988 if (*look == '{')
7989 {
7990 getvcol(curwin, trypos, &col, NULL, NULL);
7991 amount = col;
7992 if (*start == '{')
7993 start_brace = BRACE_IN_COL0;
7994 else
7995 start_brace = BRACE_AT_START;
7996 }
7997 else
7998 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007999 /* That opening brace might have been on a continuation
8000 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 curwin->w_cursor.lnum = ourscope;
8002
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008003 /* Position the cursor over the rightmost paren, so that
8004 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 lnum = ourscope;
8006 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008007 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
8008 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 lnum = trypos->lnum;
8010
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008011 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 * case 1: if (asdf &&
8013 * ldfd) {
8014 * }
8015 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02008016 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
8017 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02008019 else if (curbuf->b_ind_js)
8020 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008022 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023
8024 start_brace = BRACE_AT_END;
8025 }
8026
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008027 /* For Javascript check if the line starts with "key:". */
8028 if (curbuf->b_ind_js)
8029 js_cur_has_key = cin_has_js_key(theline);
8030
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008032 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 * we want to be. otherwise, add the amount of room
8034 * that an indent is supposed to be.
8035 */
8036 if (theline[0] == '}')
8037 {
8038 /*
8039 * they may want closing braces to line up with something
8040 * other than the open brace. indulge them, if so.
8041 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008042 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 }
8044 else
8045 {
8046 /*
8047 * If we're looking at an "else", try to find an "if"
8048 * to match it with.
8049 * If we're looking at a "while", try to find a "do"
8050 * to match it with.
8051 */
8052 lookfor = LOOKFOR_INITIAL;
8053 if (cin_iselse(theline))
8054 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008055 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 lookfor = LOOKFOR_DO;
8057 if (lookfor != LOOKFOR_INITIAL)
8058 {
8059 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008060 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 {
8062 amount = get_indent(); /* XXX */
8063 goto theend;
8064 }
8065 }
8066
8067 /*
8068 * We get here if we are not on an "while-of-do" or "else" (or
8069 * failed to find a matching "if").
8070 * Search backwards for something to line up with.
8071 * First set amount for when we don't find anything.
8072 */
8073
8074 /*
8075 * if the '{' is _really_ at the left margin, use the imaginary
8076 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008077 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 */
8079
8080 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
8081 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008082 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008083 lookfor_cpp_namespace = TRUE;
8084 }
8085 else if (start_brace == BRACE_AT_START &&
8086 lookfor_cpp_namespace) /* '{' is at start */
8087 {
8088
8089 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 }
8091 else
8092 {
8093 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008094 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008095 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008096
8097 l = skipwhite(ml_get_curline());
8098 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008099 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008100 else if (cin_is_cpp_extern_c(l))
8101 amount += curbuf->b_ind_cpp_extern_c;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 else
8104 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008105 /* Compensate for adding b_ind_open_extra later. */
8106 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 if (amount < 0)
8108 amount = 0;
8109 }
8110 }
8111
8112 lookfor_break = FALSE;
8113
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008114 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 {
8116 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008117 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 }
8119 else if (cin_isscopedecl(theline)) /* private:, ... */
8120 {
8121 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008122 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 }
8124 else
8125 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008126 if (curbuf->b_ind_case_break && cin_isbreak(theline))
8127 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 lookfor_break = TRUE;
8129
8130 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008131 /* b_ind_level from start of block */
8132 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 }
8134 scope_amount = amount;
8135 whilelevel = 0;
8136
8137 /*
8138 * Search backwards. If we find something we recognize, line up
8139 * with that.
8140 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008141 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 * the usual amount relative to the conditional
8143 * that opens the block.
8144 */
8145 curwin->w_cursor = cur_curpos;
8146 for (;;)
8147 {
8148 curwin->w_cursor.lnum--;
8149 curwin->w_cursor.col = 0;
8150
8151 /*
8152 * If we went all the way back to the start of our scope, line
8153 * up with it.
8154 */
8155 if (curwin->w_cursor.lnum <= ourscope)
8156 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008157 /* We reached end of scope:
8158 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008160 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 * don't add ind_continuation, otherwise it is a variable
8162 * declaration:
8163 * int x,
8164 * here; <-- add ind_continuation
8165 */
8166 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8167 {
8168 if (curwin->w_cursor.lnum == 0
8169 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008170 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008172 /* nothing found (abuse curbuf->b_ind_maxparen as
8173 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 * initialization) */
8175 if (cont_amount > 0)
8176 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008177 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 amount += ind_continuation;
8179 break;
8180 }
8181
8182 l = ml_get_curline();
8183
8184 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008185 * If we're in a comment or raw string now, skip to
8186 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 */
Bram Moolenaardde81312017-08-26 17:49:01 +02008188 trypos = ind_find_start_CORS(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 if (trypos != NULL)
8190 {
8191 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008192 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 continue;
8194 }
8195
8196 /*
8197 * Skip preprocessor directives and blank lines.
8198 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008199 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8200 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 continue;
8202
8203 if (cin_nocode(l))
8204 continue;
8205
8206 terminated = cin_isterminated(l, FALSE, TRUE);
8207
8208 /*
8209 * If we are at top level and the line looks like a
8210 * function declaration, we are done
8211 * (it's a variable declaration).
8212 */
8213 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008214 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 {
8216 /* if the line is terminated with another ','
8217 * it is a continued variable initialization.
8218 * don't add extra indent.
8219 * TODO: does not work, if a function
8220 * declaration is split over multiple lines:
8221 * cin_isfuncdecl returns FALSE then.
8222 */
8223 if (terminated == ',')
8224 break;
8225
8226 /* if it es a enum declaration or an assignment,
8227 * we are done.
8228 */
8229 if (terminated != ';' && cin_isinit())
8230 break;
8231
8232 /* nothing useful found */
8233 if (terminated == 0 || terminated == '{')
8234 continue;
8235 }
8236
8237 if (terminated != ';')
8238 {
8239 /* Skip parens and braces. Position the cursor
8240 * over the rightmost paren, so that matching it
8241 * will take us back to the start of the line.
8242 */ /* XXX */
8243 trypos = NULL;
8244 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008245 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008246 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247
8248 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008249 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250
8251 if (trypos != NULL)
8252 {
8253 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008254 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 continue;
8256 }
8257 }
8258
8259 /* it's a variable declaration, add indentation
8260 * like in
8261 * int a,
8262 * b;
8263 */
8264 if (cont_amount > 0)
8265 amount = cont_amount;
8266 else
8267 amount += ind_continuation;
8268 }
8269 else if (lookfor == LOOKFOR_UNTERM)
8270 {
8271 if (cont_amount > 0)
8272 amount = cont_amount;
8273 else
8274 amount += ind_continuation;
8275 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008276 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008277 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02008278 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008279 && lookfor != LOOKFOR_CPP_BASECLASS
8280 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02008281 {
8282 amount = scope_amount;
8283 if (theline[0] == '{')
8284 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008285 amount += curbuf->b_ind_open_extra;
8286 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008287 }
8288 }
8289
8290 if (lookfor_cpp_namespace)
8291 {
8292 /*
8293 * Looking for C++ namespace, need to look further
8294 * back.
8295 */
8296 if (curwin->w_cursor.lnum == ourscope)
8297 continue;
8298
8299 if (curwin->w_cursor.lnum == 0
8300 || curwin->w_cursor.lnum
8301 < ourscope - FIND_NAMESPACE_LIM)
8302 break;
8303
8304 l = ml_get_curline();
8305
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008306 /* If we're in a comment or raw string now, skip
8307 * to the start of it. */
Bram Moolenaardde81312017-08-26 17:49:01 +02008308 trypos = ind_find_start_CORS(NULL);
Bram Moolenaare79d1532011-10-04 18:03:47 +02008309 if (trypos != NULL)
8310 {
8311 curwin->w_cursor.lnum = trypos->lnum + 1;
8312 curwin->w_cursor.col = 0;
8313 continue;
8314 }
8315
8316 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008317 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8318 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02008319 continue;
8320
8321 /* Finally the actual check for "namespace". */
8322 if (cin_is_cpp_namespace(l))
8323 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008324 amount += curbuf->b_ind_cpp_namespace
8325 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008326 break;
8327 }
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008328 else if (cin_is_cpp_extern_c(l))
8329 {
8330 amount += curbuf->b_ind_cpp_extern_c
8331 - added_to_amount;
8332 break;
8333 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008334
8335 if (cin_nocode(l))
8336 continue;
8337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 }
8339 break;
8340 }
8341
8342 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008343 * If we're in a comment or raw string now, skip to the start
8344 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008346 if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 {
8348 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008349 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 continue;
8351 }
8352
8353 l = ml_get_curline();
8354
8355 /*
8356 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008357 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008359 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 if (iscase || cin_isscopedecl(l))
8361 {
8362 /* we are only looking for cpp base class
8363 * declaration/initialization any longer */
8364 if (lookfor == LOOKFOR_CPP_BASECLASS)
8365 break;
8366
8367 /* When looking for a "do" we are not interested in
8368 * labels. */
8369 if (whilelevel > 0)
8370 continue;
8371
8372 /*
8373 * case xx:
8374 * c = 99 + <- this indent plus continuation
8375 *-> here;
8376 */
8377 if (lookfor == LOOKFOR_UNTERM
8378 || lookfor == LOOKFOR_ENUM_OR_INIT)
8379 {
8380 if (cont_amount > 0)
8381 amount = cont_amount;
8382 else
8383 amount += ind_continuation;
8384 break;
8385 }
8386
8387 /*
8388 * case xx: <- line up with this case
8389 * x = 333;
8390 * case yy:
8391 */
8392 if ( (iscase && lookfor == LOOKFOR_CASE)
8393 || (iscase && lookfor_break)
8394 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8395 {
8396 /*
8397 * Check that this case label is not for another
8398 * switch()
8399 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008400 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008401 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 {
8403 amount = get_indent(); /* XXX */
8404 break;
8405 }
8406 continue;
8407 }
8408
8409 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8410
8411 /*
8412 * case xx: if (cond) <- line up with this if
8413 * y = y + 1;
8414 * -> s = 99;
8415 *
8416 * case xx:
8417 * if (cond) <- line up with this line
8418 * y = y + 1;
8419 * -> s = 99;
8420 */
8421 if (lookfor == LOOKFOR_TERM)
8422 {
8423 if (n)
8424 amount = n;
8425
8426 if (!lookfor_break)
8427 break;
8428 }
8429
8430 /*
8431 * case xx: x = x + 1; <- line up with this x
8432 * -> y = y + 1;
8433 *
8434 * case xx: if (cond) <- line up with this if
8435 * -> y = y + 1;
8436 */
8437 if (n)
8438 {
8439 amount = n;
8440 l = after_label(ml_get_curline());
8441 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008442 {
8443 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008444 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008445 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008446 amount += curbuf->b_ind_level
8447 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 break;
8450 }
8451
8452 /*
8453 * Try to get the indent of a statement before the switch
8454 * label. If nothing is found, line up relative to the
8455 * switch label.
8456 * break; <- may line up with this line
8457 * case xx:
8458 * -> y = 1;
8459 */
8460 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008461 ? curbuf->b_ind_case_code
8462 : curbuf->b_ind_scopedecl_code);
8463 lookfor = curbuf->b_ind_case_break
8464 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 continue;
8466 }
8467
8468 /*
8469 * Looking for a switch() label or C++ scope declaration,
8470 * ignore other lines, skip {}-blocks.
8471 */
8472 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8473 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008474 if (find_last_paren(l, '{', '}')
8475 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008476 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008478 curwin->w_cursor.col = 0;
8479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 continue;
8481 }
8482
8483 /*
8484 * Ignore jump labels with nothing after them.
8485 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008486 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 {
8488 l = after_label(ml_get_curline());
8489 if (l == NULL || cin_nocode(l))
8490 continue;
8491 }
8492
8493 /*
8494 * Ignore #defines, #if, etc.
8495 * Ignore comment and empty lines.
8496 * (need to get the line again, cin_islabel() may have
8497 * unlocked it)
8498 */
8499 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008500 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 || cin_nocode(l))
8502 continue;
8503
8504 /*
8505 * Are we at the start of a cpp base class declaration or
8506 * constructor initialization?
8507 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008508 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008509 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008510 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008511 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008512 l = ml_get_curline();
8513 }
8514 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 {
8516 if (lookfor == LOOKFOR_UNTERM)
8517 {
8518 if (cont_amount > 0)
8519 amount = cont_amount;
8520 else
8521 amount += ind_continuation;
8522 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008523 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008525 /* Need to find start of the declaration. */
8526 lookfor = LOOKFOR_UNTERM;
8527 ind_continuation = 0;
8528 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 }
8530 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008531 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008532 amount = get_baseclass_amount(
8533 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 break;
8535 }
8536 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8537 {
8538 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008539 * declaration or initialization before the opening brace.
8540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 if (cin_isterminated(l, TRUE, FALSE))
8542 break;
8543 else
8544 continue;
8545 }
8546
8547 /*
8548 * What happens next depends on the line being terminated.
8549 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008550 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 * 123,
8552 * sizeof
8553 * here
8554 * Otherwise check whether it is a enumeration or structure
8555 * initialisation (not indented) or a variable declaration
8556 * (indented).
8557 */
8558 terminated = cin_isterminated(l, FALSE, TRUE);
8559
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008560 if (js_cur_has_key)
8561 {
8562 js_cur_has_key = 0; /* only check the first line */
8563 if (curbuf->b_ind_js && terminated == ',')
8564 {
8565 /* For Javascript we might be inside an object:
8566 * key: something, <- align with this
8567 * key: something
8568 * or:
8569 * key: something + <- align with this
8570 * something,
8571 * key: something
8572 */
8573 lookfor = LOOKFOR_JS_KEY;
8574 }
8575 }
8576 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8577 {
8578 amount = get_indent();
8579 break;
8580 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008581 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008582 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008583 if (tryposBrace != NULL && tryposBrace->lnum
8584 >= curwin->w_cursor.lnum)
8585 break;
8586 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008587 /* line below current line is the one that starts a
8588 * (possibly broken) line ending in a comma. */
8589 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008590 else
8591 {
8592 amount = get_indent();
8593 if (curwin->w_cursor.lnum - 1 == ourscope)
8594 /* line above is start of the scope, thus current
8595 * line is the one that stars a (possibly broken)
8596 * line ending in a comma. */
8597 break;
8598 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008599 }
8600
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8602 && terminated == ','))
8603 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008604 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8605 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008606 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 /*
8608 * if we're in the middle of a paren thing,
8609 * go back to the line that starts it so
8610 * we can get the right prevailing indent
8611 * if ( foo &&
8612 * bar )
8613 */
8614 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008615 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008617 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 */
8619 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008620 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008621 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8622 || (trypos->lnum == tryposBrace->lnum
8623 && trypos->col < tryposBrace->col)))
8624 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625
8626 /*
8627 * If we are looking for ',', we also look for matching
8628 * braces.
8629 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008630 if (trypos == NULL && terminated == ','
8631 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008632 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633
8634 if (trypos != NULL)
8635 {
8636 /*
8637 * Check if we are on a case label now. This is
8638 * handled above.
8639 * case xx: if ( asdf &&
8640 * asdf)
8641 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008642 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008644 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 {
8646 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008647 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 continue;
8649 }
8650 }
8651
8652 /*
8653 * Skip over continuation lines to find the one to get the
8654 * indent from
8655 * char *usethis = "bla\
8656 * bla",
8657 * here;
8658 */
8659 if (terminated == ',')
8660 {
8661 while (curwin->w_cursor.lnum > 1)
8662 {
8663 l = ml_get(curwin->w_cursor.lnum - 1);
8664 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8665 break;
8666 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008667 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 }
8669 }
8670
8671 /*
8672 * Get indent and pointer to text for current line,
8673 * ignoring any jump label. XXX
8674 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008675 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008676 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008677 else
8678 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 /*
8680 * If this is just above the line we are indenting, and it
8681 * starts with a '{', line it up with this line.
8682 * while (not)
8683 * -> {
8684 * }
8685 */
8686 if (terminated != ',' && lookfor != LOOKFOR_TERM
8687 && theline[0] == '{')
8688 {
8689 amount = cur_amount;
8690 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008691 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 * doesn't start with a '{', which must have a match
8693 * in the same line (scope is the same). Probably:
8694 * { 1, 2 },
8695 * -> { 3, 4 }
8696 */
8697 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008698 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008700 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 {
8702 /* have to look back, whether it is a cpp base
8703 * class declaration or initialization */
8704 lookfor = LOOKFOR_CPP_BASECLASS;
8705 continue;
8706 }
8707 break;
8708 }
8709
8710 /*
8711 * Check if we are after an "if", "while", etc.
8712 * Also allow " } else".
8713 */
8714 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8715 {
8716 /*
8717 * Found an unterminated line after an if (), line up
8718 * with the last one.
8719 * if (cond)
8720 * 100 +
8721 * -> here;
8722 */
8723 if (lookfor == LOOKFOR_UNTERM
8724 || lookfor == LOOKFOR_ENUM_OR_INIT)
8725 {
8726 if (cont_amount > 0)
8727 amount = cont_amount;
8728 else
8729 amount += ind_continuation;
8730 break;
8731 }
8732
8733 /*
8734 * If this is just above the line we are indenting, we
8735 * are finished.
8736 * while (not)
8737 * -> here;
8738 * Otherwise this indent can be used when the line
8739 * before this is terminated.
8740 * yyy;
8741 * if (stat)
8742 * while (not)
8743 * xxx;
8744 * -> here;
8745 */
8746 amount = cur_amount;
8747 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008748 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 if (lookfor != LOOKFOR_TERM)
8750 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008751 amount += curbuf->b_ind_level
8752 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 break;
8754 }
8755
8756 /*
8757 * Special trick: when expecting the while () after a
8758 * do, line up with the while()
8759 * do
8760 * x = 1;
8761 * -> here
8762 */
8763 l = skipwhite(ml_get_curline());
8764 if (cin_isdo(l))
8765 {
8766 if (whilelevel == 0)
8767 break;
8768 --whilelevel;
8769 }
8770
8771 /*
8772 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008773 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 * Need to use the scope of this "else". XXX
8775 * If whilelevel != 0 continue looking for a "do {".
8776 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008777 if (cin_iselse(l) && whilelevel == 0)
8778 {
8779 /* If we're looking at "} else", let's make sure we
8780 * find the opening brace of the enclosing scope,
8781 * not the one from "if () {". */
8782 if (*l == '}')
8783 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008784 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008785
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008786 if ((trypos = find_start_brace()) == NULL
8787 || find_match(LOOKFOR_IF, trypos->lnum)
8788 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008789 break;
8790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 }
8792
8793 /*
8794 * If we're below an unterminated line that is not an
8795 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008796 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 * the line before this one.
8798 */
8799 else
8800 {
8801 /*
8802 * Found two unterminated lines on a row, line up with
8803 * the last one.
8804 * c = 99 +
8805 * 100 +
8806 * -> here;
8807 */
8808 if (lookfor == LOOKFOR_UNTERM)
8809 {
8810 /* When line ends in a comma add extra indent */
8811 if (terminated == ',')
8812 amount += ind_continuation;
8813 break;
8814 }
8815
8816 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8817 {
8818 /* Found two lines ending in ',', lineup with the
8819 * lowest one, but check for cpp base class
8820 * declaration/initialization, if it is an
8821 * opening brace or we are looking just for
8822 * enumerations/initializations. */
8823 if (terminated == ',')
8824 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008825 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 break;
8827
8828 lookfor = LOOKFOR_CPP_BASECLASS;
8829 continue;
8830 }
8831
8832 /* Ignore unterminated lines in between, but
8833 * reduce indent. */
8834 if (amount > cur_amount)
8835 amount = cur_amount;
8836 }
8837 else
8838 {
8839 /*
8840 * Found first unterminated line on a row, may
8841 * line up with this line, remember its indent
8842 * 100 +
8843 * -> here;
8844 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008845 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008847
8848 n = (int)STRLEN(l);
8849 if (terminated == ',' && (*skipwhite(l) == ']'
8850 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008851 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852
8853 /*
8854 * If previous line ends in ',', check whether we
8855 * are in an initialization or enum
8856 * struct xxx =
8857 * {
8858 * sizeof a,
8859 * 124 };
8860 * or a normal possible continuation line.
8861 * but only, of no other statement has been found
8862 * yet.
8863 */
8864 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8865 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008866 if (curbuf->b_ind_js)
8867 {
8868 /* Search for a line ending in a comma
8869 * and line up with the line below it
8870 * (could be the current line).
8871 * some = [
8872 * 1, <- line up here
8873 * 2,
8874 * some = [
8875 * 3 + <- line up here
8876 * 4 *
8877 * 5,
8878 * 6,
8879 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008880 if (cin_iscomment(skipwhite(l)))
8881 break;
8882 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008883 trypos = find_match_char('[',
8884 curbuf->b_ind_maxparen);
8885 if (trypos != NULL)
8886 {
8887 if (trypos->lnum
8888 == curwin->w_cursor.lnum - 1)
8889 {
8890 /* Current line is first inside
8891 * [], line up with it. */
8892 break;
8893 }
8894 ourscope = trypos->lnum;
8895 }
8896 }
8897 else
8898 {
8899 lookfor = LOOKFOR_ENUM_OR_INIT;
8900 cont_amount = cin_first_id_amount();
8901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 }
8903 else
8904 {
8905 if (lookfor == LOOKFOR_INITIAL
8906 && *l != NUL
8907 && l[STRLEN(l) - 1] == '\\')
8908 /* XXX */
8909 cont_amount = cin_get_equal_amount(
8910 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008911 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008912 && lookfor != LOOKFOR_JS_KEY
Bram Moolenaardde81312017-08-26 17:49:01 +02008913 && lookfor != LOOKFOR_COMMA
8914 && raw_string_start != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 lookfor = LOOKFOR_UNTERM;
8916 }
8917 }
8918 }
8919 }
8920
8921 /*
8922 * Check if we are after a while (cond);
8923 * If so: Ignore until the matching "do".
8924 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008925 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 {
8927 /*
8928 * Found an unterminated line after a while ();, line up
8929 * with the last one.
8930 * while (cond);
8931 * 100 + <- line up with this one
8932 * -> here;
8933 */
8934 if (lookfor == LOOKFOR_UNTERM
8935 || lookfor == LOOKFOR_ENUM_OR_INIT)
8936 {
8937 if (cont_amount > 0)
8938 amount = cont_amount;
8939 else
8940 amount += ind_continuation;
8941 break;
8942 }
8943
8944 if (whilelevel == 0)
8945 {
8946 lookfor = LOOKFOR_TERM;
8947 amount = get_indent(); /* XXX */
8948 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008949 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 }
8951 ++whilelevel;
8952 }
8953
8954 /*
8955 * We are after a "normal" statement.
8956 * If we had another statement we can stop now and use the
8957 * indent of that other statement.
8958 * Otherwise the indent of the current statement may be used,
8959 * search backwards for the next "normal" statement.
8960 */
8961 else
8962 {
8963 /*
8964 * Skip single break line, if before a switch label. It
8965 * may be lined up with the case label.
8966 */
8967 if (lookfor == LOOKFOR_NOBREAK
8968 && cin_isbreak(skipwhite(ml_get_curline())))
8969 {
8970 lookfor = LOOKFOR_ANY;
8971 continue;
8972 }
8973
8974 /*
8975 * Handle "do {" line.
8976 */
8977 if (whilelevel > 0)
8978 {
8979 l = cin_skipcomment(ml_get_curline());
8980 if (cin_isdo(l))
8981 {
8982 amount = get_indent(); /* XXX */
8983 --whilelevel;
8984 continue;
8985 }
8986 }
8987
8988 /*
8989 * Found a terminated line above an unterminated line. Add
8990 * the amount for a continuation line.
8991 * x = 1;
8992 * y = foo +
8993 * -> here;
8994 * or
8995 * int x = 1;
8996 * int foo,
8997 * -> here;
8998 */
8999 if (lookfor == LOOKFOR_UNTERM
9000 || lookfor == LOOKFOR_ENUM_OR_INIT)
9001 {
9002 if (cont_amount > 0)
9003 amount = cont_amount;
9004 else
9005 amount += ind_continuation;
9006 break;
9007 }
9008
9009 /*
9010 * Found a terminated line above a terminated line or "if"
9011 * etc. line. Use the amount of the line below us.
9012 * x = 1; x = 1;
9013 * if (asdf) y = 2;
9014 * while (asdf) ->here;
9015 * here;
9016 * ->foo;
9017 */
9018 if (lookfor == LOOKFOR_TERM)
9019 {
9020 if (!lookfor_break && whilelevel == 0)
9021 break;
9022 }
9023
9024 /*
9025 * First line above the one we're indenting is terminated.
9026 * To know what needs to be done look further backward for
9027 * a terminated line.
9028 */
9029 else
9030 {
9031 /*
9032 * position the cursor over the rightmost paren, so
9033 * that matching it will take us back to the start of
9034 * the line. Helps for:
9035 * func(asdr,
9036 * asdfasdf);
9037 * here;
9038 */
9039term_again:
9040 l = ml_get_curline();
9041 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009042 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009043 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044 {
9045 /*
9046 * Check if we are on a case label now. This is
9047 * handled above.
9048 * case xx: if ( asdf &&
9049 * asdf)
9050 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009051 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02009053 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 {
9055 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009056 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 continue;
9058 }
9059 }
9060
9061 /* When aligning with the case statement, don't align
9062 * with a statement after it.
9063 * case 1: { <-- don't use this { position
9064 * stat;
9065 * }
9066 * case 2:
9067 * stat;
9068 * }
9069 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009070 iscase = (curbuf->b_ind_keep_case_label
9071 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072
9073 /*
9074 * Get indent and pointer to text for current line,
9075 * ignoring any jump label.
9076 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009077 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078
9079 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009080 amount += curbuf->b_ind_open_extra;
9081 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00009082 l = skipwhite(l);
9083 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009084 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
9086
9087 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009088 * When a terminated line starts with "else" skip to
9089 * the matching "if":
9090 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009091 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009092 * Need to use the scope of this "else". XXX
9093 * If whilelevel != 0 continue looking for a "do {".
9094 */
9095 if (lookfor == LOOKFOR_TERM
9096 && *l != '}'
9097 && cin_iselse(l)
9098 && whilelevel == 0)
9099 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009100 if ((trypos = find_start_brace()) == NULL
9101 || find_match(LOOKFOR_IF, trypos->lnum)
9102 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009103 break;
9104 continue;
9105 }
9106
9107 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108 * If we're at the end of a block, skip to the start of
9109 * that block.
9110 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01009111 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009112 if (find_last_paren(l, '{', '}') /* XXX */
9113 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009115 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116 /* if not "else {" check for terminated again */
9117 /* but skip block for "} else {" */
9118 l = cin_skipcomment(ml_get_curline());
9119 if (*l == '}' || !cin_iselse(l))
9120 goto term_again;
9121 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009122 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 }
9124 }
9125 }
9126 }
9127 }
9128 }
9129
9130 /* add extra indent for a comment */
9131 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009132 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02009133
9134 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009135 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
9136 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009137
9138 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009140
9141 /*
9142 * ok -- we're not inside any sort of structure at all!
9143 *
9144 * This means we're at the top level, and everything should
9145 * basically just match where the previous line is, except
9146 * for the lines immediately following a function declaration,
9147 * which are K&R-style parameters and need to be indented.
9148 *
9149 * if our line starts with an open brace, forget about any
9150 * prevailing indent and make sure it looks like the start
9151 * of a function
9152 */
9153
9154 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009156 amount = curbuf->b_ind_first_open;
9157 goto theend;
9158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009160 /*
9161 * If the NEXT line is a function declaration, the current
9162 * line needs to be indented as a function type spec.
9163 * Don't do this if the current line looks like a comment or if the
9164 * current line is terminated, ie. ends in ';', or if the current line
9165 * contains { or }: "void f() {\n if (1)"
9166 */
9167 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
9168 && !cin_nocode(theline)
9169 && vim_strchr(theline, '{') == NULL
9170 && vim_strchr(theline, '}') == NULL
9171 && !cin_ends_in(theline, (char_u *)":", NULL)
9172 && !cin_ends_in(theline, (char_u *)",", NULL)
9173 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
9174 cur_curpos.lnum + 1)
9175 && !cin_isterminated(theline, FALSE, TRUE))
9176 {
9177 amount = curbuf->b_ind_func_type;
9178 goto theend;
9179 }
9180
9181 /* search backwards until we find something we recognize */
9182 amount = 0;
9183 curwin->w_cursor = cur_curpos;
9184 while (curwin->w_cursor.lnum > 1)
9185 {
9186 curwin->w_cursor.lnum--;
9187 curwin->w_cursor.col = 0;
9188
9189 l = ml_get_curline();
9190
9191 /*
9192 * If we're in a comment or raw string now, skip to the start
9193 * of it.
9194 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02009195 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009197 curwin->w_cursor.lnum = trypos->lnum + 1;
9198 curwin->w_cursor.col = 0;
9199 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 }
9201
9202 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009203 * Are we at the start of a cpp base class declaration or
9204 * constructor initialization?
9205 */ /* XXX */
9206 n = FALSE;
9207 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009209 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
9210 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009212 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009214 /* XXX */
9215 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
9216 break;
9217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009219 /*
9220 * Skip preprocessor directives and blank lines.
9221 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009222 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009223 continue;
9224
9225 if (cin_nocode(l))
9226 continue;
9227
9228 /*
9229 * If the previous line ends in ',', use one level of
9230 * indentation:
9231 * int foo,
9232 * bar;
9233 * do this before checking for '}' in case of eg.
9234 * enum foobar
9235 * {
9236 * ...
9237 * } foo,
9238 * bar;
9239 */
9240 n = 0;
9241 if (cin_ends_in(l, (char_u *)",", NULL)
9242 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
9243 {
9244 /* take us back to opening paren */
9245 if (find_last_paren(l, '(', ')')
9246 && (trypos = find_match_paren(
9247 curbuf->b_ind_maxparen)) != NULL)
9248 curwin->w_cursor = *trypos;
9249
9250 /* For a line ending in ',' that is a continuation line go
9251 * back to the first line with a backslash:
9252 * char *foo = "bla\
9253 * bla",
9254 * here;
9255 */
9256 while (n == 0 && curwin->w_cursor.lnum > 1)
9257 {
9258 l = ml_get(curwin->w_cursor.lnum - 1);
9259 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
9260 break;
9261 --curwin->w_cursor.lnum;
9262 curwin->w_cursor.col = 0;
9263 }
9264
9265 amount = get_indent(); /* XXX */
9266
9267 if (amount == 0)
9268 amount = cin_first_id_amount();
9269 if (amount == 0)
9270 amount = ind_continuation;
9271 break;
9272 }
9273
9274 /*
9275 * If the line looks like a function declaration, and we're
9276 * not in a comment, put it the left margin.
9277 */
9278 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
9279 break;
9280 l = ml_get_curline();
9281
9282 /*
9283 * Finding the closing '}' of a previous function. Put
9284 * current line at the left margin. For when 'cino' has "fs".
9285 */
9286 if (*skipwhite(l) == '}')
9287 break;
9288
9289 /* (matching {)
9290 * If the previous line ends on '};' (maybe followed by
9291 * comments) align at column 0. For example:
9292 * char *string_array[] = { "foo",
9293 * / * x * / "b};ar" }; / * foobar * /
9294 */
9295 if (cin_ends_in(l, (char_u *)"};", NULL))
9296 break;
9297
9298 /*
9299 * If the previous line ends on '[' we are probably in an
9300 * array constant:
9301 * something = [
9302 * 234, <- extra indent
9303 */
9304 if (cin_ends_in(l, (char_u *)"[", NULL))
9305 {
9306 amount = get_indent() + ind_continuation;
9307 break;
9308 }
9309
9310 /*
9311 * Find a line only has a semicolon that belongs to a previous
9312 * line ending in '}', e.g. before an #endif. Don't increase
9313 * indent then.
9314 */
9315 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
9316 {
9317 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318
9319 while (curwin->w_cursor.lnum > 1)
9320 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009321 look = ml_get(--curwin->w_cursor.lnum);
9322 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009323 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009325 }
9326 if (curwin->w_cursor.lnum > 0
9327 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009330 curwin->w_cursor = curpos_save;
9331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009333 /*
9334 * If the PREVIOUS line is a function declaration, the current
9335 * line (and the ones that follow) needs to be indented as
9336 * parameters.
9337 */
9338 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
9339 {
9340 amount = curbuf->b_ind_param;
9341 break;
9342 }
9343
9344 /*
9345 * If the previous line ends in ';' and the line before the
9346 * previous line ends in ',' or '\', ident to column zero:
9347 * int foo,
9348 * bar;
9349 * indent_to_0 here;
9350 */
9351 if (cin_ends_in(l, (char_u *)";", NULL))
9352 {
9353 l = ml_get(curwin->w_cursor.lnum - 1);
9354 if (cin_ends_in(l, (char_u *)",", NULL)
9355 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9356 break;
9357 l = ml_get_curline();
9358 }
9359
9360 /*
9361 * Doesn't look like anything interesting -- so just
9362 * use the indent of this line.
9363 *
9364 * Position the cursor over the rightmost paren, so that
9365 * matching it will take us back to the start of the line.
9366 */
9367 find_last_paren(l, '(', ')');
9368
9369 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9370 curwin->w_cursor = *trypos;
9371 amount = get_indent(); /* XXX */
9372 break;
9373 }
9374
9375 /* add extra indent for a comment */
9376 if (cin_iscomment(theline))
9377 amount += curbuf->b_ind_comment;
9378
9379 /* add extra indent if the previous line ended in a backslash:
9380 * "asdfasdf\
9381 * here";
9382 * char *foo = "asdf\
9383 * here";
9384 */
9385 if (cur_curpos.lnum > 1)
9386 {
9387 l = ml_get(cur_curpos.lnum - 1);
9388 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9389 {
9390 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9391 if (cur_amount > 0)
9392 amount = cur_amount;
9393 else if (cur_amount == 0)
9394 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 }
9396 }
9397
9398theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009399 if (amount < 0)
9400 amount = 0;
9401
9402laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 /* put the cursor back where it belongs */
9404 curwin->w_cursor = cur_curpos;
9405
9406 vim_free(linecopy);
9407
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 return amount;
9409}
9410
9411 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009412find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413{
9414 char_u *look;
9415 pos_T *theirscope;
9416 char_u *mightbeif;
9417 int elselevel;
9418 int whilelevel;
9419
9420 if (lookfor == LOOKFOR_IF)
9421 {
9422 elselevel = 1;
9423 whilelevel = 0;
9424 }
9425 else
9426 {
9427 elselevel = 0;
9428 whilelevel = 1;
9429 }
9430
9431 curwin->w_cursor.col = 0;
9432
9433 while (curwin->w_cursor.lnum > ourscope + 1)
9434 {
9435 curwin->w_cursor.lnum--;
9436 curwin->w_cursor.col = 0;
9437
9438 look = cin_skipcomment(ml_get_curline());
9439 if (cin_iselse(look)
9440 || cin_isif(look)
9441 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009442 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 {
9444 /*
9445 * if we've gone outside the braces entirely,
9446 * we must be out of scope...
9447 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009448 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 if (theirscope == NULL)
9450 break;
9451
9452 /*
9453 * and if the brace enclosing this is further
9454 * back than the one enclosing the else, we're
9455 * out of luck too.
9456 */
9457 if (theirscope->lnum < ourscope)
9458 break;
9459
9460 /*
9461 * and if they're enclosed in a *deeper* brace,
9462 * then we can ignore it because it's in a
9463 * different scope...
9464 */
9465 if (theirscope->lnum > ourscope)
9466 continue;
9467
9468 /*
9469 * if it was an "else" (that's not an "else if")
9470 * then we need to go back to another if, so
9471 * increment elselevel
9472 */
9473 look = cin_skipcomment(ml_get_curline());
9474 if (cin_iselse(look))
9475 {
9476 mightbeif = cin_skipcomment(look + 4);
9477 if (!cin_isif(mightbeif))
9478 ++elselevel;
9479 continue;
9480 }
9481
9482 /*
9483 * if it was a "while" then we need to go back to
9484 * another "do", so increment whilelevel. XXX
9485 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009486 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 {
9488 ++whilelevel;
9489 continue;
9490 }
9491
9492 /* If it's an "if" decrement elselevel */
9493 look = cin_skipcomment(ml_get_curline());
9494 if (cin_isif(look))
9495 {
9496 elselevel--;
9497 /*
9498 * When looking for an "if" ignore "while"s that
9499 * get in the way.
9500 */
9501 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9502 whilelevel = 0;
9503 }
9504
9505 /* If it's a "do" decrement whilelevel */
9506 if (cin_isdo(look))
9507 whilelevel--;
9508
9509 /*
9510 * if we've used up all the elses, then
9511 * this must be the if that we want!
9512 * match the indent level of that if.
9513 */
9514 if (elselevel <= 0 && whilelevel <= 0)
9515 {
9516 return OK;
9517 }
9518 }
9519 }
9520 return FAIL;
9521}
9522
9523# if defined(FEAT_EVAL) || defined(PROTO)
9524/*
9525 * Get indent level from 'indentexpr'.
9526 */
9527 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009528get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529{
Bram Moolenaar97db5542017-04-21 23:18:26 +02009530 int indent = -1;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009531 char_u *inde_copy;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009532 pos_T save_pos;
9533 colnr_T save_curswant;
9534 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009536 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9537 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009539 /* Save and restore cursor position and curswant, in case it was changed
9540 * via :normal commands */
9541 save_pos = curwin->w_cursor;
9542 save_curswant = curwin->w_curswant;
9543 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009545 if (use_sandbox)
9546 ++sandbox;
9547 ++textlock;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009548
9549 /* Need to make a copy, the 'indentexpr' option could be changed while
9550 * evaluating it. */
9551 inde_copy = vim_strsave(curbuf->b_p_inde);
9552 if (inde_copy != NULL)
9553 {
9554 indent = (int)eval_to_number(inde_copy);
9555 vim_free(inde_copy);
9556 }
9557
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009558 if (use_sandbox)
9559 --sandbox;
9560 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561
9562 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9563 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9564 * command. */
9565 save_State = State;
9566 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009567 curwin->w_cursor = save_pos;
9568 curwin->w_curswant = save_curswant;
9569 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 check_cursor();
9571 State = save_State;
9572
9573 /* If there is an error, just keep the current indent. */
9574 if (indent < 0)
9575 indent = get_indent();
9576
9577 return indent;
9578}
9579# endif
9580
9581#endif /* FEAT_CINDENT */
9582
9583#if defined(FEAT_LISP) || defined(PROTO)
9584
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009586lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587{
9588 char_u buf[LSIZE];
9589 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009590 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591
9592 while (*word != NUL)
9593 {
9594 (void)copy_option_part(&word, buf, LSIZE, ",");
9595 len = (int)STRLEN(buf);
9596 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9597 return TRUE;
9598 }
9599 return FALSE;
9600}
9601
9602/*
9603 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9604 * The incompatible newer method is quite a bit better at indenting
9605 * code in lisp-like languages than the traditional one; it's still
9606 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9607 *
9608 * TODO:
9609 * Findmatch() should be adapted for lisp, also to make showmatch
9610 * work correctly: now (v5.3) it seems all C/C++ oriented:
9611 * - it does not recognize the #\( and #\) notations as character literals
9612 * - it doesn't know about comments starting with a semicolon
9613 * - it incorrectly interprets '(' as a character literal
9614 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009615 * Update from Sergey Khorev:
9616 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 */
9618 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009619get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009621 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 int amount;
9623 char_u *that;
9624 colnr_T col;
9625 colnr_T firsttry;
9626 int parencount, quotecount;
9627 int vi_lisp;
9628
9629 /* Set vi_lisp to use the vi-compatible method */
9630 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9631
9632 realpos = curwin->w_cursor;
9633 curwin->w_cursor.col = 0;
9634
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009635 if ((pos = findmatch(NULL, '(')) == NULL)
9636 pos = findmatch(NULL, '[');
9637 else
9638 {
9639 paren = *pos;
9640 pos = findmatch(NULL, '[');
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009641 if (pos == NULL || LT_POSP(pos, &paren))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009642 pos = &paren;
9643 }
9644 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 {
9646 /* Extra trick: Take the indent of the first previous non-white
9647 * line that is at the same () level. */
9648 amount = -1;
9649 parencount = 0;
9650
9651 while (--curwin->w_cursor.lnum >= pos->lnum)
9652 {
9653 if (linewhite(curwin->w_cursor.lnum))
9654 continue;
9655 for (that = ml_get_curline(); *that != NUL; ++that)
9656 {
9657 if (*that == ';')
9658 {
9659 while (*(that + 1) != NUL)
9660 ++that;
9661 continue;
9662 }
9663 if (*that == '\\')
9664 {
9665 if (*(that + 1) != NUL)
9666 ++that;
9667 continue;
9668 }
9669 if (*that == '"' && *(that + 1) != NUL)
9670 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009671 while (*++that && *that != '"')
9672 {
9673 /* skipping escaped characters in the string */
9674 if (*that == '\\')
9675 {
9676 if (*++that == NUL)
9677 break;
9678 if (that[1] == NUL)
9679 {
9680 ++that;
9681 break;
9682 }
9683 }
9684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009686 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009688 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 --parencount;
9690 }
9691 if (parencount == 0)
9692 {
9693 amount = get_indent();
9694 break;
9695 }
9696 }
9697
9698 if (amount == -1)
9699 {
9700 curwin->w_cursor.lnum = pos->lnum;
9701 curwin->w_cursor.col = pos->col;
9702 col = pos->col;
9703
9704 that = ml_get_curline();
9705
9706 if (vi_lisp && get_indent() == 0)
9707 amount = 2;
9708 else
9709 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009710 char_u *line = that;
9711
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 amount = 0;
9713 while (*that && col)
9714 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009715 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 col--;
9717 }
9718
9719 /*
9720 * Some keywords require "body" indenting rules (the
9721 * non-standard-lisp ones are Scheme special forms):
9722 *
9723 * (let ((a 1)) instead (let ((a 1))
9724 * (...)) of (...))
9725 */
9726
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009727 if (!vi_lisp && (*that == '(' || *that == '[')
9728 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 amount += 2;
9730 else
9731 {
9732 that++;
9733 amount++;
9734 firsttry = amount;
9735
Bram Moolenaar1c465442017-03-12 20:10:05 +01009736 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009738 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 ++that;
9740 }
9741
9742 if (*that && *that != ';') /* not a comment line */
9743 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009744 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009746 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 firsttry++;
9748
9749 parencount = 0;
9750 quotecount = 0;
9751
9752 if (vi_lisp
9753 || (*that != '"'
9754 && *that != '\''
9755 && *that != '#'
9756 && (*that < '0' || *that > '9')))
9757 {
9758 while (*that
Bram Moolenaar1c465442017-03-12 20:10:05 +01009759 && (!VIM_ISWHITE(*that)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 || quotecount
9761 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009762 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 && !quotecount
9764 && !parencount
9765 && vi_lisp)))
9766 {
9767 if (*that == '"')
9768 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009769 if ((*that == '(' || *that == '[')
9770 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009772 if ((*that == ')' || *that == ']')
9773 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 --parencount;
9775 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009776 amount += lbr_chartabsize_adv(
9777 line, &that, (colnr_T)amount);
9778 amount += lbr_chartabsize_adv(
9779 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 }
9781 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01009782 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009784 amount += lbr_chartabsize(
9785 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 that++;
9787 }
9788 if (!*that || *that == ';')
9789 amount = firsttry;
9790 }
9791 }
9792 }
9793 }
9794 }
9795 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009796 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797
9798 curwin->w_cursor = realpos;
9799
9800 return amount;
9801}
9802#endif /* FEAT_LISP */
9803
9804 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009805prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009807#if defined(SIGHUP) && defined(SIG_IGN)
9808 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9809 * makes Vim exit and then handling SIGHUP causes various reentrance
9810 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009811 signal(SIGHUP, SIG_IGN);
9812#endif
9813
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814#ifdef FEAT_GUI
9815 if (gui.in_use)
9816 {
9817 gui.dying = TRUE;
9818 out_trash(); /* trash any pending output */
9819 }
9820 else
9821#endif
9822 {
9823 windgoto((int)Rows - 1, 0);
9824
9825 /*
9826 * Switch terminal mode back now, so messages end up on the "normal"
9827 * screen (if there are two screens).
9828 */
9829 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009830 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 out_flush();
9832 }
9833}
9834
9835/*
9836 * Preserve files and exit.
9837 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009838 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9839 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840 */
9841 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009842preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843{
9844 buf_T *buf;
9845
9846 prepare_to_exit();
9847
Bram Moolenaar4770d092006-01-12 23:22:24 +00009848 /* Setting this will prevent free() calls. That avoids calling free()
9849 * recursively when free() was invoked with a bad pointer. */
9850 really_exiting = TRUE;
9851
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 out_str(IObuff);
9853 screen_start(); /* don't know where cursor is now */
9854 out_flush();
9855
9856 ml_close_notmod(); /* close all not-modified buffers */
9857
Bram Moolenaar29323592016-07-24 22:04:11 +02009858 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859 {
9860 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9861 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009862 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 screen_start(); /* don't know where cursor is now */
9864 out_flush();
9865 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9866 break;
9867 }
9868 }
9869
9870 ml_close_all(FALSE); /* close all memfiles, without deleting */
9871
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009872 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873
9874 getout(1);
9875}
9876
9877/*
9878 * return TRUE if "fname" exists.
9879 */
9880 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009881vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009883 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884
9885 if (mch_stat((char *)fname, &st))
9886 return FALSE;
9887 return TRUE;
9888}
9889
9890/*
9891 * Check for CTRL-C pressed, but only once in a while.
9892 * Should be used instead of ui_breakcheck() for functions that check for
9893 * each line in the file. Calling ui_breakcheck() each time takes too much
9894 * time, because it can be a system call.
9895 */
9896
9897#ifndef BREAKCHECK_SKIP
9898# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9899# define BREAKCHECK_SKIP 200
9900# else
9901# define BREAKCHECK_SKIP 32
9902# endif
9903#endif
9904
9905static int breakcheck_count = 0;
9906
9907 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009908line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909{
9910 if (++breakcheck_count >= BREAKCHECK_SKIP)
9911 {
9912 breakcheck_count = 0;
9913 ui_breakcheck();
9914 }
9915}
9916
9917/*
9918 * Like line_breakcheck() but check 10 times less often.
9919 */
9920 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009921fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922{
9923 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9924 {
9925 breakcheck_count = 0;
9926 ui_breakcheck();
9927 }
9928}
9929
9930/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009931 * Invoke expand_wildcards() for one pattern.
9932 * Expand items like "%:h" before the expansion.
9933 * Returns OK or FAIL.
9934 */
9935 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009936expand_wildcards_eval(
9937 char_u **pat, /* pointer to input pattern */
9938 int *num_file, /* resulting number of files */
9939 char_u ***file, /* array of resulting files */
9940 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009941{
9942 int ret = FAIL;
9943 char_u *eval_pat = NULL;
9944 char_u *exp_pat = *pat;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009945 char *ignored_msg;
Bram Moolenaard7834d32009-12-02 16:14:36 +00009946 int usedlen;
9947
9948 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9949 {
9950 ++emsg_off;
9951 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9952 NULL, &ignored_msg, NULL);
9953 --emsg_off;
9954 if (eval_pat != NULL)
9955 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9956 }
9957
9958 if (exp_pat != NULL)
9959 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9960
9961 if (eval_pat != NULL)
9962 {
9963 vim_free(exp_pat);
9964 vim_free(eval_pat);
9965 }
9966
9967 return ret;
9968}
9969
9970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9972 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009973 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 */
9975 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009976expand_wildcards(
9977 int num_pat, /* number of input patterns */
9978 char_u **pat, /* array of input patterns */
9979 int *num_files, /* resulting number of files */
9980 char_u ***files, /* array of resulting files */
9981 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982{
9983 int retval;
9984 int i, j;
9985 char_u *p;
9986 int non_suf_match; /* number without matching suffix */
9987
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009988 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989
9990 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009991 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 return retval;
9993
9994#ifdef FEAT_WILDIGN
9995 /*
9996 * Remove names that match 'wildignore'.
9997 */
9998 if (*p_wig)
9999 {
10000 char_u *ffname;
10001
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010002 /* check all files in (*files)[] */
10003 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010005 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 if (ffname == NULL) /* out of memory */
10007 break;
10008# ifdef VMS
10009 vms_remove_version(ffname);
10010# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010011 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010013 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010014 vim_free((*files)[i]);
10015 for (j = i; j + 1 < *num_files; ++j)
10016 (*files)[j] = (*files)[j + 1];
10017 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018 --i;
10019 }
10020 vim_free(ffname);
10021 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010022
10023 /* If the number of matches is now zero, we fail. */
10024 if (*num_files == 0)
10025 {
Bram Moolenaard23a8232018-02-10 18:45:26 +010010026 VIM_CLEAR(*files);
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010027 return FAIL;
10028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 }
10030#endif
10031
10032 /*
10033 * Move the names where 'suffixes' match to the end.
10034 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010035 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 {
10037 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010038 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010040 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041 {
10042 /*
10043 * Move the name without matching suffix to the front
10044 * of the list.
10045 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010046 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010048 (*files)[j] = (*files)[j - 1];
10049 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 }
10051 }
10052 }
10053
10054 return retval;
10055}
10056
10057/*
10058 * Return TRUE if "fname" matches with an entry in 'suffixes'.
10059 */
10060 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010061match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062{
10063 int fnamelen, setsuflen;
10064 char_u *setsuf;
10065#define MAXSUFLEN 30 /* maximum length of a file suffix */
10066 char_u suf_buf[MAXSUFLEN];
10067
10068 fnamelen = (int)STRLEN(fname);
10069 setsuflen = 0;
10070 for (setsuf = p_su; *setsuf; )
10071 {
10072 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +000010073 if (setsuflen == 0)
10074 {
10075 char_u *tail = gettail(fname);
10076
10077 /* empty entry: match name without a '.' */
10078 if (vim_strchr(tail, '.') == NULL)
10079 {
10080 setsuflen = 1;
10081 break;
10082 }
10083 }
10084 else
10085 {
10086 if (fnamelen >= setsuflen
10087 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
10088 (size_t)setsuflen) == 0)
10089 break;
10090 setsuflen = 0;
10091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 }
10093 return (setsuflen != 0);
10094}
10095
10096#if !defined(NO_EXPANDPATH) || defined(PROTO)
10097
10098# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010099static int vim_backtick(char_u *p);
10100static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101# endif
10102
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010103# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104/*
10105 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
10106 * it's shared between these systems.
10107 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010108# if defined(PROTO)
10109# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110# else
10111# ifdef __BORLANDC__
10112# define _cdecl _RTLENTRYF
10113# endif
10114# endif
10115
10116/*
10117 * comparison function for qsort in dos_expandpath()
10118 */
10119 static int _cdecl
10120pstrcmp(const void *a, const void *b)
10121{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010122 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123}
10124
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125/*
Bram Moolenaar231334e2005-07-25 20:46:57 +000010126 * Recursively expand one path component into all matching files and/or
10127 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 * Return the number of matches found.
10129 * "path" has backslashes before chars that are not to be expanded, starting
10130 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +000010131 * Return the number of matches found.
10132 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133 */
10134 static int
10135dos_expandpath(
10136 garray_T *gap,
10137 char_u *path,
10138 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +000010139 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +000010140 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010142 char_u *buf;
10143 char_u *path_end;
10144 char_u *p, *s, *e;
10145 int start_len = gap->ga_len;
10146 char_u *pat;
10147 regmatch_T regmatch;
10148 int starts_with_dot;
10149 int matches;
10150 int len;
10151 int starstar = FALSE;
10152 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 WIN32_FIND_DATA fb;
10154 HANDLE hFind = (HANDLE)0;
10155# ifdef FEAT_MBYTE
10156 WIN32_FIND_DATAW wfb;
10157 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
10158# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010160 int ok;
10161
10162 /* Expanding "**" may take a long time, check for CTRL-C. */
10163 if (stardepth > 0)
10164 {
10165 ui_breakcheck();
10166 if (got_int)
10167 return 0;
10168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169
Bram Moolenaar7314efd2015-10-31 15:32:52 +010010170 /* Make room for file name. When doing encoding conversion the actual
10171 * length may be quite a bit longer, thus use the maximum possible length. */
10172 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173 if (buf == NULL)
10174 return 0;
10175
10176 /*
10177 * Find the first part in the path name that contains a wildcard or a ~1.
10178 * Copy it into buf, including the preceding characters.
10179 */
10180 p = buf;
10181 s = buf;
10182 e = NULL;
10183 path_end = path;
10184 while (*path_end != NUL)
10185 {
10186 /* May ignore a wildcard that has a backslash before it; it will
10187 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10188 if (path_end >= path + wildoff && rem_backslash(path_end))
10189 *p++ = *path_end++;
10190 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
10191 {
10192 if (e != NULL)
10193 break;
10194 s = p + 1;
10195 }
10196 else if (path_end >= path + wildoff
10197 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
10198 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +010010199# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 if (has_mbyte)
10201 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010202 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 STRNCPY(p, path_end, len);
10204 p += len;
10205 path_end += len;
10206 }
10207 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +010010208# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 *p++ = *path_end++;
10210 }
10211 e = p;
10212 *e = NUL;
10213
10214 /* now we have one wildcard component between s and e */
10215 /* Remove backslashes between "wildoff" and the start of the wildcard
10216 * component. */
10217 for (p = buf + wildoff; p < s; ++p)
10218 if (rem_backslash(p))
10219 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010220 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 --e;
10222 --s;
10223 }
10224
Bram Moolenaar231334e2005-07-25 20:46:57 +000010225 /* Check for "**" between "s" and "e". */
10226 for (p = s; p < e; ++p)
10227 if (p[0] == '*' && p[1] == '*')
10228 starstar = TRUE;
10229
Bram Moolenaard82103e2016-01-17 17:04:05 +010010230 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10232 if (pat == NULL)
10233 {
10234 vim_free(buf);
10235 return 0;
10236 }
10237
10238 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010239 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010240 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 regmatch.rm_ic = TRUE; /* Always ignore case */
10242 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010243 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010244 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245 vim_free(pat);
10246
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010247 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248 {
10249 vim_free(buf);
10250 return 0;
10251 }
10252
10253 /* remember the pattern or file name being looked for */
10254 matchname = vim_strsave(s);
10255
Bram Moolenaar231334e2005-07-25 20:46:57 +000010256 /* If "**" is by itself, this is the first time we encounter it and more
10257 * is following then find matches without any directory. */
10258 if (!didstar && stardepth < 100 && starstar && e - s == 2
10259 && *path_end == '/')
10260 {
10261 STRCPY(s, path_end + 1);
10262 ++stardepth;
10263 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10264 --stardepth;
10265 }
10266
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 /* Scan all files in the directory with "dir/ *.*" */
10268 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269# ifdef FEAT_MBYTE
10270 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
10271 {
10272 /* The active codepage differs from 'encoding'. Attempt using the
10273 * wide function. If it fails because it is not implemented fall back
10274 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010275 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276 if (wn != NULL)
10277 {
10278 hFind = FindFirstFileW(wn, &wfb);
10279 if (hFind == INVALID_HANDLE_VALUE
10280 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
Bram Moolenaard23a8232018-02-10 18:45:26 +010010281 VIM_CLEAR(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282 }
10283 }
10284
10285 if (wn == NULL)
10286# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010287 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289
10290 while (ok)
10291 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292# ifdef FEAT_MBYTE
10293 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010294 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295 else
10296# endif
10297 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298 /* Ignore entries starting with a dot, unless when asked for. Accept
10299 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010300 if ((p[0] != '.' || starts_with_dot
10301 || ((flags & EW_DODOT)
10302 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010304 || (regmatch.regprog != NULL
10305 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010306 || ((flags & EW_NOTWILD)
10307 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010311
10312 if (starstar && stardepth < 100)
10313 {
10314 /* For "**" in the pattern first go deeper in the tree to
10315 * find matches. */
10316 STRCPY(buf + len, "/**");
10317 STRCPY(buf + len + 3, path_end);
10318 ++stardepth;
10319 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
10320 --stardepth;
10321 }
10322
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 STRCPY(buf + len, path_end);
10324 if (mch_has_exp_wildcard(path_end))
10325 {
10326 /* need to expand another component of the path */
10327 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010328 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329 }
10330 else
10331 {
10332 /* no more wildcards, check if there is a match */
10333 /* remove backslashes for the remaining components only */
10334 if (*path_end != 0)
10335 backslash_halve(buf + len + 1);
10336 if (mch_getperm(buf) >= 0) /* add existing file */
10337 addfile(gap, buf, flags);
10338 }
10339 }
10340
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341# ifdef FEAT_MBYTE
10342 if (wn != NULL)
10343 {
10344 vim_free(p);
10345 ok = FindNextFileW(hFind, &wfb);
10346 }
10347 else
10348# endif
10349 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350
10351 /* If no more matches and no match was used, try expanding the name
10352 * itself. Finds the long name of a short filename. */
10353 if (!ok && matchname != NULL && gap->ga_len == start_len)
10354 {
10355 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 FindClose(hFind);
10357# ifdef FEAT_MBYTE
10358 if (wn != NULL)
10359 {
10360 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010361 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 if (wn != NULL)
10363 hFind = FindFirstFileW(wn, &wfb);
10364 }
10365 if (wn == NULL)
10366# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010367 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaard23a8232018-02-10 18:45:26 +010010369 VIM_CLEAR(matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 }
10371 }
10372
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373 FindClose(hFind);
10374# ifdef FEAT_MBYTE
10375 vim_free(wn);
10376# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010377 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010378 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379 vim_free(matchname);
10380
10381 matches = gap->ga_len - start_len;
10382 if (matches > 0)
10383 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10384 sizeof(char_u *), pstrcmp);
10385 return matches;
10386}
10387
10388 int
10389mch_expandpath(
10390 garray_T *gap,
10391 char_u *path,
10392 int flags) /* EW_* flags */
10393{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010394 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010396# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397
Bram Moolenaar231334e2005-07-25 20:46:57 +000010398#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10399 || defined(PROTO)
10400/*
10401 * Unix style wildcard expansion code.
10402 * It's here because it's used both for Unix and Mac.
10403 */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010404 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010405pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010406{
10407 return (pathcmp(*(char **)a, *(char **)b, -1));
10408}
10409
10410/*
10411 * Recursively expand one path component into all matching files and/or
10412 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10413 * "path" has backslashes before chars that are not to be expanded, starting
10414 * at "path + wildoff".
10415 * Return the number of matches found.
10416 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10417 */
10418 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010419unix_expandpath(
10420 garray_T *gap,
10421 char_u *path,
10422 int wildoff,
10423 int flags, /* EW_* flags */
10424 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010425{
10426 char_u *buf;
10427 char_u *path_end;
10428 char_u *p, *s, *e;
10429 int start_len = gap->ga_len;
10430 char_u *pat;
10431 regmatch_T regmatch;
10432 int starts_with_dot;
10433 int matches;
10434 int len;
10435 int starstar = FALSE;
10436 static int stardepth = 0; /* depth for "**" expansion */
10437
10438 DIR *dirp;
10439 struct dirent *dp;
10440
10441 /* Expanding "**" may take a long time, check for CTRL-C. */
10442 if (stardepth > 0)
10443 {
10444 ui_breakcheck();
10445 if (got_int)
10446 return 0;
10447 }
10448
10449 /* make room for file name */
10450 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10451 if (buf == NULL)
10452 return 0;
10453
10454 /*
10455 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010456 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010457 * Copy it into "buf", including the preceding characters.
10458 */
10459 p = buf;
10460 s = buf;
10461 e = NULL;
10462 path_end = path;
10463 while (*path_end != NUL)
10464 {
10465 /* May ignore a wildcard that has a backslash before it; it will
10466 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10467 if (path_end >= path + wildoff && rem_backslash(path_end))
10468 *p++ = *path_end++;
10469 else if (*path_end == '/')
10470 {
10471 if (e != NULL)
10472 break;
10473 s = p + 1;
10474 }
10475 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010476 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010477 || (!p_fic && (flags & EW_ICASE)
10478 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010479 e = p;
10480#ifdef FEAT_MBYTE
10481 if (has_mbyte)
10482 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010483 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010484 STRNCPY(p, path_end, len);
10485 p += len;
10486 path_end += len;
10487 }
10488 else
10489#endif
10490 *p++ = *path_end++;
10491 }
10492 e = p;
10493 *e = NUL;
10494
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010495 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010496 /* Remove backslashes between "wildoff" and the start of the wildcard
10497 * component. */
10498 for (p = buf + wildoff; p < s; ++p)
10499 if (rem_backslash(p))
10500 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010501 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010502 --e;
10503 --s;
10504 }
10505
10506 /* Check for "**" between "s" and "e". */
10507 for (p = s; p < e; ++p)
10508 if (p[0] == '*' && p[1] == '*')
10509 starstar = TRUE;
10510
10511 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010512 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010513 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10514 if (pat == NULL)
10515 {
10516 vim_free(buf);
10517 return 0;
10518 }
10519
10520 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010521 if (flags & EW_ICASE)
10522 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10523 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010524 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010525 if (flags & (EW_NOERROR | EW_NOTWILD))
10526 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010527 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
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 vim_free(pat);
10531
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010532 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010533 {
10534 vim_free(buf);
10535 return 0;
10536 }
10537
10538 /* If "**" is by itself, this is the first time we encounter it and more
10539 * is following then find matches without any directory. */
10540 if (!didstar && stardepth < 100 && starstar && e - s == 2
10541 && *path_end == '/')
10542 {
10543 STRCPY(s, path_end + 1);
10544 ++stardepth;
10545 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10546 --stardepth;
10547 }
10548
10549 /* open the directory for scanning */
10550 *s = NUL;
10551 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10552
10553 /* Find all matching entries */
10554 if (dirp != NULL)
10555 {
10556 for (;;)
10557 {
10558 dp = readdir(dirp);
10559 if (dp == NULL)
10560 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010561 if ((dp->d_name[0] != '.' || starts_with_dot
10562 || ((flags & EW_DODOT)
10563 && dp->d_name[1] != NUL
10564 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010565 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10566 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010567 || ((flags & EW_NOTWILD)
10568 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010569 {
10570 STRCPY(s, dp->d_name);
10571 len = STRLEN(buf);
10572
10573 if (starstar && stardepth < 100)
10574 {
10575 /* For "**" in the pattern first go deeper in the tree to
10576 * find matches. */
10577 STRCPY(buf + len, "/**");
10578 STRCPY(buf + len + 3, path_end);
10579 ++stardepth;
10580 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10581 --stardepth;
10582 }
10583
10584 STRCPY(buf + len, path_end);
10585 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10586 {
10587 /* need to expand another component of the path */
10588 /* remove backslashes for the remaining components only */
10589 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10590 }
10591 else
10592 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010593 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010594
Bram Moolenaar231334e2005-07-25 20:46:57 +000010595 /* no more wildcards, check if there is a match */
10596 /* remove backslashes for the remaining components only */
10597 if (*path_end != NUL)
10598 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010599 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010600 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010601 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010602 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010603#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010604 size_t precomp_len = STRLEN(buf)+1;
10605 char_u *precomp_buf =
10606 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010607
Bram Moolenaar231334e2005-07-25 20:46:57 +000010608 if (precomp_buf)
10609 {
10610 mch_memmove(buf, precomp_buf, precomp_len);
10611 vim_free(precomp_buf);
10612 }
10613#endif
10614 addfile(gap, buf, flags);
10615 }
10616 }
10617 }
10618 }
10619
10620 closedir(dirp);
10621 }
10622
10623 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010624 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010625
10626 matches = gap->ga_len - start_len;
10627 if (matches > 0)
10628 qsort(((char_u **)gap->ga_data) + start_len, matches,
10629 sizeof(char_u *), pstrcmp);
10630 return matches;
10631}
10632#endif
10633
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010634#if defined(FEAT_SEARCHPATH)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010635/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010636 * Moves "*psep" back to the previous path separator in "path".
10637 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010638 */
10639 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010640find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010641{
10642 /* skip the current separator */
10643 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010644 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010645
10646 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010647 while (*psep > path)
10648 {
10649 if (vim_ispathsep(**psep))
10650 return OK;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010651 MB_PTR_BACK(path, *psep);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010652 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010653
10654 return FAIL;
10655}
10656
10657/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010658 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10659 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010660 */
10661 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010662is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010663{
10664 int j;
10665 int candidate_len;
10666 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010667 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010668 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010669
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010670 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010671 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010672 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010673 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010674
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010675 candidate_len = (int)STRLEN(maybe_unique);
10676 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010677 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010678 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010679
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010680 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010681 if (fnamecmp(maybe_unique, rival) == 0
10682 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010683 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010684 }
10685
Bram Moolenaar162bd912010-07-28 22:29:10 +020010686 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010687}
10688
10689/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010690 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010691 * paths are expanded to their equivalent fullpath. This includes the "."
10692 * (relative to current buffer directory) and empty path (relative to current
10693 * directory) notations.
10694 *
10695 * TODO: handle upward search (;) and path limiter (**N) notations by
10696 * expanding each into their equivalent path(s).
10697 */
10698 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010699expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010700{
10701 char_u *path_option = *curbuf->b_p_path == NUL
10702 ? p_path : curbuf->b_p_path;
10703 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010704 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010705 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010706
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010707 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010708 return;
10709
10710 while (*path_option != NUL)
10711 {
10712 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10713
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010714 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010715 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010716 /* Relative to current buffer:
10717 * "/path/file" + "." -> "/path/"
10718 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010719 if (curbuf->b_ffname == NULL)
10720 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010721 p = gettail(curbuf->b_ffname);
10722 len = (int)(p - curbuf->b_ffname);
10723 if (len + (int)STRLEN(buf) >= MAXPATHL)
10724 continue;
10725 if (buf[1] == NUL)
10726 buf[len] = NUL;
10727 else
10728 STRMOVE(buf + len, buf + 2);
10729 mch_memmove(buf, curbuf->b_ffname, len);
10730 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010731 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010732 else if (buf[0] == NUL)
10733 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010734 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010735 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010736 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010737 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010738 else if (!mch_isFullName(buf))
10739 {
10740 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010741 len = (int)STRLEN(curdir);
10742 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010743 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010744 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010745 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010746 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010747 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010748 }
10749
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010750 if (ga_grow(gap, 1) == FAIL)
10751 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010752
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010753# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010754 /* Avoid the path ending in a backslash, it fails when a comma is
10755 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010756 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010757 if (buf[len - 1] == '\\')
10758 buf[len - 1] = '/';
10759# endif
10760
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010761 p = vim_strsave(buf);
10762 if (p == NULL)
10763 break;
10764 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010765 }
10766
10767 vim_free(buf);
10768}
10769
10770/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010771 * Returns a pointer to the file or directory name in "fname" that matches the
10772 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010773 *
10774 * path: /foo/bar/baz
10775 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010776 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010777 */
10778 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010779get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010780{
10781 int i;
10782 int maxlen = 0;
10783 char_u **path_part = (char_u **)gap->ga_data;
10784 char_u *cutoff = NULL;
10785
10786 for (i = 0; i < gap->ga_len; i++)
10787 {
10788 int j = 0;
10789
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010790 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010791# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010792 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10793#endif
10794 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010795 j++;
10796 if (j > maxlen)
10797 {
10798 maxlen = j;
10799 cutoff = &fname[j];
10800 }
10801 }
10802
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010803 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010804 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010805 while (vim_ispathsep(*cutoff))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010806 MB_PTR_ADV(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010807
10808 return cutoff;
10809}
10810
10811/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010812 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10813 * that they are unique with respect to each other while conserving the part
10814 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010815 */
10816 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010817uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010818{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010819 int i;
10820 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010821 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010822 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010823 char_u *pat;
10824 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010825 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010826 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010827 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010828 char_u **in_curdir = NULL;
10829 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010830
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010831 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010832 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010833
10834 /*
10835 * We need to prepend a '*' at the beginning of file_pattern so that the
10836 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010837 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010838 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010839 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010840 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010841 if (file_pattern == NULL)
10842 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010843 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010844 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010845 STRCAT(file_pattern, pattern);
10846 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10847 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010848 if (pat == NULL)
10849 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010850
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010851 regmatch.rm_ic = TRUE; /* always ignore case */
10852 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10853 vim_free(pat);
10854 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010855 return;
10856
Bram Moolenaar162bd912010-07-28 22:29:10 +020010857 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010858 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010859 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010860 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010861
10862 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010863 if (in_curdir == NULL)
10864 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010865
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010866 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010867 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010868 char_u *path = fnames[i];
10869 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010870 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010871 char_u *pathsep_p;
10872 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010873
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010874 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010875 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010876 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010877 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010878 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010879
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010880 /* Shorten the filename while maintaining its uniqueness */
10881 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010882
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010883 /* Don't assume all files can be reached without path when search
10884 * pattern starts with star star slash, so only remove path_cutoff
10885 * when possible. */
10886 if (pattern[0] == '*' && pattern[1] == '*'
10887 && vim_ispathsep_nocolon(pattern[2])
10888 && path_cutoff != NULL
10889 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10890 && is_unique(path_cutoff, gap, i))
10891 {
10892 sort_again = TRUE;
10893 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10894 }
10895 else
10896 {
10897 /* Here all files can be reached without path, so get shortest
10898 * unique path. We start at the end of the path. */
10899 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010900
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010901 while (find_previous_pathsep(path, &pathsep_p))
10902 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10903 && is_unique(pathsep_p + 1, gap, i)
10904 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10905 {
10906 sort_again = TRUE;
10907 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10908 break;
10909 }
10910 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010911
10912 if (mch_isFullName(path))
10913 {
10914 /*
10915 * Last resort: shorten relative to curdir if possible.
10916 * 'possible' means:
10917 * 1. It is under the current directory.
10918 * 2. The result is actually shorter than the original.
10919 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010920 * Before curdir After
10921 * /foo/bar/file.txt /foo/bar ./file.txt
10922 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10923 * /file.txt / /file.txt
10924 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010925 */
10926 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010927 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010928#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010929 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010930 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010931 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010932 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010933 && !vim_ispathsep(*short_name)
10934#endif
10935 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010936 {
10937 STRCPY(path, ".");
10938 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010939 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010940 }
10941 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010942 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010943 }
10944
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010945 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010946 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010947 {
10948 char_u *rel_path;
10949 char_u *path = in_curdir[i];
10950
10951 if (path == NULL)
10952 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010953
10954 /* If the {filename} is not unique, change it to ./{filename}.
10955 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010956 short_name = shorten_fname(path, curdir);
10957 if (short_name == NULL)
10958 short_name = path;
10959 if (is_unique(short_name, gap, i))
10960 {
10961 STRCPY(fnames[i], short_name);
10962 continue;
10963 }
10964
10965 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10966 if (rel_path == NULL)
10967 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010968 STRCPY(rel_path, ".");
10969 add_pathsep(rel_path);
10970 STRCAT(rel_path, short_name);
10971
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010972 vim_free(fnames[i]);
10973 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010974 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010975 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010976 }
10977
Bram Moolenaar162bd912010-07-28 22:29:10 +020010978theend:
10979 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010980 if (in_curdir != NULL)
10981 {
10982 for (i = 0; i < gap->ga_len; i++)
10983 vim_free(in_curdir[i]);
10984 vim_free(in_curdir);
10985 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010986 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010987 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010988
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010989 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010990 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010991}
10992
10993/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010994 * Calls globpath() with 'path' values for the given pattern and stores the
10995 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010996 * Returns the total number of matches.
10997 */
10998 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010999expand_in_path(
11000 garray_T *gap,
11001 char_u *pattern,
11002 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011003{
Bram Moolenaar162bd912010-07-28 22:29:10 +020011004 char_u *curdir;
11005 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020011006 char_u *paths = NULL;
Bram Moolenaar8a37b032018-02-03 20:43:08 +010011007 int glob_flags = 0;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011008
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020011009 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020011010 return 0;
11011 mch_dirname(curdir, MAXPATHL);
11012
Bram Moolenaar0be992e2010-08-12 21:50:51 +020011013 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020011014 expand_path_option(curdir, &path_ga);
11015 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020011016 if (path_ga.ga_len == 0)
11017 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011018
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020011019 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011020 ga_clear_strings(&path_ga);
11021 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020011022 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020011023
Bram Moolenaar8a37b032018-02-03 20:43:08 +010011024 if (flags & EW_ICASE)
11025 glob_flags |= WILD_ICASE;
11026 if (flags & EW_ADDSLASH)
11027 glob_flags |= WILD_ADD_SLASH;
11028 globpath(paths, pattern, gap, glob_flags);
Bram Moolenaar162bd912010-07-28 22:29:10 +020011029 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011030
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020011031 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011032}
11033#endif
11034
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011035#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
11036/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020011037 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
11038 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011039 */
11040 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011041remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011042{
11043 int i;
11044 int j;
11045 char_u **fnames = (char_u **)gap->ga_data;
11046
Bram Moolenaardc685ab2010-08-13 21:16:49 +020011047 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011048 for (i = gap->ga_len - 1; i > 0; --i)
11049 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
11050 {
11051 vim_free(fnames[i]);
11052 for (j = i + 1; j < gap->ga_len; ++j)
11053 fnames[j - 1] = fnames[j];
11054 --gap->ga_len;
11055 }
11056}
11057#endif
11058
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011059/*
11060 * Return TRUE if "p" contains what looks like an environment variable.
11061 * Allowing for escaping.
11062 */
11063 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011064has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011065{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011066 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011067 {
11068 if (*p == '\\' && p[1] != NUL)
11069 ++p;
11070 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010011071#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011072 "$%"
11073#else
11074 "$"
11075#endif
11076 , *p) != NULL)
11077 return TRUE;
11078 }
11079 return FALSE;
11080}
11081
11082#ifdef SPECIAL_WILDCHAR
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011083/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +010011084 * Return TRUE if "p" contains a special wildcard character, one that Vim
11085 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011086 */
11087 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011088has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011089{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011090 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011091 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010011092 /* Allow for escaping. */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011093 if (*p == '\\' && p[1] != NUL)
11094 ++p;
11095 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
11096 return TRUE;
11097 }
11098 return FALSE;
11099}
11100#endif
11101
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102/*
11103 * Generic wildcard expansion code.
11104 *
11105 * Characters in "pat" that should not be expanded must be preceded with a
11106 * backslash. E.g., "/path\ with\ spaces/my\*star*"
11107 *
11108 * Return FAIL when no single file was found. In this case "num_file" is not
11109 * set, and "file" may contain an error message.
11110 * Return OK when some files found. "num_file" is set to the number of
11111 * matches, "file" to the array of matches. Call FreeWild() later.
11112 */
11113 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011114gen_expand_wildcards(
11115 int num_pat, /* number of input patterns */
11116 char_u **pat, /* array of input patterns */
11117 int *num_file, /* resulting number of files */
11118 char_u ***file, /* array of resulting files */
11119 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120{
11121 int i;
11122 garray_T ga;
11123 char_u *p;
11124 static int recursive = FALSE;
11125 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020011126 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011127#if defined(FEAT_SEARCHPATH)
11128 int did_expand_in_path = FALSE;
11129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130
11131 /*
11132 * expand_env() is called to expand things like "~user". If this fails,
11133 * it calls ExpandOne(), which brings us back here. In this case, always
11134 * call the machine specific expansion function, if possible. Otherwise,
11135 * return FAIL.
11136 */
11137 if (recursive)
11138#ifdef SPECIAL_WILDCHAR
11139 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11140#else
11141 return FAIL;
11142#endif
11143
11144#ifdef SPECIAL_WILDCHAR
11145 /*
11146 * If there are any special wildcard characters which we cannot handle
11147 * here, call machine specific function for all the expansion. This
11148 * avoids starting the shell for each argument separately.
11149 * For `=expr` do use the internal function.
11150 */
11151 for (i = 0; i < num_pat; i++)
11152 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011153 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000011154# ifdef VIM_BACKTICK
11155 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
11156# endif
11157 )
11158 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11159 }
11160#endif
11161
11162 recursive = TRUE;
11163
11164 /*
11165 * The matching file names are stored in a growarray. Init it empty.
11166 */
11167 ga_init2(&ga, (int)sizeof(char_u *), 30);
11168
11169 for (i = 0; i < num_pat; ++i)
11170 {
11171 add_pat = -1;
11172 p = pat[i];
11173
11174#ifdef VIM_BACKTICK
11175 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020011176 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020011178 if (add_pat == -1)
11179 retval = FAIL;
11180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181 else
11182#endif
11183 {
11184 /*
11185 * First expand environment variables, "~/" and "~user/".
11186 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011187 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000011189 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190 if (p == NULL)
11191 p = pat[i];
11192#ifdef UNIX
11193 /*
11194 * On Unix, if expand_env() can't expand an environment
11195 * variable, use the shell to do that. Discard previously
11196 * found file names and start all over again.
11197 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011198 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199 {
11200 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000011201 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020011203 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204 recursive = FALSE;
11205 return i;
11206 }
11207#endif
11208 }
11209
11210 /*
11211 * If there are wildcards: Expand file names and add each match to
11212 * the list. If there is no match, and EW_NOTFOUND is given, add
11213 * the pattern.
11214 * If there are no wildcards: Add the file name if it exists or
11215 * when EW_NOTFOUND is given.
11216 */
11217 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011218 {
11219#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011220 if ((flags & EW_PATH)
11221 && !mch_isFullName(p)
11222 && !(p[0] == '.'
11223 && (vim_ispathsep(p[1])
11224 || (p[1] == '.' && vim_ispathsep(p[2]))))
11225 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011226 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011227 /* :find completion where 'path' is used.
11228 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011229 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011230 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011231 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011232 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011233 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011234 else
11235#endif
11236 add_pat = mch_expandpath(&ga, p, flags);
11237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238 }
11239
11240 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
11241 {
11242 char_u *t = backslash_halve_save(p);
11243
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244 /* When EW_NOTFOUND is used, always add files and dirs. Makes
11245 * "vim c:/" work. */
11246 if (flags & EW_NOTFOUND)
11247 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020011248 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249 addfile(&ga, t, flags);
11250 vim_free(t);
11251 }
11252
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011253#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011254 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011255 uniquefy_paths(&ga, p);
11256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 if (p != pat[i])
11258 vim_free(p);
11259 }
11260
11261 *num_file = ga.ga_len;
11262 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
11263
11264 recursive = FALSE;
11265
Bram Moolenaar336bd622016-01-17 18:23:58 +010011266 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267}
11268
11269# ifdef VIM_BACKTICK
11270
11271/*
11272 * Return TRUE if we can expand this backtick thing here.
11273 */
11274 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011275vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276{
11277 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
11278}
11279
11280/*
11281 * Expand an item in `backticks` by executing it as a command.
11282 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020011283 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011284 */
11285 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011286expand_backtick(
11287 garray_T *gap,
11288 char_u *pat,
11289 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290{
11291 char_u *p;
11292 char_u *cmd;
11293 char_u *buffer;
11294 int cnt = 0;
11295 int i;
11296
11297 /* Create the command: lop off the backticks. */
11298 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
11299 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011300 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301
11302#ifdef FEAT_EVAL
11303 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011304 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305 else
11306#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011307 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011308 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011309 vim_free(cmd);
11310 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011311 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312
11313 cmd = buffer;
11314 while (*cmd != NUL)
11315 {
11316 cmd = skipwhite(cmd); /* skip over white space */
11317 p = cmd;
11318 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
11319 ++p;
11320 /* add an entry if it is not empty */
11321 if (p > cmd)
11322 {
11323 i = *p;
11324 *p = NUL;
11325 addfile(gap, cmd, flags);
11326 *p = i;
11327 ++cnt;
11328 }
11329 cmd = p;
11330 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
11331 ++cmd;
11332 }
11333
11334 vim_free(buffer);
11335 return cnt;
11336}
11337# endif /* VIM_BACKTICK */
11338
11339/*
11340 * Add a file to a file list. Accepted flags:
11341 * EW_DIR add directories
11342 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011343 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344 * EW_NOTFOUND add even when it doesn't exist
11345 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011346 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347 */
11348 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011349addfile(
11350 garray_T *gap,
11351 char_u *f, /* filename */
11352 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353{
11354 char_u *p;
11355 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011356 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011357
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011358 /* if the file/dir/link doesn't exist, may not add it */
11359 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011360 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361 return;
11362
11363#ifdef FNAME_ILLEGAL
11364 /* if the file/dir contains illegal characters, don't add it */
11365 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11366 return;
11367#endif
11368
11369 isdir = mch_isdir(f);
11370 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11371 return;
11372
Bram Moolenaarb5971142015-03-21 17:32:19 +010011373 /* If the file isn't executable, may not add it. Do accept directories.
11374 * When invoked from expand_shellcmd() do not use $PATH. */
11375 if (!isdir && (flags & EW_EXEC)
11376 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011377 return;
11378
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379 /* Make room for another item in the file list. */
11380 if (ga_grow(gap, 1) == FAIL)
11381 return;
11382
11383 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11384 if (p == NULL)
11385 return;
11386
11387 STRCPY(p, f);
11388#ifdef BACKSLASH_IN_FILENAME
11389 slash_adjust(p);
11390#endif
11391 /*
11392 * Append a slash or backslash after directory names if none is present.
11393 */
11394#ifndef DONT_ADD_PATHSEP_TO_DIR
11395 if (isdir && (flags & EW_ADDSLASH))
11396 add_pathsep(p);
11397#endif
11398 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399}
11400#endif /* !NO_EXPANDPATH */
11401
11402#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11403
11404#ifndef SEEK_SET
11405# define SEEK_SET 0
11406#endif
11407#ifndef SEEK_END
11408# define SEEK_END 2
11409#endif
11410
11411/*
11412 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011413 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11414 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415 * Returns an allocated string, or NULL for error.
11416 */
11417 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011418get_cmd_output(
11419 char_u *cmd,
11420 char_u *infile, /* optional input file name */
11421 int flags, /* can be SHELL_SILENT */
11422 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423{
11424 char_u *tempname;
11425 char_u *command;
11426 char_u *buffer = NULL;
11427 int len;
11428 int i = 0;
11429 FILE *fd;
11430
11431 if (check_restricted() || check_secure())
11432 return NULL;
11433
11434 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011435 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011437 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438 return NULL;
11439 }
11440
11441 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011442 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443 if (command == NULL)
11444 goto done;
11445
11446 /*
11447 * Call the shell to execute the command (errors are ignored).
11448 * Don't check timestamps here.
11449 */
11450 ++no_check_timestamps;
11451 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11452 --no_check_timestamps;
11453
11454 vim_free(command);
11455
11456 /*
11457 * read the names from the file into memory
11458 */
11459# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011460 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011461 fd = mch_fopen((char *)tempname, "r");
11462# else
11463 fd = mch_fopen((char *)tempname, READBIN);
11464# endif
11465
11466 if (fd == NULL)
11467 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011468 semsg(_(e_notopen), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469 goto done;
11470 }
11471
11472 fseek(fd, 0L, SEEK_END);
11473 len = ftell(fd); /* get size of temp file */
11474 fseek(fd, 0L, SEEK_SET);
11475
11476 buffer = alloc(len + 1);
11477 if (buffer != NULL)
11478 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11479 fclose(fd);
11480 mch_remove(tempname);
11481 if (buffer == NULL)
11482 goto done;
11483#ifdef VMS
11484 len = i; /* VMS doesn't give us what we asked for... */
11485#endif
11486 if (i != len)
11487 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011488 semsg(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +010011489 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011490 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011491 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011492 {
11493 /* Change NUL into SOH, otherwise the string is truncated. */
11494 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011495 if (buffer[i] == NUL)
11496 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011497
Bram Moolenaar162bd912010-07-28 22:29:10 +020011498 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011499 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011500 else
11501 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502
11503done:
11504 vim_free(tempname);
11505 return buffer;
11506}
11507#endif
11508
11509/*
11510 * Free the list of files returned by expand_wildcards() or other expansion
11511 * functions.
11512 */
11513 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011514FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011516 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011517 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518 while (count--)
11519 vim_free(files[count]);
11520 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521}
11522
11523/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011524 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011525 * Don't do this when still processing a command or a mapping.
11526 * Don't do this when inside a ":normal" command.
11527 */
11528 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011529goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011530{
11531 return (p_im && stuff_empty() && typebuf_typed());
11532}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011533
11534/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011535 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011536 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11537 * - Remove any argument. E.g., "csh -f" -> "csh".
11538 * But don't allow a space in the path, so that this works:
11539 * "/usr/bin/csh --rcfile ~/.cshrc"
11540 * But don't do that for Windows, it's common to have a space in the path.
11541 */
11542 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011543get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011544{
11545 char_u *p;
11546
11547#ifdef WIN3264
11548 p = gettail(p_sh);
11549 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11550#else
11551 p = skiptowhite(p_sh);
11552 if (*p == NUL)
11553 {
11554 /* No white space, use the tail. */
11555 p = vim_strsave(gettail(p_sh));
11556 }
11557 else
11558 {
11559 char_u *p1, *p2;
11560
11561 /* Find the last path separator before the space. */
11562 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011563 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011564 if (vim_ispathsep(*p2))
11565 p1 = p2 + 1;
11566 p = vim_strnsave(p1, (int)(p - p1));
11567 }
11568#endif
11569 return p;
11570}