blob: 692d5c769e9f177d1d69ffd90a80a548098168ff [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
414 /* Replace the line (unless undo fails). */
415 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 Moolenaar2c019c82013-10-06 17:46:56 +0200420 /* Correct saved cursor position if it is in this line. */
421 if (saved_cursor.lnum == curwin->w_cursor.lnum)
422 {
423 if (saved_cursor.col >= (colnr_T)(p - oldline))
424 /* cursor was after the indent, adjust for the number of
425 * bytes added/removed */
426 saved_cursor.col += ind_len - (colnr_T)(p - oldline);
427 else if (saved_cursor.col >= (colnr_T)(s - newline))
428 /* cursor was in the indent, and is now after it, put it back
429 * at the start of the indent (replacing spaces with TAB) */
430 saved_cursor.col = (colnr_T)(s - newline);
431 }
Bram Moolenaar5409c052005-03-18 20:27:04 +0000432 retval = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 }
434 else
435 vim_free(newline);
436
437 curwin->w_cursor.col = ind_len;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000438 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439}
440
441/*
442 * Copy the indent from ptr to the current line (and fill to size)
443 * Leaves the cursor on the first non-blank in the line.
444 * Returns TRUE if the line was changed.
445 */
446 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100447copy_indent(int size, char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448{
449 char_u *p = NULL;
450 char_u *line = NULL;
451 char_u *s;
452 int todo;
453 int ind_len;
454 int line_len = 0;
455 int tab_pad;
456 int ind_done;
457 int round;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200458#ifdef FEAT_VARTABS
459 int ind_col;
460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461
462 /* Round 1: compute the number of characters needed for the indent
463 * Round 2: copy the characters. */
464 for (round = 1; round <= 2; ++round)
465 {
466 todo = size;
467 ind_len = 0;
468 ind_done = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200469#ifdef FEAT_VARTABS
470 ind_col = 0;
471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 s = src;
473
474 /* Count/copy the usable portion of the source line */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100475 while (todo > 0 && VIM_ISWHITE(*s))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 {
477 if (*s == TAB)
478 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200479#ifdef FEAT_VARTABS
480 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
481 curbuf->b_p_vts_array);
482#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 tab_pad = (int)curbuf->b_p_ts
484 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 /* Stop if this tab will overshoot the target */
487 if (todo < tab_pad)
488 break;
489 todo -= tab_pad;
490 ind_done += tab_pad;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200491#ifdef FEAT_VARTABS
492 ind_col += tab_pad;
493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 }
495 else
496 {
497 --todo;
498 ++ind_done;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200499#ifdef FEAT_VARTABS
500 ++ind_col;
501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 }
503 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000504 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 *p++ = *s;
506 ++s;
507 }
508
509 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200510#ifdef FEAT_VARTABS
511 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
512 curbuf->b_p_vts_array);
513#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200515#endif
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200516 if (todo >= tab_pad && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 {
518 todo -= tab_pad;
519 ++ind_len;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200520#ifdef FEAT_VARTABS
521 ind_col += tab_pad;
522#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000523 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 *p++ = TAB;
525 }
526
527 /* Add tabs required for indent */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200528 if (!curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200530#ifdef FEAT_VARTABS
531 for (;;)
532 {
533 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
534 curbuf->b_p_vts_array);
535 if (todo < tab_pad)
536 break;
537 todo -= tab_pad;
538 ++ind_len;
539 ind_col += tab_pad;
540 if (p != NULL)
541 *p++ = TAB;
542 }
543#else
544 while (todo >= (int)curbuf->b_p_ts)
545 {
546 todo -= (int)curbuf->b_p_ts;
547 ++ind_len;
548 if (p != NULL)
549 *p++ = TAB;
550 }
551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 }
553
554 /* Count/add spaces required for indent */
555 while (todo > 0)
556 {
557 --todo;
558 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000559 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 *p++ = ' ';
561 }
562
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000563 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 {
565 /* Allocate memory for the result: the copied indent, new indent
566 * and the rest of the line. */
567 line_len = (int)STRLEN(ml_get_curline()) + 1;
568 line = alloc(ind_len + line_len);
569 if (line == NULL)
570 return FALSE;
571 p = line;
572 }
573 }
574
575 /* Append the original line */
576 mch_memmove(p, ml_get_curline(), (size_t)line_len);
577
578 /* Replace the line */
579 ml_replace(curwin->w_cursor.lnum, line, FALSE);
580
581 /* Put the cursor after the indent. */
582 curwin->w_cursor.col = ind_len;
583 return TRUE;
584}
585
586/*
587 * Return the indent of the current line after a number. Return -1 if no
588 * number was found. Used for 'n' in 'formatoptions': numbered list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000589 * Since a pattern is used it can actually handle more than numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 */
591 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100592get_number_indent(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 colnr_T col;
595 pos_T pos;
596
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200597 regmatch_T regmatch;
598 int lead_len = 0; /* length of comment leader */
599
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 if (lnum > curbuf->b_ml.ml_line_count)
601 return -1;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000602 pos.lnum = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200603
604#ifdef FEAT_COMMENTS
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200605 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
606 if ((State & INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200607 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000608#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200609 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
610 if (regmatch.regprog != NULL)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200611 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200612 regmatch.rm_ic = FALSE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200613
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200614 /* vim_regexec() expects a pointer to a line. This lets us
615 * start matching for the flp beyond any comment leader... */
616 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200617 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200618 pos.lnum = lnum;
619 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200620#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200621 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200622#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200623 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200624 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200625 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000626
627 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 getvcol(curwin, &pos, &col, NULL, NULL);
630 return (int)col;
631}
632
Bram Moolenaar597a4222014-06-25 14:39:50 +0200633#if defined(FEAT_LINEBREAK) || defined(PROTO)
634/*
635 * Return appropriate space number for breakindent, taking influencing
636 * parameters into account. Window must be specified, since it is not
637 * necessarily always the current one.
638 */
639 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100640get_breakindent_win(
641 win_T *wp,
642 char_u *line) /* start of the line */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200643{
644 static int prev_indent = 0; /* cached indent value */
645 static long prev_ts = 0L; /* cached tabstop value */
646 static char_u *prev_line = NULL; /* cached pointer to line */
Bram Moolenaar79518e22017-02-17 16:31:35 +0100647 static varnumber_T prev_tick = 0; /* changedtick of cached value */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200648#ifdef FEAT_VARTABS
649 static int *prev_vts = NULL; /* cached vartabs values */
650#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200651 int bri = 0;
652 /* window width minus window margin space, i.e. what rests for text */
Bram Moolenaar02631462017-09-22 15:20:32 +0200653 const int eff_wwidth = wp->w_width
Bram Moolenaar597a4222014-06-25 14:39:50 +0200654 - ((wp->w_p_nu || wp->w_p_rnu)
655 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
656 ? number_width(wp) + 1 : 0);
657
658 /* used cached indent, unless pointer or 'tabstop' changed */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200659 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200660 || prev_tick != CHANGEDTICK(wp->w_buffer)
661#ifdef FEAT_VARTABS
662 || prev_vts != wp->w_buffer->b_p_vts_array
663#endif
664 )
Bram Moolenaar597a4222014-06-25 14:39:50 +0200665 {
666 prev_line = line;
667 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100668 prev_tick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200669#ifdef FEAT_VARTABS
670 prev_vts = wp->w_buffer->b_p_vts_array;
671 prev_indent = get_indent_str_vtab(line,
672 (int)wp->w_buffer->b_p_ts,
673 wp->w_buffer->b_p_vts_array, wp->w_p_list);
674#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200675 prev_indent = get_indent_str(line,
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200676 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200677#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200678 }
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200679 bri = prev_indent + wp->w_p_brishift;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200680
681 /* indent minus the length of the showbreak string */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200682 if (wp->w_p_brisbr)
683 bri -= vim_strsize(p_sbr);
684
685 /* Add offset for number column, if 'n' is in 'cpoptions' */
686 bri += win_col_off2(wp);
687
688 /* never indent past left window margin */
689 if (bri < 0)
690 bri = 0;
691 /* always leave at least bri_min characters on the left,
692 * if text width is sufficient */
693 else if (bri > eff_wwidth - wp->w_p_brimin)
694 bri = (eff_wwidth - wp->w_p_brimin < 0)
695 ? 0 : eff_wwidth - wp->w_p_brimin;
696
697 return bri;
698}
699#endif
700
701
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
703
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704/*
705 * Return TRUE if the string "line" starts with a word from 'cinwords'.
706 */
707 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100708cin_is_cinword(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709{
710 char_u *cinw;
711 char_u *cinw_buf;
712 int cinw_len;
713 int retval = FALSE;
714 int len;
715
716 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1;
717 cinw_buf = alloc((unsigned)cinw_len);
718 if (cinw_buf != NULL)
719 {
720 line = skipwhite(line);
721 for (cinw = curbuf->b_p_cinw; *cinw; )
722 {
723 len = copy_option_part(&cinw, cinw_buf, cinw_len, ",");
724 if (STRNCMP(line, cinw_buf, len) == 0
725 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1])))
726 {
727 retval = TRUE;
728 break;
729 }
730 }
731 vim_free(cinw_buf);
732 }
733 return retval;
734}
735#endif
736
737/*
738 * open_line: Add a new line below or above the current line.
739 *
740 * For VREPLACE mode, we only add a new line when we get to the end of the
741 * file, otherwise we just start replacing the next line.
742 *
743 * Caller must take care of undo. Since VREPLACE may affect any number of
744 * lines however, it may call u_save_cursor() again when starting to change a
745 * new line.
746 * "flags": OPENLINE_DELSPACES delete spaces after cursor
747 * OPENLINE_DO_COM format comments
748 * OPENLINE_KEEPTRAIL keep trailing spaces
749 * OPENLINE_MARKFIX adjust mark positions after the line break
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200750 * OPENLINE_COM_LIST format comments with list or 2nd line indent
751 *
752 * "second_line_indent": indent for after ^^D in Insert mode or if flag
753 * OPENLINE_COM_LIST
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 *
Bram Moolenaar24a2d722018-04-24 19:36:43 +0200755 * Return OK for success, FAIL for failure
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 */
757 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100758open_line(
759 int dir, /* FORWARD or BACKWARD */
760 int flags,
761 int second_line_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762{
763 char_u *saved_line; /* copy of the original line */
764 char_u *next_line = NULL; /* copy of the next line */
765 char_u *p_extra = NULL; /* what goes to next line */
766 int less_cols = 0; /* less columns for mark in new line */
767 int less_cols_off = 0; /* columns to skip for mark adjust */
768 pos_T old_cursor; /* old cursor position */
769 int newcol = 0; /* new cursor column */
770 int newindent = 0; /* auto-indent of the new line */
771 int n;
772 int trunc_line = FALSE; /* truncate current line afterwards */
Bram Moolenaar24a2d722018-04-24 19:36:43 +0200773 int retval = FAIL; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774#ifdef FEAT_COMMENTS
775 int extra_len = 0; /* length of p_extra string */
776 int lead_len; /* length of comment leader */
777 char_u *lead_flags; /* position in 'comments' for comment leader */
778 char_u *leader = NULL; /* copy of comment leader */
779#endif
780 char_u *allocated = NULL; /* allocated memory */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 int saved_char = NUL; /* init for GCC */
783#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
784 pos_T *pos;
785#endif
786#ifdef FEAT_SMARTINDENT
787 int do_si = (!p_paste && curbuf->b_p_si
788# ifdef FEAT_CINDENT
789 && !curbuf->b_p_cin
790# endif
Bram Moolenaar69a76fe2017-08-03 17:54:03 +0200791# ifdef FEAT_EVAL
792 && *curbuf->b_p_inde == NUL
793# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 );
795 int no_si = FALSE; /* reset did_si afterwards */
796 int first_char = NUL; /* init for GCC */
797#endif
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +0200798#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 int vreplace_mode;
800#endif
801 int did_append; /* appended a new line */
802 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */
803
804 /*
805 * make a copy of the current line so we can mess with it
806 */
807 saved_line = vim_strsave(ml_get_curline());
808 if (saved_line == NULL) /* out of memory! */
809 return FALSE;
810
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 if (State & VREPLACE_FLAG)
812 {
813 /*
814 * With VREPLACE we make a copy of the next line, which we will be
815 * starting to replace. First make the new line empty and let vim play
816 * with the indenting and comment leader to its heart's content. Then
817 * we grab what it ended up putting on the new line, put back the
818 * original line, and call ins_char() to put each new character onto
819 * the line, replacing what was there before and pushing the right
820 * stuff onto the replace stack. -- webb.
821 */
822 if (curwin->w_cursor.lnum < orig_line_count)
823 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
824 else
825 next_line = vim_strsave((char_u *)"");
826 if (next_line == NULL) /* out of memory! */
827 goto theend;
828
829 /*
830 * In VREPLACE mode, a NL replaces the rest of the line, and starts
831 * replacing the next line, so push all of the characters left on the
832 * line onto the replace stack. We'll push any other characters that
833 * might be replaced at the start of the next line (due to autoindent
834 * etc) a bit later.
835 */
836 replace_push(NUL); /* Call twice because BS over NL expects it */
837 replace_push(NUL);
838 p = saved_line + curwin->w_cursor.col;
839 while (*p != NUL)
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000840 {
841#ifdef FEAT_MBYTE
842 if (has_mbyte)
843 p += replace_push_mb(p);
844 else
845#endif
846 replace_push(*p++);
847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 saved_line[curwin->w_cursor.col] = NUL;
849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +0200851 if ((State & INSERT) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {
853 p_extra = saved_line + curwin->w_cursor.col;
854#ifdef FEAT_SMARTINDENT
855 if (do_si) /* need first char after new line break */
856 {
857 p = skipwhite(p_extra);
858 first_char = *p;
859 }
860#endif
861#ifdef FEAT_COMMENTS
862 extra_len = (int)STRLEN(p_extra);
863#endif
864 saved_char = *p_extra;
865 *p_extra = NUL;
866 }
867
868 u_clearline(); /* cannot do "U" command when adding lines */
869#ifdef FEAT_SMARTINDENT
870 did_si = FALSE;
871#endif
872 ai_col = 0;
873
874 /*
875 * If we just did an auto-indent, then we didn't type anything on
876 * the prior line, and it should be truncated. Do this even if 'ai' is not
877 * set because automatically inserting a comment leader also sets did_ai.
878 */
879 if (dir == FORWARD && did_ai)
880 trunc_line = TRUE;
881
882 /*
883 * If 'autoindent' and/or 'smartindent' is set, try to figure out what
884 * indent to use for the new line.
885 */
886 if (curbuf->b_p_ai
887#ifdef FEAT_SMARTINDENT
888 || do_si
889#endif
890 )
891 {
892 /*
893 * count white space on current line
894 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200895#ifdef FEAT_VARTABS
896 newindent = get_indent_str_vtab(saved_line, curbuf->b_p_ts,
897 curbuf->b_p_vts_array, FALSE);
898#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200899 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200900#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200901 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
902 newindent = second_line_indent; /* for ^^D command in insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903
904#ifdef FEAT_SMARTINDENT
905 /*
906 * Do smart indenting.
907 * In insert/replace mode (only when dir == FORWARD)
908 * we may move some text to the next line. If it starts with '{'
909 * don't add an indent. Fixes inserting a NL before '{' in line
910 * "if (condition) {"
911 */
912 if (!trunc_line && do_si && *saved_line != NUL
913 && (p_extra == NULL || first_char != '{'))
914 {
915 char_u *ptr;
916 char_u last_char;
917
918 old_cursor = curwin->w_cursor;
919 ptr = saved_line;
920# ifdef FEAT_COMMENTS
921 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200922 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 else
924 lead_len = 0;
925# endif
926 if (dir == FORWARD)
927 {
928 /*
929 * Skip preprocessor directives, unless they are
930 * recognised as comments.
931 */
932 if (
933# ifdef FEAT_COMMENTS
934 lead_len == 0 &&
935# endif
936 ptr[0] == '#')
937 {
938 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
939 ptr = ml_get(--curwin->w_cursor.lnum);
940 newindent = get_indent();
941 }
942# ifdef FEAT_COMMENTS
943 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200944 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 else
946 lead_len = 0;
947 if (lead_len > 0)
948 {
949 /*
950 * This case gets the following right:
951 * \*
952 * * A comment (read '\' as '/').
953 * *\
954 * #define IN_THE_WAY
955 * This should line up here;
956 */
957 p = skipwhite(ptr);
958 if (p[0] == '/' && p[1] == '*')
959 p++;
960 if (p[0] == '*')
961 {
962 for (p++; *p; p++)
963 {
964 if (p[0] == '/' && p[-1] == '*')
965 {
966 /*
967 * End of C comment, indent should line up
968 * with the line containing the start of
969 * the comment
970 */
971 curwin->w_cursor.col = (colnr_T)(p - ptr);
972 if ((pos = findmatch(NULL, NUL)) != NULL)
973 {
974 curwin->w_cursor.lnum = pos->lnum;
975 newindent = get_indent();
976 }
977 }
978 }
979 }
980 }
981 else /* Not a comment line */
982# endif
983 {
984 /* Find last non-blank in line */
985 p = ptr + STRLEN(ptr) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100986 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 --p;
988 last_char = *p;
989
990 /*
991 * find the character just before the '{' or ';'
992 */
993 if (last_char == '{' || last_char == ';')
994 {
995 if (p > ptr)
996 --p;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100997 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 --p;
999 }
1000 /*
1001 * Try to catch lines that are split over multiple
1002 * lines. eg:
1003 * if (condition &&
1004 * condition) {
1005 * Should line up here!
1006 * }
1007 */
1008 if (*p == ')')
1009 {
1010 curwin->w_cursor.col = (colnr_T)(p - ptr);
1011 if ((pos = findmatch(NULL, '(')) != NULL)
1012 {
1013 curwin->w_cursor.lnum = pos->lnum;
1014 newindent = get_indent();
1015 ptr = ml_get_curline();
1016 }
1017 }
1018 /*
1019 * If last character is '{' do indent, without
1020 * checking for "if" and the like.
1021 */
1022 if (last_char == '{')
1023 {
1024 did_si = TRUE; /* do indent */
1025 no_si = TRUE; /* don't delete it when '{' typed */
1026 }
1027 /*
1028 * Look for "if" and the like, use 'cinwords'.
1029 * Don't do this if the previous line ended in ';' or
1030 * '}'.
1031 */
1032 else if (last_char != ';' && last_char != '}'
1033 && cin_is_cinword(ptr))
1034 did_si = TRUE;
1035 }
1036 }
1037 else /* dir == BACKWARD */
1038 {
1039 /*
1040 * Skip preprocessor directives, unless they are
1041 * recognised as comments.
1042 */
1043 if (
1044# ifdef FEAT_COMMENTS
1045 lead_len == 0 &&
1046# endif
1047 ptr[0] == '#')
1048 {
1049 int was_backslashed = FALSE;
1050
1051 while ((ptr[0] == '#' || was_backslashed) &&
1052 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
1053 {
1054 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
1055 was_backslashed = TRUE;
1056 else
1057 was_backslashed = FALSE;
1058 ptr = ml_get(++curwin->w_cursor.lnum);
1059 }
1060 if (was_backslashed)
1061 newindent = 0; /* Got to end of file */
1062 else
1063 newindent = get_indent();
1064 }
1065 p = skipwhite(ptr);
1066 if (*p == '}') /* if line starts with '}': do indent */
1067 did_si = TRUE;
1068 else /* can delete indent when '{' typed */
1069 can_si_back = TRUE;
1070 }
1071 curwin->w_cursor = old_cursor;
1072 }
1073 if (do_si)
1074 can_si = TRUE;
1075#endif /* FEAT_SMARTINDENT */
1076
1077 did_ai = TRUE;
1078 }
1079
1080#ifdef FEAT_COMMENTS
1081 /*
1082 * Find out if the current line starts with a comment leader.
1083 * This may then be inserted in front of the new line.
1084 */
1085 end_comment_pending = NUL;
1086 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +02001087 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 else
1089 lead_len = 0;
1090 if (lead_len > 0)
1091 {
1092 char_u *lead_repl = NULL; /* replaces comment leader */
1093 int lead_repl_len = 0; /* length of *lead_repl */
1094 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
1095 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
1096 char_u *comment_end = NULL; /* where lead_end has been found */
1097 int extra_space = FALSE; /* append extra space */
1098 int current_flag;
1099 int require_blank = FALSE; /* requires blank after middle */
1100 char_u *p2;
1101
1102 /*
1103 * If the comment leader has the start, middle or end flag, it may not
1104 * be used or may be replaced with the middle leader.
1105 */
1106 for (p = lead_flags; *p && *p != ':'; ++p)
1107 {
1108 if (*p == COM_BLANK)
1109 {
1110 require_blank = TRUE;
1111 continue;
1112 }
1113 if (*p == COM_START || *p == COM_MIDDLE)
1114 {
1115 current_flag = *p;
1116 if (*p == COM_START)
1117 {
1118 /*
1119 * Doing "O" on a start of comment does not insert leader.
1120 */
1121 if (dir == BACKWARD)
1122 {
1123 lead_len = 0;
1124 break;
1125 }
1126
1127 /* find start of middle part */
1128 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1129 require_blank = FALSE;
1130 }
1131
1132 /*
1133 * Isolate the strings of the middle and end leader.
1134 */
1135 while (*p && p[-1] != ':') /* find end of middle flags */
1136 {
1137 if (*p == COM_BLANK)
1138 require_blank = TRUE;
1139 ++p;
1140 }
1141 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1142
1143 while (*p && p[-1] != ':') /* find end of end flags */
1144 {
1145 /* Check whether we allow automatic ending of comments */
1146 if (*p == COM_AUTO_END)
1147 end_comment_pending = -1; /* means we want to set it */
1148 ++p;
1149 }
1150 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
1151
1152 if (end_comment_pending == -1) /* we can set it now */
1153 end_comment_pending = lead_end[n - 1];
1154
1155 /*
1156 * If the end of the comment is in the same line, don't use
1157 * the comment leader.
1158 */
1159 if (dir == FORWARD)
1160 {
1161 for (p = saved_line + lead_len; *p; ++p)
1162 if (STRNCMP(p, lead_end, n) == 0)
1163 {
1164 comment_end = p;
1165 lead_len = 0;
1166 break;
1167 }
1168 }
1169
1170 /*
1171 * Doing "o" on a start of comment inserts the middle leader.
1172 */
1173 if (lead_len > 0)
1174 {
1175 if (current_flag == COM_START)
1176 {
1177 lead_repl = lead_middle;
1178 lead_repl_len = (int)STRLEN(lead_middle);
1179 }
1180
1181 /*
1182 * If we have hit RETURN immediately after the start
1183 * comment leader, then put a space after the middle
1184 * comment leader on the next line.
1185 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001186 if (!VIM_ISWHITE(saved_line[lead_len - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 && ((p_extra != NULL
1188 && (int)curwin->w_cursor.col == lead_len)
1189 || (p_extra == NULL
1190 && saved_line[lead_len] == NUL)
1191 || require_blank))
1192 extra_space = TRUE;
1193 }
1194 break;
1195 }
1196 if (*p == COM_END)
1197 {
1198 /*
1199 * Doing "o" on the end of a comment does not insert leader.
1200 * Remember where the end is, might want to use it to find the
1201 * start (for C-comments).
1202 */
1203 if (dir == FORWARD)
1204 {
1205 comment_end = skipwhite(saved_line);
1206 lead_len = 0;
1207 break;
1208 }
1209
1210 /*
1211 * Doing "O" on the end of a comment inserts the middle leader.
1212 * Find the string for the middle leader, searching backwards.
1213 */
1214 while (p > curbuf->b_p_com && *p != ',')
1215 --p;
1216 for (lead_repl = p; lead_repl > curbuf->b_p_com
1217 && lead_repl[-1] != ':'; --lead_repl)
1218 ;
1219 lead_repl_len = (int)(p - lead_repl);
1220
1221 /* We can probably always add an extra space when doing "O" on
1222 * the comment-end */
1223 extra_space = TRUE;
1224
1225 /* Check whether we allow automatic ending of comments */
1226 for (p2 = p; *p2 && *p2 != ':'; p2++)
1227 {
1228 if (*p2 == COM_AUTO_END)
1229 end_comment_pending = -1; /* means we want to set it */
1230 }
1231 if (end_comment_pending == -1)
1232 {
1233 /* Find last character in end-comment string */
1234 while (*p2 && *p2 != ',')
1235 p2++;
1236 end_comment_pending = p2[-1];
1237 }
1238 break;
1239 }
1240 if (*p == COM_FIRST)
1241 {
1242 /*
1243 * Comment leader for first line only: Don't repeat leader
1244 * when using "O", blank out leader when using "o".
1245 */
1246 if (dir == BACKWARD)
1247 lead_len = 0;
1248 else
1249 {
1250 lead_repl = (char_u *)"";
1251 lead_repl_len = 0;
1252 }
1253 break;
1254 }
1255 }
1256 if (lead_len)
1257 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001258 /* allocate buffer (may concatenate p_extra later) */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001259 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001260 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 allocated = leader; /* remember to free it later */
1262
1263 if (leader == NULL)
1264 lead_len = 0;
1265 else
1266 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001267 vim_strncpy(leader, saved_line, lead_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268
1269 /*
1270 * Replace leader with lead_repl, right or left adjusted
1271 */
1272 if (lead_repl != NULL)
1273 {
1274 int c = 0;
1275 int off = 0;
1276
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001277 for (p = lead_flags; *p != NUL && *p != ':'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 {
1279 if (*p == COM_RIGHT || *p == COM_LEFT)
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001280 c = *p++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 else if (VIM_ISDIGIT(*p) || *p == '-')
1282 off = getdigits(&p);
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001283 else
1284 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 }
1286 if (c == COM_RIGHT) /* right adjusted leader */
1287 {
1288 /* find last non-white in the leader to line up with */
1289 for (p = leader + lead_len - 1; p > leader
Bram Moolenaar1c465442017-03-12 20:10:05 +01001290 && VIM_ISWHITE(*p); --p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 ++p;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001293
1294#ifdef FEAT_MBYTE
1295 /* Compute the length of the replaced characters in
1296 * screen characters, not bytes. */
1297 {
1298 int repl_size = vim_strnsize(lead_repl,
1299 lead_repl_len);
1300 int old_size = 0;
1301 char_u *endp = p;
1302 int l;
1303
1304 while (old_size < repl_size && p > leader)
1305 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001306 MB_PTR_BACK(leader, p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001307 old_size += ptr2cells(p);
1308 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001309 l = lead_repl_len - (int)(endp - p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001310 if (l != 0)
1311 mch_memmove(endp + l, endp,
1312 (size_t)((leader + lead_len) - endp));
1313 lead_len += l;
1314 }
1315#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 if (p < leader + lead_repl_len)
1317 p = leader;
1318 else
1319 p -= lead_repl_len;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1322 if (p + lead_repl_len > leader + lead_len)
1323 p[lead_repl_len] = NUL;
1324
1325 /* blank-out any other chars from the old leader. */
1326 while (--p >= leader)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001327 {
1328#ifdef FEAT_MBYTE
1329 int l = mb_head_off(leader, p);
1330
1331 if (l > 1)
1332 {
1333 p -= l;
1334 if (ptr2cells(p) > 1)
1335 {
1336 p[1] = ' ';
1337 --l;
1338 }
1339 mch_memmove(p + 1, p + l + 1,
1340 (size_t)((leader + lead_len) - (p + l + 1)));
1341 lead_len -= l;
1342 *p = ' ';
1343 }
1344 else
1345#endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01001346 if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 }
1350 else /* left adjusted leader */
1351 {
1352 p = skipwhite(leader);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001353#ifdef FEAT_MBYTE
1354 /* Compute the length of the replaced characters in
1355 * screen characters, not bytes. Move the part that is
1356 * not to be overwritten. */
1357 {
1358 int repl_size = vim_strnsize(lead_repl,
1359 lead_repl_len);
1360 int i;
1361 int l;
1362
Bram Moolenaardc633cf2016-04-23 14:33:19 +02001363 for (i = 0; i < lead_len && p[i] != NUL; i += l)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001364 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001365 l = (*mb_ptr2len)(p + i);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001366 if (vim_strnsize(p, i + l) > repl_size)
1367 break;
1368 }
1369 if (i != lead_repl_len)
1370 {
1371 mch_memmove(p + lead_repl_len, p + i,
Bram Moolenaar2d7ff052009-11-17 15:08:26 +00001372 (size_t)(lead_len - i - (p - leader)));
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001373 lead_len += lead_repl_len - i;
1374 }
1375 }
1376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1378
1379 /* Replace any remaining non-white chars in the old
1380 * leader by spaces. Keep Tabs, the indent must
1381 * remain the same. */
1382 for (p += lead_repl_len; p < leader + lead_len; ++p)
Bram Moolenaar1c465442017-03-12 20:10:05 +01001383 if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 {
1385 /* Don't put a space before a TAB. */
1386 if (p + 1 < leader + lead_len && p[1] == TAB)
1387 {
1388 --lead_len;
1389 mch_memmove(p, p + 1,
1390 (leader + lead_len) - p);
1391 }
1392 else
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001393 {
1394#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001395 int l = (*mb_ptr2len)(p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001396
1397 if (l > 1)
1398 {
1399 if (ptr2cells(p) > 1)
1400 {
1401 /* Replace a double-wide char with
1402 * two spaces */
1403 --l;
1404 *p++ = ' ';
1405 }
1406 mch_memmove(p + 1, p + l,
1407 (leader + lead_len) - p);
1408 lead_len -= l - 1;
1409 }
1410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 }
1414 *p = NUL;
1415 }
1416
1417 /* Recompute the indent, it may have changed. */
1418 if (curbuf->b_p_ai
1419#ifdef FEAT_SMARTINDENT
1420 || do_si
1421#endif
1422 )
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001423#ifdef FEAT_VARTABS
1424 newindent = get_indent_str_vtab(leader, curbuf->b_p_ts,
1425 curbuf->b_p_vts_array, FALSE);
1426#else
1427 newindent = get_indent_str(leader,
1428 (int)curbuf->b_p_ts, FALSE);
1429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430
1431 /* Add the indent offset */
1432 if (newindent + off < 0)
1433 {
1434 off = -newindent;
1435 newindent = 0;
1436 }
1437 else
1438 newindent += off;
1439
1440 /* Correct trailing spaces for the shift, so that
1441 * alignment remains equal. */
1442 while (off > 0 && lead_len > 0
1443 && leader[lead_len - 1] == ' ')
1444 {
1445 /* Don't do it when there is a tab before the space */
1446 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1447 break;
1448 --lead_len;
1449 --off;
1450 }
1451
1452 /* If the leader ends in white space, don't add an
1453 * extra space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001454 if (lead_len > 0 && VIM_ISWHITE(leader[lead_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 extra_space = FALSE;
1456 leader[lead_len] = NUL;
1457 }
1458
1459 if (extra_space)
1460 {
1461 leader[lead_len++] = ' ';
1462 leader[lead_len] = NUL;
1463 }
1464
1465 newcol = lead_len;
1466
1467 /*
1468 * if a new indent will be set below, remove the indent that
1469 * is in the comment leader
1470 */
1471 if (newindent
1472#ifdef FEAT_SMARTINDENT
1473 || did_si
1474#endif
1475 )
1476 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001477 while (lead_len && VIM_ISWHITE(*leader))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 {
1479 --lead_len;
1480 --newcol;
1481 ++leader;
1482 }
1483 }
1484
1485 }
1486#ifdef FEAT_SMARTINDENT
1487 did_si = can_si = FALSE;
1488#endif
1489 }
1490 else if (comment_end != NULL)
1491 {
1492 /*
1493 * We have finished a comment, so we don't use the leader.
1494 * If this was a C-comment and 'ai' or 'si' is set do a normal
1495 * indent to align with the line containing the start of the
1496 * comment.
1497 */
1498 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1499 (curbuf->b_p_ai
1500#ifdef FEAT_SMARTINDENT
1501 || do_si
1502#endif
1503 ))
1504 {
1505 old_cursor = curwin->w_cursor;
1506 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1507 if ((pos = findmatch(NULL, NUL)) != NULL)
1508 {
1509 curwin->w_cursor.lnum = pos->lnum;
1510 newindent = get_indent();
1511 }
1512 curwin->w_cursor = old_cursor;
1513 }
1514 }
1515 }
1516#endif
1517
1518 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */
1519 if (p_extra != NULL)
1520 {
1521 *p_extra = saved_char; /* restore char that NUL replaced */
1522
1523 /*
1524 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
1525 * non-blank.
1526 *
1527 * When in REPLACE mode, put the deleted blanks on the replace stack,
1528 * preceded by a NUL, so they can be put back when a BS is entered.
1529 */
1530 if (REPLACE_NORMAL(State))
1531 replace_push(NUL); /* end of extra blanks */
1532 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
1533 {
1534 while ((*p_extra == ' ' || *p_extra == '\t')
1535#ifdef FEAT_MBYTE
1536 && (!enc_utf8
1537 || !utf_iscomposing(utf_ptr2char(p_extra + 1)))
1538#endif
1539 )
1540 {
1541 if (REPLACE_NORMAL(State))
1542 replace_push(*p_extra);
1543 ++p_extra;
1544 ++less_cols_off;
1545 }
1546 }
1547 if (*p_extra != NUL)
1548 did_ai = FALSE; /* append some text, don't truncate now */
1549
1550 /* columns for marks adjusted for removed columns */
1551 less_cols = (int)(p_extra - saved_line);
1552 }
1553
1554 if (p_extra == NULL)
1555 p_extra = (char_u *)""; /* append empty line */
1556
1557#ifdef FEAT_COMMENTS
1558 /* concatenate leader and p_extra, if there is a leader */
1559 if (lead_len)
1560 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001561 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
1562 {
1563 int i;
Bram Moolenaar36105782012-06-14 20:59:25 +02001564 int padding = second_line_indent
1565 - (newindent + (int)STRLEN(leader));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001566
1567 /* Here whitespace is inserted after the comment char.
1568 * Below, set_indent(newindent, SIN_INSERT) will insert the
1569 * whitespace needed before the comment char. */
1570 for (i = 0; i < padding; i++)
1571 {
1572 STRCAT(leader, " ");
Bram Moolenaar5fb9ec52012-07-25 16:10:03 +02001573 less_cols--;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001574 newcol++;
1575 }
1576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 STRCAT(leader, p_extra);
1578 p_extra = leader;
1579 did_ai = TRUE; /* So truncating blanks works with comments */
1580 less_cols -= lead_len;
1581 }
1582 else
1583 end_comment_pending = NUL; /* turns out there was no leader */
1584#endif
1585
1586 old_cursor = curwin->w_cursor;
1587 if (dir == BACKWARD)
1588 --curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 {
1591 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
1592 == FAIL)
1593 goto theend;
1594 /* Postpone calling changed_lines(), because it would mess up folding
Bram Moolenaar82faa252016-06-04 20:14:07 +02001595 * with markers.
1596 * Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01001597 * be marks there. But still needed in diff mode. */
1598 if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count
1599#ifdef FEAT_DIFF
1600 || curwin->w_p_diff
1601#endif
1602 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02001603 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 did_append = TRUE;
1605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 else
1607 {
1608 /*
1609 * In VREPLACE mode we are starting to replace the next line.
1610 */
1611 curwin->w_cursor.lnum++;
1612 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1613 {
1614 /* In case we NL to a new line, BS to the previous one, and NL
1615 * again, we don't want to save the new line for undo twice.
1616 */
1617 (void)u_save_cursor(); /* errors are ignored! */
1618 vr_lines_changed++;
1619 }
1620 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1621 changed_bytes(curwin->w_cursor.lnum, 0);
1622 curwin->w_cursor.lnum--;
1623 did_append = FALSE;
1624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625
1626 if (newindent
1627#ifdef FEAT_SMARTINDENT
1628 || did_si
1629#endif
1630 )
1631 {
1632 ++curwin->w_cursor.lnum;
1633#ifdef FEAT_SMARTINDENT
1634 if (did_si)
1635 {
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001636 int sw = (int)get_sw_value(curbuf);
Bram Moolenaar14f24742012-08-08 18:01:05 +02001637
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001639 newindent -= newindent % sw;
1640 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 }
1642#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001643 /* Copy the indent */
1644 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 {
1646 (void)copy_indent(newindent, saved_line);
1647
1648 /*
1649 * Set the 'preserveindent' option so that any further screwing
1650 * with the line doesn't entirely destroy our efforts to preserve
1651 * it. It gets restored at the function end.
1652 */
1653 curbuf->b_p_pi = TRUE;
1654 }
1655 else
1656 (void)set_indent(newindent, SIN_INSERT);
1657 less_cols -= curwin->w_cursor.col;
1658
1659 ai_col = curwin->w_cursor.col;
1660
1661 /*
1662 * In REPLACE mode, for each character in the new indent, there must
1663 * be a NUL on the replace stack, for when it is deleted with BS
1664 */
1665 if (REPLACE_NORMAL(State))
1666 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1667 replace_push(NUL);
1668 newcol += curwin->w_cursor.col;
1669#ifdef FEAT_SMARTINDENT
1670 if (no_si)
1671 did_si = FALSE;
1672#endif
1673 }
1674
1675#ifdef FEAT_COMMENTS
1676 /*
1677 * In REPLACE mode, for each character in the extra leader, there must be
1678 * a NUL on the replace stack, for when it is deleted with BS.
1679 */
1680 if (REPLACE_NORMAL(State))
1681 while (lead_len-- > 0)
1682 replace_push(NUL);
1683#endif
1684
1685 curwin->w_cursor = old_cursor;
1686
1687 if (dir == FORWARD)
1688 {
1689 if (trunc_line || (State & INSERT))
1690 {
1691 /* truncate current line at cursor */
1692 saved_line[curwin->w_cursor.col] = NUL;
1693 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1694 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1695 truncate_spaces(saved_line);
1696 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1697 saved_line = NULL;
1698 if (did_append)
1699 {
1700 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1701 curwin->w_cursor.lnum + 1, 1L);
1702 did_append = FALSE;
1703
1704 /* Move marks after the line break to the new line. */
1705 if (flags & OPENLINE_MARKFIX)
1706 mark_col_adjust(curwin->w_cursor.lnum,
1707 curwin->w_cursor.col + less_cols_off,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01001708 1L, (long)-less_cols, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 }
1710 else
1711 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1712 }
1713
1714 /*
1715 * Put the cursor on the new line. Careful: the scrollup() above may
1716 * have moved w_cursor, we must use old_cursor.
1717 */
1718 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1719 }
1720 if (did_append)
1721 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1722
1723 curwin->w_cursor.col = newcol;
1724#ifdef FEAT_VIRTUALEDIT
1725 curwin->w_cursor.coladd = 0;
1726#endif
1727
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02001728#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 /*
1730 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1731 * fixthisline() from doing it (via change_indent()) by telling it we're in
1732 * normal INSERT mode.
1733 */
1734 if (State & VREPLACE_FLAG)
1735 {
1736 vreplace_mode = State; /* So we know to put things right later */
1737 State = INSERT;
1738 }
1739 else
1740 vreplace_mode = 0;
1741#endif
1742#ifdef FEAT_LISP
1743 /*
1744 * May do lisp indenting.
1745 */
1746 if (!p_paste
1747# ifdef FEAT_COMMENTS
1748 && leader == NULL
1749# endif
1750 && curbuf->b_p_lisp
1751 && curbuf->b_p_ai)
1752 {
1753 fixthisline(get_lisp_indent);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001754 ai_col = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 }
1756#endif
1757#ifdef FEAT_CINDENT
1758 /*
1759 * May do indenting after opening a new line.
1760 */
1761 if (!p_paste
1762 && (curbuf->b_p_cin
1763# ifdef FEAT_EVAL
1764 || *curbuf->b_p_inde != NUL
1765# endif
1766 )
1767 && in_cinkeys(dir == FORWARD
1768 ? KEY_OPEN_FORW
1769 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1770 {
1771 do_c_expr_indent();
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001772 ai_col = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 }
1774#endif
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02001775#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 if (vreplace_mode != 0)
1777 State = vreplace_mode;
1778#endif
1779
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 /*
1781 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1782 * original line, and inserts the new stuff char by char, pushing old stuff
1783 * onto the replace stack (via ins_char()).
1784 */
1785 if (State & VREPLACE_FLAG)
1786 {
1787 /* Put new line in p_extra */
1788 p_extra = vim_strsave(ml_get_curline());
1789 if (p_extra == NULL)
1790 goto theend;
1791
1792 /* Put back original line */
1793 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1794
1795 /* Insert new stuff into line again */
1796 curwin->w_cursor.col = 0;
1797#ifdef FEAT_VIRTUALEDIT
1798 curwin->w_cursor.coladd = 0;
1799#endif
1800 ins_bytes(p_extra); /* will call changed_bytes() */
1801 vim_free(p_extra);
1802 next_line = NULL;
1803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001805 retval = OK; /* success! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806theend:
1807 curbuf->b_p_pi = saved_pi;
1808 vim_free(saved_line);
1809 vim_free(next_line);
1810 vim_free(allocated);
1811 return retval;
1812}
1813
1814#if defined(FEAT_COMMENTS) || defined(PROTO)
1815/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +02001816 * get_leader_len() returns the length in bytes of the prefix of the given
1817 * string which introduces a comment. If this string is not a comment then
1818 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 * When "flags" is not NULL, it is set to point to the flags of the recognized
1820 * comment leader.
1821 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001822 * If "include_space" is set, include trailing whitespace while calculating the
1823 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 */
1825 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001826get_leader_len(
1827 char_u *line,
1828 char_u **flags,
1829 int backward,
1830 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831{
1832 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001833 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 int got_com = FALSE;
1835 int found_one;
1836 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1837 char_u *string; /* pointer to comment string */
1838 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001839 int middle_match_len = 0;
1840 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001841 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842
Bram Moolenaar81340392012-06-06 16:12:59 +02001843 result = i = 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001844 while (VIM_ISWHITE(line[i])) /* leading white space is ignored */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 ++i;
1846
1847 /*
1848 * Repeat to match several nested comment strings.
1849 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001850 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 {
1852 /*
1853 * scan through the 'comments' option for a match
1854 */
1855 found_one = FALSE;
1856 for (list = curbuf->b_p_com; *list; )
1857 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001858 /* Get one option part into part_buf[]. Advance "list" to next
1859 * one. Put "string" at start of string. */
1860 if (!got_com && flags != NULL)
1861 *flags = list; /* remember where flags started */
1862 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1864 string = vim_strchr(part_buf, ':');
1865 if (string == NULL) /* missing ':', ignore this part */
1866 continue;
1867 *string++ = NUL; /* isolate flags from string */
1868
Bram Moolenaara4271d52011-05-10 13:38:27 +02001869 /* If we found a middle match previously, use that match when this
1870 * is not a middle or end. */
1871 if (middle_match_len != 0
1872 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1873 && vim_strchr(part_buf, COM_END) == NULL)
1874 break;
1875
1876 /* When we already found a nested comment, only accept further
1877 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1879 continue;
1880
Bram Moolenaara4271d52011-05-10 13:38:27 +02001881 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1883 continue;
1884
Bram Moolenaara4271d52011-05-10 13:38:27 +02001885 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 * When string starts with white space, must have some white space
1887 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001888 * TABs and spaces). */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001889 if (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001891 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001892 continue; /* missing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001893 while (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 ++string;
1895 }
1896 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1897 ;
1898 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001899 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900
Bram Moolenaara4271d52011-05-10 13:38:27 +02001901 /* When 'b' flag used, there must be white space or an
1902 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001904 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 continue;
1906
Bram Moolenaara4271d52011-05-10 13:38:27 +02001907 /* We have found a match, stop searching unless this is a middle
1908 * comment. The middle comment can be a substring of the end
1909 * comment in which case it's better to return the length of the
1910 * end comment and its flags. Thus we keep searching with middle
1911 * and end matches and use an end match if it matches better. */
1912 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1913 {
1914 if (middle_match_len == 0)
1915 {
1916 middle_match_len = j;
1917 saved_flags = prev_list;
1918 }
1919 continue;
1920 }
1921 if (middle_match_len != 0 && j > middle_match_len)
1922 /* Use this match instead of the middle match, since it's a
1923 * longer thus better match. */
1924 middle_match_len = 0;
1925
1926 if (middle_match_len == 0)
1927 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 found_one = TRUE;
1929 break;
1930 }
1931
Bram Moolenaara4271d52011-05-10 13:38:27 +02001932 if (middle_match_len != 0)
1933 {
1934 /* Use the previously found middle match after failing to find a
1935 * match with an end. */
1936 if (!got_com && flags != NULL)
1937 *flags = saved_flags;
1938 i += middle_match_len;
1939 found_one = TRUE;
1940 }
1941
1942 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 if (!found_one)
1944 break;
1945
Bram Moolenaar81340392012-06-06 16:12:59 +02001946 result = i;
1947
Bram Moolenaara4271d52011-05-10 13:38:27 +02001948 /* Include any trailing white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001949 while (VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 ++i;
1951
Bram Moolenaar81340392012-06-06 16:12:59 +02001952 if (include_space)
1953 result = i;
1954
Bram Moolenaara4271d52011-05-10 13:38:27 +02001955 /* If this comment doesn't nest, stop here. */
1956 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 if (vim_strchr(part_buf, COM_NEST) == NULL)
1958 break;
1959 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001960 return result;
1961}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001962
Bram Moolenaar81340392012-06-06 16:12:59 +02001963/*
1964 * Return the offset at which the last comment in line starts. If there is no
1965 * comment in the whole line, -1 is returned.
1966 *
1967 * When "flags" is not null, it is set to point to the flags describing the
1968 * recognized comment leader.
1969 */
1970 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001971get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +02001972{
1973 int result = -1;
1974 int i, j;
1975 int lower_check_bound = 0;
1976 char_u *string;
1977 char_u *com_leader;
1978 char_u *com_flags;
1979 char_u *list;
1980 int found_one;
1981 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1982
1983 /*
1984 * Repeat to match several nested comment strings.
1985 */
1986 i = (int)STRLEN(line);
1987 while (--i >= lower_check_bound)
1988 {
1989 /*
1990 * scan through the 'comments' option for a match
1991 */
1992 found_one = FALSE;
1993 for (list = curbuf->b_p_com; *list; )
1994 {
1995 char_u *flags_save = list;
1996
1997 /*
1998 * Get one option part into part_buf[]. Advance list to next one.
1999 * put string at start of string.
2000 */
2001 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
2002 string = vim_strchr(part_buf, ':');
2003 if (string == NULL) /* If everything is fine, this cannot actually
2004 * happen. */
2005 {
2006 continue;
2007 }
2008 *string++ = NUL; /* Isolate flags from string. */
2009 com_leader = string;
2010
2011 /*
2012 * Line contents and string must match.
2013 * When string starts with white space, must have some white space
2014 * (but the amount does not need to match, there might be a mix of
2015 * TABs and spaces).
2016 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002017 if (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +02002018 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002019 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar81340392012-06-06 16:12:59 +02002020 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +01002021 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +02002022 ++string;
2023 }
2024 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
2025 /* do nothing */;
2026 if (string[j] != NUL)
2027 continue;
2028
2029 /*
2030 * When 'b' flag used, there must be white space or an
2031 * end-of-line after the string in the line.
2032 */
2033 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002034 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar81340392012-06-06 16:12:59 +02002035 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +01002036
Bram Moolenaar4af72592018-12-09 15:00:52 +01002037 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
Bram Moolenaar53932812018-12-07 21:08:49 +01002038 {
Bram Moolenaar4af72592018-12-09 15:00:52 +01002039 // For a middlepart comment, only consider it to match if
2040 // everything before the current position in the line is
2041 // whitespace. Otherwise we would think we are inside a
2042 // comment if the middle part appears somewhere in the middle
2043 // of the line. E.g. for C the "*" appears often.
Bram Moolenaar53932812018-12-07 21:08:49 +01002044 for (j = 0; VIM_ISWHITE(line[j]) && j <= i; j++)
2045 ;
2046 if (j < i)
2047 continue;
Bram Moolenaar81340392012-06-06 16:12:59 +02002048 }
2049
2050 /*
2051 * We have found a match, stop searching.
2052 */
2053 found_one = TRUE;
2054
2055 if (flags)
2056 *flags = flags_save;
2057 com_flags = flags_save;
2058
2059 break;
2060 }
2061
2062 if (found_one)
2063 {
2064 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
2065 int len1, len2, off;
2066
2067 result = i;
2068 /*
2069 * If this comment nests, continue searching.
2070 */
2071 if (vim_strchr(part_buf, COM_NEST) != NULL)
2072 continue;
2073
2074 lower_check_bound = i;
2075
2076 /* Let's verify whether the comment leader found is a substring
2077 * of other comment leaders. If it is, let's adjust the
2078 * lower_check_bound so that we make sure that we have determined
2079 * the comment leader correctly.
2080 */
2081
Bram Moolenaar1c465442017-03-12 20:10:05 +01002082 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +02002083 ++com_leader;
2084 len1 = (int)STRLEN(com_leader);
2085
2086 for (list = curbuf->b_p_com; *list; )
2087 {
2088 char_u *flags_save = list;
2089
2090 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
2091 if (flags_save == com_flags)
2092 continue;
2093 string = vim_strchr(part_buf2, ':');
2094 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002095 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +02002096 ++string;
2097 len2 = (int)STRLEN(string);
2098 if (len2 == 0)
2099 continue;
2100
2101 /* Now we have to verify whether string ends with a substring
2102 * beginning the com_leader. */
2103 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
2104 {
2105 --off;
2106 if (!STRNCMP(string + off, com_leader, len2 - off))
2107 {
2108 if (i - off < lower_check_bound)
2109 lower_check_bound = i - off;
2110 }
2111 }
2112 }
2113 }
2114 }
2115 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116}
2117#endif
2118
2119/*
2120 * Return the number of window lines occupied by buffer line "lnum".
2121 */
2122 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002123plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124{
2125 return plines_win(curwin, lnum, TRUE);
2126}
2127
2128 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002129plines_win(
2130 win_T *wp,
2131 linenr_T lnum,
2132 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133{
2134#if defined(FEAT_DIFF) || defined(PROTO)
2135 /* Check for filler lines above this buffer line. When folded the result
2136 * is one line anyway. */
2137 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
2138}
2139
2140 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002141plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142{
2143 return plines_win_nofill(curwin, lnum, TRUE);
2144}
2145
2146 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002147plines_win_nofill(
2148 win_T *wp,
2149 linenr_T lnum,
2150 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151{
2152#endif
2153 int lines;
2154
2155 if (!wp->w_p_wrap)
2156 return 1;
2157
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 if (wp->w_width == 0)
2159 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160
2161#ifdef FEAT_FOLDING
2162 /* A folded lines is handled just like an empty line. */
2163 /* NOTE: Caller must handle lines that are MAYBE folded. */
2164 if (lineFolded(wp, lnum) == TRUE)
2165 return 1;
2166#endif
2167
2168 lines = plines_win_nofold(wp, lnum);
2169 if (winheight > 0 && lines > wp->w_height)
2170 return (int)wp->w_height;
2171 return lines;
2172}
2173
2174/*
2175 * Return number of window lines physical line "lnum" will occupy in window
2176 * "wp". Does not care about folding, 'wrap' or 'diff'.
2177 */
2178 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002179plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180{
2181 char_u *s;
2182 long col;
2183 int width;
2184
2185 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2186 if (*s == NUL) /* empty line */
2187 return 1;
2188 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
2189
2190 /*
2191 * If list mode is on, then the '$' at the end of the line may take up one
2192 * extra column.
2193 */
2194 if (wp->w_p_list && lcs_eol != NUL)
2195 col += 1;
2196
2197 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002198 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002200 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 if (width <= 0)
2202 return 32000;
2203 if (col <= width)
2204 return 1;
2205 col -= width;
2206 width += win_col_off2(wp);
2207 return (col + (width - 1)) / width + 1;
2208}
2209
2210/*
2211 * Like plines_win(), but only reports the number of physical screen lines
2212 * used from the start of the line to the given column number.
2213 */
2214 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002215plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216{
2217 long col;
2218 char_u *s;
2219 int lines = 0;
2220 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002221 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222
2223#ifdef FEAT_DIFF
2224 /* Check for filler lines above this buffer line. When folded the result
2225 * is one line anyway. */
2226 lines = diff_check_fill(wp, lnum);
2227#endif
2228
2229 if (!wp->w_p_wrap)
2230 return lines + 1;
2231
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 if (wp->w_width == 0)
2233 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234
Bram Moolenaar597a4222014-06-25 14:39:50 +02002235 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236
2237 col = 0;
2238 while (*s != NUL && --column >= 0)
2239 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02002240 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002241 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 }
2243
2244 /*
2245 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2246 * INSERT mode, then col must be adjusted so that it represents the last
2247 * screen position of the TAB. This only fixes an error when the TAB wraps
2248 * from one screen line to the next (when 'columns' is not a multiple of
2249 * 'ts') -- webb.
2250 */
2251 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02002252 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253
2254 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002255 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002257 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002258 if (width <= 0)
2259 return 9999;
2260
2261 lines += 1;
2262 if (col > width)
2263 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2264 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265}
2266
2267 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002268plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269{
2270 int count = 0;
2271
2272 while (first <= last)
2273 {
2274#ifdef FEAT_FOLDING
2275 int x;
2276
2277 /* Check if there are any really folded lines, but also included lines
2278 * that are maybe folded. */
2279 x = foldedCount(wp, first, NULL);
2280 if (x > 0)
2281 {
2282 ++count; /* count 1 for "+-- folded" line */
2283 first += x;
2284 }
2285 else
2286#endif
2287 {
2288#ifdef FEAT_DIFF
2289 if (first == wp->w_topline)
2290 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2291 else
2292#endif
2293 count += plines_win(wp, first, TRUE);
2294 ++first;
2295 }
2296 }
2297 return (count);
2298}
2299
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300/*
2301 * Insert string "p" at the cursor position. Stops at a NUL byte.
2302 * Handles Replace mode and multi-byte characters.
2303 */
2304 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002305ins_bytes(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306{
2307 ins_bytes_len(p, (int)STRLEN(p));
2308}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310/*
2311 * Insert string "p" with length "len" at the cursor position.
2312 * Handles Replace mode and multi-byte characters.
2313 */
2314 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002315ins_bytes_len(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316{
2317 int i;
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02002318#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 int n;
2320
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002321 if (has_mbyte)
2322 for (i = 0; i < len; i += n)
2323 {
2324 if (enc_utf8)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002325 // avoid reading past p[len]
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002326 n = utfc_ptr2len_len(p + i, len - i);
2327 else
2328 n = (*mb_ptr2len)(p + i);
2329 ins_char_bytes(p + i, n);
2330 }
2331 else
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02002332#endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002333 for (i = 0; i < len; ++i)
2334 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336
2337/*
2338 * Insert or replace a single character at the cursor position.
2339 * When in REPLACE or VREPLACE mode, replace any existing character.
2340 * Caller must have prepared for undo.
2341 * For multi-byte characters we get the whole character, the caller must
2342 * convert bytes to a character.
2343 */
2344 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002345ins_char(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002347 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar560379d2017-01-21 22:50:00 +01002348 int n = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349
Bram Moolenaar6a8ede92017-01-22 19:49:12 +01002350#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 n = (*mb_char2bytes)(c, buf);
2352
2353 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2354 * Happens for CTRL-Vu9900. */
2355 if (buf[0] == 0)
2356 buf[0] = '\n';
Bram Moolenaar560379d2017-01-21 22:50:00 +01002357#else
2358 buf[0] = c;
2359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360
2361 ins_char_bytes(buf, n);
2362}
2363
2364 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002365ins_char_bytes(char_u *buf, int charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366{
2367 int c = buf[0];
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002368 int newlen; // nr of bytes inserted
2369 int oldlen; // nr of bytes deleted (0 when not replacing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 char_u *p;
2371 char_u *newp;
2372 char_u *oldp;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002373 int linelen; // length of old line including NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 colnr_T col;
2375 linenr_T lnum = curwin->w_cursor.lnum;
2376 int i;
2377
2378#ifdef FEAT_VIRTUALEDIT
2379 /* Break tabs if needed. */
2380 if (virtual_active() && curwin->w_cursor.coladd > 0)
2381 coladvance_force(getviscol());
2382#endif
2383
2384 col = curwin->w_cursor.col;
2385 oldp = ml_get(lnum);
2386 linelen = (int)STRLEN(oldp) + 1;
2387
2388 /* The lengths default to the values for when not replacing. */
2389 oldlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 newlen = charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391
2392 if (State & REPLACE_FLAG)
2393 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 if (State & VREPLACE_FLAG)
2395 {
2396 colnr_T new_vcol = 0; /* init for GCC */
2397 colnr_T vcol;
2398 int old_list;
2399#ifndef FEAT_MBYTE
2400 char_u buf[2];
2401#endif
2402
2403 /*
2404 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2405 * Returns the old value of list, so when finished,
2406 * curwin->w_p_list should be set back to this.
2407 */
2408 old_list = curwin->w_p_list;
2409 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2410 curwin->w_p_list = FALSE;
2411
2412 /*
2413 * In virtual replace mode each character may replace one or more
2414 * characters (zero if it's a TAB). Count the number of bytes to
2415 * be deleted to make room for the new character, counting screen
2416 * cells. May result in adding spaces to fill a gap.
2417 */
2418 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2419#ifndef FEAT_MBYTE
2420 buf[0] = c;
2421 buf[1] = NUL;
2422#endif
2423 new_vcol = vcol + chartabsize(buf, vcol);
2424 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2425 {
2426 vcol += chartabsize(oldp + col + oldlen, vcol);
2427 /* Don't need to remove a TAB that takes us to the right
2428 * position. */
2429 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2430 break;
2431#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002432 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433#else
2434 ++oldlen;
2435#endif
2436 /* Deleted a bit too much, insert spaces. */
2437 if (vcol > new_vcol)
2438 newlen += vcol - new_vcol;
2439 }
2440 curwin->w_p_list = old_list;
2441 }
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002442 else if (oldp[col] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 {
2444 /* normal replace */
2445#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002446 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447#else
2448 oldlen = 1;
2449#endif
2450 }
2451
2452
2453 /* Push the replaced bytes onto the replace stack, so that they can be
2454 * put back when BS is used. The bytes of a multi-byte character are
2455 * done the other way around, so that the first byte is popped off
2456 * first (it tells the byte length of the character). */
2457 replace_push(NUL);
2458 for (i = 0; i < oldlen; ++i)
2459 {
2460#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002461 if (has_mbyte)
2462 i += replace_push_mb(oldp + col + i) - 1;
2463 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002465 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 }
2467 }
2468
2469 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2470 if (newp == NULL)
2471 return;
2472
2473 /* Copy bytes before the cursor. */
2474 if (col > 0)
2475 mch_memmove(newp, oldp, (size_t)col);
2476
2477 /* Copy bytes after the changed character(s). */
2478 p = newp + col;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02002479 if (linelen > col + oldlen)
2480 mch_memmove(p + newlen, oldp + col + oldlen,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 (size_t)(linelen - col - oldlen));
2482
2483 /* Insert or overwrite the new character. */
2484#ifdef FEAT_MBYTE
2485 mch_memmove(p, buf, charlen);
2486 i = charlen;
2487#else
2488 *p = c;
2489 i = 1;
2490#endif
2491
2492 /* Fill with spaces when necessary. */
2493 while (i < newlen)
2494 p[i++] = ' ';
2495
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002496 // Replace the line in the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 ml_replace(lnum, newp, FALSE);
2498
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002499 // mark the buffer as changed and prepare for displaying
2500 inserted_bytes(lnum, col, newlen - oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501
2502 /*
2503 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2504 * show the match for right parens and braces.
2505 */
2506 if (p_sm && (State & INSERT)
2507 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002508#ifdef FEAT_INS_EXPAND
2509 && !ins_compl_active()
2510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002512 {
2513#ifdef FEAT_MBYTE
2514 if (has_mbyte)
2515 showmatch(mb_ptr2char(buf));
2516 else
2517#endif
2518 showmatch(c);
2519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520
2521#ifdef FEAT_RIGHTLEFT
2522 if (!p_ri || (State & REPLACE_FLAG))
2523#endif
2524 {
2525 /* Normal insert: move cursor right */
2526#ifdef FEAT_MBYTE
2527 curwin->w_cursor.col += charlen;
2528#else
2529 ++curwin->w_cursor.col;
2530#endif
2531 }
2532 /*
2533 * TODO: should try to update w_row here, to avoid recomputing it later.
2534 */
2535}
2536
2537/*
2538 * Insert a string at the cursor position.
2539 * Note: Does NOT handle Replace mode.
2540 * Caller must have prepared for undo.
2541 */
2542 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002543ins_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544{
2545 char_u *oldp, *newp;
2546 int newlen = (int)STRLEN(s);
2547 int oldlen;
2548 colnr_T col;
2549 linenr_T lnum = curwin->w_cursor.lnum;
2550
2551#ifdef FEAT_VIRTUALEDIT
2552 if (virtual_active() && curwin->w_cursor.coladd > 0)
2553 coladvance_force(getviscol());
2554#endif
2555
2556 col = curwin->w_cursor.col;
2557 oldp = ml_get(lnum);
2558 oldlen = (int)STRLEN(oldp);
2559
2560 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2561 if (newp == NULL)
2562 return;
2563 if (col > 0)
2564 mch_memmove(newp, oldp, (size_t)col);
2565 mch_memmove(newp + col, s, (size_t)newlen);
2566 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2567 ml_replace(lnum, newp, FALSE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002568 inserted_bytes(lnum, col, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 curwin->w_cursor.col += newlen;
2570}
2571
2572/*
2573 * Delete one character under the cursor.
2574 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2575 * Caller must have prepared for undo.
2576 *
2577 * return FAIL for failure, OK otherwise
2578 */
2579 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002580del_char(int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581{
2582#ifdef FEAT_MBYTE
2583 if (has_mbyte)
2584 {
2585 /* Make sure the cursor is at the start of a character. */
2586 mb_adjust_cursor();
2587 if (*ml_get_cursor() == NUL)
2588 return FAIL;
2589 return del_chars(1L, fixpos);
2590 }
2591#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002592 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593}
2594
2595#if defined(FEAT_MBYTE) || defined(PROTO)
2596/*
2597 * Like del_bytes(), but delete characters instead of bytes.
2598 */
2599 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002600del_chars(long count, int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601{
2602 long bytes = 0;
2603 long i;
2604 char_u *p;
2605 int l;
2606
2607 p = ml_get_cursor();
2608 for (i = 0; i < count && *p != NUL; ++i)
2609 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002610 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 bytes += l;
2612 p += l;
2613 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002614 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615}
2616#endif
2617
2618/*
2619 * Delete "count" bytes under the cursor.
2620 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2621 * Caller must have prepared for undo.
2622 *
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002623 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 */
2625 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002626del_bytes(
2627 long count,
2628 int fixpos_arg,
2629 int use_delcombine UNUSED) /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630{
2631 char_u *oldp, *newp;
2632 colnr_T oldlen;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002633 colnr_T newlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 linenr_T lnum = curwin->w_cursor.lnum;
2635 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002636 int alloc_newp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002638 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639
2640 oldp = ml_get(lnum);
2641 oldlen = (int)STRLEN(oldp);
2642
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002643 /* Can't do anything when the cursor is on the NUL after the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 if (col >= oldlen)
2645 return FAIL;
2646
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002647 /* If "count" is zero there is nothing to do. */
2648 if (count == 0)
2649 return OK;
2650
2651 /* If "count" is negative the caller must be doing something wrong. */
2652 if (count < 1)
2653 {
2654 IEMSGN("E950: Invalid count for del_bytes(): %ld", count);
2655 return FAIL;
2656 }
2657
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658#ifdef FEAT_MBYTE
2659 /* If 'delcombine' is set and deleting (less than) one character, only
2660 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002661 if (p_deco && use_delcombine && enc_utf8
2662 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002664 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 int n;
2666
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002667 (void)utfc_ptr2char(oldp + col, cc);
2668 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 {
2670 /* Find the last composing char, there can be several. */
2671 n = col;
2672 do
2673 {
2674 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002675 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 n += count;
2677 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2678 fixpos = 0;
2679 }
2680 }
2681#endif
2682
2683 /*
2684 * When count is too big, reduce it.
2685 */
2686 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2687 if (movelen <= 1)
2688 {
2689 /*
2690 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002691 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2692 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002694 if (col > 0 && fixpos && restart_edit == 0
2695#ifdef FEAT_VIRTUALEDIT
2696 && (ve_flags & VE_ONEMORE) == 0
2697#endif
2698 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 {
2700 --curwin->w_cursor.col;
2701#ifdef FEAT_VIRTUALEDIT
2702 curwin->w_cursor.coladd = 0;
2703#endif
2704#ifdef FEAT_MBYTE
2705 if (has_mbyte)
2706 curwin->w_cursor.col -=
2707 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2708#endif
2709 }
2710 count = oldlen - col;
2711 movelen = 1;
2712 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002713 newlen = oldlen - count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714
2715 /*
2716 * If the old line has been allocated the deletion can be done in the
2717 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002718 * Can't do this when using Netbeans, because we would need to invoke
2719 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002720 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002723 if (netbeans_active())
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002724 alloc_newp = TRUE;
Bram Moolenaare21877a2008-02-13 09:58:14 +00002725 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002727 alloc_newp = !ml_line_alloced(); // check if oldp was allocated
2728 if (!alloc_newp)
2729 newp = oldp; // use same allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 else
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002731 { // need to allocate a new line
2732 newp = alloc((unsigned)(newlen + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 if (newp == NULL)
2734 return FAIL;
2735 mch_memmove(newp, oldp, (size_t)col);
2736 }
2737 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002738 if (alloc_newp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 ml_replace(lnum, newp, FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002740#ifdef FEAT_TEXT_PROP
2741 else
2742 {
2743 // Also move any following text properties.
2744 if (oldlen + 1 < curbuf->b_ml.ml_line_len)
2745 mch_memmove(newp + newlen + 1, oldp + oldlen + 1,
2746 (size_t)curbuf->b_ml.ml_line_len - oldlen - 1);
2747 curbuf->b_ml.ml_line_len -= count;
2748 }
2749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002751 // mark the buffer as changed and prepare for displaying
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 changed_bytes(lnum, curwin->w_cursor.col);
2753
2754 return OK;
2755}
2756
2757/*
2758 * Delete from cursor to end of line.
2759 * Caller must have prepared for undo.
2760 *
2761 * return FAIL for failure, OK otherwise
2762 */
2763 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002764truncate_line(
2765 int fixpos) /* if TRUE fix the cursor position when done */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766{
2767 char_u *newp;
2768 linenr_T lnum = curwin->w_cursor.lnum;
2769 colnr_T col = curwin->w_cursor.col;
2770
2771 if (col == 0)
2772 newp = vim_strsave((char_u *)"");
2773 else
2774 newp = vim_strnsave(ml_get(lnum), col);
2775
2776 if (newp == NULL)
2777 return FAIL;
2778
2779 ml_replace(lnum, newp, FALSE);
2780
2781 /* mark the buffer as changed and prepare for displaying */
2782 changed_bytes(lnum, curwin->w_cursor.col);
2783
2784 /*
2785 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2786 */
2787 if (fixpos && curwin->w_cursor.col > 0)
2788 --curwin->w_cursor.col;
2789
2790 return OK;
2791}
2792
2793/*
2794 * Delete "nlines" lines at the cursor.
2795 * Saves the lines for undo first if "undo" is TRUE.
2796 */
2797 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002798del_lines(
2799 long nlines, /* number of lines to delete */
2800 int undo) /* if TRUE, prepare for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801{
2802 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002803 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804
2805 if (nlines <= 0)
2806 return;
2807
2808 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002809 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 return;
2811
2812 for (n = 0; n < nlines; )
2813 {
2814 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2815 break;
2816
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002817 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 ++n;
2819
2820 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002821 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 break;
2823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002825 /* Correct the cursor position before calling deleted_lines_mark(), it may
2826 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 curwin->w_cursor.col = 0;
2828 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002829
2830 /* adjust marks, mark the buffer as changed and prepare for displaying */
2831 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832}
2833
2834 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002835gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002837 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002839 /* When searching columns is sometimes put at the end of a line. */
2840 if (pos->col == MAXCOL)
2841 return NUL;
2842 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843#ifdef FEAT_MBYTE
2844 if (has_mbyte)
2845 return (*mb_ptr2char)(ptr);
2846#endif
2847 return (int)*ptr;
2848}
2849
2850 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002851gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852{
2853#ifdef FEAT_MBYTE
2854 if (has_mbyte)
2855 return (*mb_ptr2char)(ml_get_cursor());
2856#endif
2857 return (int)*ml_get_cursor();
2858}
2859
2860/*
2861 * Write a character at the current cursor position.
2862 * It is directly written into the block.
2863 */
2864 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002865pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866{
2867 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2868 + curwin->w_cursor.col) = c;
2869}
2870
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871/*
2872 * When extra == 0: Return TRUE if the cursor is before or on the first
2873 * non-blank in the line.
2874 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2875 * the line.
2876 */
2877 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002878inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879{
2880 char_u *ptr;
2881 colnr_T col;
2882
Bram Moolenaar1c465442017-03-12 20:10:05 +01002883 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 ++ptr;
2885 if (col >= curwin->w_cursor.col + extra)
2886 return TRUE;
2887 else
2888 return FALSE;
2889}
2890
2891/*
2892 * Skip to next part of an option argument: Skip space and comma.
2893 */
2894 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002895skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896{
2897 if (*p == ',')
2898 ++p;
2899 while (*p == ' ')
2900 ++p;
2901 return p;
2902}
2903
2904/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002905 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 *
2907 * Most often called through changed_bytes() and changed_lines(), which also
2908 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002909 *
2910 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 */
2912 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002913changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914{
2915#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02002916 if (p_imst == IM_ON_THE_SPOT)
2917 {
2918 /* The text of the preediting area is inserted, but this doesn't
2919 * mean a change of the buffer yet. That is delayed until the
2920 * text is committed. (this means preedit becomes empty) */
2921 if (im_is_preediting() && !xim_changed_while_preediting)
2922 return;
2923 xim_changed_while_preediting = FALSE;
2924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925#endif
2926
2927 if (!curbuf->b_changed)
2928 {
2929 int save_msg_scroll = msg_scroll;
2930
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002931 /* Give a warning about changing a read-only file. This may also
2932 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002934
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 /* Create a swap file if that is wanted.
2936 * Don't do this for "nofile" and "nowrite" buffer types. */
2937 if (curbuf->b_may_swap
2938#ifdef FEAT_QUICKFIX
2939 && !bt_dontwrite(curbuf)
2940#endif
2941 )
2942 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002943 int save_need_wait_return = need_wait_return;
2944
2945 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 ml_open_file(curbuf);
2947
2948 /* The ml_open_file() can cause an ATTENTION message.
2949 * Wait two seconds, to make sure the user reads this unexpected
2950 * message. Since we could be anywhere, call wait_return() now,
2951 * and don't let the emsg() set msg_scroll. */
2952 if (need_wait_return && emsg_silent == 0)
2953 {
2954 out_flush();
2955 ui_delay(2000L, TRUE);
2956 wait_return(TRUE);
2957 msg_scroll = save_msg_scroll;
2958 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002959 else
2960 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002962 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002964 ++CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965}
2966
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002967/*
2968 * Internal part of changed(), no user interaction.
2969 */
2970 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002971changed_int(void)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002972{
2973 curbuf->b_changed = TRUE;
2974 ml_setflags(curbuf);
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002975 check_status(curbuf);
2976 redraw_tabline = TRUE;
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002977#ifdef FEAT_TITLE
2978 need_maketitle = TRUE; /* set window title later */
2979#endif
2980}
2981
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002982static void changedOneline(buf_T *buf, linenr_T lnum);
2983static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
2984static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985
2986/*
2987 * Changed bytes within a single line for the current buffer.
2988 * - marks the windows on this buffer to be redisplayed
2989 * - marks the buffer changed by calling changed()
2990 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002991 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 */
2993 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002994changed_bytes(linenr_T lnum, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002996 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002998
2999#ifdef FEAT_DIFF
3000 /* Diff highlighting in other diff windows may need to be updated too. */
3001 if (curwin->w_p_diff)
3002 {
3003 win_T *wp;
3004 linenr_T wlnum;
3005
Bram Moolenaar29323592016-07-24 22:04:11 +02003006 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00003007 if (wp->w_p_diff && wp != curwin)
3008 {
3009 redraw_win_later(wp, VALID);
3010 wlnum = diff_lnum_win(lnum, wp);
3011 if (wlnum > 0)
3012 changedOneline(wp->w_buffer, wlnum);
3013 }
3014 }
3015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016}
3017
Bram Moolenaar44746aa2019-01-02 00:02:11 +01003018/*
3019 * Like changed_bytes() but also adjust text properties for "added" bytes.
3020 * When "added" is negative text was deleted.
3021 */
3022 void
3023inserted_bytes(linenr_T lnum, colnr_T col, int added)
3024{
3025 changed_bytes(lnum, col);
3026
3027#ifdef FEAT_TEXT_PROP
3028 if (curbuf->b_has_textprop && added != 0)
3029 adjust_prop_columns(lnum, col, added);
3030#endif
3031}
3032
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003034changedOneline(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035{
Bram Moolenaardba8a912005-04-24 22:08:39 +00003036 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 {
3038 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003039 if (lnum < buf->b_mod_top)
3040 buf->b_mod_top = lnum;
3041 else if (lnum >= buf->b_mod_bot)
3042 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 }
3044 else
3045 {
3046 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003047 buf->b_mod_set = TRUE;
3048 buf->b_mod_top = lnum;
3049 buf->b_mod_bot = lnum + 1;
3050 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 }
3052}
3053
3054/*
3055 * Appended "count" lines below line "lnum" in the current buffer.
3056 * Must be called AFTER the change and after mark_adjust().
3057 * Takes care of marking the buffer to be redrawn and sets the changed flag.
3058 */
3059 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003060appended_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061{
3062 changed_lines(lnum + 1, 0, lnum + 1, count);
3063}
3064
3065/*
3066 * Like appended_lines(), but adjust marks first.
3067 */
3068 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003069appended_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070{
Bram Moolenaar82faa252016-06-04 20:14:07 +02003071 /* Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003072 * be marks there. But it's still needed in diff mode. */
3073 if (lnum + count < curbuf->b_ml.ml_line_count
3074#ifdef FEAT_DIFF
3075 || curwin->w_p_diff
3076#endif
3077 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02003078 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 changed_lines(lnum + 1, 0, lnum + 1, count);
3080}
3081
3082/*
3083 * Deleted "count" lines at line "lnum" in the current buffer.
3084 * Must be called AFTER the change and after mark_adjust().
3085 * Takes care of marking the buffer to be redrawn and sets the changed flag.
3086 */
3087 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003088deleted_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089{
3090 changed_lines(lnum, 0, lnum + count, -count);
3091}
3092
3093/*
3094 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00003095 * Make sure the cursor is on a valid line before calling, a GUI callback may
3096 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 */
3098 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003099deleted_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100{
3101 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
3102 changed_lines(lnum, 0, lnum + count, -count);
3103}
3104
3105/*
3106 * Changed lines for the current buffer.
3107 * Must be called AFTER the change and after mark_adjust().
3108 * - mark the buffer changed by calling changed()
3109 * - mark the windows on this buffer to be redisplayed
3110 * - invalidate cached values
3111 * "lnum" is the first line that needs displaying, "lnume" the first line
3112 * below the changed lines (BEFORE the change).
3113 * When only inserting lines, "lnum" and "lnume" are equal.
3114 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003115 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 */
3117 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003118changed_lines(
3119 linenr_T lnum, /* first line with change */
3120 colnr_T col, /* column in first line with change */
3121 linenr_T lnume, /* line below last changed line */
3122 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123{
Bram Moolenaardba8a912005-04-24 22:08:39 +00003124 changed_lines_buf(curbuf, lnum, lnume, xtra);
3125
3126#ifdef FEAT_DIFF
Bram Moolenaare3521d92018-09-16 14:10:31 +02003127 if (xtra == 0 && curwin->w_p_diff && !diff_internal())
Bram Moolenaardba8a912005-04-24 22:08:39 +00003128 {
3129 /* When the number of lines doesn't change then mark_adjust() isn't
3130 * called and other diff buffers still need to be marked for
3131 * displaying. */
3132 win_T *wp;
3133 linenr_T wlnum;
3134
Bram Moolenaar29323592016-07-24 22:04:11 +02003135 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00003136 if (wp->w_p_diff && wp != curwin)
3137 {
3138 redraw_win_later(wp, VALID);
3139 wlnum = diff_lnum_win(lnum, wp);
3140 if (wlnum > 0)
3141 changed_lines_buf(wp->w_buffer, wlnum,
3142 lnume - lnum + wlnum, 0L);
3143 }
3144 }
3145#endif
3146
3147 changed_common(lnum, col, lnume, xtra);
3148}
3149
3150 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003151changed_lines_buf(
3152 buf_T *buf,
3153 linenr_T lnum, /* first line with change */
3154 linenr_T lnume, /* line below last changed line */
3155 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003156{
3157 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 {
3159 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003160 if (lnum < buf->b_mod_top)
3161 buf->b_mod_top = lnum;
3162 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 {
3164 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003165 buf->b_mod_bot += xtra;
3166 if (buf->b_mod_bot < lnum)
3167 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00003169 if (lnume + xtra > buf->b_mod_bot)
3170 buf->b_mod_bot = lnume + xtra;
3171 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 }
3173 else
3174 {
3175 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003176 buf->b_mod_set = TRUE;
3177 buf->b_mod_top = lnum;
3178 buf->b_mod_bot = lnume + xtra;
3179 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181}
3182
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003183/*
3184 * Common code for when a change is was made.
3185 * See changed_lines() for the arguments.
3186 * Careful: may trigger autocommands that reload the buffer.
3187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003189changed_common(
3190 linenr_T lnum,
3191 colnr_T col,
3192 linenr_T lnume,
3193 long xtra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194{
3195 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003196 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 int i;
3198#ifdef FEAT_JUMPLIST
3199 int cols;
3200 pos_T *p;
3201 int add;
3202#endif
3203
3204 /* mark the buffer as modified */
3205 changed();
3206
Bram Moolenaare3521d92018-09-16 14:10:31 +02003207#ifdef FEAT_DIFF
3208 if (curwin->w_p_diff && diff_internal())
3209 curtab->tp_diff_update = TRUE;
3210#endif
3211
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 /* set the '. mark */
3213 if (!cmdmod.keepjumps)
3214 {
3215 curbuf->b_last_change.lnum = lnum;
3216 curbuf->b_last_change.col = col;
3217
3218#ifdef FEAT_JUMPLIST
3219 /* Create a new entry if a new undo-able change was started or we
3220 * don't have an entry yet. */
3221 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3222 {
3223 if (curbuf->b_changelistlen == 0)
3224 add = TRUE;
3225 else
3226 {
3227 /* Don't create a new entry when the line number is the same
3228 * as the last one and the column is not too far away. Avoids
3229 * creating many entries for typing "xxxxx". */
3230 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3231 if (p->lnum != lnum)
3232 add = TRUE;
3233 else
3234 {
3235 cols = comp_textwidth(FALSE);
3236 if (cols == 0)
3237 cols = 79;
3238 add = (p->col + cols < col || col + cols < p->col);
3239 }
3240 }
3241 if (add)
3242 {
3243 /* This is the first of a new sequence of undo-able changes
3244 * and it's at some distance of the last change. Use a new
3245 * position in the changelist. */
3246 curbuf->b_new_change = FALSE;
3247
3248 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3249 {
3250 /* changelist is full: remove oldest entry */
3251 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3252 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3253 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003254 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 {
3256 /* Correct position in changelist for other windows on
3257 * this buffer. */
3258 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3259 --wp->w_changelistidx;
3260 }
3261 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003262 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 {
3264 /* For other windows, if the position in the changelist is
3265 * at the end it stays at the end. */
3266 if (wp->w_buffer == curbuf
3267 && wp->w_changelistidx == curbuf->b_changelistlen)
3268 ++wp->w_changelistidx;
3269 }
3270 ++curbuf->b_changelistlen;
3271 }
3272 }
3273 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3274 curbuf->b_last_change;
3275 /* The current window is always after the last change, so that "g,"
3276 * takes you back to it. */
3277 curwin->w_changelistidx = curbuf->b_changelistlen;
3278#endif
3279 }
3280
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003281 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 {
3283 if (wp->w_buffer == curbuf)
3284 {
3285 /* Mark this window to be redrawn later. */
3286 if (wp->w_redr_type < VALID)
3287 wp->w_redr_type = VALID;
3288
3289 /* Check if a change in the buffer has invalidated the cached
3290 * values for the cursor. */
3291#ifdef FEAT_FOLDING
3292 /*
3293 * Update the folds for this window. Can't postpone this, because
3294 * a following operator might work on the whole fold: ">>dd".
3295 */
3296 foldUpdate(wp, lnum, lnume + xtra - 1);
3297
3298 /* The change may cause lines above or below the change to become
3299 * included in a fold. Set lnum/lnume to the first/last line that
3300 * might be displayed differently.
3301 * Set w_cline_folded here as an efficient way to update it when
3302 * inserting lines just above a closed fold. */
3303 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3304 if (wp->w_cursor.lnum == lnum)
3305 wp->w_cline_folded = i;
3306 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3307 if (wp->w_cursor.lnum == lnume)
3308 wp->w_cline_folded = i;
3309
3310 /* If the changed line is in a range of previously folded lines,
3311 * compare with the first line in that range. */
3312 if (wp->w_cursor.lnum <= lnum)
3313 {
3314 i = find_wl_entry(wp, lnum);
3315 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3316 changed_line_abv_curs_win(wp);
3317 }
3318#endif
3319
3320 if (wp->w_cursor.lnum > lnum)
3321 changed_line_abv_curs_win(wp);
3322 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3323 changed_cline_bef_curs_win(wp);
3324 if (wp->w_botline >= lnum)
3325 {
3326 /* Assume that botline doesn't change (inserted lines make
3327 * other lines scroll down below botline). */
3328 approximate_botline_win(wp);
3329 }
3330
3331 /* Check if any w_lines[] entries have become invalid.
3332 * For entries below the change: Correct the lnums for
3333 * inserted/deleted lines. Makes it possible to stop displaying
3334 * after the change. */
3335 for (i = 0; i < wp->w_lines_valid; ++i)
3336 if (wp->w_lines[i].wl_valid)
3337 {
3338 if (wp->w_lines[i].wl_lnum >= lnum)
3339 {
3340 if (wp->w_lines[i].wl_lnum < lnume)
3341 {
3342 /* line included in change */
3343 wp->w_lines[i].wl_valid = FALSE;
3344 }
3345 else if (xtra != 0)
3346 {
3347 /* line below change */
3348 wp->w_lines[i].wl_lnum += xtra;
3349#ifdef FEAT_FOLDING
3350 wp->w_lines[i].wl_lastlnum += xtra;
3351#endif
3352 }
3353 }
3354#ifdef FEAT_FOLDING
3355 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3356 {
3357 /* change somewhere inside this range of folded lines,
3358 * may need to be redrawn */
3359 wp->w_lines[i].wl_valid = FALSE;
3360 }
3361#endif
3362 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003363
3364#ifdef FEAT_FOLDING
3365 /* Take care of side effects for setting w_topline when folds have
3366 * changed. Esp. when the buffer was changed in another window. */
3367 if (hasAnyFolding(wp))
3368 set_topline(wp, wp->w_topline);
3369#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003370 /* relative numbering may require updating more */
3371 if (wp->w_p_rnu)
3372 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 }
3374 }
3375
3376 /* Call update_screen() later, which checks out what needs to be redrawn,
3377 * since it notices b_mod_set and then uses b_mod_*. */
3378 if (must_redraw < VALID)
3379 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003380
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003381 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003382 if (lnum <= curwin->w_cursor.lnum
3383 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003384 last_cursormoved.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385}
3386
3387/*
3388 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3389 */
3390 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003391unchanged(
3392 buf_T *buf,
3393 int ff) /* also reset 'fileformat' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003395 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 {
3397 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003398 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 if (ff)
3400 save_file_ff(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003402 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403#ifdef FEAT_TITLE
3404 need_maketitle = TRUE; /* set window title later */
3405#endif
3406 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003407 ++CHANGEDTICK(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408#ifdef FEAT_NETBEANS_INTG
3409 netbeans_unmodified(buf);
3410#endif
3411}
3412
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413/*
3414 * check_status: called when the status bars for the buffer 'buf'
3415 * need to be updated
3416 */
3417 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003418check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419{
3420 win_T *wp;
3421
Bram Moolenaar29323592016-07-24 22:04:11 +02003422 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 if (wp->w_buffer == buf && wp->w_status_height)
3424 {
3425 wp->w_redr_status = TRUE;
3426 if (must_redraw < VALID)
3427 must_redraw = VALID;
3428 }
3429}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
3431/*
3432 * If the file is readonly, give a warning message with the first change.
3433 * Don't do this for autocommands.
3434 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003435 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003437 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 */
3439 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003440change_warning(
3441 int col) /* column for message; non-zero when in insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 mode and 'showmode' is on */
3443{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003444 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3445
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 if (curbuf->b_did_warn == FALSE
3447 && curbufIsChanged() == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 && !autocmd_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 && curbuf->b_p_ro)
3450 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003451 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003453 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 if (!curbuf->b_p_ro)
3455 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 /*
3457 * Do what msg() does, but with a column offset if the warning should
3458 * be after the mode message.
3459 */
3460 msg_start();
3461 if (msg_row == Rows - 1)
3462 msg_col = col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003463 msg_source(HL_ATTR(HLF_W));
3464 MSG_PUTS_ATTR(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST);
Bram Moolenaar496c5262009-03-18 14:42:00 +00003465#ifdef FEAT_EVAL
3466 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 msg_clr_eos();
3469 (void)msg_end();
Bram Moolenaare5f2a072017-02-01 22:31:49 +01003470 if (msg_silent == 0 && !silent_mode
3471#ifdef FEAT_EVAL
3472 && time_for_testing != 1
3473#endif
3474 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
3476 out_flush();
3477 ui_delay(1000L, TRUE); /* give the user time to think about it */
3478 }
3479 curbuf->b_did_warn = TRUE;
3480 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3481 if (msg_row < Rows - 1)
3482 showmode();
3483 }
3484}
3485
3486/*
3487 * Ask for a reply from the user, a 'y' or a 'n'.
3488 * No other characters are accepted, the message is repeated until a valid
3489 * reply is entered or CTRL-C is hit.
3490 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3491 * from any buffers but directly from the user.
3492 *
3493 * return the 'y' or 'n'
3494 */
3495 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003496ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497{
3498 int r = ' ';
3499 int save_State = State;
3500
3501 if (exiting) /* put terminal in raw mode for this question */
3502 settmode(TMODE_RAW);
3503 ++no_wait_return;
3504#ifdef USE_ON_FLY_SCROLL
3505 dont_scroll = TRUE; /* disallow scrolling here */
3506#endif
3507 State = CONFIRM; /* mouse behaves like with :confirm */
3508#ifdef FEAT_MOUSE
3509 setmouse(); /* disables mouse for xterm */
3510#endif
3511 ++no_mapping;
3512 ++allow_keys; /* no mapping here, but recognize keys */
3513
3514 while (r != 'y' && r != 'n')
3515 {
3516 /* same highlighting as for wait_return */
Bram Moolenaar8820b482017-03-16 17:23:31 +01003517 smsg_attr(HL_ATTR(HLF_R), (char_u *)"%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 if (direct)
3519 r = get_keystroke();
3520 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003521 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 if (r == Ctrl_C || r == ESC)
3523 r = 'n';
3524 msg_putchar(r); /* show what you typed */
3525 out_flush();
3526 }
3527 --no_wait_return;
3528 State = save_State;
3529#ifdef FEAT_MOUSE
3530 setmouse();
3531#endif
3532 --no_mapping;
3533 --allow_keys;
3534
3535 return r;
3536}
3537
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003538#if defined(FEAT_MOUSE) || defined(PROTO)
3539/*
3540 * Return TRUE if "c" is a mouse key.
3541 */
3542 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003543is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003544{
3545 return c == K_LEFTMOUSE
3546 || c == K_LEFTMOUSE_NM
3547 || c == K_LEFTDRAG
3548 || c == K_LEFTRELEASE
3549 || c == K_LEFTRELEASE_NM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003550 || c == K_MOUSEMOVE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003551 || c == K_MIDDLEMOUSE
3552 || c == K_MIDDLEDRAG
3553 || c == K_MIDDLERELEASE
3554 || c == K_RIGHTMOUSE
3555 || c == K_RIGHTDRAG
3556 || c == K_RIGHTRELEASE
3557 || c == K_MOUSEDOWN
3558 || c == K_MOUSEUP
3559 || c == K_MOUSELEFT
3560 || c == K_MOUSERIGHT
3561 || c == K_X1MOUSE
3562 || c == K_X1DRAG
3563 || c == K_X1RELEASE
3564 || c == K_X2MOUSE
3565 || c == K_X2DRAG
3566 || c == K_X2RELEASE;
3567}
3568#endif
3569
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570/*
3571 * Get a key stroke directly from the user.
3572 * Ignores mouse clicks and scrollbar events, except a click for the left
3573 * button (used at the more prompt).
3574 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3575 * Disadvantage: typeahead is ignored.
3576 * Translates the interrupt character for unix to ESC.
3577 */
3578 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003579get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003581 char_u *buf = NULL;
3582 int buflen = 150;
3583 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 int len = 0;
3585 int n;
3586 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003587 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588
3589 mapped_ctrl_c = FALSE; /* mappings are not used here */
3590 for (;;)
3591 {
3592 cursor_on();
3593 out_flush();
3594
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003595 /* Leave some room for check_termcode() to insert a key code into (max
3596 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3597 * bytes. */
3598 maxlen = (buflen - 6 - len) / 3;
3599 if (buf == NULL)
3600 buf = alloc(buflen);
3601 else if (maxlen < 10)
3602 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003603 char_u *t_buf = buf;
3604
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003605 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003606 * escape sequence. */
3607 buflen += 100;
3608 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003609 if (buf == NULL)
3610 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003611 maxlen = (buflen - 6 - len) / 3;
3612 }
3613 if (buf == NULL)
3614 {
3615 do_outofmem_msg((long_u)buflen);
3616 return ESC; /* panic! */
3617 }
3618
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003620 * terminal code to complete. */
3621 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 if (n > 0)
3623 {
3624 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003625 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003627 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003629 else if (len > 0)
3630 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631
Bram Moolenaar4395a712006-09-05 18:57:57 +00003632 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003633 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003634 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003636
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003637 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003638 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003639 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003640 {
3641 /* Redrawing was postponed, do it now. */
3642 update_screen(0);
3643 setcursor(); /* put cursor back where it belongs */
3644 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003645 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003646 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003647 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003649 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 continue;
3651
3652 /* Handle modifier and/or special key code. */
3653 n = buf[0];
3654 if (n == K_SPECIAL)
3655 {
3656 n = TO_SPECIAL(buf[1], buf[2]);
3657 if (buf[1] == KS_MODIFIER
3658 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003659#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003660 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003661#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003662#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 || n == K_VER_SCROLLBAR
3664 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665#endif
3666 )
3667 {
3668 if (buf[1] == KS_MODIFIER)
3669 mod_mask = buf[2];
3670 len -= 3;
3671 if (len > 0)
3672 mch_memmove(buf, buf + 3, (size_t)len);
3673 continue;
3674 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003675 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 }
3677#ifdef FEAT_MBYTE
3678 if (has_mbyte)
3679 {
3680 if (MB_BYTE2LEN(n) > len)
3681 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003682 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 n = (*mb_ptr2char)(buf);
3684 }
3685#endif
3686#ifdef UNIX
3687 if (n == intr_char)
3688 n = ESC;
3689#endif
3690 break;
3691 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003692 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693
3694 mapped_ctrl_c = save_mapped_ctrl_c;
3695 return n;
3696}
3697
3698/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003699 * Get a number from the user.
3700 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 */
3702 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003703get_number(
3704 int colon, /* allow colon to abort */
3705 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706{
3707 int n = 0;
3708 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003709 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003711 if (mouse_used != NULL)
3712 *mouse_used = FALSE;
3713
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 /* When not printing messages, the user won't know what to type, return a
3715 * zero (as if CR was hit). */
3716 if (msg_silent != 0)
3717 return 0;
3718
3719#ifdef USE_ON_FLY_SCROLL
3720 dont_scroll = TRUE; /* disallow scrolling here */
3721#endif
3722 ++no_mapping;
3723 ++allow_keys; /* no mapping here, but recognize keys */
3724 for (;;)
3725 {
3726 windgoto(msg_row, msg_col);
3727 c = safe_vgetc();
3728 if (VIM_ISDIGIT(c))
3729 {
3730 n = n * 10 + c - '0';
3731 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003732 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 }
3734 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3735 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003736 if (typed > 0)
3737 {
3738 MSG_PUTS("\b \b");
3739 --typed;
3740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003743#ifdef FEAT_MOUSE
3744 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3745 {
3746 *mouse_used = TRUE;
3747 n = mouse_row + 1;
3748 break;
3749 }
3750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 else if (n == 0 && c == ':' && colon)
3752 {
3753 stuffcharReadbuff(':');
3754 if (!exmode_active)
3755 cmdline_row = msg_row;
3756 skip_redraw = TRUE; /* skip redraw once */
3757 do_redraw = FALSE;
3758 break;
3759 }
3760 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3761 break;
3762 }
3763 --no_mapping;
3764 --allow_keys;
3765 return n;
3766}
3767
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003768/*
3769 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003770 * When "mouse_used" is not NULL allow using the mouse and in that case return
3771 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003772 */
3773 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003774prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003775{
3776 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003777 int save_cmdline_row;
3778 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003779
3780 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003781 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003782 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003783 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003784 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003785
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003786 // Set the state such that text can be selected/copied/pasted and we still
3787 // get mouse events. redraw_after_callback() will not redraw if cmdline_row
3788 // is zero.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003789 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003790 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003791 save_State = State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003792 State = CMDLINE;
Bram Moolenaar73658312018-04-24 17:41:57 +02003793#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003794 // May show different mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02003795 setmouse();
3796#endif
3797
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003798 i = get_number(TRUE, mouse_used);
3799 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003800 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003801 /* don't call wait_return() now */
3802 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003803 cmdline_row = msg_row - 1;
3804 need_wait_return = FALSE;
3805 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003806 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003807 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003808 else
3809 cmdline_row = save_cmdline_row;
3810 State = save_State;
Bram Moolenaar73658312018-04-24 17:41:57 +02003811#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003812 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02003813 setmouse();
3814#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003815
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003816 return i;
3817}
3818
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003820msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821{
3822 long pn;
3823
3824 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3826 return;
3827
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003828 /* We don't want to overwrite another important message, but do overwrite
3829 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3830 * then "put" reports the last action. */
3831 if (keep_msg != NULL && !keep_msg_more)
3832 return;
3833
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 if (n > 0)
3835 pn = n;
3836 else
3837 pn = -n;
3838
3839 if (pn > p_report)
3840 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003841 if (n > 0)
3842 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3843 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003845 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3846 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003848 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 if (msg(msg_buf))
3850 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003851 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003852 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 }
3854 }
3855}
3856
3857/*
3858 * flush map and typeahead buffers and give a warning for an error
3859 */
3860 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003861beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862{
3863 if (emsg_silent == 0)
3864 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003865 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003866 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 }
3868}
3869
3870/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003871 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 */
3873 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003874vim_beep(
3875 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003877#ifdef FEAT_EVAL
3878 called_vim_beep = TRUE;
3879#endif
3880
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 if (emsg_silent == 0)
3882 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003883 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3884 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003885#ifdef ELAPSED_FUNC
3886 static int did_init = FALSE;
3887 static ELAPSED_TYPE start_tv;
3888
3889 /* Only beep once per half a second, otherwise a sequence of beeps
3890 * would freeze Vim. */
3891 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
3892 {
3893 did_init = TRUE;
3894 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003896 if (p_vb
3897#ifdef FEAT_GUI
3898 /* While the GUI is starting up the termcap is set for
3899 * the GUI but the output still goes to a terminal. */
3900 && !(gui.in_use && gui.starting)
3901#endif
3902 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003903 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003904 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003905#ifdef FEAT_VTP
3906 /* No restore color information, refresh the screen. */
3907 if (has_vtp_working() != 0
3908# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003909 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003910# endif
3911 )
3912 {
3913 redraw_later(CLEAR);
3914 update_screen(0);
3915 redrawcmd();
3916 }
3917#endif
3918 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003919 else
3920 out_char(BELL);
3921#ifdef ELAPSED_FUNC
3922 }
3923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003925
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003926 /* When 'debug' contains "beep" produce a message. If we are sourcing
3927 * a script or executing a function give the user a hint where the beep
3928 * comes from. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003929 if (vim_strchr(p_debug, 'e') != NULL)
3930 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003931 msg_source(HL_ATTR(HLF_W));
3932 msg_attr((char_u *)_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 }
3935}
3936
3937/*
3938 * To get the "real" home directory:
3939 * - get value of $HOME
3940 * For Unix:
3941 * - go to that directory
3942 * - do mch_dirname() to get the real name of that directory.
3943 * This also works with mounts and links.
3944 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
Bram Moolenaar25a494c2018-11-16 19:39:50 +01003945 * For Windows:
3946 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 */
3948static char_u *homedir = NULL;
3949
3950 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003951init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952{
3953 char_u *var;
3954
Bram Moolenaar05159a02005-02-26 23:04:13 +00003955 /* In case we are called a second time (when 'encoding' changes). */
Bram Moolenaard23a8232018-02-10 18:45:26 +01003956 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003957
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958#ifdef VMS
3959 var = mch_getenv((char_u *)"SYS$LOGIN");
3960#else
3961 var = mch_getenv((char_u *)"HOME");
3962#endif
3963
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964#ifdef WIN3264
3965 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02003966 * Typically, $HOME is not defined on Windows, unless the user has
3967 * specifically defined it for Vim's sake. However, on Windows NT
3968 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3969 * each user. Try constructing $HOME from these.
3970 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02003971 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02003972 {
3973 char_u *homedrive, *homepath;
3974
3975 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3976 homepath = mch_getenv((char_u *)"HOMEPATH");
3977 if (homepath == NULL || *homepath == NUL)
3978 homepath = (char_u *)"\\";
3979 if (homedrive != NULL
3980 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3981 {
3982 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3983 if (NameBuff[0] != NUL)
3984 var = NameBuff;
3985 }
3986 }
3987
3988 if (var == NULL)
3989 var = mch_getenv((char_u *)"USERPROFILE");
3990
3991 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 * Weird but true: $HOME may contain an indirect reference to another
3993 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3994 * when $HOME is being set.
3995 */
3996 if (var != NULL && *var == '%')
3997 {
3998 char_u *p;
3999 char_u *exp;
4000
4001 p = vim_strchr(var + 1, '%');
4002 if (p != NULL)
4003 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004004 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 exp = mch_getenv(NameBuff);
4006 if (exp != NULL && *exp != NUL
4007 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
4008 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00004009 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 }
4012 }
4013 }
4014
Bram Moolenaar48340b62017-08-29 22:08:53 +02004015 if (var != NULL && *var == NUL) /* empty is same as not set */
4016 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017
Bram Moolenaar48340b62017-08-29 22:08:53 +02004018# ifdef FEAT_MBYTE
Bram Moolenaar05159a02005-02-26 23:04:13 +00004019 if (enc_utf8 && var != NULL)
4020 {
4021 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004022 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004023
4024 /* Convert from active codepage to UTF-8. Other conversions are
4025 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004026 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004027 if (pp != NULL)
4028 {
4029 homedir = pp;
4030 return;
4031 }
4032 }
4033# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 /*
4036 * Default home dir is C:/
4037 * Best assumption we can make in such a situation.
4038 */
4039 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004040 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004042
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 if (var != NULL)
4044 {
4045#ifdef UNIX
4046 /*
4047 * Change to the directory and get the actual path. This resolves
4048 * links. Don't do it when we can't return.
4049 */
4050 if (mch_dirname(NameBuff, MAXPATHL) == OK
4051 && mch_chdir((char *)NameBuff) == 0)
4052 {
4053 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
4054 var = IObuff;
4055 if (mch_chdir((char *)NameBuff) != 0)
4056 EMSG(_(e_prev_dir));
4057 }
4058#endif
4059 homedir = vim_strsave(var);
4060 }
4061}
4062
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004063#if defined(EXITFREE) || defined(PROTO)
4064 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004065free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004066{
4067 vim_free(homedir);
4068}
Bram Moolenaar24305862012-08-15 14:05:05 +02004069
4070# ifdef FEAT_CMDL_COMPL
4071 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004072free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02004073{
4074 ga_clear_strings(&ga_users);
4075}
4076# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004077#endif
4078
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004080 * Call expand_env() and store the result in an allocated string.
4081 * This is not very memory efficient, this expects the result to be freed
4082 * again soon.
4083 */
4084 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004085expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004086{
4087 return expand_env_save_opt(src, FALSE);
4088}
4089
4090/*
4091 * Idem, but when "one" is TRUE handle the string as one file name, only
4092 * expand "~" at the start.
4093 */
4094 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004095expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004096{
4097 char_u *p;
4098
4099 p = alloc(MAXPATHL);
4100 if (p != NULL)
4101 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
4102 return p;
4103}
4104
4105/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 * Expand environment variable with path name.
4107 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004108 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 * If anything fails no expansion is done and dst equals src.
4110 */
4111 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004112expand_env(
4113 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
4114 char_u *dst, /* where to put the result */
4115 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004117 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118}
4119
4120 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004121expand_env_esc(
4122 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
4123 char_u *dst, /* where to put the result */
4124 int dstlen, /* maximum length of the result */
4125 int esc, /* escape spaces in expanded variables */
4126 int one, /* "srcp" is one file name */
4127 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004129 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 char_u *tail;
4131 int c;
4132 char_u *var;
4133 int copy_char;
4134 int mustfree; /* var was allocated, need to free it later */
4135 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004136 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004138 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004139 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004140
4141 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 --dstlen; /* leave one char space for "\," */
4143 while (*src && dstlen > 0)
4144 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004145#ifdef FEAT_EVAL
4146 /* Skip over `=expr`. */
4147 if (src[0] == '`' && src[1] == '=')
4148 {
4149 size_t len;
4150
4151 var = src;
4152 src += 2;
4153 (void)skip_expr(&src);
4154 if (*src == '`')
4155 ++src;
4156 len = src - var;
4157 if (len > (size_t)dstlen)
4158 len = dstlen;
4159 vim_strncpy(dst, var, len);
4160 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02004161 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004162 continue;
4163 }
4164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004166 if ((*src == '$'
4167#ifdef VMS
4168 && at_start
4169#endif
4170 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004171#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 || *src == '%'
4173#endif
4174 || (*src == '~' && at_start))
4175 {
4176 mustfree = FALSE;
4177
4178 /*
4179 * The variable name is copied into dst temporarily, because it may
4180 * be a string in read-only memory and a NUL needs to be appended.
4181 */
4182 if (*src != '~') /* environment var */
4183 {
4184 tail = src + 1;
4185 var = dst;
4186 c = dstlen - 1;
4187
4188#ifdef UNIX
4189 /* Unix has ${var-name} type environment vars */
4190 if (*tail == '{' && !vim_isIDc('{'))
4191 {
4192 tail++; /* ignore '{' */
4193 while (c-- > 0 && *tail && *tail != '}')
4194 *var++ = *tail++;
4195 }
4196 else
4197#endif
4198 {
4199 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004200#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 || (*src == '%' && *tail != '%')
4202#endif
4203 ))
4204 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 }
4207 }
4208
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004209#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210# ifdef UNIX
4211 if (src[1] == '{' && *tail != '}')
4212# else
4213 if (*src == '%' && *tail != '%')
4214# endif
4215 var = NULL;
4216 else
4217 {
4218# ifdef UNIX
4219 if (src[1] == '{')
4220# else
4221 if (*src == '%')
4222#endif
4223 ++tail;
4224#endif
4225 *var = NUL;
4226 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004227#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 }
4229#endif
4230 }
4231 /* home directory */
4232 else if ( src[1] == NUL
4233 || vim_ispathsep(src[1])
4234 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4235 {
4236 var = homedir;
4237 tail = src + 1;
4238 }
4239 else /* user directory */
4240 {
4241#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4242 /*
4243 * Copy ~user to dst[], so we can put a NUL after it.
4244 */
4245 tail = src;
4246 var = dst;
4247 c = dstlen - 1;
4248 while ( c-- > 0
4249 && *tail
4250 && vim_isfilec(*tail)
4251 && !vim_ispathsep(*tail))
4252 *var++ = *tail++;
4253 *var = NUL;
4254# ifdef UNIX
4255 /*
4256 * If the system supports getpwnam(), use it.
4257 * Otherwise, or if getpwnam() fails, the shell is used to
4258 * expand ~user. This is slower and may fail if the shell
4259 * does not support ~user (old versions of /bin/sh).
4260 */
4261# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4262 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004263 /* Note: memory allocated by getpwnam() is never freed.
4264 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01004265 struct passwd *pw = (*dst == NUL)
4266 ? NULL : getpwnam((char *)dst + 1);
4267
4268 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 }
4270 if (var == NULL)
4271# endif
4272 {
4273 expand_T xpc;
4274
4275 ExpandInit(&xpc);
4276 xpc.xp_context = EXPAND_FILES;
4277 var = ExpandOne(&xpc, dst, NULL,
4278 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 mustfree = TRUE;
4280 }
4281
4282# else /* !UNIX, thus VMS */
4283 /*
4284 * USER_HOME is a comma-separated list of
4285 * directories to search for the user account in.
4286 */
4287 {
4288 char_u test[MAXPATHL], paths[MAXPATHL];
4289 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004290 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291
4292 STRCPY(paths, USER_HOME);
4293 next_path = paths;
4294 while (*next_path)
4295 {
4296 for (path = next_path; *next_path && *next_path != ',';
4297 next_path++);
4298 if (*next_path)
4299 *next_path++ = NUL;
4300 STRCPY(test, path);
4301 STRCAT(test, "/");
4302 STRCAT(test, dst + 1);
4303 if (mch_stat(test, &st) == 0)
4304 {
4305 var = alloc(STRLEN(test) + 1);
4306 STRCPY(var, test);
4307 mustfree = TRUE;
4308 break;
4309 }
4310 }
4311 }
4312# endif /* UNIX */
4313#else
4314 /* cannot expand user's home directory, so don't try */
4315 var = NULL;
4316 tail = (char_u *)""; /* for gcc */
4317#endif /* UNIX || VMS */
4318 }
4319
4320#ifdef BACKSLASH_IN_FILENAME
4321 /* If 'shellslash' is set change backslashes to forward slashes.
4322 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4323 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4324 {
4325 char_u *p = vim_strsave(var);
4326
4327 if (p != NULL)
4328 {
4329 if (mustfree)
4330 vim_free(var);
4331 var = p;
4332 mustfree = TRUE;
4333 forward_slash(var);
4334 }
4335 }
4336#endif
4337
4338 /* If "var" contains white space, escape it with a backslash.
4339 * Required for ":e ~/tt" when $HOME includes a space. */
4340 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4341 {
4342 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4343
4344 if (p != NULL)
4345 {
4346 if (mustfree)
4347 vim_free(var);
4348 var = p;
4349 mustfree = TRUE;
4350 }
4351 }
4352
4353 if (var != NULL && *var != NUL
4354 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4355 {
4356 STRCPY(dst, var);
4357 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004358 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 /* if var[] ends in a path separator and tail[] starts
4360 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004361 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4363 && dst[-1] != ':'
4364#endif
4365 && vim_ispathsep(*tail))
4366 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004367 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 src = tail;
4369 copy_char = FALSE;
4370 }
4371 if (mustfree)
4372 vim_free(var);
4373 }
4374
4375 if (copy_char) /* copy at least one char */
4376 {
4377 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004378 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004379 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4380 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 */
4382 at_start = FALSE;
4383 if (src[0] == '\\' && src[1] != NUL)
4384 {
4385 *dst++ = *src++;
4386 --dstlen;
4387 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004388 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02004390 if (dstlen > 0)
4391 {
4392 *dst++ = *src++;
4393 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004394
Bram Moolenaar1c864092017-08-06 18:15:45 +02004395 if (startstr != NULL && src - startstr_len >= srcp
4396 && STRNCMP(src - startstr_len, startstr,
4397 startstr_len) == 0)
4398 at_start = TRUE;
4399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02004401
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 }
4403 *dst = NUL;
4404}
4405
4406/*
4407 * Vim's version of getenv().
4408 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004409 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004410 * "mustfree" is set to TRUE when returned is allocated, it must be
4411 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 */
4413 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004414vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415{
4416 char_u *p;
4417 char_u *pend;
4418 int vimruntime;
4419
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004420#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 /* use "C:/" when $HOME is not set */
4422 if (STRCMP(name, "HOME") == 0)
4423 return homedir;
4424#endif
4425
4426 p = mch_getenv(name);
4427 if (p != NULL && *p == NUL) /* empty is the same as not set */
4428 p = NULL;
4429
4430 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004431 {
4432#if defined(FEAT_MBYTE) && defined(WIN3264)
4433 if (enc_utf8)
4434 {
4435 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004436 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004437
4438 /* Convert from active codepage to UTF-8. Other conversions are
4439 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004440 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004441 if (pp != NULL)
4442 {
4443 p = pp;
4444 *mustfree = TRUE;
4445 }
4446 }
4447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450
4451 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4452 if (!vimruntime && STRCMP(name, "VIM") != 0)
4453 return NULL;
4454
4455 /*
4456 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4457 * Don't do this when default_vimruntime_dir is non-empty.
4458 */
4459 if (vimruntime
4460#ifdef HAVE_PATHDEF
4461 && *default_vimruntime_dir == NUL
4462#endif
4463 )
4464 {
4465 p = mch_getenv((char_u *)"VIM");
4466 if (p != NULL && *p == NUL) /* empty is the same as not set */
4467 p = NULL;
4468 if (p != NULL)
4469 {
4470 p = vim_version_dir(p);
4471 if (p != NULL)
4472 *mustfree = TRUE;
4473 else
4474 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004475
4476#if defined(FEAT_MBYTE) && defined(WIN3264)
4477 if (enc_utf8)
4478 {
4479 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004480 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004481
4482 /* Convert from active codepage to UTF-8. Other conversions
4483 * are not done, because they would fail for non-ASCII
4484 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004485 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004486 if (pp != NULL)
4487 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004488 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004489 vim_free(p);
4490 p = pp;
4491 *mustfree = TRUE;
4492 }
4493 }
4494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 }
4496 }
4497
4498 /*
4499 * When expanding $VIM or $VIMRUNTIME fails, try using:
4500 * - the directory name from 'helpfile' (unless it contains '$')
4501 * - the executable name from argv[0]
4502 */
4503 if (p == NULL)
4504 {
4505 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4506 p = p_hf;
4507#ifdef USE_EXE_NAME
4508 /*
4509 * Use the name of the executable, obtained from argv[0].
4510 */
4511 else
4512 p = exe_name;
4513#endif
4514 if (p != NULL)
4515 {
4516 /* remove the file name */
4517 pend = gettail(p);
4518
4519 /* remove "doc/" from 'helpfile', if present */
4520 if (p == p_hf)
4521 pend = remove_tail(p, pend, (char_u *)"doc");
4522
4523#ifdef USE_EXE_NAME
4524# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004525 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 if (p == exe_name)
4527 {
4528 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004529 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004531 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4532 if (pend1 != pend)
4533 {
4534 pnew = alloc((unsigned)(pend1 - p) + 15);
4535 if (pnew != NULL)
4536 {
4537 STRNCPY(pnew, p, (pend1 - p));
4538 STRCPY(pnew + (pend1 - p), "Resources/vim");
4539 p = pnew;
4540 pend = p + STRLEN(p);
4541 }
4542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 }
4544# endif
4545 /* remove "src/" from exe_name, if present */
4546 if (p == exe_name)
4547 pend = remove_tail(p, pend, (char_u *)"src");
4548#endif
4549
4550 /* for $VIM, remove "runtime/" or "vim54/", if present */
4551 if (!vimruntime)
4552 {
4553 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4554 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4555 }
4556
4557 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004558 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004561#ifdef MACOS_X
4562 if (p == exe_name || p == p_hf)
4563#endif
4564 /* check that the result is a directory name */
4565 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566
4567 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01004568 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 else
4570 {
4571#ifdef USE_EXE_NAME
4572 /* may add "/vim54" or "/runtime" if it exists */
4573 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4574 {
4575 vim_free(p);
4576 p = pend;
4577 }
4578#endif
4579 *mustfree = TRUE;
4580 }
4581 }
4582 }
4583
4584#ifdef HAVE_PATHDEF
4585 /* When there is a pathdef.c file we can use default_vim_dir and
4586 * default_vimruntime_dir */
4587 if (p == NULL)
4588 {
4589 /* Only use default_vimruntime_dir when it is not empty */
4590 if (vimruntime && *default_vimruntime_dir != NUL)
4591 {
4592 p = default_vimruntime_dir;
4593 *mustfree = FALSE;
4594 }
4595 else if (*default_vim_dir != NUL)
4596 {
4597 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4598 *mustfree = TRUE;
4599 else
4600 {
4601 p = default_vim_dir;
4602 *mustfree = FALSE;
4603 }
4604 }
4605 }
4606#endif
4607
4608 /*
4609 * Set the environment variable, so that the new value can be found fast
4610 * next time, and others can also use it (e.g. Perl).
4611 */
4612 if (p != NULL)
4613 {
4614 if (vimruntime)
4615 {
4616 vim_setenv((char_u *)"VIMRUNTIME", p);
4617 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 }
4619 else
4620 {
4621 vim_setenv((char_u *)"VIM", p);
4622 didset_vim = TRUE;
4623 }
4624 }
4625 return p;
4626}
4627
4628/*
4629 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4630 * Return NULL if not, return its name in allocated memory otherwise.
4631 */
4632 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004633vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634{
4635 char_u *p;
4636
4637 if (vimdir == NULL || *vimdir == NUL)
4638 return NULL;
4639 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4640 if (p != NULL && mch_isdir(p))
4641 return p;
4642 vim_free(p);
4643 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4644 if (p != NULL && mch_isdir(p))
4645 return p;
4646 vim_free(p);
4647 return NULL;
4648}
4649
4650/*
4651 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4652 * the length of "name/". Otherwise return "pend".
4653 */
4654 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004655remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656{
4657 int len = (int)STRLEN(name) + 1;
4658 char_u *newend = pend - len;
4659
4660 if (newend >= p
4661 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004662 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 return newend;
4664 return pend;
4665}
4666
Bram Moolenaar137374f2018-05-13 15:59:50 +02004667 void
4668vim_unsetenv(char_u *var)
4669{
4670#ifdef HAVE_UNSETENV
4671 unsetenv((char *)var);
4672#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02004673 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02004674#endif
4675}
4676
4677
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 * Our portable version of setenv.
4680 */
4681 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004682vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683{
4684#ifdef HAVE_SETENV
4685 mch_setenv((char *)name, (char *)val, 1);
4686#else
4687 char_u *envbuf;
4688
4689 /*
4690 * Putenv does not copy the string, it has to remain
4691 * valid. The allocated memory will never be freed.
4692 */
4693 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4694 if (envbuf != NULL)
4695 {
4696 sprintf((char *)envbuf, "%s=%s", name, val);
4697 putenv((char *)envbuf);
4698 }
4699#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004700#ifdef FEAT_GETTEXT
4701 /*
4702 * When setting $VIMRUNTIME adjust the directory to find message
4703 * translations to $VIMRUNTIME/lang.
4704 */
4705 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4706 {
4707 char_u *buf = concat_str(val, (char_u *)"/lang");
4708
4709 if (buf != NULL)
4710 {
4711 bindtextdomain(VIMPACKAGE, (char *)buf);
4712 vim_free(buf);
4713 }
4714 }
4715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716}
4717
4718#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4719/*
4720 * Function given to ExpandGeneric() to obtain an environment variable name.
4721 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004723get_env_name(
4724 expand_T *xp UNUSED,
4725 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726{
Bram Moolenaard0573012017-10-28 21:11:06 +02004727# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02004729 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 */
4731 return NULL;
4732# else
4733# ifndef __WIN32__
4734 /* Borland C++ 5.2 has this in a header file. */
4735 extern char **environ;
4736# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004737# define ENVNAMELEN 100
4738 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 char_u *str;
4740 int n;
4741
4742 str = (char_u *)environ[idx];
4743 if (str == NULL)
4744 return NULL;
4745
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004746 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 {
4748 if (str[n] == '=' || str[n] == NUL)
4749 break;
4750 name[n] = str[n];
4751 }
4752 name[n] = NUL;
4753 return name;
4754# endif
4755}
Bram Moolenaar24305862012-08-15 14:05:05 +02004756
4757/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004758 * Add a user name to the list of users in ga_users.
4759 * Do nothing if user name is NULL or empty.
4760 */
4761 static void
4762add_user(char_u *user, int need_copy)
4763{
4764 char_u *user_copy = (user != NULL && need_copy)
4765 ? vim_strsave(user) : user;
4766
4767 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
4768 {
4769 if (need_copy)
4770 vim_free(user);
4771 return;
4772 }
4773 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
4774}
4775
4776/*
Bram Moolenaar24305862012-08-15 14:05:05 +02004777 * Find all user names for user completion.
4778 * Done only once and then cached.
4779 */
4780 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004781init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004782{
Bram Moolenaar24305862012-08-15 14:05:05 +02004783 static int lazy_init_done = FALSE;
4784
4785 if (lazy_init_done)
4786 return;
4787
4788 lazy_init_done = TRUE;
4789 ga_init2(&ga_users, sizeof(char_u *), 20);
4790
4791# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4792 {
Bram Moolenaar24305862012-08-15 14:05:05 +02004793 struct passwd* pw;
4794
4795 setpwent();
4796 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004797 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02004798 endpwent();
4799 }
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004800# elif defined(WIN3264)
4801 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004802 DWORD nusers = 0, ntotal = 0, i;
4803 PUSER_INFO_0 uinfo;
4804
4805 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
4806 &nusers, &ntotal, NULL) == NERR_Success)
4807 {
4808 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004809 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004810
4811 NetApiBufferFree(uinfo);
4812 }
4813 }
Bram Moolenaar24305862012-08-15 14:05:05 +02004814# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004815# if defined(HAVE_GETPWNAM)
4816 {
4817 char_u *user_env = mch_getenv((char_u *)"USER");
4818
4819 // The $USER environment variable may be a valid remote user name (NIS,
4820 // LDAP) not already listed by getpwent(), as getpwent() only lists
4821 // local user names. If $USER is not already listed, check whether it
4822 // is a valid remote user name using getpwnam() and if it is, add it to
4823 // the list of user names.
4824
4825 if (user_env != NULL && *user_env != NUL)
4826 {
4827 int i;
4828
4829 for (i = 0; i < ga_users.ga_len; i++)
4830 {
4831 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
4832
4833 if (STRCMP(local_user, user_env) == 0)
4834 break;
4835 }
4836
4837 if (i == ga_users.ga_len)
4838 {
4839 struct passwd *pw = getpwnam((char *)user_env);
4840
4841 if (pw != NULL)
4842 add_user((char_u *)pw->pw_name, TRUE);
4843 }
4844 }
4845 }
4846# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02004847}
4848
4849/*
4850 * Function given to ExpandGeneric() to obtain an user names.
4851 */
4852 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004853get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004854{
4855 init_users();
4856 if (idx < ga_users.ga_len)
4857 return ((char_u **)ga_users.ga_data)[idx];
4858 return NULL;
4859}
4860
4861/*
4862 * Check whether name matches a user name. Return:
4863 * 0 if name does not match any user name.
4864 * 1 if name partially matches the beginning of a user name.
4865 * 2 is name fully matches a user name.
4866 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02004867 int
4868match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004869{
4870 int i;
4871 int n = (int)STRLEN(name);
4872 int result = 0;
4873
4874 init_users();
4875 for (i = 0; i < ga_users.ga_len; i++)
4876 {
4877 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4878 return 2; /* full match */
4879 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4880 result = 1; /* partial match */
4881 }
4882 return result;
4883}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884#endif
4885
4886/*
4887 * Replace home directory by "~" in each space or comma separated file name in
4888 * 'src'.
4889 * If anything fails (except when out of space) dst equals src.
4890 */
4891 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004892home_replace(
4893 buf_T *buf, /* when not NULL, check for help files */
4894 char_u *src, /* input file name */
4895 char_u *dst, /* where to put the result */
4896 int dstlen, /* maximum length of the result */
4897 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 spaces and commas in the file name. */
4899{
4900 size_t dirlen = 0, envlen = 0;
4901 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004902 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 char_u *p;
4904
4905 if (src == NULL)
4906 {
4907 *dst = NUL;
4908 return;
4909 }
4910
4911 /*
4912 * If the file is a help file, remove the path completely.
4913 */
4914 if (buf != NULL && buf->b_help)
4915 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02004916 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 return;
4918 }
4919
4920 /*
4921 * We check both the value of the $HOME environment variable and the
4922 * "real" home directory.
4923 */
4924 if (homedir != NULL)
4925 dirlen = STRLEN(homedir);
4926
4927#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004928 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004930 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4931#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004932#ifdef WIN3264
4933 if (homedir_env == NULL)
4934 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
4935#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004936 /* Empty is the same as not set. */
4937 if (homedir_env != NULL && *homedir_env == NUL)
4938 homedir_env = NULL;
4939
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004940#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaaref0a1d52018-12-30 11:38:57 +01004941 if (homedir_env != NULL && *homedir_env == '~')
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004942 {
4943 int usedlen = 0;
4944 int flen;
4945 char_u *fbuf = NULL;
4946
4947 flen = (int)STRLEN(homedir_env);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004948 (void)modify_fname((char_u *)":p", FALSE, &usedlen,
Bram Moolenaard12f8112012-06-20 17:56:09 +02004949 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004950 flen = (int)STRLEN(homedir_env);
4951 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4952 /* Remove the trailing / that is added to a directory. */
4953 homedir_env[flen - 1] = NUL;
4954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955#endif
4956
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 if (homedir_env != NULL)
4958 envlen = STRLEN(homedir_env);
4959
4960 if (!one)
4961 src = skipwhite(src);
4962 while (*src && dstlen > 0)
4963 {
4964 /*
4965 * Here we are at the beginning of a file name.
4966 * First, check to see if the beginning of the file name matches
4967 * $HOME or the "real" home directory. Check that there is a '/'
4968 * after the match (so that if e.g. the file is "/home/pieter/bla",
4969 * and the home directory is "/home/piet", the file does not end up
4970 * as "~er/bla" (which would seem to indicate the file "bla" in user
4971 * er's home directory)).
4972 */
4973 p = homedir;
4974 len = dirlen;
4975 for (;;)
4976 {
4977 if ( len
4978 && fnamencmp(src, p, len) == 0
4979 && (vim_ispathsep(src[len])
4980 || (!one && (src[len] == ',' || src[len] == ' '))
4981 || src[len] == NUL))
4982 {
4983 src += len;
4984 if (--dstlen > 0)
4985 *dst++ = '~';
4986
4987 /*
4988 * If it's just the home directory, add "/".
4989 */
4990 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4991 *dst++ = '/';
4992 break;
4993 }
4994 if (p == homedir_env)
4995 break;
4996 p = homedir_env;
4997 len = envlen;
4998 }
4999
5000 /* if (!one) skip to separator: space or comma */
5001 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
5002 *dst++ = *src++;
5003 /* skip separator */
5004 while ((*src == ' ' || *src == ',') && --dstlen > 0)
5005 *dst++ = *src++;
5006 }
5007 /* if (dstlen == 0) out of space, what to do??? */
5008
5009 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02005010
5011 if (homedir_env != homedir_env_orig)
5012 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013}
5014
5015/*
5016 * Like home_replace, store the replaced string in allocated memory.
5017 * When something fails, NULL is returned.
5018 */
5019 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005020home_replace_save(
5021 buf_T *buf, /* when not NULL, check for help files */
5022 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023{
5024 char_u *dst;
5025 unsigned len;
5026
5027 len = 3; /* space for "~/" and trailing NUL */
5028 if (src != NULL) /* just in case */
5029 len += (unsigned)STRLEN(src);
5030 dst = alloc(len);
5031 if (dst != NULL)
5032 home_replace(buf, src, dst, len, TRUE);
5033 return dst;
5034}
5035
5036/*
5037 * Compare two file names and return:
5038 * FPC_SAME if they both exist and are the same file.
5039 * FPC_SAMEX if they both don't exist and have the same file name.
5040 * FPC_DIFF if they both exist and are different files.
5041 * FPC_NOTX if they both don't exist.
5042 * FPC_DIFFX if one of them doesn't exist.
5043 * For the first name environment variables are expanded
5044 */
5045 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005046fullpathcmp(
5047 char_u *s1,
5048 char_u *s2,
5049 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050{
5051#ifdef UNIX
5052 char_u exp1[MAXPATHL];
5053 char_u full1[MAXPATHL];
5054 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02005055 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 int r1, r2;
5057
5058 expand_env(s1, exp1, MAXPATHL);
5059 r1 = mch_stat((char *)exp1, &st1);
5060 r2 = mch_stat((char *)s2, &st2);
5061 if (r1 != 0 && r2 != 0)
5062 {
5063 /* if mch_stat() doesn't work, may compare the names */
5064 if (checkname)
5065 {
5066 if (fnamecmp(exp1, s2) == 0)
5067 return FPC_SAMEX;
5068 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
5069 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
5070 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
5071 return FPC_SAMEX;
5072 }
5073 return FPC_NOTX;
5074 }
5075 if (r1 != 0 || r2 != 0)
5076 return FPC_DIFFX;
5077 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
5078 return FPC_SAME;
5079 return FPC_DIFF;
5080#else
5081 char_u *exp1; /* expanded s1 */
5082 char_u *full1; /* full path of s1 */
5083 char_u *full2; /* full path of s2 */
5084 int retval = FPC_DIFF;
5085 int r1, r2;
5086
5087 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
5088 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
5089 {
5090 full1 = exp1 + MAXPATHL;
5091 full2 = full1 + MAXPATHL;
5092
5093 expand_env(s1, exp1, MAXPATHL);
5094 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
5095 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
5096
5097 /* If vim_FullName() fails, the file probably doesn't exist. */
5098 if (r1 != OK && r2 != OK)
5099 {
5100 if (checkname && fnamecmp(exp1, s2) == 0)
5101 retval = FPC_SAMEX;
5102 else
5103 retval = FPC_NOTX;
5104 }
5105 else if (r1 != OK || r2 != OK)
5106 retval = FPC_DIFFX;
5107 else if (fnamecmp(full1, full2))
5108 retval = FPC_DIFF;
5109 else
5110 retval = FPC_SAME;
5111 vim_free(exp1);
5112 }
5113 return retval;
5114#endif
5115}
5116
5117/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005118 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02005119 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005120 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 */
5122 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005123gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124{
5125 char_u *p1, *p2;
5126
5127 if (fname == NULL)
5128 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01005129 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01005131 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005133 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 }
5135 return p1;
5136}
5137
Bram Moolenaar31710262010-08-13 13:36:15 +02005138#if defined(FEAT_SEARCHPATH)
Bram Moolenaar31710262010-08-13 13:36:15 +02005139/*
5140 * Return the end of the directory name, on the first path
5141 * separator:
5142 * "/path/file", "/path/dir/", "/path//dir", "/file"
5143 * ^ ^ ^ ^
5144 */
5145 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005146gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02005147{
5148 char_u *dir_end = fname;
5149 char_u *next_dir_end = fname;
5150 int look_for_sep = TRUE;
5151 char_u *p;
5152
5153 for (p = fname; *p != NUL; )
5154 {
5155 if (vim_ispathsep(*p))
5156 {
5157 if (look_for_sep)
5158 {
5159 next_dir_end = p;
5160 look_for_sep = FALSE;
5161 }
5162 }
5163 else
5164 {
5165 if (!look_for_sep)
5166 dir_end = next_dir_end;
5167 look_for_sep = TRUE;
5168 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005169 MB_PTR_ADV(p);
Bram Moolenaar31710262010-08-13 13:36:15 +02005170 }
5171 return dir_end;
5172}
5173#endif
5174
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005176 * Get pointer to tail of "fname", including path separators. Putting a NUL
5177 * here leaves the directory name. Takes care of "c:/" and "//".
5178 * Always returns a valid pointer.
5179 */
5180 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005181gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005182{
5183 char_u *p;
5184 char_u *t;
5185
5186 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
5187 t = gettail(fname);
5188 while (t > p && after_pathsep(fname, t))
5189 --t;
5190#ifdef VMS
5191 /* path separator is part of the path */
5192 ++t;
5193#endif
5194 return t;
5195}
5196
5197/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 * get the next path component (just after the next path separator).
5199 */
5200 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005201getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202{
5203 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005204 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 if (*fname)
5206 ++fname;
5207 return fname;
5208}
5209
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210/*
5211 * Get a pointer to one character past the head of a path name.
5212 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
5213 * If there is no head, path is returned.
5214 */
5215 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005216get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217{
5218 char_u *retval;
5219
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005220#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 /* may skip "c:" */
5222 if (isalpha(path[0]) && path[1] == ':')
5223 retval = path + 2;
5224 else
5225 retval = path;
5226#else
5227# if defined(AMIGA)
5228 /* may skip "label:" */
5229 retval = vim_strchr(path, ':');
5230 if (retval == NULL)
5231 retval = path;
5232# else /* Unix */
5233 retval = path;
5234# endif
5235#endif
5236
5237 while (vim_ispathsep(*retval))
5238 ++retval;
5239
5240 return retval;
5241}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242
5243/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01005244 * Return TRUE if 'c' is a path separator.
5245 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 */
5247 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005248vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249{
Bram Moolenaare60acc12011-05-10 16:41:25 +02005250#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02005252#else
5253# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005255# else
5256# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
5258 return (c == ':' || c == '[' || c == ']' || c == '/'
5259 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02005260# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005262# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02005264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265}
5266
Bram Moolenaar69c35002013-11-04 02:54:12 +01005267/*
5268 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
5269 */
5270 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005271vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01005272{
5273 return vim_ispathsep(c)
5274#ifdef BACKSLASH_IN_FILENAME
5275 && c != ':'
5276#endif
5277 ;
5278}
5279
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5281/*
5282 * return TRUE if 'c' is a path list separator.
5283 */
5284 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005285vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286{
5287#ifdef UNIX
5288 return (c == ':');
5289#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005290 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291#endif
5292}
5293#endif
5294
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005295/*
5296 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5297 * It's done in-place.
5298 */
5299 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005300shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005301{
5302 char_u *tail, *s, *d;
5303 int skip = FALSE;
5304
5305 tail = gettail(str);
5306 d = str;
5307 for (s = str; ; ++s)
5308 {
5309 if (s >= tail) /* copy the whole tail */
5310 {
5311 *d++ = *s;
5312 if (*s == NUL)
5313 break;
5314 }
5315 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5316 {
5317 *d++ = *s;
5318 skip = FALSE;
5319 }
5320 else if (!skip)
5321 {
5322 *d++ = *s; /* copy next char */
5323 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5324 skip = TRUE;
5325# ifdef FEAT_MBYTE
5326 if (has_mbyte)
5327 {
5328 int l = mb_ptr2len(s);
5329
5330 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005331 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005332 }
5333# endif
5334 }
5335 }
5336}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005337
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005338/*
5339 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5340 * Also returns TRUE if there is no directory name.
5341 * "fname" must be writable!.
5342 */
5343 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005344dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005345{
5346 char_u *p;
5347 int c;
5348 int retval;
5349
5350 p = gettail_sep(fname);
5351 if (p == fname)
5352 return TRUE;
5353 c = *p;
5354 *p = NUL;
5355 retval = mch_isdir(fname);
5356 *p = c;
5357 return retval;
5358}
5359
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005361 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5362 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 */
5364 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005365vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005367#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005369#else
5370 if (p_fic)
5371 return MB_STRICMP(x, y);
5372 return STRCMP(x, y);
5373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374}
5375
5376 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005377vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005379#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005380 char_u *px = x;
5381 char_u *py = y;
5382 int cx = NUL;
5383 int cy = NUL;
5384
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005385 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005387 cx = PTR2CHAR(px);
5388 cy = PTR2CHAR(py);
5389 if (cx == NUL || cy == NUL
5390 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5391 && !(cx == '/' && cy == '\\')
5392 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005394 len -= MB_PTR2LEN(px);
5395 px += MB_PTR2LEN(px);
5396 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 }
5398 if (len == 0)
5399 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005400 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005401#else
5402 if (p_fic)
5403 return MB_STRNICMP(x, y, len);
5404 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005406}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407
5408/*
5409 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005410 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 */
5412 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005413concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414{
5415 char_u *dest;
5416
5417 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5418 if (dest != NULL)
5419 {
5420 STRCPY(dest, fname1);
5421 if (sep)
5422 add_pathsep(dest);
5423 STRCAT(dest, fname2);
5424 }
5425 return dest;
5426}
5427
Bram Moolenaard6754642005-01-17 22:18:45 +00005428/*
5429 * Concatenate two strings and return the result in allocated memory.
5430 * Returns NULL when out of memory.
5431 */
5432 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005433concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005434{
5435 char_u *dest;
5436 size_t l = STRLEN(str1);
5437
5438 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5439 if (dest != NULL)
5440 {
5441 STRCPY(dest, str1);
5442 STRCPY(dest + l, str2);
5443 }
5444 return dest;
5445}
Bram Moolenaard6754642005-01-17 22:18:45 +00005446
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447/*
5448 * Add a path separator to a file name, unless it already ends in a path
5449 * separator.
5450 */
5451 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005452add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005454 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 STRCAT(p, PATHSEPSTR);
5456}
5457
5458/*
5459 * FullName_save - Make an allocated copy of a full file name.
5460 * Returns NULL when out of memory.
5461 */
5462 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005463FullName_save(
5464 char_u *fname,
5465 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005466 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467{
5468 char_u *buf;
5469 char_u *new_fname = NULL;
5470
5471 if (fname == NULL)
5472 return NULL;
5473
5474 buf = alloc((unsigned)MAXPATHL);
5475 if (buf != NULL)
5476 {
5477 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5478 new_fname = vim_strsave(buf);
5479 else
5480 new_fname = vim_strsave(fname);
5481 vim_free(buf);
5482 }
5483 return new_fname;
5484}
5485
5486#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5487
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005488static char_u *skip_string(char_u *p);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005489static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490
5491/*
5492 * Find the start of a comment, not knowing if we are in a comment right now.
5493 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005494 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005496 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005497ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005498{
5499 return find_start_comment(curbuf->b_ind_maxcomment);
5500}
5501
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005503find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504{
5505 pos_T *pos;
5506 char_u *line;
5507 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005508 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005510 for (;;)
5511 {
5512 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5513 if (pos == NULL)
5514 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005516 /*
5517 * Check if the comment start we found is inside a string.
5518 * If it is then restrict the search to below this line and try again.
5519 */
5520 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005521 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005522 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005523 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005524 break;
5525 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5526 if (cur_maxcomment <= 0)
5527 {
5528 pos = NULL;
5529 break;
5530 }
5531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 return pos;
5533}
5534
5535/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005536 * Find the start of a comment or raw string, not knowing if we are in a
5537 * comment or raw string right now.
5538 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaardde81312017-08-26 17:49:01 +02005539 * If is_raw is given and returns start of raw_string, sets it to true.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005540 * Return NULL when not inside a comment or raw string.
5541 * "CORS" -> Comment Or Raw String
5542 */
5543 static pos_T *
Bram Moolenaardde81312017-08-26 17:49:01 +02005544ind_find_start_CORS(linenr_T *is_raw) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005545{
Bram Moolenaar089af182015-10-07 11:41:49 +02005546 static pos_T comment_pos_copy;
5547 pos_T *comment_pos;
5548 pos_T *rs_pos;
5549
5550 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5551 if (comment_pos != NULL)
5552 {
5553 /* Need to make a copy of the static pos in findmatchlimit(),
5554 * calling find_start_rawstring() may change it. */
5555 comment_pos_copy = *comment_pos;
5556 comment_pos = &comment_pos_copy;
5557 }
5558 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005559
5560 /* If comment_pos is before rs_pos the raw string is inside the comment.
5561 * If rs_pos is before comment_pos the comment is inside the raw string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005562 if (comment_pos == NULL || (rs_pos != NULL
5563 && LT_POS(*rs_pos, *comment_pos)))
Bram Moolenaardde81312017-08-26 17:49:01 +02005564 {
5565 if (is_raw != NULL && rs_pos != NULL)
5566 *is_raw = rs_pos->lnum;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005567 return rs_pos;
Bram Moolenaardde81312017-08-26 17:49:01 +02005568 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005569 return comment_pos;
5570}
5571
5572/*
5573 * Find the start of a raw string, not knowing if we are in one right now.
5574 * Search starts at w_cursor.lnum and goes backwards.
5575 * Return NULL when not inside a raw string.
5576 */
5577 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005578find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005579{
5580 pos_T *pos;
5581 char_u *line;
5582 char_u *p;
5583 int cur_maxcomment = ind_maxcomment;
5584
5585 for (;;)
5586 {
5587 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5588 if (pos == NULL)
5589 break;
5590
5591 /*
5592 * Check if the raw string start we found is inside a string.
5593 * If it is then restrict the search to below this line and try again.
5594 */
5595 line = ml_get(pos->lnum);
5596 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5597 p = skip_string(p);
5598 if ((colnr_T)(p - line) <= pos->col)
5599 break;
5600 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5601 if (cur_maxcomment <= 0)
5602 {
5603 pos = NULL;
5604 break;
5605 }
5606 }
5607 return pos;
5608}
5609
5610/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 * Skip to the end of a "string" and a 'c' character.
5612 * If there is no string or character, return argument unmodified.
5613 */
5614 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005615skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616{
5617 int i;
5618
5619 /*
5620 * We loop, because strings may be concatenated: "date""time".
5621 */
5622 for ( ; ; ++p)
5623 {
5624 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5625 {
5626 if (!p[1]) /* ' at end of line */
5627 break;
5628 i = 2;
5629 if (p[1] == '\\') /* '\n' or '\000' */
5630 {
5631 ++i;
5632 while (vim_isdigit(p[i - 1])) /* '\000' */
5633 ++i;
5634 }
5635 if (p[i] == '\'') /* check for trailing ' */
5636 {
5637 p += i;
5638 continue;
5639 }
5640 }
5641 else if (p[0] == '"') /* start of string */
5642 {
5643 for (++p; p[0]; ++p)
5644 {
5645 if (p[0] == '\\' && p[1] != NUL)
5646 ++p;
5647 else if (p[0] == '"') /* end of string */
5648 break;
5649 }
5650 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005651 continue; /* continue for another string */
5652 }
5653 else if (p[0] == 'R' && p[1] == '"')
5654 {
5655 /* Raw string: R"[delim](...)[delim]" */
5656 char_u *delim = p + 2;
5657 char_u *paren = vim_strchr(delim, '(');
5658
5659 if (paren != NULL)
5660 {
5661 size_t delim_len = paren - delim;
5662
5663 for (p += 3; *p; ++p)
5664 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5665 && p[delim_len + 1] == '"')
5666 {
5667 p += delim_len + 1;
5668 break;
5669 }
5670 if (p[0] == '"')
5671 continue; /* continue for another string */
5672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 }
5674 break; /* no string found */
5675 }
5676 if (!*p)
5677 --p; /* backup from NUL */
5678 return p;
5679}
5680#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5681
5682#if defined(FEAT_CINDENT) || defined(PROTO)
5683
5684/*
5685 * Do C or expression indenting on the current line.
5686 */
5687 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005688do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689{
5690# ifdef FEAT_EVAL
5691 if (*curbuf->b_p_inde != NUL)
5692 fixthisline(get_expr_indent);
5693 else
5694# endif
5695 fixthisline(get_c_indent);
5696}
5697
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005698/* Find result cache for cpp_baseclass */
5699typedef struct {
5700 int found;
5701 lpos_T lpos;
5702} cpp_baseclass_cache_T;
5703
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704/*
5705 * Functions for C-indenting.
5706 * Most of this originally comes from Eric Fischer.
5707 */
5708/*
5709 * Below "XXX" means that this function may unlock the current line.
5710 */
5711
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005712static int cin_isdefault(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005713static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005714static int cin_iscomment(char_u *);
5715static int cin_islinecomment(char_u *);
5716static int cin_isterminated(char_u *, int, int);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005717static int cin_iselse(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005718static int cin_ends_in(char_u *, char_u *, char_u *);
5719static int cin_starts_with(char_u *s, char *word);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005720static pos_T *find_match_paren(int);
5721static pos_T *find_match_char(int c, int ind_maxparen);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005722static int find_last_paren(char_u *l, int start, int end);
5723static int find_match(int lookfor, linenr_T ourscope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724
5725/*
5726 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005727 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 */
5729 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005730cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731{
5732 while (*s)
5733 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005734 char_u *prev_s = s;
5735
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005737
5738 /* Perl/shell # comment comment continues until eol. Require a space
5739 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005740 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005741 {
5742 s += STRLEN(s);
5743 break;
5744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 if (*s != '/')
5746 break;
5747 ++s;
5748 if (*s == '/') /* slash-slash comment continues till eol */
5749 {
5750 s += STRLEN(s);
5751 break;
5752 }
5753 if (*s != '*')
5754 break;
5755 for (++s; *s; ++s) /* skip slash-star comment */
5756 if (s[0] == '*' && s[1] == '/')
5757 {
5758 s += 2;
5759 break;
5760 }
5761 }
5762 return s;
5763}
5764
5765/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005766 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 * not considered code.
5768 */
5769 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005770cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771{
5772 return *cin_skipcomment(s) == NUL;
5773}
5774
5775/*
5776 * Check previous lines for a "//" line comment, skipping over blank lines.
5777 */
5778 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005779find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780{
5781 static pos_T pos;
5782 char_u *line;
5783 char_u *p;
5784
5785 pos = curwin->w_cursor;
5786 while (--pos.lnum > 0)
5787 {
5788 line = ml_get(pos.lnum);
5789 p = skipwhite(line);
5790 if (cin_islinecomment(p))
5791 {
5792 pos.col = (int)(p - line);
5793 return &pos;
5794 }
5795 if (*p != NUL)
5796 break;
5797 }
5798 return NULL;
5799}
5800
5801/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005802 * Return TRUE if "text" starts with "key:".
5803 */
5804 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005805cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005806{
5807 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005808 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005809
5810 if (*s == '\'' || *s == '"')
5811 {
5812 /* can be 'key': or "key": */
5813 quote = *s;
5814 ++s;
5815 }
5816 if (!vim_isIDc(*s)) /* need at least one ID character */
5817 return FALSE;
5818
5819 while (vim_isIDc(*s))
5820 ++s;
5821 if (*s == quote)
5822 ++s;
5823
5824 s = cin_skipcomment(s);
5825
5826 /* "::" is not a label, it's C++ */
5827 return (*s == ':' && s[1] != ':');
5828}
5829
5830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005832 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 */
5834 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005835cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836{
5837 if (!vim_isIDc(**s)) /* need at least one ID character */
5838 return FALSE;
5839
5840 while (vim_isIDc(**s))
5841 (*s)++;
5842
5843 *s = cin_skipcomment(*s);
5844
5845 /* "::" is not a label, it's C++ */
5846 return (**s == ':' && *++*s != ':');
5847}
5848
5849/*
5850 * Recognize a label: "label:".
5851 * Note: curwin->w_cursor must be where we are looking for the label.
5852 */
5853 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005854cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855{
5856 char_u *s;
5857
5858 s = cin_skipcomment(ml_get_curline());
5859
5860 /*
5861 * Exclude "default" from labels, since it should be indented
5862 * like a switch label. Same for C++ scope declarations.
5863 */
5864 if (cin_isdefault(s))
5865 return FALSE;
5866 if (cin_isscopedecl(s))
5867 return FALSE;
5868
5869 if (cin_islabel_skip(&s))
5870 {
5871 /*
5872 * Only accept a label if the previous line is terminated or is a case
5873 * label.
5874 */
5875 pos_T cursor_save;
5876 pos_T *trypos;
5877 char_u *line;
5878
5879 cursor_save = curwin->w_cursor;
5880 while (curwin->w_cursor.lnum > 1)
5881 {
5882 --curwin->w_cursor.lnum;
5883
5884 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005885 * If we're in a comment or raw string now, skip to the start of
5886 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 */
5888 curwin->w_cursor.col = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02005889 if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 curwin->w_cursor = *trypos;
5891
5892 line = ml_get_curline();
5893 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5894 continue;
5895 if (*(line = cin_skipcomment(line)) == NUL)
5896 continue;
5897
5898 curwin->w_cursor = cursor_save;
5899 if (cin_isterminated(line, TRUE, FALSE)
5900 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005901 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 || (cin_islabel_skip(&line) && cin_nocode(line)))
5903 return TRUE;
5904 return FALSE;
5905 }
5906 curwin->w_cursor = cursor_save;
5907 return TRUE; /* label at start of file??? */
5908 }
5909 return FALSE;
5910}
5911
5912/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005913 * Recognize structure initialization and enumerations:
5914 * "[typedef] [static|public|protected|private] enum"
5915 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 */
5917 static int
5918cin_isinit(void)
5919{
5920 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005921 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922
5923 s = cin_skipcomment(ml_get_curline());
5924
Bram Moolenaar75342212013-03-07 13:13:52 +01005925 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 s = cin_skipcomment(s + 7);
5927
Bram Moolenaar75342212013-03-07 13:13:52 +01005928 for (;;)
5929 {
5930 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005931
Bram Moolenaar75342212013-03-07 13:13:52 +01005932 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5933 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005934 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005935 if (cin_starts_with(s, skip[i]))
5936 {
5937 s = cin_skipcomment(s + l);
5938 l = 0;
5939 break;
5940 }
5941 }
5942 if (l != 0)
5943 break;
5944 }
5945
5946 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 return TRUE;
5948
5949 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5950 return TRUE;
5951
5952 return FALSE;
5953}
5954
5955/*
5956 * Recognize a switch label: "case .*:" or "default:".
5957 */
5958 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005959cin_iscase(
5960 char_u *s,
5961 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962{
5963 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005964 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 {
5966 for (s += 4; *s; ++s)
5967 {
5968 s = cin_skipcomment(s);
5969 if (*s == ':')
5970 {
5971 if (s[1] == ':') /* skip over "::" for C++ */
5972 ++s;
5973 else
5974 return TRUE;
5975 }
5976 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005977 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5979 return FALSE; /* stop at comment */
5980 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005981 {
5982 /* JS etc. */
5983 if (strict)
5984 return FALSE; /* stop at string */
5985 else
5986 return TRUE;
5987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 }
5989 return FALSE;
5990 }
5991
5992 if (cin_isdefault(s))
5993 return TRUE;
5994 return FALSE;
5995}
5996
5997/*
5998 * Recognize a "default" switch label.
5999 */
6000 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006001cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002{
6003 return (STRNCMP(s, "default", 7) == 0
6004 && *(s = cin_skipcomment(s + 7)) == ':'
6005 && s[1] != ':');
6006}
6007
6008/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02006009 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 */
6011 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006012cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013{
6014 int i;
6015
6016 s = cin_skipcomment(s);
6017 if (STRNCMP(s, "public", 6) == 0)
6018 i = 6;
6019 else if (STRNCMP(s, "protected", 9) == 0)
6020 i = 9;
6021 else if (STRNCMP(s, "private", 7) == 0)
6022 i = 7;
6023 else
6024 return FALSE;
6025 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
6026}
6027
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006028/* Maximum number of lines to search back for a "namespace" line. */
6029#define FIND_NAMESPACE_LIM 20
6030
6031/*
6032 * Recognize a "namespace" scope declaration.
6033 */
6034 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006035cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006036{
6037 char_u *p;
6038 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006039 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006040
6041 s = cin_skipcomment(s);
6042 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
6043 {
6044 p = cin_skipcomment(skipwhite(s + 9));
6045 while (*p != NUL)
6046 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006047 if (VIM_ISWHITE(*p))
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006048 {
6049 has_name = TRUE; /* found end of a name */
6050 p = cin_skipcomment(skipwhite(p));
6051 }
6052 else if (*p == '{')
6053 {
6054 break;
6055 }
6056 else if (vim_iswordc(*p))
6057 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006058 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006059 if (has_name)
6060 return FALSE; /* word character after skipping past name */
6061 ++p;
6062 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006063 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
6064 {
6065 if (!has_name_start || has_name)
6066 return FALSE;
6067 /* C++ 17 nested namespace */
6068 p += 3;
6069 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006070 else
6071 {
6072 return FALSE;
6073 }
6074 }
6075 return TRUE;
6076 }
6077 return FALSE;
6078}
6079
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080/*
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006081 * Recognize a `extern "C"` or `extern "C++"` linkage specifications.
6082 */
6083 static int
6084cin_is_cpp_extern_c(char_u *s)
6085{
6086 char_u *p;
6087 int has_string_literal = FALSE;
6088
6089 s = cin_skipcomment(s);
6090 if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
6091 {
6092 p = cin_skipcomment(skipwhite(s + 6));
6093 while (*p != NUL)
6094 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006095 if (VIM_ISWHITE(*p))
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006096 {
6097 p = cin_skipcomment(skipwhite(p));
6098 }
6099 else if (*p == '{')
6100 {
6101 break;
6102 }
6103 else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
6104 {
6105 if (has_string_literal)
6106 return FALSE;
6107 has_string_literal = TRUE;
6108 p += 3;
6109 }
6110 else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
6111 && p[4] == '"')
6112 {
6113 if (has_string_literal)
6114 return FALSE;
6115 has_string_literal = TRUE;
6116 p += 5;
6117 }
6118 else
6119 {
6120 return FALSE;
6121 }
6122 }
6123 return has_string_literal ? TRUE : FALSE;
6124 }
6125 return FALSE;
6126}
6127
6128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 * Return a pointer to the first non-empty non-comment character after a ':'.
6130 * Return NULL if not found.
6131 * case 234: a = b;
6132 * ^
6133 */
6134 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006135after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136{
6137 for ( ; *l; ++l)
6138 {
6139 if (*l == ':')
6140 {
6141 if (l[1] == ':') /* skip over "::" for C++ */
6142 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006143 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 break;
6145 }
6146 else if (*l == '\'' && l[1] && l[2] == '\'')
6147 l += 2; /* skip over 'x' */
6148 }
6149 if (*l == NUL)
6150 return NULL;
6151 l = cin_skipcomment(l + 1);
6152 if (*l == NUL)
6153 return NULL;
6154 return l;
6155}
6156
6157/*
6158 * Get indent of line "lnum", skipping a label.
6159 * Return 0 if there is nothing after the label.
6160 */
6161 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006162get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163{
6164 char_u *l;
6165 pos_T fp;
6166 colnr_T col;
6167 char_u *p;
6168
6169 l = ml_get(lnum);
6170 p = after_label(l);
6171 if (p == NULL)
6172 return 0;
6173
6174 fp.col = (colnr_T)(p - l);
6175 fp.lnum = lnum;
6176 getvcol(curwin, &fp, &col, NULL, NULL);
6177 return (int)col;
6178}
6179
6180/*
6181 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006182 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 * label: if (asdf && asdfasdf)
6184 * ^
6185 */
6186 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006187skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188{
6189 char_u *l;
6190 int amount;
6191 pos_T cursor_save;
6192
6193 cursor_save = curwin->w_cursor;
6194 curwin->w_cursor.lnum = lnum;
6195 l = ml_get_curline();
6196 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006197 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 {
6199 amount = get_indent_nolabel(lnum);
6200 l = after_label(ml_get_curline());
6201 if (l == NULL) /* just in case */
6202 l = ml_get_curline();
6203 }
6204 else
6205 {
6206 amount = get_indent();
6207 l = ml_get_curline();
6208 }
6209 *pp = l;
6210
6211 curwin->w_cursor = cursor_save;
6212 return amount;
6213}
6214
6215/*
6216 * Return the indent of the first variable name after a type in a declaration.
6217 * int a, indent of "a"
6218 * static struct foo b, indent of "b"
6219 * enum bla c, indent of "c"
6220 * Returns zero when it doesn't look like a declaration.
6221 */
6222 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006223cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224{
6225 char_u *line, *p, *s;
6226 int len;
6227 pos_T fp;
6228 colnr_T col;
6229
6230 line = ml_get_curline();
6231 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006232 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 if (len == 6 && STRNCMP(p, "static", 6) == 0)
6234 {
6235 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006236 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 }
6238 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
6239 p = skipwhite(p + 6);
6240 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
6241 p = skipwhite(p + 4);
6242 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
6243 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
6244 {
6245 s = skipwhite(p + len);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006246 if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3]))
6247 || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4]))
6248 || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5]))
6249 || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 p = s;
6251 }
6252 for (len = 0; vim_isIDc(p[len]); ++len)
6253 ;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006254 if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 return 0;
6256
6257 p = skipwhite(p + len);
6258 fp.lnum = curwin->w_cursor.lnum;
6259 fp.col = (colnr_T)(p - line);
6260 getvcol(curwin, &fp, &col, NULL, NULL);
6261 return (int)col;
6262}
6263
6264/*
6265 * Return the indent of the first non-blank after an equal sign.
6266 * char *foo = "here";
6267 * Return zero if no (useful) equal sign found.
6268 * Return -1 if the line above "lnum" ends in a backslash.
6269 * foo = "asdf\
6270 * asdf\
6271 * here";
6272 */
6273 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006274cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275{
6276 char_u *line;
6277 char_u *s;
6278 colnr_T col;
6279 pos_T fp;
6280
6281 if (lnum > 1)
6282 {
6283 line = ml_get(lnum - 1);
6284 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6285 return -1;
6286 }
6287
6288 line = s = ml_get(lnum);
6289 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
6290 {
6291 if (cin_iscomment(s)) /* ignore comments */
6292 s = cin_skipcomment(s);
6293 else
6294 ++s;
6295 }
6296 if (*s != '=')
6297 return 0;
6298
6299 s = skipwhite(s + 1);
6300 if (cin_nocode(s))
6301 return 0;
6302
6303 if (*s == '"') /* nice alignment for continued strings */
6304 ++s;
6305
6306 fp.lnum = lnum;
6307 fp.col = (colnr_T)(s - line);
6308 getvcol(curwin, &fp, &col, NULL, NULL);
6309 return (int)col;
6310}
6311
6312/*
6313 * Recognize a preprocessor statement: Any line that starts with '#'.
6314 */
6315 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006316cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006318 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 return TRUE;
6320 return FALSE;
6321}
6322
6323/*
6324 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6325 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6326 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006327 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 */
6329 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006330cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331{
6332 char_u *line = *pp;
6333 linenr_T lnum = *lnump;
6334 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006335 int candidate_amount = *amount;
6336
6337 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6338 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006340 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 {
6342 if (cin_ispreproc(line))
6343 {
6344 retval = TRUE;
6345 *lnump = lnum;
6346 break;
6347 }
6348 if (lnum == 1)
6349 break;
6350 line = ml_get(--lnum);
6351 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6352 break;
6353 }
6354
6355 if (lnum != *lnump)
6356 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006357 if (retval)
6358 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 return retval;
6360}
6361
6362/*
6363 * Recognize the start of a C or C++ comment.
6364 */
6365 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006366cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367{
6368 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6369}
6370
6371/*
6372 * Recognize the start of a "//" comment.
6373 */
6374 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006375cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376{
6377 return (p[0] == '/' && p[1] == '/');
6378}
6379
6380/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006381 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6382 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006384 * If a line begins with an "else", only consider it terminated if no unmatched
6385 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 * Return the character terminating the line (ending char's have precedence if
6387 * both apply in order to determine initializations).
6388 */
6389 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006390cin_isterminated(
6391 char_u *s,
6392 int incl_open, /* include '{' at the end as terminator */
6393 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006395 char_u found_start = 0;
6396 unsigned n_open = 0;
6397 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398
6399 s = cin_skipcomment(s);
6400
6401 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6402 found_start = *s;
6403
Bram Moolenaar496f9512011-05-19 16:35:09 +02006404 if (!found_start)
6405 is_else = cin_iselse(s);
6406
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 while (*s)
6408 {
6409 /* skip over comments, "" strings and 'c'haracters */
6410 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006411 if (*s == '}' && n_open > 0)
6412 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006413 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006414 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 && cin_nocode(s + 1))
6416 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006417 else if (*s == '{')
6418 {
6419 if (incl_open && cin_nocode(s + 1))
6420 return *s;
6421 else
6422 ++n_open;
6423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424
6425 if (*s)
6426 s++;
6427 }
6428 return found_start;
6429}
6430
6431/*
6432 * Recognize the basic picture of a function declaration -- it needs to
6433 * have an open paren somewhere and a close paren at the end of the line and
6434 * no semicolons anywhere.
6435 * When a line ends in a comma we continue looking in the next line.
6436 * "sp" points to a string with the line. When looking at other lines it must
6437 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006438 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006439 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 */
6441 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006442cin_isfuncdecl(
6443 char_u **sp,
6444 linenr_T first_lnum,
6445 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446{
6447 char_u *s;
6448 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006449 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006451 pos_T *trypos;
6452 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453
6454 if (sp == NULL)
6455 s = ml_get(lnum);
6456 else
6457 s = *sp;
6458
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006459 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006460 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006461 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006462 {
6463 lnum = trypos->lnum;
6464 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006465 {
6466 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006467 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006468 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006469
6470 s = ml_get(lnum);
6471 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006472 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006473
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006474 /* Ignore line starting with #. */
6475 if (cin_ispreproc(s))
6476 return FALSE;
6477
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6479 {
6480 if (cin_iscomment(s)) /* ignore comments */
6481 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006482 else if (*s == ':')
6483 {
6484 if (*(s + 1) == ':')
6485 s += 2;
6486 else
6487 /* To avoid a mistake in the following situation:
6488 * A::A(int a, int b)
6489 * : a(0) // <--not a function decl
6490 * , b(0)
6491 * {...
6492 */
6493 return FALSE;
6494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 else
6496 ++s;
6497 }
6498 if (*s != '(')
6499 return FALSE; /* ';', ' or " before any () or no '(' */
6500
6501 while (*s && *s != ';' && *s != '\'' && *s != '"')
6502 {
6503 if (*s == ')' && cin_nocode(s + 1))
6504 {
6505 /* ')' at the end: may have found a match
6506 * Check for he previous line not to end in a backslash:
6507 * #if defined(x) && \
6508 * defined(y)
6509 */
6510 lnum = first_lnum - 1;
6511 s = ml_get(lnum);
6512 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6513 retval = TRUE;
6514 goto done;
6515 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006516 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006518 int comma = (*s == ',');
6519
6520 /* ',' at the end: continue looking in the next line.
6521 * At the end: check for ',' in the next line, for this style:
6522 * func(arg1
6523 * , arg2) */
6524 for (;;)
6525 {
6526 if (lnum >= curbuf->b_ml.ml_line_count)
6527 break;
6528 s = ml_get(++lnum);
6529 if (!cin_ispreproc(s))
6530 break;
6531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 if (lnum >= curbuf->b_ml.ml_line_count)
6533 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006534 /* Require a comma at end of the line or a comma or ')' at the
6535 * start of next line. */
6536 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006537 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006538 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006539 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 }
6541 else if (cin_iscomment(s)) /* ignore comments */
6542 s = cin_skipcomment(s);
6543 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006544 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006546 just_started = FALSE;
6547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 }
6549
6550done:
6551 if (lnum != first_lnum && sp != NULL)
6552 *sp = ml_get(first_lnum);
6553
6554 return retval;
6555}
6556
6557 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006558cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006560 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561}
6562
6563 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006564cin_iselse(
6565 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566{
6567 if (*p == '}') /* accept "} else" */
6568 p = cin_skipcomment(p + 1);
6569 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6570}
6571
6572 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006573cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574{
6575 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6576}
6577
6578/*
6579 * Check if this is a "while" that should have a matching "do".
6580 * We only accept a "while (condition) ;", with only white space between the
6581 * ')' and ';'. The condition may be spread over several lines.
6582 */
6583 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006584cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585{
6586 pos_T cursor_save;
6587 pos_T *trypos;
6588 int retval = FALSE;
6589
6590 p = cin_skipcomment(p);
6591 if (*p == '}') /* accept "} while (cond);" */
6592 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006593 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 {
6595 cursor_save = curwin->w_cursor;
6596 curwin->w_cursor.lnum = lnum;
6597 curwin->w_cursor.col = 0;
6598 p = ml_get_curline();
6599 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6600 {
6601 ++p;
6602 ++curwin->w_cursor.col;
6603 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006604 if ((trypos = findmatchlimit(NULL, 0, 0,
6605 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6607 retval = TRUE;
6608 curwin->w_cursor = cursor_save;
6609 }
6610 return retval;
6611}
6612
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006613/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006614 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006615 * Return 0 if there is none.
6616 * Otherwise return !0 and update "*poffset" to point to the place where the
6617 * string was found.
6618 */
6619 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006620cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006621{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006622 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006623
6624 if (offset-- < 2)
6625 return 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006626 while (offset > 2 && VIM_ISWHITE(line[offset]))
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006627 --offset;
6628
6629 offset -= 1;
6630 if (!STRNCMP(line + offset, "if", 2))
6631 goto probablyFound;
6632
6633 if (offset >= 1)
6634 {
6635 offset -= 1;
6636 if (!STRNCMP(line + offset, "for", 3))
6637 goto probablyFound;
6638
6639 if (offset >= 2)
6640 {
6641 offset -= 2;
6642 if (!STRNCMP(line + offset, "while", 5))
6643 goto probablyFound;
6644 }
6645 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006646 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006647
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006648probablyFound:
6649 if (!offset || !vim_isIDc(line[offset - 1]))
6650 {
6651 *poffset = offset;
6652 return 1;
6653 }
6654 return 0;
6655}
6656
6657/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006658 * Return TRUE if we are at the end of a do-while.
6659 * do
6660 * nothing;
6661 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006662 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006663 * Adjust the cursor to the line with "while".
6664 */
6665 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006666cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006667{
6668 char_u *line;
6669 char_u *p;
6670 char_u *s;
6671 pos_T *trypos;
6672 int i;
6673
6674 if (terminated != ';') /* there must be a ';' at the end */
6675 return FALSE;
6676
6677 p = line = ml_get_curline();
6678 while (*p != NUL)
6679 {
6680 p = cin_skipcomment(p);
6681 if (*p == ')')
6682 {
6683 s = skipwhite(p + 1);
6684 if (*s == ';' && cin_nocode(s + 1))
6685 {
6686 /* Found ");" at end of the line, now check there is "while"
6687 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006688 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006689 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006690 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006691 if (trypos != NULL)
6692 {
6693 s = cin_skipcomment(ml_get(trypos->lnum));
6694 if (*s == '}') /* accept "} while (cond);" */
6695 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006696 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006697 {
6698 curwin->w_cursor.lnum = trypos->lnum;
6699 return TRUE;
6700 }
6701 }
6702
6703 /* Searching may have made "line" invalid, get it again. */
6704 line = ml_get_curline();
6705 p = line + i;
6706 }
6707 }
6708 if (*p != NUL)
6709 ++p;
6710 }
6711 return FALSE;
6712}
6713
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006715cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716{
6717 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6718}
6719
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006720/*
6721 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 * constructor-initialization. eg:
6723 *
6724 * class MyClass :
6725 * baseClass <-- here
6726 * class MyClass : public baseClass,
6727 * anotherBaseClass <-- here (should probably lineup ??)
6728 * MyClass::MyClass(...) :
6729 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006730 *
6731 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 */
6733 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006734cin_is_cpp_baseclass(
6735 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006737 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 char_u *s;
6739 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006740 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006741 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006743 if (pos->lnum <= lnum)
6744 return cached->found; /* Use the cached result */
6745
6746 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006748 s = skipwhite(line);
6749 if (*s == '#') /* skip #define FOO x ? (x) : x */
6750 return FALSE;
6751 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 if (*s == NUL)
6753 return FALSE;
6754
6755 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6756
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006757 /* Search for a line starting with '#', empty, ending in ';' or containing
6758 * '{' or '}' and start below it. This handles the following situations:
6759 * a = cond ?
6760 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006761 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006762 * func::foo()
6763 * : something
6764 * {}
6765 * Foo::Foo (int one, int two)
6766 * : something(4),
6767 * somethingelse(3)
6768 * {}
6769 */
6770 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006772 line = ml_get(lnum - 1);
6773 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006774 if (*s == '#' || *s == NUL)
6775 break;
6776 while (*s != NUL)
6777 {
6778 s = cin_skipcomment(s);
6779 if (*s == '{' || *s == '}'
6780 || (*s == ';' && cin_nocode(s + 1)))
6781 break;
6782 if (*s != NUL)
6783 ++s;
6784 }
6785 if (*s != NUL)
6786 break;
6787 --lnum;
6788 }
6789
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006790 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006791 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006792 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006793 for (;;)
6794 {
6795 if (*s == NUL)
6796 {
6797 if (lnum == curwin->w_cursor.lnum)
6798 break;
6799 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006800 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006801 s = line;
6802 }
6803 if (s == line)
6804 {
6805 /* don't recognize "case (foo):" as a baseclass */
6806 if (cin_iscase(s, FALSE))
6807 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006808 s = cin_skipcomment(line);
6809 if (*s == NUL)
6810 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006811 }
6812
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006813 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006814 s = skip_string(s) + 1;
6815 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 {
6817 if (s[1] == ':')
6818 {
6819 /* skip double colon. It can't be a constructor
6820 * initialization any more */
6821 lookfor_ctor_init = FALSE;
6822 s = cin_skipcomment(s + 2);
6823 }
6824 else if (lookfor_ctor_init || class_or_struct)
6825 {
6826 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006827 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 cpp_base_class = TRUE;
6829 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006830 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 s = cin_skipcomment(s + 1);
6832 }
6833 else
6834 s = cin_skipcomment(s + 1);
6835 }
6836 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6837 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6838 {
6839 class_or_struct = TRUE;
6840 lookfor_ctor_init = FALSE;
6841
6842 if (*s == 'c')
6843 s = cin_skipcomment(s + 5);
6844 else
6845 s = cin_skipcomment(s + 6);
6846 }
6847 else
6848 {
6849 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6850 {
6851 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6852 }
6853 else if (s[0] == ')')
6854 {
6855 /* Constructor-initialization is assumed if we come across
6856 * something like "):" */
6857 class_or_struct = FALSE;
6858 lookfor_ctor_init = TRUE;
6859 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006860 else if (s[0] == '?')
6861 {
6862 /* Avoid seeing '() :' after '?' as constructor init. */
6863 return FALSE;
6864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 else if (!vim_isIDc(s[0]))
6866 {
6867 /* if it is not an identifier, we are wrong */
6868 class_or_struct = FALSE;
6869 lookfor_ctor_init = FALSE;
6870 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006871 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 {
6873 /* it can't be a constructor-initialization any more */
6874 lookfor_ctor_init = FALSE;
6875
6876 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006877 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006878 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 }
6880
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006881 /* When the line ends in a comma don't align with it. */
6882 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006883 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006884
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 s = cin_skipcomment(s + 1);
6886 }
6887 }
6888
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006889 cached->found = cpp_base_class;
6890 if (cpp_base_class)
6891 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 return cpp_base_class;
6893}
6894
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006895 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006896get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006897{
6898 int amount;
6899 colnr_T vcol;
6900 pos_T *trypos;
6901
6902 if (col == 0)
6903 {
6904 amount = get_indent();
6905 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006906 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006907 amount = get_indent_lnum(trypos->lnum); /* XXX */
6908 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006909 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006910 }
6911 else
6912 {
6913 curwin->w_cursor.col = col;
6914 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6915 amount = (int)vcol;
6916 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006917 if (amount < curbuf->b_ind_cpp_baseclass)
6918 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006919 return amount;
6920}
6921
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922/*
6923 * Return TRUE if string "s" ends with the string "find", possibly followed by
6924 * white space and comments. Skip strings and comments.
6925 * Ignore "ignore" after "find" if it's not NULL.
6926 */
6927 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006928cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929{
6930 char_u *p = s;
6931 char_u *r;
6932 int len = (int)STRLEN(find);
6933
6934 while (*p != NUL)
6935 {
6936 p = cin_skipcomment(p);
6937 if (STRNCMP(p, find, len) == 0)
6938 {
6939 r = skipwhite(p + len);
6940 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6941 r = skipwhite(r + STRLEN(ignore));
6942 if (cin_nocode(r))
6943 return TRUE;
6944 }
6945 if (*p != NUL)
6946 ++p;
6947 }
6948 return FALSE;
6949}
6950
6951/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006952 * Return TRUE when "s" starts with "word" and then a non-ID character.
6953 */
6954 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006955cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006956{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006957 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006958
6959 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6960}
6961
6962/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 * Skip strings, chars and comments until at or past "trypos".
6964 * Return the column found.
6965 */
6966 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006967cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968{
6969 char_u *line;
6970 char_u *p;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006971 char_u *new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972
6973 p = line = ml_get(trypos->lnum);
6974 while (*p && (colnr_T)(p - line) < trypos->col)
6975 {
6976 if (cin_iscomment(p))
6977 p = cin_skipcomment(p);
6978 else
6979 {
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006980 new_p = skip_string(p);
6981 if (new_p == p)
6982 ++p;
6983 else
6984 p = new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 }
6986 }
6987 return (int)(p - line);
6988}
6989
6990/*
6991 * Find the '{' at the start of the block we are in.
6992 * Return NULL if no match found.
6993 * Ignore a '{' that is in a comment, makes indenting the next three lines
6994 * work. */
6995/* foo() */
6996/* { */
6997/* } */
6998
6999 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007000find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001{
7002 pos_T cursor_save;
7003 pos_T *trypos;
7004 pos_T *pos;
7005 static pos_T pos_copy;
7006
7007 cursor_save = curwin->w_cursor;
7008 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
7009 {
7010 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
7011 trypos = &pos_copy;
7012 curwin->w_cursor = *trypos;
7013 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007014 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaardde81312017-08-26 17:49:01 +02007016 && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 break;
7018 if (pos != NULL)
7019 curwin->w_cursor.lnum = pos->lnum;
7020 }
7021 curwin->w_cursor = cursor_save;
7022 return trypos;
7023}
7024
7025/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02007026 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007027 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 */
7029 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007030find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02007032 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007033}
7034
7035 static pos_T *
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02007036find_match_char(int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007037{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 pos_T cursor_save;
7039 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007040 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007041 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042
7043 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007044 ind_maxp_wk = ind_maxparen;
7045retry:
7046 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 {
7048 /* check if the ( is in a // comment */
7049 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01007050 {
7051 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
7052 if (ind_maxp_wk > 0)
7053 {
7054 curwin->w_cursor = *trypos;
7055 curwin->w_cursor.col = 0; /* XXX */
7056 goto retry;
7057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 else
7061 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01007062 pos_T *trypos_wk;
7063
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 pos_copy = *trypos; /* copy trypos, findmatch will change it */
7065 trypos = &pos_copy;
7066 curwin->w_cursor = *trypos;
Bram Moolenaardde81312017-08-26 17:49:01 +02007067 if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01007068 {
7069 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
7070 - trypos_wk->lnum);
7071 if (ind_maxp_wk > 0)
7072 {
7073 curwin->w_cursor = *trypos_wk;
7074 goto retry;
7075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 }
7079 }
7080 curwin->w_cursor = cursor_save;
7081 return trypos;
7082}
7083
7084/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02007085 * Find the matching '(', ignoring it if it is in a comment or before an
7086 * unmatched {.
7087 * Return NULL if no match found.
7088 */
7089 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007090find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02007091{
7092 pos_T *trypos = find_match_paren(ind_maxparen);
7093
7094 if (trypos != NULL)
7095 {
7096 pos_T *tryposBrace = find_start_brace();
7097
7098 /* If both an unmatched '(' and '{' is found. Ignore the '('
7099 * position if the '{' is further down. */
7100 if (tryposBrace != NULL
7101 && (trypos->lnum != tryposBrace->lnum
7102 ? trypos->lnum < tryposBrace->lnum
7103 : trypos->col < tryposBrace->col))
7104 trypos = NULL;
7105 }
7106 return trypos;
7107}
7108
7109/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 * Return ind_maxparen corrected for the difference in line number between the
7111 * cursor position and "startpos". This makes sure that searching for a
7112 * matching paren above the cursor line doesn't find a match because of
7113 * looking a few lines further.
7114 */
7115 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007116corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117{
7118 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
7119
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007120 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
7121 return curbuf->b_ind_maxparen - (int)n;
7122 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123}
7124
7125/*
7126 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007127 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 */
7129 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007130find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131{
7132 int i;
7133 int retval = FALSE;
7134 int open_count = 0;
7135
7136 curwin->w_cursor.col = 0; /* default is start of line */
7137
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007138 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 {
7140 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
7141 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
7142 if (l[i] == start)
7143 ++open_count;
7144 else if (l[i] == end)
7145 {
7146 if (open_count > 0)
7147 --open_count;
7148 else
7149 {
7150 curwin->w_cursor.col = i;
7151 retval = TRUE;
7152 }
7153 }
7154 }
7155 return retval;
7156}
7157
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007158/*
7159 * Parse 'cinoptions' and set the values in "curbuf".
7160 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
7161 */
7162 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007163parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007164{
7165 char_u *p;
7166 char_u *l;
7167 char_u *digits;
7168 int n;
7169 int divider;
7170 int fraction = 0;
7171 int sw = (int)get_sw_value(buf);
7172
7173 /*
7174 * Set the default values.
7175 */
7176 /* Spaces from a block's opening brace the prevailing indent for that
7177 * block should be. */
7178 buf->b_ind_level = sw;
7179
7180 /* Spaces from the edge of the line an open brace that's at the end of a
7181 * line is imagined to be. */
7182 buf->b_ind_open_imag = 0;
7183
7184 /* Spaces from the prevailing indent for a line that is not preceded by
7185 * an opening brace. */
7186 buf->b_ind_no_brace = 0;
7187
7188 /* Column where the first { of a function should be located }. */
7189 buf->b_ind_first_open = 0;
7190
7191 /* Spaces from the prevailing indent a leftmost open brace should be
7192 * located. */
7193 buf->b_ind_open_extra = 0;
7194
7195 /* Spaces from the matching open brace (real location for one at the left
7196 * edge; imaginary location from one that ends a line) the matching close
7197 * brace should be located. */
7198 buf->b_ind_close_extra = 0;
7199
7200 /* Spaces from the edge of the line an open brace sitting in the leftmost
7201 * column is imagined to be. */
7202 buf->b_ind_open_left_imag = 0;
7203
7204 /* Spaces jump labels should be shifted to the left if N is non-negative,
7205 * otherwise the jump label will be put to column 1. */
7206 buf->b_ind_jump_label = -1;
7207
7208 /* Spaces from the switch() indent a "case xx" label should be located. */
7209 buf->b_ind_case = sw;
7210
7211 /* Spaces from the "case xx:" code after a switch() should be located. */
7212 buf->b_ind_case_code = sw;
7213
7214 /* Lineup break at end of case in switch() with case label. */
7215 buf->b_ind_case_break = 0;
7216
7217 /* Spaces from the class declaration indent a scope declaration label
7218 * should be located. */
7219 buf->b_ind_scopedecl = sw;
7220
7221 /* Spaces from the scope declaration label code should be located. */
7222 buf->b_ind_scopedecl_code = sw;
7223
7224 /* Amount K&R-style parameters should be indented. */
7225 buf->b_ind_param = sw;
7226
7227 /* Amount a function type spec should be indented. */
7228 buf->b_ind_func_type = sw;
7229
7230 /* Amount a cpp base class declaration or constructor initialization
7231 * should be indented. */
7232 buf->b_ind_cpp_baseclass = sw;
7233
7234 /* additional spaces beyond the prevailing indent a continuation line
7235 * should be located. */
7236 buf->b_ind_continuation = sw;
7237
7238 /* Spaces from the indent of the line with an unclosed parentheses. */
7239 buf->b_ind_unclosed = sw * 2;
7240
7241 /* Spaces from the indent of the line with an unclosed parentheses, which
7242 * itself is also unclosed. */
7243 buf->b_ind_unclosed2 = sw;
7244
7245 /* Suppress ignoring spaces from the indent of a line starting with an
7246 * unclosed parentheses. */
7247 buf->b_ind_unclosed_noignore = 0;
7248
7249 /* If the opening paren is the last nonwhite character on the line, and
7250 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
7251 * context (for very long lines). */
7252 buf->b_ind_unclosed_wrapped = 0;
7253
7254 /* Suppress ignoring white space when lining up with the character after
7255 * an unclosed parentheses. */
7256 buf->b_ind_unclosed_whiteok = 0;
7257
7258 /* Indent a closing parentheses under the line start of the matching
7259 * opening parentheses. */
7260 buf->b_ind_matching_paren = 0;
7261
7262 /* Indent a closing parentheses under the previous line. */
7263 buf->b_ind_paren_prev = 0;
7264
7265 /* Extra indent for comments. */
7266 buf->b_ind_comment = 0;
7267
7268 /* Spaces from the comment opener when there is nothing after it. */
7269 buf->b_ind_in_comment = 3;
7270
7271 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
7272 * after the comment opener. */
7273 buf->b_ind_in_comment2 = 0;
7274
7275 /* Max lines to search for an open paren. */
7276 buf->b_ind_maxparen = 20;
7277
7278 /* Max lines to search for an open comment. */
7279 buf->b_ind_maxcomment = 70;
7280
7281 /* Handle braces for java code. */
7282 buf->b_ind_java = 0;
7283
7284 /* Not to confuse JS object properties with labels. */
7285 buf->b_ind_js = 0;
7286
7287 /* Handle blocked cases correctly. */
7288 buf->b_ind_keep_case_label = 0;
7289
7290 /* Handle C++ namespace. */
7291 buf->b_ind_cpp_namespace = 0;
7292
7293 /* Handle continuation lines containing conditions of if(), for() and
7294 * while(). */
7295 buf->b_ind_if_for_while = 0;
7296
Bram Moolenaar6b643942017-03-05 19:44:06 +01007297 /* indentation for # comments */
7298 buf->b_ind_hash_comment = 0;
7299
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007300 /* Handle C++ extern "C" or "C++" */
7301 buf->b_ind_cpp_extern_c = 0;
7302
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007303 for (p = buf->b_p_cino; *p; )
7304 {
7305 l = p++;
7306 if (*p == '-')
7307 ++p;
7308 digits = p; /* remember where the digits start */
7309 n = getdigits(&p);
7310 divider = 0;
7311 if (*p == '.') /* ".5s" means a fraction */
7312 {
7313 fraction = atol((char *)++p);
7314 while (VIM_ISDIGIT(*p))
7315 {
7316 ++p;
7317 if (divider)
7318 divider *= 10;
7319 else
7320 divider = 10;
7321 }
7322 }
7323 if (*p == 's') /* "2s" means two times 'shiftwidth' */
7324 {
7325 if (p == digits)
7326 n = sw; /* just "s" is one 'shiftwidth' */
7327 else
7328 {
7329 n *= sw;
7330 if (divider)
7331 n += (sw * fraction + divider / 2) / divider;
7332 }
7333 ++p;
7334 }
7335 if (l[1] == '-')
7336 n = -n;
7337
7338 /* When adding an entry here, also update the default 'cinoptions' in
7339 * doc/indent.txt, and add explanation for it! */
7340 switch (*l)
7341 {
7342 case '>': buf->b_ind_level = n; break;
7343 case 'e': buf->b_ind_open_imag = n; break;
7344 case 'n': buf->b_ind_no_brace = n; break;
7345 case 'f': buf->b_ind_first_open = n; break;
7346 case '{': buf->b_ind_open_extra = n; break;
7347 case '}': buf->b_ind_close_extra = n; break;
7348 case '^': buf->b_ind_open_left_imag = n; break;
7349 case 'L': buf->b_ind_jump_label = n; break;
7350 case ':': buf->b_ind_case = n; break;
7351 case '=': buf->b_ind_case_code = n; break;
7352 case 'b': buf->b_ind_case_break = n; break;
7353 case 'p': buf->b_ind_param = n; break;
7354 case 't': buf->b_ind_func_type = n; break;
7355 case '/': buf->b_ind_comment = n; break;
7356 case 'c': buf->b_ind_in_comment = n; break;
7357 case 'C': buf->b_ind_in_comment2 = n; break;
7358 case 'i': buf->b_ind_cpp_baseclass = n; break;
7359 case '+': buf->b_ind_continuation = n; break;
7360 case '(': buf->b_ind_unclosed = n; break;
7361 case 'u': buf->b_ind_unclosed2 = n; break;
7362 case 'U': buf->b_ind_unclosed_noignore = n; break;
7363 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7364 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7365 case 'm': buf->b_ind_matching_paren = n; break;
7366 case 'M': buf->b_ind_paren_prev = n; break;
7367 case ')': buf->b_ind_maxparen = n; break;
7368 case '*': buf->b_ind_maxcomment = n; break;
7369 case 'g': buf->b_ind_scopedecl = n; break;
7370 case 'h': buf->b_ind_scopedecl_code = n; break;
7371 case 'j': buf->b_ind_java = n; break;
7372 case 'J': buf->b_ind_js = n; break;
7373 case 'l': buf->b_ind_keep_case_label = n; break;
7374 case '#': buf->b_ind_hash_comment = n; break;
7375 case 'N': buf->b_ind_cpp_namespace = n; break;
7376 case 'k': buf->b_ind_if_for_while = n; break;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007377 case 'E': buf->b_ind_cpp_extern_c = n; break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007378 }
7379 if (*p == ',')
7380 ++p;
7381 }
7382}
7383
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007384/*
7385 * Return the desired indent for C code.
7386 * Return -1 if the indent should be left alone (inside a raw string).
7387 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007389get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 pos_T cur_curpos;
7392 int amount;
7393 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007394 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 colnr_T col;
7396 char_u *theline;
7397 char_u *linecopy;
7398 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007399 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007401 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 pos_T our_paren_pos;
7403 char_u *start;
7404 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007405#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406#define BRACE_AT_START 2 /* '{' is at start of line */
7407#define BRACE_AT_END 3 /* '{' is at end of line */
7408 linenr_T ourscope;
7409 char_u *l;
7410 char_u *look;
7411 char_u terminated;
7412 int lookfor;
7413#define LOOKFOR_INITIAL 0
7414#define LOOKFOR_IF 1
7415#define LOOKFOR_DO 2
7416#define LOOKFOR_CASE 3
7417#define LOOKFOR_ANY 4
7418#define LOOKFOR_TERM 5
7419#define LOOKFOR_UNTERM 6
7420#define LOOKFOR_SCOPEDECL 7
7421#define LOOKFOR_NOBREAK 8
7422#define LOOKFOR_CPP_BASECLASS 9
7423#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007424#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007425#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426
7427 int whilelevel;
7428 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 int n;
7430 int iscase;
7431 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007432 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007434 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007435 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007436 int js_cur_has_key = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02007437 linenr_T raw_string_start = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007438 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007440 /* make a copy, value is changed below */
7441 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442
7443 /* remember where the cursor was when we started */
7444 cur_curpos = curwin->w_cursor;
7445
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007446 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007447 if (cur_curpos.lnum == 1)
7448 return 0;
7449
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 /* Get a copy of the current contents of the line.
7451 * This is required, because only the most recent line obtained with
7452 * ml_get is valid! */
7453 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7454 if (linecopy == NULL)
7455 return 0;
7456
7457 /*
7458 * In insert mode and the cursor is on a ')' truncate the line at the
7459 * cursor position. We don't want to line up with the matching '(' when
7460 * inserting new stuff.
7461 * For unknown reasons the cursor might be past the end of the line, thus
7462 * check for that.
7463 */
7464 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007465 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 && linecopy[curwin->w_cursor.col] == ')')
7467 linecopy[curwin->w_cursor.col] = NUL;
7468
7469 theline = skipwhite(linecopy);
7470
7471 /* move the cursor to the start of the line */
7472
7473 curwin->w_cursor.col = 0;
7474
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007475 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007476
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007478 * If we are inside a raw string don't change the indent.
7479 * Ignore a raw string inside a comment.
7480 */
7481 comment_pos = ind_find_start_comment();
7482 if (comment_pos != NULL)
7483 {
7484 /* findmatchlimit() static pos is overwritten, make a copy */
7485 tryposCopy = *comment_pos;
7486 comment_pos = &tryposCopy;
7487 }
7488 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007489 if (trypos != NULL && (comment_pos == NULL
7490 || LT_POS(*trypos, *comment_pos)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007491 {
7492 amount = -1;
7493 goto laterend;
7494 }
7495
7496 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 * #defines and so on always go at the left when included in 'cinkeys'.
7498 */
7499 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007500 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007501 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007502 goto theend;
7503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504
7505 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007506 * Is it a non-case label? Then that goes at the left margin too unless:
7507 * - JS flag is set.
7508 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007510 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007511 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 {
7513 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007514 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 }
7516
7517 /*
7518 * If we're inside a "//" comment and there is a "//" comment in a
7519 * previous line, lineup with that one.
7520 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007521 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 && (trypos = find_line_comment()) != NULL) /* XXX */
7523 {
7524 /* find how indented the line beginning the comment is */
7525 getvcol(curwin, trypos, &col, NULL, NULL);
7526 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007527 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 }
7529
7530 /*
7531 * If we're inside a comment and not looking at the start of the
7532 * comment, try using the 'comments' option.
7533 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007534 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 {
7536 int lead_start_len = 2;
7537 int lead_middle_len = 1;
7538 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7539 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7540 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7541 char_u *p;
7542 int start_align = 0;
7543 int start_off = 0;
7544 int done = FALSE;
7545
7546 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007547 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007549 *lead_start = NUL;
7550 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551
7552 p = curbuf->b_p_com;
7553 while (*p != NUL)
7554 {
7555 int align = 0;
7556 int off = 0;
7557 int what = 0;
7558
7559 while (*p != NUL && *p != ':')
7560 {
7561 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7562 what = *p++;
7563 else if (*p == COM_LEFT || *p == COM_RIGHT)
7564 align = *p++;
7565 else if (VIM_ISDIGIT(*p) || *p == '-')
7566 off = getdigits(&p);
7567 else
7568 ++p;
7569 }
7570
7571 if (*p == ':')
7572 ++p;
7573 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7574 if (what == COM_START)
7575 {
7576 STRCPY(lead_start, lead_end);
7577 lead_start_len = (int)STRLEN(lead_start);
7578 start_off = off;
7579 start_align = align;
7580 }
7581 else if (what == COM_MIDDLE)
7582 {
7583 STRCPY(lead_middle, lead_end);
7584 lead_middle_len = (int)STRLEN(lead_middle);
7585 }
7586 else if (what == COM_END)
7587 {
7588 /* If our line starts with the middle comment string, line it
7589 * up with the comment opener per the 'comments' option. */
7590 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7591 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7592 {
7593 done = TRUE;
7594 if (curwin->w_cursor.lnum > 1)
7595 {
7596 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007597 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 * the middle comment string matches in the previous
7599 * line, use the indent of that line. XXX */
7600 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7601 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7602 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7603 else if (STRNCMP(look, lead_middle,
7604 lead_middle_len) == 0)
7605 {
7606 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7607 break;
7608 }
7609 /* If the start comment string doesn't match with the
7610 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007611 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 lead_start, lead_start_len) != 0)
7613 continue;
7614 }
7615 if (start_off != 0)
7616 amount += start_off;
7617 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007618 amount += vim_strsize(lead_start)
7619 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 break;
7621 }
7622
7623 /* If our line starts with the end comment string, line it up
7624 * with the middle comment */
7625 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7626 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7627 {
7628 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7629 /* XXX */
7630 if (off != 0)
7631 amount += off;
7632 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007633 amount += vim_strsize(lead_start)
7634 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 done = TRUE;
7636 break;
7637 }
7638 }
7639 }
7640
7641 /* If our line starts with an asterisk, line up with the
7642 * asterisk in the comment opener; otherwise, line up
7643 * with the first character of the comment text.
7644 */
7645 if (done)
7646 ;
7647 else if (theline[0] == '*')
7648 amount += 1;
7649 else
7650 {
7651 /*
7652 * If we are more than one line away from the comment opener, take
7653 * the indent of the previous non-empty line. If 'cino' has "CO"
7654 * and we are just below the comment opener and there are any
7655 * white characters after it line up with the text after it;
7656 * otherwise, add the amount specified by "c" in 'cino'
7657 */
7658 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007659 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 {
7661 if (linewhite(lnum)) /* skip blank lines */
7662 continue;
7663 amount = get_indent_lnum(lnum); /* XXX */
7664 break;
7665 }
7666 if (amount == -1) /* use the comment opener */
7667 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007668 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007670 start = ml_get(comment_pos->lnum);
7671 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007673 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007675 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007677 if (curbuf->b_ind_in_comment2 || *look == NUL)
7678 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 }
7680 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007681 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 }
7683
7684 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007685 * Are we looking at a ']' that has a match?
7686 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007687 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007688 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7689 {
7690 /* align with the line containing the '['. */
7691 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007692 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007693 }
7694
7695 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 * Are we inside parentheses or braces?
7697 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007698 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007699 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007700 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 || trypos != NULL)
7702 {
7703 if (trypos != NULL && tryposBrace != NULL)
7704 {
7705 /* Both an unmatched '(' and '{' is found. Use the one which is
7706 * closer to the current cursor position, set the other to NULL. */
7707 if (trypos->lnum != tryposBrace->lnum
7708 ? trypos->lnum < tryposBrace->lnum
7709 : trypos->col < tryposBrace->col)
7710 trypos = NULL;
7711 else
7712 tryposBrace = NULL;
7713 }
7714
7715 if (trypos != NULL)
7716 {
7717 /*
7718 * If the matching paren is more than one line away, use the indent of
7719 * a previous non-empty line that matches the same paren.
7720 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007721 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007723 /* Line up with the start of the matching paren line. */
7724 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7725 }
7726 else
7727 {
7728 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007729 our_paren_pos = *trypos;
7730 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007732 l = skipwhite(ml_get(lnum));
7733 if (cin_nocode(l)) /* skip comment lines */
7734 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007735 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007736 continue; /* ignore #define, #if, etc. */
7737 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007739 /* Skip a comment or raw string. XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02007740 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007741 {
7742 lnum = trypos->lnum + 1;
7743 continue;
7744 }
7745
7746 /* XXX */
7747 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007748 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007749 && trypos->lnum == our_paren_pos.lnum
7750 && trypos->col == our_paren_pos.col)
7751 {
7752 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007754 if (theline[0] == ')')
7755 {
7756 if (our_paren_pos.lnum != lnum
7757 && cur_amount > amount)
7758 cur_amount = amount;
7759 amount = -1;
7760 }
7761 break;
7762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 }
7764 }
7765
7766 /*
7767 * Line up with line where the matching paren is. XXX
7768 * If the line starts with a '(' or the indent for unclosed
7769 * parentheses is zero, line up with the unclosed parentheses.
7770 */
7771 if (amount == -1)
7772 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007773 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007774 int is_if_for_while = 0;
7775
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007776 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007777 {
7778 /* Look for the outermost opening parenthesis on this line
7779 * and check whether it belongs to an "if", "for" or "while". */
7780
7781 pos_T cursor_save = curwin->w_cursor;
7782 pos_T outermost;
7783 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007784
7785 trypos = &our_paren_pos;
7786 do {
7787 outermost = *trypos;
7788 curwin->w_cursor.lnum = outermost.lnum;
7789 curwin->w_cursor.col = outermost.col;
7790
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007791 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007792 } while (trypos && trypos->lnum == outermost.lnum);
7793
7794 curwin->w_cursor = cursor_save;
7795
7796 line = ml_get(outermost.lnum);
7797
7798 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007799 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007800 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007801
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007802 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007803 look = skipwhite(look);
7804 if (*look == '(')
7805 {
7806 linenr_T save_lnum = curwin->w_cursor.lnum;
7807 char_u *line;
7808 int look_col;
7809
7810 /* Ignore a '(' in front of the line that has a match before
7811 * our matching '('. */
7812 curwin->w_cursor.lnum = our_paren_pos.lnum;
7813 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007814 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007815 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007816 if ((trypos = findmatchlimit(NULL, ')', 0,
7817 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007818 != NULL
7819 && trypos->lnum == our_paren_pos.lnum
7820 && trypos->col < our_paren_pos.col)
7821 ignore_paren_col = trypos->col + 1;
7822
7823 curwin->w_cursor.lnum = save_lnum;
7824 look = ml_get(our_paren_pos.lnum) + look_col;
7825 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007826 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7827 && is_if_for_while == 0)
7828 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007829 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 {
7831 /*
7832 * If we're looking at a close paren, line up right there;
7833 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007834 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 * the last nonwhite character of the line, use either the
7836 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007837 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 * lines).
7839 */
7840 if (theline[0] != ')')
7841 {
7842 cur_amount = MAXCOL;
7843 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007844 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 && cin_ends_in(l, (char_u *)"(", NULL))
7846 {
7847 /* look for opening unmatched paren, indent one level
7848 * for each additional level */
7849 n = 1;
7850 for (col = 0; col < our_paren_pos.col; ++col)
7851 {
7852 switch (l[col])
7853 {
7854 case '(':
7855 case '{': ++n;
7856 break;
7857
7858 case ')':
7859 case '}': if (n > 1)
7860 --n;
7861 break;
7862 }
7863 }
7864
7865 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007866 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007868 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 our_paren_pos.col++;
7870 else
7871 {
7872 col = our_paren_pos.col + 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01007873 while (VIM_ISWHITE(l[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 col++;
7875 if (l[col] != NUL) /* In case of trailing space */
7876 our_paren_pos.col = col;
7877 else
7878 our_paren_pos.col++;
7879 }
7880 }
7881
7882 /*
7883 * Find how indented the paren is, or the character after it
7884 * if we did the above "if".
7885 */
7886 if (our_paren_pos.col > 0)
7887 {
7888 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7889 if (cur_amount > (int)col)
7890 cur_amount = col;
7891 }
7892 }
7893
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007894 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 {
7896 /* Line up with the start of the matching paren line. */
7897 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007898 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7899 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007900 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 {
7902 if (cur_amount != MAXCOL)
7903 amount = cur_amount;
7904 }
7905 else
7906 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007907 /* Add b_ind_unclosed2 for each '(' before our matching one,
7908 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007910 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 {
7912 --our_paren_pos.col;
7913 switch (*ml_get_pos(&our_paren_pos))
7914 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007915 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 col = our_paren_pos.col;
7917 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007918 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 col = MAXCOL;
7920 break;
7921 }
7922 }
7923
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007924 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 * braces */
7926 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007927 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 else
7929 {
7930 curwin->w_cursor.lnum = our_paren_pos.lnum;
7931 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007932 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7933 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007934 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007936 {
7937 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007938 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007939 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007940 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 }
7943 /*
7944 * For a line starting with ')' use the minimum of the two
7945 * positions, to avoid giving it more indent than the previous
7946 * lines:
7947 * func_long_name( if (x
7948 * arg && yy
7949 * ) ^ not here ) ^ not here
7950 */
7951 if (cur_amount < amount)
7952 amount = cur_amount;
7953 }
7954 }
7955
7956 /* add extra indent for a comment */
7957 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007958 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 else
7961 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007962 /*
7963 * We are inside braces, there is a { before this line at the position
7964 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007965 * Make a copy of tryposBrace, it may point to pos_copy inside
7966 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007967 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007968 tryposCopy = *tryposBrace;
7969 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 ourscope = trypos->lnum;
7972 start = ml_get(ourscope);
7973
7974 /*
7975 * Now figure out how indented the line is in general.
7976 * If the brace was at the start of the line, we use that;
7977 * otherwise, check out the indentation of the line as
7978 * a whole and then add the "imaginary indent" to that.
7979 */
7980 look = skipwhite(start);
7981 if (*look == '{')
7982 {
7983 getvcol(curwin, trypos, &col, NULL, NULL);
7984 amount = col;
7985 if (*start == '{')
7986 start_brace = BRACE_IN_COL0;
7987 else
7988 start_brace = BRACE_AT_START;
7989 }
7990 else
7991 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007992 /* That opening brace might have been on a continuation
7993 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 curwin->w_cursor.lnum = ourscope;
7995
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007996 /* Position the cursor over the rightmost paren, so that
7997 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 lnum = ourscope;
7999 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008000 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
8001 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 lnum = trypos->lnum;
8003
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008004 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 * case 1: if (asdf &&
8006 * ldfd) {
8007 * }
8008 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02008009 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
8010 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02008012 else if (curbuf->b_ind_js)
8013 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008015 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016
8017 start_brace = BRACE_AT_END;
8018 }
8019
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008020 /* For Javascript check if the line starts with "key:". */
8021 if (curbuf->b_ind_js)
8022 js_cur_has_key = cin_has_js_key(theline);
8023
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008025 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 * we want to be. otherwise, add the amount of room
8027 * that an indent is supposed to be.
8028 */
8029 if (theline[0] == '}')
8030 {
8031 /*
8032 * they may want closing braces to line up with something
8033 * other than the open brace. indulge them, if so.
8034 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008035 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 }
8037 else
8038 {
8039 /*
8040 * If we're looking at an "else", try to find an "if"
8041 * to match it with.
8042 * If we're looking at a "while", try to find a "do"
8043 * to match it with.
8044 */
8045 lookfor = LOOKFOR_INITIAL;
8046 if (cin_iselse(theline))
8047 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008048 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 lookfor = LOOKFOR_DO;
8050 if (lookfor != LOOKFOR_INITIAL)
8051 {
8052 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008053 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 {
8055 amount = get_indent(); /* XXX */
8056 goto theend;
8057 }
8058 }
8059
8060 /*
8061 * We get here if we are not on an "while-of-do" or "else" (or
8062 * failed to find a matching "if").
8063 * Search backwards for something to line up with.
8064 * First set amount for when we don't find anything.
8065 */
8066
8067 /*
8068 * if the '{' is _really_ at the left margin, use the imaginary
8069 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008070 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 */
8072
8073 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
8074 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008075 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008076 lookfor_cpp_namespace = TRUE;
8077 }
8078 else if (start_brace == BRACE_AT_START &&
8079 lookfor_cpp_namespace) /* '{' is at start */
8080 {
8081
8082 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 }
8084 else
8085 {
8086 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008087 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008088 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008089
8090 l = skipwhite(ml_get_curline());
8091 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008092 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008093 else if (cin_is_cpp_extern_c(l))
8094 amount += curbuf->b_ind_cpp_extern_c;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 else
8097 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008098 /* Compensate for adding b_ind_open_extra later. */
8099 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 if (amount < 0)
8101 amount = 0;
8102 }
8103 }
8104
8105 lookfor_break = FALSE;
8106
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008107 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 {
8109 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008110 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 }
8112 else if (cin_isscopedecl(theline)) /* private:, ... */
8113 {
8114 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008115 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 }
8117 else
8118 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008119 if (curbuf->b_ind_case_break && cin_isbreak(theline))
8120 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 lookfor_break = TRUE;
8122
8123 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008124 /* b_ind_level from start of block */
8125 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 }
8127 scope_amount = amount;
8128 whilelevel = 0;
8129
8130 /*
8131 * Search backwards. If we find something we recognize, line up
8132 * with that.
8133 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008134 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 * the usual amount relative to the conditional
8136 * that opens the block.
8137 */
8138 curwin->w_cursor = cur_curpos;
8139 for (;;)
8140 {
8141 curwin->w_cursor.lnum--;
8142 curwin->w_cursor.col = 0;
8143
8144 /*
8145 * If we went all the way back to the start of our scope, line
8146 * up with it.
8147 */
8148 if (curwin->w_cursor.lnum <= ourscope)
8149 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008150 /* We reached end of scope:
8151 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008153 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 * don't add ind_continuation, otherwise it is a variable
8155 * declaration:
8156 * int x,
8157 * here; <-- add ind_continuation
8158 */
8159 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8160 {
8161 if (curwin->w_cursor.lnum == 0
8162 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008163 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008165 /* nothing found (abuse curbuf->b_ind_maxparen as
8166 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 * initialization) */
8168 if (cont_amount > 0)
8169 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008170 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 amount += ind_continuation;
8172 break;
8173 }
8174
8175 l = ml_get_curline();
8176
8177 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008178 * If we're in a comment or raw string now, skip to
8179 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 */
Bram Moolenaardde81312017-08-26 17:49:01 +02008181 trypos = ind_find_start_CORS(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 if (trypos != NULL)
8183 {
8184 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008185 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 continue;
8187 }
8188
8189 /*
8190 * Skip preprocessor directives and blank lines.
8191 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008192 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8193 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 continue;
8195
8196 if (cin_nocode(l))
8197 continue;
8198
8199 terminated = cin_isterminated(l, FALSE, TRUE);
8200
8201 /*
8202 * If we are at top level and the line looks like a
8203 * function declaration, we are done
8204 * (it's a variable declaration).
8205 */
8206 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008207 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 {
8209 /* if the line is terminated with another ','
8210 * it is a continued variable initialization.
8211 * don't add extra indent.
8212 * TODO: does not work, if a function
8213 * declaration is split over multiple lines:
8214 * cin_isfuncdecl returns FALSE then.
8215 */
8216 if (terminated == ',')
8217 break;
8218
8219 /* if it es a enum declaration or an assignment,
8220 * we are done.
8221 */
8222 if (terminated != ';' && cin_isinit())
8223 break;
8224
8225 /* nothing useful found */
8226 if (terminated == 0 || terminated == '{')
8227 continue;
8228 }
8229
8230 if (terminated != ';')
8231 {
8232 /* Skip parens and braces. Position the cursor
8233 * over the rightmost paren, so that matching it
8234 * will take us back to the start of the line.
8235 */ /* XXX */
8236 trypos = NULL;
8237 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008238 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008239 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240
8241 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008242 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243
8244 if (trypos != NULL)
8245 {
8246 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008247 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 continue;
8249 }
8250 }
8251
8252 /* it's a variable declaration, add indentation
8253 * like in
8254 * int a,
8255 * b;
8256 */
8257 if (cont_amount > 0)
8258 amount = cont_amount;
8259 else
8260 amount += ind_continuation;
8261 }
8262 else if (lookfor == LOOKFOR_UNTERM)
8263 {
8264 if (cont_amount > 0)
8265 amount = cont_amount;
8266 else
8267 amount += ind_continuation;
8268 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008269 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008270 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02008271 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008272 && lookfor != LOOKFOR_CPP_BASECLASS
8273 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02008274 {
8275 amount = scope_amount;
8276 if (theline[0] == '{')
8277 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008278 amount += curbuf->b_ind_open_extra;
8279 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008280 }
8281 }
8282
8283 if (lookfor_cpp_namespace)
8284 {
8285 /*
8286 * Looking for C++ namespace, need to look further
8287 * back.
8288 */
8289 if (curwin->w_cursor.lnum == ourscope)
8290 continue;
8291
8292 if (curwin->w_cursor.lnum == 0
8293 || curwin->w_cursor.lnum
8294 < ourscope - FIND_NAMESPACE_LIM)
8295 break;
8296
8297 l = ml_get_curline();
8298
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008299 /* If we're in a comment or raw string now, skip
8300 * to the start of it. */
Bram Moolenaardde81312017-08-26 17:49:01 +02008301 trypos = ind_find_start_CORS(NULL);
Bram Moolenaare79d1532011-10-04 18:03:47 +02008302 if (trypos != NULL)
8303 {
8304 curwin->w_cursor.lnum = trypos->lnum + 1;
8305 curwin->w_cursor.col = 0;
8306 continue;
8307 }
8308
8309 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008310 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8311 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02008312 continue;
8313
8314 /* Finally the actual check for "namespace". */
8315 if (cin_is_cpp_namespace(l))
8316 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008317 amount += curbuf->b_ind_cpp_namespace
8318 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008319 break;
8320 }
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008321 else if (cin_is_cpp_extern_c(l))
8322 {
8323 amount += curbuf->b_ind_cpp_extern_c
8324 - added_to_amount;
8325 break;
8326 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008327
8328 if (cin_nocode(l))
8329 continue;
8330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 }
8332 break;
8333 }
8334
8335 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008336 * If we're in a comment or raw string now, skip to the start
8337 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008339 if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 {
8341 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008342 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 continue;
8344 }
8345
8346 l = ml_get_curline();
8347
8348 /*
8349 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008350 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008352 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 if (iscase || cin_isscopedecl(l))
8354 {
8355 /* we are only looking for cpp base class
8356 * declaration/initialization any longer */
8357 if (lookfor == LOOKFOR_CPP_BASECLASS)
8358 break;
8359
8360 /* When looking for a "do" we are not interested in
8361 * labels. */
8362 if (whilelevel > 0)
8363 continue;
8364
8365 /*
8366 * case xx:
8367 * c = 99 + <- this indent plus continuation
8368 *-> here;
8369 */
8370 if (lookfor == LOOKFOR_UNTERM
8371 || lookfor == LOOKFOR_ENUM_OR_INIT)
8372 {
8373 if (cont_amount > 0)
8374 amount = cont_amount;
8375 else
8376 amount += ind_continuation;
8377 break;
8378 }
8379
8380 /*
8381 * case xx: <- line up with this case
8382 * x = 333;
8383 * case yy:
8384 */
8385 if ( (iscase && lookfor == LOOKFOR_CASE)
8386 || (iscase && lookfor_break)
8387 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8388 {
8389 /*
8390 * Check that this case label is not for another
8391 * switch()
8392 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008393 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008394 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 {
8396 amount = get_indent(); /* XXX */
8397 break;
8398 }
8399 continue;
8400 }
8401
8402 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8403
8404 /*
8405 * case xx: if (cond) <- line up with this if
8406 * y = y + 1;
8407 * -> s = 99;
8408 *
8409 * case xx:
8410 * if (cond) <- line up with this line
8411 * y = y + 1;
8412 * -> s = 99;
8413 */
8414 if (lookfor == LOOKFOR_TERM)
8415 {
8416 if (n)
8417 amount = n;
8418
8419 if (!lookfor_break)
8420 break;
8421 }
8422
8423 /*
8424 * case xx: x = x + 1; <- line up with this x
8425 * -> y = y + 1;
8426 *
8427 * case xx: if (cond) <- line up with this if
8428 * -> y = y + 1;
8429 */
8430 if (n)
8431 {
8432 amount = n;
8433 l = after_label(ml_get_curline());
8434 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008435 {
8436 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008437 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008438 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008439 amount += curbuf->b_ind_level
8440 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 break;
8443 }
8444
8445 /*
8446 * Try to get the indent of a statement before the switch
8447 * label. If nothing is found, line up relative to the
8448 * switch label.
8449 * break; <- may line up with this line
8450 * case xx:
8451 * -> y = 1;
8452 */
8453 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008454 ? curbuf->b_ind_case_code
8455 : curbuf->b_ind_scopedecl_code);
8456 lookfor = curbuf->b_ind_case_break
8457 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 continue;
8459 }
8460
8461 /*
8462 * Looking for a switch() label or C++ scope declaration,
8463 * ignore other lines, skip {}-blocks.
8464 */
8465 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8466 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008467 if (find_last_paren(l, '{', '}')
8468 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008469 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008471 curwin->w_cursor.col = 0;
8472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 continue;
8474 }
8475
8476 /*
8477 * Ignore jump labels with nothing after them.
8478 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008479 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 {
8481 l = after_label(ml_get_curline());
8482 if (l == NULL || cin_nocode(l))
8483 continue;
8484 }
8485
8486 /*
8487 * Ignore #defines, #if, etc.
8488 * Ignore comment and empty lines.
8489 * (need to get the line again, cin_islabel() may have
8490 * unlocked it)
8491 */
8492 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008493 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 || cin_nocode(l))
8495 continue;
8496
8497 /*
8498 * Are we at the start of a cpp base class declaration or
8499 * constructor initialization?
8500 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008501 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008502 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008503 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008504 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008505 l = ml_get_curline();
8506 }
8507 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 {
8509 if (lookfor == LOOKFOR_UNTERM)
8510 {
8511 if (cont_amount > 0)
8512 amount = cont_amount;
8513 else
8514 amount += ind_continuation;
8515 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008516 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008518 /* Need to find start of the declaration. */
8519 lookfor = LOOKFOR_UNTERM;
8520 ind_continuation = 0;
8521 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 }
8523 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008524 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008525 amount = get_baseclass_amount(
8526 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 break;
8528 }
8529 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8530 {
8531 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008532 * declaration or initialization before the opening brace.
8533 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 if (cin_isterminated(l, TRUE, FALSE))
8535 break;
8536 else
8537 continue;
8538 }
8539
8540 /*
8541 * What happens next depends on the line being terminated.
8542 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008543 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 * 123,
8545 * sizeof
8546 * here
8547 * Otherwise check whether it is a enumeration or structure
8548 * initialisation (not indented) or a variable declaration
8549 * (indented).
8550 */
8551 terminated = cin_isterminated(l, FALSE, TRUE);
8552
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008553 if (js_cur_has_key)
8554 {
8555 js_cur_has_key = 0; /* only check the first line */
8556 if (curbuf->b_ind_js && terminated == ',')
8557 {
8558 /* For Javascript we might be inside an object:
8559 * key: something, <- align with this
8560 * key: something
8561 * or:
8562 * key: something + <- align with this
8563 * something,
8564 * key: something
8565 */
8566 lookfor = LOOKFOR_JS_KEY;
8567 }
8568 }
8569 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8570 {
8571 amount = get_indent();
8572 break;
8573 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008574 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008575 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008576 if (tryposBrace != NULL && tryposBrace->lnum
8577 >= curwin->w_cursor.lnum)
8578 break;
8579 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008580 /* line below current line is the one that starts a
8581 * (possibly broken) line ending in a comma. */
8582 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008583 else
8584 {
8585 amount = get_indent();
8586 if (curwin->w_cursor.lnum - 1 == ourscope)
8587 /* line above is start of the scope, thus current
8588 * line is the one that stars a (possibly broken)
8589 * line ending in a comma. */
8590 break;
8591 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008592 }
8593
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8595 && terminated == ','))
8596 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008597 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8598 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008599 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 /*
8601 * if we're in the middle of a paren thing,
8602 * go back to the line that starts it so
8603 * we can get the right prevailing indent
8604 * if ( foo &&
8605 * bar )
8606 */
8607 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008608 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008610 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 */
8612 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008613 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008614 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8615 || (trypos->lnum == tryposBrace->lnum
8616 && trypos->col < tryposBrace->col)))
8617 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618
8619 /*
8620 * If we are looking for ',', we also look for matching
8621 * braces.
8622 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008623 if (trypos == NULL && terminated == ','
8624 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008625 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626
8627 if (trypos != NULL)
8628 {
8629 /*
8630 * Check if we are on a case label now. This is
8631 * handled above.
8632 * case xx: if ( asdf &&
8633 * asdf)
8634 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008635 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008637 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 {
8639 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008640 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 continue;
8642 }
8643 }
8644
8645 /*
8646 * Skip over continuation lines to find the one to get the
8647 * indent from
8648 * char *usethis = "bla\
8649 * bla",
8650 * here;
8651 */
8652 if (terminated == ',')
8653 {
8654 while (curwin->w_cursor.lnum > 1)
8655 {
8656 l = ml_get(curwin->w_cursor.lnum - 1);
8657 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8658 break;
8659 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008660 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 }
8662 }
8663
8664 /*
8665 * Get indent and pointer to text for current line,
8666 * ignoring any jump label. XXX
8667 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008668 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008669 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008670 else
8671 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 /*
8673 * If this is just above the line we are indenting, and it
8674 * starts with a '{', line it up with this line.
8675 * while (not)
8676 * -> {
8677 * }
8678 */
8679 if (terminated != ',' && lookfor != LOOKFOR_TERM
8680 && theline[0] == '{')
8681 {
8682 amount = cur_amount;
8683 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008684 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 * doesn't start with a '{', which must have a match
8686 * in the same line (scope is the same). Probably:
8687 * { 1, 2 },
8688 * -> { 3, 4 }
8689 */
8690 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008691 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008693 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 {
8695 /* have to look back, whether it is a cpp base
8696 * class declaration or initialization */
8697 lookfor = LOOKFOR_CPP_BASECLASS;
8698 continue;
8699 }
8700 break;
8701 }
8702
8703 /*
8704 * Check if we are after an "if", "while", etc.
8705 * Also allow " } else".
8706 */
8707 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8708 {
8709 /*
8710 * Found an unterminated line after an if (), line up
8711 * with the last one.
8712 * if (cond)
8713 * 100 +
8714 * -> here;
8715 */
8716 if (lookfor == LOOKFOR_UNTERM
8717 || lookfor == LOOKFOR_ENUM_OR_INIT)
8718 {
8719 if (cont_amount > 0)
8720 amount = cont_amount;
8721 else
8722 amount += ind_continuation;
8723 break;
8724 }
8725
8726 /*
8727 * If this is just above the line we are indenting, we
8728 * are finished.
8729 * while (not)
8730 * -> here;
8731 * Otherwise this indent can be used when the line
8732 * before this is terminated.
8733 * yyy;
8734 * if (stat)
8735 * while (not)
8736 * xxx;
8737 * -> here;
8738 */
8739 amount = cur_amount;
8740 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008741 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 if (lookfor != LOOKFOR_TERM)
8743 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008744 amount += curbuf->b_ind_level
8745 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 break;
8747 }
8748
8749 /*
8750 * Special trick: when expecting the while () after a
8751 * do, line up with the while()
8752 * do
8753 * x = 1;
8754 * -> here
8755 */
8756 l = skipwhite(ml_get_curline());
8757 if (cin_isdo(l))
8758 {
8759 if (whilelevel == 0)
8760 break;
8761 --whilelevel;
8762 }
8763
8764 /*
8765 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008766 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 * Need to use the scope of this "else". XXX
8768 * If whilelevel != 0 continue looking for a "do {".
8769 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008770 if (cin_iselse(l) && whilelevel == 0)
8771 {
8772 /* If we're looking at "} else", let's make sure we
8773 * find the opening brace of the enclosing scope,
8774 * not the one from "if () {". */
8775 if (*l == '}')
8776 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008777 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008778
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008779 if ((trypos = find_start_brace()) == NULL
8780 || find_match(LOOKFOR_IF, trypos->lnum)
8781 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008782 break;
8783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 }
8785
8786 /*
8787 * If we're below an unterminated line that is not an
8788 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008789 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 * the line before this one.
8791 */
8792 else
8793 {
8794 /*
8795 * Found two unterminated lines on a row, line up with
8796 * the last one.
8797 * c = 99 +
8798 * 100 +
8799 * -> here;
8800 */
8801 if (lookfor == LOOKFOR_UNTERM)
8802 {
8803 /* When line ends in a comma add extra indent */
8804 if (terminated == ',')
8805 amount += ind_continuation;
8806 break;
8807 }
8808
8809 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8810 {
8811 /* Found two lines ending in ',', lineup with the
8812 * lowest one, but check for cpp base class
8813 * declaration/initialization, if it is an
8814 * opening brace or we are looking just for
8815 * enumerations/initializations. */
8816 if (terminated == ',')
8817 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008818 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 break;
8820
8821 lookfor = LOOKFOR_CPP_BASECLASS;
8822 continue;
8823 }
8824
8825 /* Ignore unterminated lines in between, but
8826 * reduce indent. */
8827 if (amount > cur_amount)
8828 amount = cur_amount;
8829 }
8830 else
8831 {
8832 /*
8833 * Found first unterminated line on a row, may
8834 * line up with this line, remember its indent
8835 * 100 +
8836 * -> here;
8837 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008838 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008840
8841 n = (int)STRLEN(l);
8842 if (terminated == ',' && (*skipwhite(l) == ']'
8843 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008844 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845
8846 /*
8847 * If previous line ends in ',', check whether we
8848 * are in an initialization or enum
8849 * struct xxx =
8850 * {
8851 * sizeof a,
8852 * 124 };
8853 * or a normal possible continuation line.
8854 * but only, of no other statement has been found
8855 * yet.
8856 */
8857 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8858 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008859 if (curbuf->b_ind_js)
8860 {
8861 /* Search for a line ending in a comma
8862 * and line up with the line below it
8863 * (could be the current line).
8864 * some = [
8865 * 1, <- line up here
8866 * 2,
8867 * some = [
8868 * 3 + <- line up here
8869 * 4 *
8870 * 5,
8871 * 6,
8872 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008873 if (cin_iscomment(skipwhite(l)))
8874 break;
8875 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008876 trypos = find_match_char('[',
8877 curbuf->b_ind_maxparen);
8878 if (trypos != NULL)
8879 {
8880 if (trypos->lnum
8881 == curwin->w_cursor.lnum - 1)
8882 {
8883 /* Current line is first inside
8884 * [], line up with it. */
8885 break;
8886 }
8887 ourscope = trypos->lnum;
8888 }
8889 }
8890 else
8891 {
8892 lookfor = LOOKFOR_ENUM_OR_INIT;
8893 cont_amount = cin_first_id_amount();
8894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 }
8896 else
8897 {
8898 if (lookfor == LOOKFOR_INITIAL
8899 && *l != NUL
8900 && l[STRLEN(l) - 1] == '\\')
8901 /* XXX */
8902 cont_amount = cin_get_equal_amount(
8903 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008904 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008905 && lookfor != LOOKFOR_JS_KEY
Bram Moolenaardde81312017-08-26 17:49:01 +02008906 && lookfor != LOOKFOR_COMMA
8907 && raw_string_start != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 lookfor = LOOKFOR_UNTERM;
8909 }
8910 }
8911 }
8912 }
8913
8914 /*
8915 * Check if we are after a while (cond);
8916 * If so: Ignore until the matching "do".
8917 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008918 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 {
8920 /*
8921 * Found an unterminated line after a while ();, line up
8922 * with the last one.
8923 * while (cond);
8924 * 100 + <- line up with this one
8925 * -> here;
8926 */
8927 if (lookfor == LOOKFOR_UNTERM
8928 || lookfor == LOOKFOR_ENUM_OR_INIT)
8929 {
8930 if (cont_amount > 0)
8931 amount = cont_amount;
8932 else
8933 amount += ind_continuation;
8934 break;
8935 }
8936
8937 if (whilelevel == 0)
8938 {
8939 lookfor = LOOKFOR_TERM;
8940 amount = get_indent(); /* XXX */
8941 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008942 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 }
8944 ++whilelevel;
8945 }
8946
8947 /*
8948 * We are after a "normal" statement.
8949 * If we had another statement we can stop now and use the
8950 * indent of that other statement.
8951 * Otherwise the indent of the current statement may be used,
8952 * search backwards for the next "normal" statement.
8953 */
8954 else
8955 {
8956 /*
8957 * Skip single break line, if before a switch label. It
8958 * may be lined up with the case label.
8959 */
8960 if (lookfor == LOOKFOR_NOBREAK
8961 && cin_isbreak(skipwhite(ml_get_curline())))
8962 {
8963 lookfor = LOOKFOR_ANY;
8964 continue;
8965 }
8966
8967 /*
8968 * Handle "do {" line.
8969 */
8970 if (whilelevel > 0)
8971 {
8972 l = cin_skipcomment(ml_get_curline());
8973 if (cin_isdo(l))
8974 {
8975 amount = get_indent(); /* XXX */
8976 --whilelevel;
8977 continue;
8978 }
8979 }
8980
8981 /*
8982 * Found a terminated line above an unterminated line. Add
8983 * the amount for a continuation line.
8984 * x = 1;
8985 * y = foo +
8986 * -> here;
8987 * or
8988 * int x = 1;
8989 * int foo,
8990 * -> here;
8991 */
8992 if (lookfor == LOOKFOR_UNTERM
8993 || lookfor == LOOKFOR_ENUM_OR_INIT)
8994 {
8995 if (cont_amount > 0)
8996 amount = cont_amount;
8997 else
8998 amount += ind_continuation;
8999 break;
9000 }
9001
9002 /*
9003 * Found a terminated line above a terminated line or "if"
9004 * etc. line. Use the amount of the line below us.
9005 * x = 1; x = 1;
9006 * if (asdf) y = 2;
9007 * while (asdf) ->here;
9008 * here;
9009 * ->foo;
9010 */
9011 if (lookfor == LOOKFOR_TERM)
9012 {
9013 if (!lookfor_break && whilelevel == 0)
9014 break;
9015 }
9016
9017 /*
9018 * First line above the one we're indenting is terminated.
9019 * To know what needs to be done look further backward for
9020 * a terminated line.
9021 */
9022 else
9023 {
9024 /*
9025 * position the cursor over the rightmost paren, so
9026 * that matching it will take us back to the start of
9027 * the line. Helps for:
9028 * func(asdr,
9029 * asdfasdf);
9030 * here;
9031 */
9032term_again:
9033 l = ml_get_curline();
9034 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009035 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009036 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 {
9038 /*
9039 * Check if we are on a case label now. This is
9040 * handled above.
9041 * case xx: if ( asdf &&
9042 * asdf)
9043 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009044 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02009046 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 {
9048 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009049 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 continue;
9051 }
9052 }
9053
9054 /* When aligning with the case statement, don't align
9055 * with a statement after it.
9056 * case 1: { <-- don't use this { position
9057 * stat;
9058 * }
9059 * case 2:
9060 * stat;
9061 * }
9062 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009063 iscase = (curbuf->b_ind_keep_case_label
9064 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065
9066 /*
9067 * Get indent and pointer to text for current line,
9068 * ignoring any jump label.
9069 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009070 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071
9072 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009073 amount += curbuf->b_ind_open_extra;
9074 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00009075 l = skipwhite(l);
9076 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009077 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
9079
9080 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009081 * When a terminated line starts with "else" skip to
9082 * the matching "if":
9083 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009084 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009085 * Need to use the scope of this "else". XXX
9086 * If whilelevel != 0 continue looking for a "do {".
9087 */
9088 if (lookfor == LOOKFOR_TERM
9089 && *l != '}'
9090 && cin_iselse(l)
9091 && whilelevel == 0)
9092 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009093 if ((trypos = find_start_brace()) == NULL
9094 || find_match(LOOKFOR_IF, trypos->lnum)
9095 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009096 break;
9097 continue;
9098 }
9099
9100 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 * If we're at the end of a block, skip to the start of
9102 * that block.
9103 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01009104 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009105 if (find_last_paren(l, '{', '}') /* XXX */
9106 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009108 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 /* if not "else {" check for terminated again */
9110 /* but skip block for "} else {" */
9111 l = cin_skipcomment(ml_get_curline());
9112 if (*l == '}' || !cin_iselse(l))
9113 goto term_again;
9114 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009115 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116 }
9117 }
9118 }
9119 }
9120 }
9121 }
9122
9123 /* add extra indent for a comment */
9124 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009125 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02009126
9127 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009128 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
9129 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009130
9131 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009133
9134 /*
9135 * ok -- we're not inside any sort of structure at all!
9136 *
9137 * This means we're at the top level, and everything should
9138 * basically just match where the previous line is, except
9139 * for the lines immediately following a function declaration,
9140 * which are K&R-style parameters and need to be indented.
9141 *
9142 * if our line starts with an open brace, forget about any
9143 * prevailing indent and make sure it looks like the start
9144 * of a function
9145 */
9146
9147 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009149 amount = curbuf->b_ind_first_open;
9150 goto theend;
9151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009153 /*
9154 * If the NEXT line is a function declaration, the current
9155 * line needs to be indented as a function type spec.
9156 * Don't do this if the current line looks like a comment or if the
9157 * current line is terminated, ie. ends in ';', or if the current line
9158 * contains { or }: "void f() {\n if (1)"
9159 */
9160 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
9161 && !cin_nocode(theline)
9162 && vim_strchr(theline, '{') == NULL
9163 && vim_strchr(theline, '}') == NULL
9164 && !cin_ends_in(theline, (char_u *)":", NULL)
9165 && !cin_ends_in(theline, (char_u *)",", NULL)
9166 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
9167 cur_curpos.lnum + 1)
9168 && !cin_isterminated(theline, FALSE, TRUE))
9169 {
9170 amount = curbuf->b_ind_func_type;
9171 goto theend;
9172 }
9173
9174 /* search backwards until we find something we recognize */
9175 amount = 0;
9176 curwin->w_cursor = cur_curpos;
9177 while (curwin->w_cursor.lnum > 1)
9178 {
9179 curwin->w_cursor.lnum--;
9180 curwin->w_cursor.col = 0;
9181
9182 l = ml_get_curline();
9183
9184 /*
9185 * If we're in a comment or raw string now, skip to the start
9186 * of it.
9187 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02009188 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009190 curwin->w_cursor.lnum = trypos->lnum + 1;
9191 curwin->w_cursor.col = 0;
9192 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 }
9194
9195 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009196 * Are we at the start of a cpp base class declaration or
9197 * constructor initialization?
9198 */ /* XXX */
9199 n = FALSE;
9200 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009202 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
9203 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009205 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009207 /* XXX */
9208 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
9209 break;
9210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009212 /*
9213 * Skip preprocessor directives and blank lines.
9214 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009215 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009216 continue;
9217
9218 if (cin_nocode(l))
9219 continue;
9220
9221 /*
9222 * If the previous line ends in ',', use one level of
9223 * indentation:
9224 * int foo,
9225 * bar;
9226 * do this before checking for '}' in case of eg.
9227 * enum foobar
9228 * {
9229 * ...
9230 * } foo,
9231 * bar;
9232 */
9233 n = 0;
9234 if (cin_ends_in(l, (char_u *)",", NULL)
9235 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
9236 {
9237 /* take us back to opening paren */
9238 if (find_last_paren(l, '(', ')')
9239 && (trypos = find_match_paren(
9240 curbuf->b_ind_maxparen)) != NULL)
9241 curwin->w_cursor = *trypos;
9242
9243 /* For a line ending in ',' that is a continuation line go
9244 * back to the first line with a backslash:
9245 * char *foo = "bla\
9246 * bla",
9247 * here;
9248 */
9249 while (n == 0 && curwin->w_cursor.lnum > 1)
9250 {
9251 l = ml_get(curwin->w_cursor.lnum - 1);
9252 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
9253 break;
9254 --curwin->w_cursor.lnum;
9255 curwin->w_cursor.col = 0;
9256 }
9257
9258 amount = get_indent(); /* XXX */
9259
9260 if (amount == 0)
9261 amount = cin_first_id_amount();
9262 if (amount == 0)
9263 amount = ind_continuation;
9264 break;
9265 }
9266
9267 /*
9268 * If the line looks like a function declaration, and we're
9269 * not in a comment, put it the left margin.
9270 */
9271 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
9272 break;
9273 l = ml_get_curline();
9274
9275 /*
9276 * Finding the closing '}' of a previous function. Put
9277 * current line at the left margin. For when 'cino' has "fs".
9278 */
9279 if (*skipwhite(l) == '}')
9280 break;
9281
9282 /* (matching {)
9283 * If the previous line ends on '};' (maybe followed by
9284 * comments) align at column 0. For example:
9285 * char *string_array[] = { "foo",
9286 * / * x * / "b};ar" }; / * foobar * /
9287 */
9288 if (cin_ends_in(l, (char_u *)"};", NULL))
9289 break;
9290
9291 /*
9292 * If the previous line ends on '[' we are probably in an
9293 * array constant:
9294 * something = [
9295 * 234, <- extra indent
9296 */
9297 if (cin_ends_in(l, (char_u *)"[", NULL))
9298 {
9299 amount = get_indent() + ind_continuation;
9300 break;
9301 }
9302
9303 /*
9304 * Find a line only has a semicolon that belongs to a previous
9305 * line ending in '}', e.g. before an #endif. Don't increase
9306 * indent then.
9307 */
9308 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
9309 {
9310 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311
9312 while (curwin->w_cursor.lnum > 1)
9313 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009314 look = ml_get(--curwin->w_cursor.lnum);
9315 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009316 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009318 }
9319 if (curwin->w_cursor.lnum > 0
9320 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009323 curwin->w_cursor = curpos_save;
9324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009326 /*
9327 * If the PREVIOUS line is a function declaration, the current
9328 * line (and the ones that follow) needs to be indented as
9329 * parameters.
9330 */
9331 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
9332 {
9333 amount = curbuf->b_ind_param;
9334 break;
9335 }
9336
9337 /*
9338 * If the previous line ends in ';' and the line before the
9339 * previous line ends in ',' or '\', ident to column zero:
9340 * int foo,
9341 * bar;
9342 * indent_to_0 here;
9343 */
9344 if (cin_ends_in(l, (char_u *)";", NULL))
9345 {
9346 l = ml_get(curwin->w_cursor.lnum - 1);
9347 if (cin_ends_in(l, (char_u *)",", NULL)
9348 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9349 break;
9350 l = ml_get_curline();
9351 }
9352
9353 /*
9354 * Doesn't look like anything interesting -- so just
9355 * use the indent of this line.
9356 *
9357 * Position the cursor over the rightmost paren, so that
9358 * matching it will take us back to the start of the line.
9359 */
9360 find_last_paren(l, '(', ')');
9361
9362 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9363 curwin->w_cursor = *trypos;
9364 amount = get_indent(); /* XXX */
9365 break;
9366 }
9367
9368 /* add extra indent for a comment */
9369 if (cin_iscomment(theline))
9370 amount += curbuf->b_ind_comment;
9371
9372 /* add extra indent if the previous line ended in a backslash:
9373 * "asdfasdf\
9374 * here";
9375 * char *foo = "asdf\
9376 * here";
9377 */
9378 if (cur_curpos.lnum > 1)
9379 {
9380 l = ml_get(cur_curpos.lnum - 1);
9381 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9382 {
9383 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9384 if (cur_amount > 0)
9385 amount = cur_amount;
9386 else if (cur_amount == 0)
9387 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 }
9389 }
9390
9391theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009392 if (amount < 0)
9393 amount = 0;
9394
9395laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 /* put the cursor back where it belongs */
9397 curwin->w_cursor = cur_curpos;
9398
9399 vim_free(linecopy);
9400
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 return amount;
9402}
9403
9404 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009405find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406{
9407 char_u *look;
9408 pos_T *theirscope;
9409 char_u *mightbeif;
9410 int elselevel;
9411 int whilelevel;
9412
9413 if (lookfor == LOOKFOR_IF)
9414 {
9415 elselevel = 1;
9416 whilelevel = 0;
9417 }
9418 else
9419 {
9420 elselevel = 0;
9421 whilelevel = 1;
9422 }
9423
9424 curwin->w_cursor.col = 0;
9425
9426 while (curwin->w_cursor.lnum > ourscope + 1)
9427 {
9428 curwin->w_cursor.lnum--;
9429 curwin->w_cursor.col = 0;
9430
9431 look = cin_skipcomment(ml_get_curline());
9432 if (cin_iselse(look)
9433 || cin_isif(look)
9434 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009435 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 {
9437 /*
9438 * if we've gone outside the braces entirely,
9439 * we must be out of scope...
9440 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009441 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442 if (theirscope == NULL)
9443 break;
9444
9445 /*
9446 * and if the brace enclosing this is further
9447 * back than the one enclosing the else, we're
9448 * out of luck too.
9449 */
9450 if (theirscope->lnum < ourscope)
9451 break;
9452
9453 /*
9454 * and if they're enclosed in a *deeper* brace,
9455 * then we can ignore it because it's in a
9456 * different scope...
9457 */
9458 if (theirscope->lnum > ourscope)
9459 continue;
9460
9461 /*
9462 * if it was an "else" (that's not an "else if")
9463 * then we need to go back to another if, so
9464 * increment elselevel
9465 */
9466 look = cin_skipcomment(ml_get_curline());
9467 if (cin_iselse(look))
9468 {
9469 mightbeif = cin_skipcomment(look + 4);
9470 if (!cin_isif(mightbeif))
9471 ++elselevel;
9472 continue;
9473 }
9474
9475 /*
9476 * if it was a "while" then we need to go back to
9477 * another "do", so increment whilelevel. XXX
9478 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009479 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 {
9481 ++whilelevel;
9482 continue;
9483 }
9484
9485 /* If it's an "if" decrement elselevel */
9486 look = cin_skipcomment(ml_get_curline());
9487 if (cin_isif(look))
9488 {
9489 elselevel--;
9490 /*
9491 * When looking for an "if" ignore "while"s that
9492 * get in the way.
9493 */
9494 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9495 whilelevel = 0;
9496 }
9497
9498 /* If it's a "do" decrement whilelevel */
9499 if (cin_isdo(look))
9500 whilelevel--;
9501
9502 /*
9503 * if we've used up all the elses, then
9504 * this must be the if that we want!
9505 * match the indent level of that if.
9506 */
9507 if (elselevel <= 0 && whilelevel <= 0)
9508 {
9509 return OK;
9510 }
9511 }
9512 }
9513 return FAIL;
9514}
9515
9516# if defined(FEAT_EVAL) || defined(PROTO)
9517/*
9518 * Get indent level from 'indentexpr'.
9519 */
9520 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009521get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522{
Bram Moolenaar97db5542017-04-21 23:18:26 +02009523 int indent = -1;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009524 char_u *inde_copy;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009525 pos_T save_pos;
9526 colnr_T save_curswant;
9527 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009529 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9530 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009532 /* Save and restore cursor position and curswant, in case it was changed
9533 * via :normal commands */
9534 save_pos = curwin->w_cursor;
9535 save_curswant = curwin->w_curswant;
9536 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009538 if (use_sandbox)
9539 ++sandbox;
9540 ++textlock;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009541
9542 /* Need to make a copy, the 'indentexpr' option could be changed while
9543 * evaluating it. */
9544 inde_copy = vim_strsave(curbuf->b_p_inde);
9545 if (inde_copy != NULL)
9546 {
9547 indent = (int)eval_to_number(inde_copy);
9548 vim_free(inde_copy);
9549 }
9550
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009551 if (use_sandbox)
9552 --sandbox;
9553 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554
9555 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9556 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9557 * command. */
9558 save_State = State;
9559 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009560 curwin->w_cursor = save_pos;
9561 curwin->w_curswant = save_curswant;
9562 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 check_cursor();
9564 State = save_State;
9565
9566 /* If there is an error, just keep the current indent. */
9567 if (indent < 0)
9568 indent = get_indent();
9569
9570 return indent;
9571}
9572# endif
9573
9574#endif /* FEAT_CINDENT */
9575
9576#if defined(FEAT_LISP) || defined(PROTO)
9577
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009579lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580{
9581 char_u buf[LSIZE];
9582 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009583 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584
9585 while (*word != NUL)
9586 {
9587 (void)copy_option_part(&word, buf, LSIZE, ",");
9588 len = (int)STRLEN(buf);
9589 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9590 return TRUE;
9591 }
9592 return FALSE;
9593}
9594
9595/*
9596 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9597 * The incompatible newer method is quite a bit better at indenting
9598 * code in lisp-like languages than the traditional one; it's still
9599 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9600 *
9601 * TODO:
9602 * Findmatch() should be adapted for lisp, also to make showmatch
9603 * work correctly: now (v5.3) it seems all C/C++ oriented:
9604 * - it does not recognize the #\( and #\) notations as character literals
9605 * - it doesn't know about comments starting with a semicolon
9606 * - it incorrectly interprets '(' as a character literal
9607 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009608 * Update from Sergey Khorev:
9609 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 */
9611 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009612get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009614 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 int amount;
9616 char_u *that;
9617 colnr_T col;
9618 colnr_T firsttry;
9619 int parencount, quotecount;
9620 int vi_lisp;
9621
9622 /* Set vi_lisp to use the vi-compatible method */
9623 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9624
9625 realpos = curwin->w_cursor;
9626 curwin->w_cursor.col = 0;
9627
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009628 if ((pos = findmatch(NULL, '(')) == NULL)
9629 pos = findmatch(NULL, '[');
9630 else
9631 {
9632 paren = *pos;
9633 pos = findmatch(NULL, '[');
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009634 if (pos == NULL || LT_POSP(pos, &paren))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009635 pos = &paren;
9636 }
9637 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638 {
9639 /* Extra trick: Take the indent of the first previous non-white
9640 * line that is at the same () level. */
9641 amount = -1;
9642 parencount = 0;
9643
9644 while (--curwin->w_cursor.lnum >= pos->lnum)
9645 {
9646 if (linewhite(curwin->w_cursor.lnum))
9647 continue;
9648 for (that = ml_get_curline(); *that != NUL; ++that)
9649 {
9650 if (*that == ';')
9651 {
9652 while (*(that + 1) != NUL)
9653 ++that;
9654 continue;
9655 }
9656 if (*that == '\\')
9657 {
9658 if (*(that + 1) != NUL)
9659 ++that;
9660 continue;
9661 }
9662 if (*that == '"' && *(that + 1) != NUL)
9663 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009664 while (*++that && *that != '"')
9665 {
9666 /* skipping escaped characters in the string */
9667 if (*that == '\\')
9668 {
9669 if (*++that == NUL)
9670 break;
9671 if (that[1] == NUL)
9672 {
9673 ++that;
9674 break;
9675 }
9676 }
9677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009679 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009681 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 --parencount;
9683 }
9684 if (parencount == 0)
9685 {
9686 amount = get_indent();
9687 break;
9688 }
9689 }
9690
9691 if (amount == -1)
9692 {
9693 curwin->w_cursor.lnum = pos->lnum;
9694 curwin->w_cursor.col = pos->col;
9695 col = pos->col;
9696
9697 that = ml_get_curline();
9698
9699 if (vi_lisp && get_indent() == 0)
9700 amount = 2;
9701 else
9702 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009703 char_u *line = that;
9704
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 amount = 0;
9706 while (*that && col)
9707 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009708 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 col--;
9710 }
9711
9712 /*
9713 * Some keywords require "body" indenting rules (the
9714 * non-standard-lisp ones are Scheme special forms):
9715 *
9716 * (let ((a 1)) instead (let ((a 1))
9717 * (...)) of (...))
9718 */
9719
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009720 if (!vi_lisp && (*that == '(' || *that == '[')
9721 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 amount += 2;
9723 else
9724 {
9725 that++;
9726 amount++;
9727 firsttry = amount;
9728
Bram Moolenaar1c465442017-03-12 20:10:05 +01009729 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009731 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 ++that;
9733 }
9734
9735 if (*that && *that != ';') /* not a comment line */
9736 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009737 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009739 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 firsttry++;
9741
9742 parencount = 0;
9743 quotecount = 0;
9744
9745 if (vi_lisp
9746 || (*that != '"'
9747 && *that != '\''
9748 && *that != '#'
9749 && (*that < '0' || *that > '9')))
9750 {
9751 while (*that
Bram Moolenaar1c465442017-03-12 20:10:05 +01009752 && (!VIM_ISWHITE(*that)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 || quotecount
9754 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009755 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 && !quotecount
9757 && !parencount
9758 && vi_lisp)))
9759 {
9760 if (*that == '"')
9761 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009762 if ((*that == '(' || *that == '[')
9763 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009765 if ((*that == ')' || *that == ']')
9766 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 --parencount;
9768 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009769 amount += lbr_chartabsize_adv(
9770 line, &that, (colnr_T)amount);
9771 amount += lbr_chartabsize_adv(
9772 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 }
9774 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01009775 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009777 amount += lbr_chartabsize(
9778 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 that++;
9780 }
9781 if (!*that || *that == ';')
9782 amount = firsttry;
9783 }
9784 }
9785 }
9786 }
9787 }
9788 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009789 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790
9791 curwin->w_cursor = realpos;
9792
9793 return amount;
9794}
9795#endif /* FEAT_LISP */
9796
9797 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009798prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009800#if defined(SIGHUP) && defined(SIG_IGN)
9801 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9802 * makes Vim exit and then handling SIGHUP causes various reentrance
9803 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009804 signal(SIGHUP, SIG_IGN);
9805#endif
9806
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807#ifdef FEAT_GUI
9808 if (gui.in_use)
9809 {
9810 gui.dying = TRUE;
9811 out_trash(); /* trash any pending output */
9812 }
9813 else
9814#endif
9815 {
9816 windgoto((int)Rows - 1, 0);
9817
9818 /*
9819 * Switch terminal mode back now, so messages end up on the "normal"
9820 * screen (if there are two screens).
9821 */
9822 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009823 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824 out_flush();
9825 }
9826}
9827
9828/*
9829 * Preserve files and exit.
9830 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009831 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9832 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 */
9834 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009835preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836{
9837 buf_T *buf;
9838
9839 prepare_to_exit();
9840
Bram Moolenaar4770d092006-01-12 23:22:24 +00009841 /* Setting this will prevent free() calls. That avoids calling free()
9842 * recursively when free() was invoked with a bad pointer. */
9843 really_exiting = TRUE;
9844
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 out_str(IObuff);
9846 screen_start(); /* don't know where cursor is now */
9847 out_flush();
9848
9849 ml_close_notmod(); /* close all not-modified buffers */
9850
Bram Moolenaar29323592016-07-24 22:04:11 +02009851 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 {
9853 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9854 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009855 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 screen_start(); /* don't know where cursor is now */
9857 out_flush();
9858 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9859 break;
9860 }
9861 }
9862
9863 ml_close_all(FALSE); /* close all memfiles, without deleting */
9864
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009865 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866
9867 getout(1);
9868}
9869
9870/*
9871 * return TRUE if "fname" exists.
9872 */
9873 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009874vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009876 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877
9878 if (mch_stat((char *)fname, &st))
9879 return FALSE;
9880 return TRUE;
9881}
9882
9883/*
9884 * Check for CTRL-C pressed, but only once in a while.
9885 * Should be used instead of ui_breakcheck() for functions that check for
9886 * each line in the file. Calling ui_breakcheck() each time takes too much
9887 * time, because it can be a system call.
9888 */
9889
9890#ifndef BREAKCHECK_SKIP
9891# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9892# define BREAKCHECK_SKIP 200
9893# else
9894# define BREAKCHECK_SKIP 32
9895# endif
9896#endif
9897
9898static int breakcheck_count = 0;
9899
9900 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009901line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902{
9903 if (++breakcheck_count >= BREAKCHECK_SKIP)
9904 {
9905 breakcheck_count = 0;
9906 ui_breakcheck();
9907 }
9908}
9909
9910/*
9911 * Like line_breakcheck() but check 10 times less often.
9912 */
9913 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009914fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915{
9916 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9917 {
9918 breakcheck_count = 0;
9919 ui_breakcheck();
9920 }
9921}
9922
9923/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009924 * Invoke expand_wildcards() for one pattern.
9925 * Expand items like "%:h" before the expansion.
9926 * Returns OK or FAIL.
9927 */
9928 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009929expand_wildcards_eval(
9930 char_u **pat, /* pointer to input pattern */
9931 int *num_file, /* resulting number of files */
9932 char_u ***file, /* array of resulting files */
9933 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009934{
9935 int ret = FAIL;
9936 char_u *eval_pat = NULL;
9937 char_u *exp_pat = *pat;
9938 char_u *ignored_msg;
9939 int usedlen;
9940
9941 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9942 {
9943 ++emsg_off;
9944 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9945 NULL, &ignored_msg, NULL);
9946 --emsg_off;
9947 if (eval_pat != NULL)
9948 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9949 }
9950
9951 if (exp_pat != NULL)
9952 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9953
9954 if (eval_pat != NULL)
9955 {
9956 vim_free(exp_pat);
9957 vim_free(eval_pat);
9958 }
9959
9960 return ret;
9961}
9962
9963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9965 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009966 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 */
9968 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009969expand_wildcards(
9970 int num_pat, /* number of input patterns */
9971 char_u **pat, /* array of input patterns */
9972 int *num_files, /* resulting number of files */
9973 char_u ***files, /* array of resulting files */
9974 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975{
9976 int retval;
9977 int i, j;
9978 char_u *p;
9979 int non_suf_match; /* number without matching suffix */
9980
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009981 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982
9983 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009984 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985 return retval;
9986
9987#ifdef FEAT_WILDIGN
9988 /*
9989 * Remove names that match 'wildignore'.
9990 */
9991 if (*p_wig)
9992 {
9993 char_u *ffname;
9994
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009995 /* check all files in (*files)[] */
9996 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009998 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 if (ffname == NULL) /* out of memory */
10000 break;
10001# ifdef VMS
10002 vms_remove_version(ffname);
10003# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010004 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010006 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010007 vim_free((*files)[i]);
10008 for (j = i; j + 1 < *num_files; ++j)
10009 (*files)[j] = (*files)[j + 1];
10010 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011 --i;
10012 }
10013 vim_free(ffname);
10014 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010015
10016 /* If the number of matches is now zero, we fail. */
10017 if (*num_files == 0)
10018 {
Bram Moolenaard23a8232018-02-10 18:45:26 +010010019 VIM_CLEAR(*files);
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010020 return FAIL;
10021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 }
10023#endif
10024
10025 /*
10026 * Move the names where 'suffixes' match to the end.
10027 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010028 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 {
10030 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010031 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010033 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034 {
10035 /*
10036 * Move the name without matching suffix to the front
10037 * of the list.
10038 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010039 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010041 (*files)[j] = (*files)[j - 1];
10042 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 }
10044 }
10045 }
10046
10047 return retval;
10048}
10049
10050/*
10051 * Return TRUE if "fname" matches with an entry in 'suffixes'.
10052 */
10053 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010054match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055{
10056 int fnamelen, setsuflen;
10057 char_u *setsuf;
10058#define MAXSUFLEN 30 /* maximum length of a file suffix */
10059 char_u suf_buf[MAXSUFLEN];
10060
10061 fnamelen = (int)STRLEN(fname);
10062 setsuflen = 0;
10063 for (setsuf = p_su; *setsuf; )
10064 {
10065 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +000010066 if (setsuflen == 0)
10067 {
10068 char_u *tail = gettail(fname);
10069
10070 /* empty entry: match name without a '.' */
10071 if (vim_strchr(tail, '.') == NULL)
10072 {
10073 setsuflen = 1;
10074 break;
10075 }
10076 }
10077 else
10078 {
10079 if (fnamelen >= setsuflen
10080 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
10081 (size_t)setsuflen) == 0)
10082 break;
10083 setsuflen = 0;
10084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 }
10086 return (setsuflen != 0);
10087}
10088
10089#if !defined(NO_EXPANDPATH) || defined(PROTO)
10090
10091# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010092static int vim_backtick(char_u *p);
10093static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094# endif
10095
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010096# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097/*
10098 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
10099 * it's shared between these systems.
10100 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010101# if defined(PROTO)
10102# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103# else
10104# ifdef __BORLANDC__
10105# define _cdecl _RTLENTRYF
10106# endif
10107# endif
10108
10109/*
10110 * comparison function for qsort in dos_expandpath()
10111 */
10112 static int _cdecl
10113pstrcmp(const void *a, const void *b)
10114{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010115 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116}
10117
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118/*
Bram Moolenaar231334e2005-07-25 20:46:57 +000010119 * Recursively expand one path component into all matching files and/or
10120 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 * Return the number of matches found.
10122 * "path" has backslashes before chars that are not to be expanded, starting
10123 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +000010124 * Return the number of matches found.
10125 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 */
10127 static int
10128dos_expandpath(
10129 garray_T *gap,
10130 char_u *path,
10131 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +000010132 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +000010133 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010135 char_u *buf;
10136 char_u *path_end;
10137 char_u *p, *s, *e;
10138 int start_len = gap->ga_len;
10139 char_u *pat;
10140 regmatch_T regmatch;
10141 int starts_with_dot;
10142 int matches;
10143 int len;
10144 int starstar = FALSE;
10145 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 WIN32_FIND_DATA fb;
10147 HANDLE hFind = (HANDLE)0;
10148# ifdef FEAT_MBYTE
10149 WIN32_FIND_DATAW wfb;
10150 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
10151# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010153 int ok;
10154
10155 /* Expanding "**" may take a long time, check for CTRL-C. */
10156 if (stardepth > 0)
10157 {
10158 ui_breakcheck();
10159 if (got_int)
10160 return 0;
10161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162
Bram Moolenaar7314efd2015-10-31 15:32:52 +010010163 /* Make room for file name. When doing encoding conversion the actual
10164 * length may be quite a bit longer, thus use the maximum possible length. */
10165 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166 if (buf == NULL)
10167 return 0;
10168
10169 /*
10170 * Find the first part in the path name that contains a wildcard or a ~1.
10171 * Copy it into buf, including the preceding characters.
10172 */
10173 p = buf;
10174 s = buf;
10175 e = NULL;
10176 path_end = path;
10177 while (*path_end != NUL)
10178 {
10179 /* May ignore a wildcard that has a backslash before it; it will
10180 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10181 if (path_end >= path + wildoff && rem_backslash(path_end))
10182 *p++ = *path_end++;
10183 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
10184 {
10185 if (e != NULL)
10186 break;
10187 s = p + 1;
10188 }
10189 else if (path_end >= path + wildoff
10190 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
10191 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +010010192# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193 if (has_mbyte)
10194 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010195 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196 STRNCPY(p, path_end, len);
10197 p += len;
10198 path_end += len;
10199 }
10200 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +010010201# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 *p++ = *path_end++;
10203 }
10204 e = p;
10205 *e = NUL;
10206
10207 /* now we have one wildcard component between s and e */
10208 /* Remove backslashes between "wildoff" and the start of the wildcard
10209 * component. */
10210 for (p = buf + wildoff; p < s; ++p)
10211 if (rem_backslash(p))
10212 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010213 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 --e;
10215 --s;
10216 }
10217
Bram Moolenaar231334e2005-07-25 20:46:57 +000010218 /* Check for "**" between "s" and "e". */
10219 for (p = s; p < e; ++p)
10220 if (p[0] == '*' && p[1] == '*')
10221 starstar = TRUE;
10222
Bram Moolenaard82103e2016-01-17 17:04:05 +010010223 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10225 if (pat == NULL)
10226 {
10227 vim_free(buf);
10228 return 0;
10229 }
10230
10231 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010232 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010233 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234 regmatch.rm_ic = TRUE; /* Always ignore case */
10235 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010236 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010237 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238 vim_free(pat);
10239
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010240 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 {
10242 vim_free(buf);
10243 return 0;
10244 }
10245
10246 /* remember the pattern or file name being looked for */
10247 matchname = vim_strsave(s);
10248
Bram Moolenaar231334e2005-07-25 20:46:57 +000010249 /* If "**" is by itself, this is the first time we encounter it and more
10250 * is following then find matches without any directory. */
10251 if (!didstar && stardepth < 100 && starstar && e - s == 2
10252 && *path_end == '/')
10253 {
10254 STRCPY(s, path_end + 1);
10255 ++stardepth;
10256 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10257 --stardepth;
10258 }
10259
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 /* Scan all files in the directory with "dir/ *.*" */
10261 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262# ifdef FEAT_MBYTE
10263 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
10264 {
10265 /* The active codepage differs from 'encoding'. Attempt using the
10266 * wide function. If it fails because it is not implemented fall back
10267 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010268 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269 if (wn != NULL)
10270 {
10271 hFind = FindFirstFileW(wn, &wfb);
10272 if (hFind == INVALID_HANDLE_VALUE
10273 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
Bram Moolenaard23a8232018-02-10 18:45:26 +010010274 VIM_CLEAR(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275 }
10276 }
10277
10278 if (wn == NULL)
10279# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010280 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282
10283 while (ok)
10284 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285# ifdef FEAT_MBYTE
10286 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010287 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 else
10289# endif
10290 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291 /* Ignore entries starting with a dot, unless when asked for. Accept
10292 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010293 if ((p[0] != '.' || starts_with_dot
10294 || ((flags & EW_DODOT)
10295 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010297 || (regmatch.regprog != NULL
10298 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010299 || ((flags & EW_NOTWILD)
10300 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010304
10305 if (starstar && stardepth < 100)
10306 {
10307 /* For "**" in the pattern first go deeper in the tree to
10308 * find matches. */
10309 STRCPY(buf + len, "/**");
10310 STRCPY(buf + len + 3, path_end);
10311 ++stardepth;
10312 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
10313 --stardepth;
10314 }
10315
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 STRCPY(buf + len, path_end);
10317 if (mch_has_exp_wildcard(path_end))
10318 {
10319 /* need to expand another component of the path */
10320 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010321 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 }
10323 else
10324 {
10325 /* no more wildcards, check if there is a match */
10326 /* remove backslashes for the remaining components only */
10327 if (*path_end != 0)
10328 backslash_halve(buf + len + 1);
10329 if (mch_getperm(buf) >= 0) /* add existing file */
10330 addfile(gap, buf, flags);
10331 }
10332 }
10333
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334# ifdef FEAT_MBYTE
10335 if (wn != NULL)
10336 {
10337 vim_free(p);
10338 ok = FindNextFileW(hFind, &wfb);
10339 }
10340 else
10341# endif
10342 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343
10344 /* If no more matches and no match was used, try expanding the name
10345 * itself. Finds the long name of a short filename. */
10346 if (!ok && matchname != NULL && gap->ga_len == start_len)
10347 {
10348 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 FindClose(hFind);
10350# ifdef FEAT_MBYTE
10351 if (wn != NULL)
10352 {
10353 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010354 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355 if (wn != NULL)
10356 hFind = FindFirstFileW(wn, &wfb);
10357 }
10358 if (wn == NULL)
10359# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010360 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaard23a8232018-02-10 18:45:26 +010010362 VIM_CLEAR(matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 }
10364 }
10365
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366 FindClose(hFind);
10367# ifdef FEAT_MBYTE
10368 vim_free(wn);
10369# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010371 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 vim_free(matchname);
10373
10374 matches = gap->ga_len - start_len;
10375 if (matches > 0)
10376 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10377 sizeof(char_u *), pstrcmp);
10378 return matches;
10379}
10380
10381 int
10382mch_expandpath(
10383 garray_T *gap,
10384 char_u *path,
10385 int flags) /* EW_* flags */
10386{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010387 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010389# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390
Bram Moolenaar231334e2005-07-25 20:46:57 +000010391#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10392 || defined(PROTO)
10393/*
10394 * Unix style wildcard expansion code.
10395 * It's here because it's used both for Unix and Mac.
10396 */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010397 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010398pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010399{
10400 return (pathcmp(*(char **)a, *(char **)b, -1));
10401}
10402
10403/*
10404 * Recursively expand one path component into all matching files and/or
10405 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10406 * "path" has backslashes before chars that are not to be expanded, starting
10407 * at "path + wildoff".
10408 * Return the number of matches found.
10409 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10410 */
10411 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010412unix_expandpath(
10413 garray_T *gap,
10414 char_u *path,
10415 int wildoff,
10416 int flags, /* EW_* flags */
10417 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010418{
10419 char_u *buf;
10420 char_u *path_end;
10421 char_u *p, *s, *e;
10422 int start_len = gap->ga_len;
10423 char_u *pat;
10424 regmatch_T regmatch;
10425 int starts_with_dot;
10426 int matches;
10427 int len;
10428 int starstar = FALSE;
10429 static int stardepth = 0; /* depth for "**" expansion */
10430
10431 DIR *dirp;
10432 struct dirent *dp;
10433
10434 /* Expanding "**" may take a long time, check for CTRL-C. */
10435 if (stardepth > 0)
10436 {
10437 ui_breakcheck();
10438 if (got_int)
10439 return 0;
10440 }
10441
10442 /* make room for file name */
10443 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10444 if (buf == NULL)
10445 return 0;
10446
10447 /*
10448 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010449 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010450 * Copy it into "buf", including the preceding characters.
10451 */
10452 p = buf;
10453 s = buf;
10454 e = NULL;
10455 path_end = path;
10456 while (*path_end != NUL)
10457 {
10458 /* May ignore a wildcard that has a backslash before it; it will
10459 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10460 if (path_end >= path + wildoff && rem_backslash(path_end))
10461 *p++ = *path_end++;
10462 else if (*path_end == '/')
10463 {
10464 if (e != NULL)
10465 break;
10466 s = p + 1;
10467 }
10468 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010469 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010470 || (!p_fic && (flags & EW_ICASE)
10471 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010472 e = p;
10473#ifdef FEAT_MBYTE
10474 if (has_mbyte)
10475 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010476 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010477 STRNCPY(p, path_end, len);
10478 p += len;
10479 path_end += len;
10480 }
10481 else
10482#endif
10483 *p++ = *path_end++;
10484 }
10485 e = p;
10486 *e = NUL;
10487
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010488 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010489 /* Remove backslashes between "wildoff" and the start of the wildcard
10490 * component. */
10491 for (p = buf + wildoff; p < s; ++p)
10492 if (rem_backslash(p))
10493 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010494 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010495 --e;
10496 --s;
10497 }
10498
10499 /* Check for "**" between "s" and "e". */
10500 for (p = s; p < e; ++p)
10501 if (p[0] == '*' && p[1] == '*')
10502 starstar = TRUE;
10503
10504 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010505 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010506 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10507 if (pat == NULL)
10508 {
10509 vim_free(buf);
10510 return 0;
10511 }
10512
10513 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010514 if (flags & EW_ICASE)
10515 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10516 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010517 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010518 if (flags & (EW_NOERROR | EW_NOTWILD))
10519 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010520 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010521 if (flags & (EW_NOERROR | EW_NOTWILD))
10522 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010523 vim_free(pat);
10524
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010525 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010526 {
10527 vim_free(buf);
10528 return 0;
10529 }
10530
10531 /* If "**" is by itself, this is the first time we encounter it and more
10532 * is following then find matches without any directory. */
10533 if (!didstar && stardepth < 100 && starstar && e - s == 2
10534 && *path_end == '/')
10535 {
10536 STRCPY(s, path_end + 1);
10537 ++stardepth;
10538 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10539 --stardepth;
10540 }
10541
10542 /* open the directory for scanning */
10543 *s = NUL;
10544 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10545
10546 /* Find all matching entries */
10547 if (dirp != NULL)
10548 {
10549 for (;;)
10550 {
10551 dp = readdir(dirp);
10552 if (dp == NULL)
10553 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010554 if ((dp->d_name[0] != '.' || starts_with_dot
10555 || ((flags & EW_DODOT)
10556 && dp->d_name[1] != NUL
10557 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010558 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10559 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010560 || ((flags & EW_NOTWILD)
10561 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010562 {
10563 STRCPY(s, dp->d_name);
10564 len = STRLEN(buf);
10565
10566 if (starstar && stardepth < 100)
10567 {
10568 /* For "**" in the pattern first go deeper in the tree to
10569 * find matches. */
10570 STRCPY(buf + len, "/**");
10571 STRCPY(buf + len + 3, path_end);
10572 ++stardepth;
10573 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10574 --stardepth;
10575 }
10576
10577 STRCPY(buf + len, path_end);
10578 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10579 {
10580 /* need to expand another component of the path */
10581 /* remove backslashes for the remaining components only */
10582 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10583 }
10584 else
10585 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010586 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010587
Bram Moolenaar231334e2005-07-25 20:46:57 +000010588 /* no more wildcards, check if there is a match */
10589 /* remove backslashes for the remaining components only */
10590 if (*path_end != NUL)
10591 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010592 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010593 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010594 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010595 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010596#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010597 size_t precomp_len = STRLEN(buf)+1;
10598 char_u *precomp_buf =
10599 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010600
Bram Moolenaar231334e2005-07-25 20:46:57 +000010601 if (precomp_buf)
10602 {
10603 mch_memmove(buf, precomp_buf, precomp_len);
10604 vim_free(precomp_buf);
10605 }
10606#endif
10607 addfile(gap, buf, flags);
10608 }
10609 }
10610 }
10611 }
10612
10613 closedir(dirp);
10614 }
10615
10616 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010617 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010618
10619 matches = gap->ga_len - start_len;
10620 if (matches > 0)
10621 qsort(((char_u **)gap->ga_data) + start_len, matches,
10622 sizeof(char_u *), pstrcmp);
10623 return matches;
10624}
10625#endif
10626
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010627#if defined(FEAT_SEARCHPATH)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010628/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010629 * Moves "*psep" back to the previous path separator in "path".
10630 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010631 */
10632 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010633find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010634{
10635 /* skip the current separator */
10636 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010637 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010638
10639 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010640 while (*psep > path)
10641 {
10642 if (vim_ispathsep(**psep))
10643 return OK;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010644 MB_PTR_BACK(path, *psep);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010645 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010646
10647 return FAIL;
10648}
10649
10650/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010651 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10652 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010653 */
10654 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010655is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010656{
10657 int j;
10658 int candidate_len;
10659 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010660 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010661 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010662
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010663 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010664 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010665 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010666 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010667
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010668 candidate_len = (int)STRLEN(maybe_unique);
10669 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010670 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010671 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010672
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010673 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010674 if (fnamecmp(maybe_unique, rival) == 0
10675 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010676 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010677 }
10678
Bram Moolenaar162bd912010-07-28 22:29:10 +020010679 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010680}
10681
10682/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010683 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010684 * paths are expanded to their equivalent fullpath. This includes the "."
10685 * (relative to current buffer directory) and empty path (relative to current
10686 * directory) notations.
10687 *
10688 * TODO: handle upward search (;) and path limiter (**N) notations by
10689 * expanding each into their equivalent path(s).
10690 */
10691 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010692expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010693{
10694 char_u *path_option = *curbuf->b_p_path == NUL
10695 ? p_path : curbuf->b_p_path;
10696 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010697 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010698 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010699
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010700 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010701 return;
10702
10703 while (*path_option != NUL)
10704 {
10705 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10706
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010707 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010708 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010709 /* Relative to current buffer:
10710 * "/path/file" + "." -> "/path/"
10711 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010712 if (curbuf->b_ffname == NULL)
10713 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010714 p = gettail(curbuf->b_ffname);
10715 len = (int)(p - curbuf->b_ffname);
10716 if (len + (int)STRLEN(buf) >= MAXPATHL)
10717 continue;
10718 if (buf[1] == NUL)
10719 buf[len] = NUL;
10720 else
10721 STRMOVE(buf + len, buf + 2);
10722 mch_memmove(buf, curbuf->b_ffname, len);
10723 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010724 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010725 else if (buf[0] == NUL)
10726 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010727 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010728 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010729 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010730 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010731 else if (!mch_isFullName(buf))
10732 {
10733 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010734 len = (int)STRLEN(curdir);
10735 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010736 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010737 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010738 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010739 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010740 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010741 }
10742
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010743 if (ga_grow(gap, 1) == FAIL)
10744 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010745
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010746# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010747 /* Avoid the path ending in a backslash, it fails when a comma is
10748 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010749 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010750 if (buf[len - 1] == '\\')
10751 buf[len - 1] = '/';
10752# endif
10753
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010754 p = vim_strsave(buf);
10755 if (p == NULL)
10756 break;
10757 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010758 }
10759
10760 vim_free(buf);
10761}
10762
10763/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010764 * Returns a pointer to the file or directory name in "fname" that matches the
10765 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010766 *
10767 * path: /foo/bar/baz
10768 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010769 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010770 */
10771 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010772get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010773{
10774 int i;
10775 int maxlen = 0;
10776 char_u **path_part = (char_u **)gap->ga_data;
10777 char_u *cutoff = NULL;
10778
10779 for (i = 0; i < gap->ga_len; i++)
10780 {
10781 int j = 0;
10782
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010783 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010784# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010785 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10786#endif
10787 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010788 j++;
10789 if (j > maxlen)
10790 {
10791 maxlen = j;
10792 cutoff = &fname[j];
10793 }
10794 }
10795
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010796 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010797 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010798 while (vim_ispathsep(*cutoff))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010799 MB_PTR_ADV(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010800
10801 return cutoff;
10802}
10803
10804/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010805 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10806 * that they are unique with respect to each other while conserving the part
10807 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010808 */
10809 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010810uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010811{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010812 int i;
10813 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010814 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010815 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010816 char_u *pat;
10817 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010818 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010819 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010820 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010821 char_u **in_curdir = NULL;
10822 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010823
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010824 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010825 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010826
10827 /*
10828 * We need to prepend a '*' at the beginning of file_pattern so that the
10829 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010830 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010831 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010832 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010833 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010834 if (file_pattern == NULL)
10835 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010836 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010837 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010838 STRCAT(file_pattern, pattern);
10839 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10840 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010841 if (pat == NULL)
10842 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010843
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010844 regmatch.rm_ic = TRUE; /* always ignore case */
10845 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10846 vim_free(pat);
10847 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010848 return;
10849
Bram Moolenaar162bd912010-07-28 22:29:10 +020010850 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010851 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010852 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010853 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010854
10855 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010856 if (in_curdir == NULL)
10857 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010858
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010859 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010860 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010861 char_u *path = fnames[i];
10862 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010863 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010864 char_u *pathsep_p;
10865 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010866
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010867 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010868 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010869 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010870 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010871 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010872
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010873 /* Shorten the filename while maintaining its uniqueness */
10874 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010875
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010876 /* Don't assume all files can be reached without path when search
10877 * pattern starts with star star slash, so only remove path_cutoff
10878 * when possible. */
10879 if (pattern[0] == '*' && pattern[1] == '*'
10880 && vim_ispathsep_nocolon(pattern[2])
10881 && path_cutoff != NULL
10882 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10883 && is_unique(path_cutoff, gap, i))
10884 {
10885 sort_again = TRUE;
10886 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10887 }
10888 else
10889 {
10890 /* Here all files can be reached without path, so get shortest
10891 * unique path. We start at the end of the path. */
10892 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010893
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010894 while (find_previous_pathsep(path, &pathsep_p))
10895 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10896 && is_unique(pathsep_p + 1, gap, i)
10897 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10898 {
10899 sort_again = TRUE;
10900 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10901 break;
10902 }
10903 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010904
10905 if (mch_isFullName(path))
10906 {
10907 /*
10908 * Last resort: shorten relative to curdir if possible.
10909 * 'possible' means:
10910 * 1. It is under the current directory.
10911 * 2. The result is actually shorter than the original.
10912 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010913 * Before curdir After
10914 * /foo/bar/file.txt /foo/bar ./file.txt
10915 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10916 * /file.txt / /file.txt
10917 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010918 */
10919 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010920 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010921#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010922 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010923 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010924 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010925 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010926 && !vim_ispathsep(*short_name)
10927#endif
10928 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010929 {
10930 STRCPY(path, ".");
10931 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010932 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010933 }
10934 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010935 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010936 }
10937
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010938 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010939 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010940 {
10941 char_u *rel_path;
10942 char_u *path = in_curdir[i];
10943
10944 if (path == NULL)
10945 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010946
10947 /* If the {filename} is not unique, change it to ./{filename}.
10948 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010949 short_name = shorten_fname(path, curdir);
10950 if (short_name == NULL)
10951 short_name = path;
10952 if (is_unique(short_name, gap, i))
10953 {
10954 STRCPY(fnames[i], short_name);
10955 continue;
10956 }
10957
10958 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10959 if (rel_path == NULL)
10960 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010961 STRCPY(rel_path, ".");
10962 add_pathsep(rel_path);
10963 STRCAT(rel_path, short_name);
10964
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010965 vim_free(fnames[i]);
10966 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010967 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010968 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010969 }
10970
Bram Moolenaar162bd912010-07-28 22:29:10 +020010971theend:
10972 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010973 if (in_curdir != NULL)
10974 {
10975 for (i = 0; i < gap->ga_len; i++)
10976 vim_free(in_curdir[i]);
10977 vim_free(in_curdir);
10978 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010979 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010980 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010981
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010982 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010983 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010984}
10985
10986/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010987 * Calls globpath() with 'path' values for the given pattern and stores the
10988 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010989 * Returns the total number of matches.
10990 */
10991 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010992expand_in_path(
10993 garray_T *gap,
10994 char_u *pattern,
10995 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010996{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010997 char_u *curdir;
10998 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010999 char_u *paths = NULL;
Bram Moolenaar8a37b032018-02-03 20:43:08 +010011000 int glob_flags = 0;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011001
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020011002 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020011003 return 0;
11004 mch_dirname(curdir, MAXPATHL);
11005
Bram Moolenaar0be992e2010-08-12 21:50:51 +020011006 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020011007 expand_path_option(curdir, &path_ga);
11008 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020011009 if (path_ga.ga_len == 0)
11010 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011011
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020011012 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011013 ga_clear_strings(&path_ga);
11014 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020011015 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020011016
Bram Moolenaar8a37b032018-02-03 20:43:08 +010011017 if (flags & EW_ICASE)
11018 glob_flags |= WILD_ICASE;
11019 if (flags & EW_ADDSLASH)
11020 glob_flags |= WILD_ADD_SLASH;
11021 globpath(paths, pattern, gap, glob_flags);
Bram Moolenaar162bd912010-07-28 22:29:10 +020011022 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011023
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020011024 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011025}
11026#endif
11027
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011028#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
11029/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020011030 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
11031 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011032 */
11033 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011034remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011035{
11036 int i;
11037 int j;
11038 char_u **fnames = (char_u **)gap->ga_data;
11039
Bram Moolenaardc685ab2010-08-13 21:16:49 +020011040 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011041 for (i = gap->ga_len - 1; i > 0; --i)
11042 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
11043 {
11044 vim_free(fnames[i]);
11045 for (j = i + 1; j < gap->ga_len; ++j)
11046 fnames[j - 1] = fnames[j];
11047 --gap->ga_len;
11048 }
11049}
11050#endif
11051
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011052/*
11053 * Return TRUE if "p" contains what looks like an environment variable.
11054 * Allowing for escaping.
11055 */
11056 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011057has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011058{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011059 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011060 {
11061 if (*p == '\\' && p[1] != NUL)
11062 ++p;
11063 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010011064#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011065 "$%"
11066#else
11067 "$"
11068#endif
11069 , *p) != NULL)
11070 return TRUE;
11071 }
11072 return FALSE;
11073}
11074
11075#ifdef SPECIAL_WILDCHAR
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011076/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +010011077 * Return TRUE if "p" contains a special wildcard character, one that Vim
11078 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011079 */
11080 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011081has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011082{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011083 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011084 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010011085 /* Allow for escaping. */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011086 if (*p == '\\' && p[1] != NUL)
11087 ++p;
11088 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
11089 return TRUE;
11090 }
11091 return FALSE;
11092}
11093#endif
11094
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095/*
11096 * Generic wildcard expansion code.
11097 *
11098 * Characters in "pat" that should not be expanded must be preceded with a
11099 * backslash. E.g., "/path\ with\ spaces/my\*star*"
11100 *
11101 * Return FAIL when no single file was found. In this case "num_file" is not
11102 * set, and "file" may contain an error message.
11103 * Return OK when some files found. "num_file" is set to the number of
11104 * matches, "file" to the array of matches. Call FreeWild() later.
11105 */
11106 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011107gen_expand_wildcards(
11108 int num_pat, /* number of input patterns */
11109 char_u **pat, /* array of input patterns */
11110 int *num_file, /* resulting number of files */
11111 char_u ***file, /* array of resulting files */
11112 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113{
11114 int i;
11115 garray_T ga;
11116 char_u *p;
11117 static int recursive = FALSE;
11118 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020011119 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011120#if defined(FEAT_SEARCHPATH)
11121 int did_expand_in_path = FALSE;
11122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123
11124 /*
11125 * expand_env() is called to expand things like "~user". If this fails,
11126 * it calls ExpandOne(), which brings us back here. In this case, always
11127 * call the machine specific expansion function, if possible. Otherwise,
11128 * return FAIL.
11129 */
11130 if (recursive)
11131#ifdef SPECIAL_WILDCHAR
11132 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11133#else
11134 return FAIL;
11135#endif
11136
11137#ifdef SPECIAL_WILDCHAR
11138 /*
11139 * If there are any special wildcard characters which we cannot handle
11140 * here, call machine specific function for all the expansion. This
11141 * avoids starting the shell for each argument separately.
11142 * For `=expr` do use the internal function.
11143 */
11144 for (i = 0; i < num_pat; i++)
11145 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011146 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147# ifdef VIM_BACKTICK
11148 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
11149# endif
11150 )
11151 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11152 }
11153#endif
11154
11155 recursive = TRUE;
11156
11157 /*
11158 * The matching file names are stored in a growarray. Init it empty.
11159 */
11160 ga_init2(&ga, (int)sizeof(char_u *), 30);
11161
11162 for (i = 0; i < num_pat; ++i)
11163 {
11164 add_pat = -1;
11165 p = pat[i];
11166
11167#ifdef VIM_BACKTICK
11168 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020011169 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011170 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020011171 if (add_pat == -1)
11172 retval = FAIL;
11173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174 else
11175#endif
11176 {
11177 /*
11178 * First expand environment variables, "~/" and "~user/".
11179 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011180 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011181 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000011182 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011183 if (p == NULL)
11184 p = pat[i];
11185#ifdef UNIX
11186 /*
11187 * On Unix, if expand_env() can't expand an environment
11188 * variable, use the shell to do that. Discard previously
11189 * found file names and start all over again.
11190 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011191 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192 {
11193 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000011194 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011195 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020011196 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197 recursive = FALSE;
11198 return i;
11199 }
11200#endif
11201 }
11202
11203 /*
11204 * If there are wildcards: Expand file names and add each match to
11205 * the list. If there is no match, and EW_NOTFOUND is given, add
11206 * the pattern.
11207 * If there are no wildcards: Add the file name if it exists or
11208 * when EW_NOTFOUND is given.
11209 */
11210 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011211 {
11212#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011213 if ((flags & EW_PATH)
11214 && !mch_isFullName(p)
11215 && !(p[0] == '.'
11216 && (vim_ispathsep(p[1])
11217 || (p[1] == '.' && vim_ispathsep(p[2]))))
11218 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011219 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011220 /* :find completion where 'path' is used.
11221 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011222 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011223 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011224 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011225 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011226 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011227 else
11228#endif
11229 add_pat = mch_expandpath(&ga, p, flags);
11230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231 }
11232
11233 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
11234 {
11235 char_u *t = backslash_halve_save(p);
11236
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237 /* When EW_NOTFOUND is used, always add files and dirs. Makes
11238 * "vim c:/" work. */
11239 if (flags & EW_NOTFOUND)
11240 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020011241 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242 addfile(&ga, t, flags);
11243 vim_free(t);
11244 }
11245
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011246#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011247 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011248 uniquefy_paths(&ga, p);
11249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250 if (p != pat[i])
11251 vim_free(p);
11252 }
11253
11254 *num_file = ga.ga_len;
11255 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
11256
11257 recursive = FALSE;
11258
Bram Moolenaar336bd622016-01-17 18:23:58 +010011259 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260}
11261
11262# ifdef VIM_BACKTICK
11263
11264/*
11265 * Return TRUE if we can expand this backtick thing here.
11266 */
11267 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011268vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269{
11270 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
11271}
11272
11273/*
11274 * Expand an item in `backticks` by executing it as a command.
11275 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020011276 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277 */
11278 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011279expand_backtick(
11280 garray_T *gap,
11281 char_u *pat,
11282 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283{
11284 char_u *p;
11285 char_u *cmd;
11286 char_u *buffer;
11287 int cnt = 0;
11288 int i;
11289
11290 /* Create the command: lop off the backticks. */
11291 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
11292 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011293 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294
11295#ifdef FEAT_EVAL
11296 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011297 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298 else
11299#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011300 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011301 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302 vim_free(cmd);
11303 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011304 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305
11306 cmd = buffer;
11307 while (*cmd != NUL)
11308 {
11309 cmd = skipwhite(cmd); /* skip over white space */
11310 p = cmd;
11311 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
11312 ++p;
11313 /* add an entry if it is not empty */
11314 if (p > cmd)
11315 {
11316 i = *p;
11317 *p = NUL;
11318 addfile(gap, cmd, flags);
11319 *p = i;
11320 ++cnt;
11321 }
11322 cmd = p;
11323 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
11324 ++cmd;
11325 }
11326
11327 vim_free(buffer);
11328 return cnt;
11329}
11330# endif /* VIM_BACKTICK */
11331
11332/*
11333 * Add a file to a file list. Accepted flags:
11334 * EW_DIR add directories
11335 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011336 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337 * EW_NOTFOUND add even when it doesn't exist
11338 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011339 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340 */
11341 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011342addfile(
11343 garray_T *gap,
11344 char_u *f, /* filename */
11345 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346{
11347 char_u *p;
11348 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011349 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011351 /* if the file/dir/link doesn't exist, may not add it */
11352 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011353 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354 return;
11355
11356#ifdef FNAME_ILLEGAL
11357 /* if the file/dir contains illegal characters, don't add it */
11358 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11359 return;
11360#endif
11361
11362 isdir = mch_isdir(f);
11363 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11364 return;
11365
Bram Moolenaarb5971142015-03-21 17:32:19 +010011366 /* If the file isn't executable, may not add it. Do accept directories.
11367 * When invoked from expand_shellcmd() do not use $PATH. */
11368 if (!isdir && (flags & EW_EXEC)
11369 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011370 return;
11371
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372 /* Make room for another item in the file list. */
11373 if (ga_grow(gap, 1) == FAIL)
11374 return;
11375
11376 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11377 if (p == NULL)
11378 return;
11379
11380 STRCPY(p, f);
11381#ifdef BACKSLASH_IN_FILENAME
11382 slash_adjust(p);
11383#endif
11384 /*
11385 * Append a slash or backslash after directory names if none is present.
11386 */
11387#ifndef DONT_ADD_PATHSEP_TO_DIR
11388 if (isdir && (flags & EW_ADDSLASH))
11389 add_pathsep(p);
11390#endif
11391 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392}
11393#endif /* !NO_EXPANDPATH */
11394
11395#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11396
11397#ifndef SEEK_SET
11398# define SEEK_SET 0
11399#endif
11400#ifndef SEEK_END
11401# define SEEK_END 2
11402#endif
11403
11404/*
11405 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011406 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11407 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408 * Returns an allocated string, or NULL for error.
11409 */
11410 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011411get_cmd_output(
11412 char_u *cmd,
11413 char_u *infile, /* optional input file name */
11414 int flags, /* can be SHELL_SILENT */
11415 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011416{
11417 char_u *tempname;
11418 char_u *command;
11419 char_u *buffer = NULL;
11420 int len;
11421 int i = 0;
11422 FILE *fd;
11423
11424 if (check_restricted() || check_secure())
11425 return NULL;
11426
11427 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011428 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429 {
11430 EMSG(_(e_notmp));
11431 return NULL;
11432 }
11433
11434 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011435 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436 if (command == NULL)
11437 goto done;
11438
11439 /*
11440 * Call the shell to execute the command (errors are ignored).
11441 * Don't check timestamps here.
11442 */
11443 ++no_check_timestamps;
11444 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11445 --no_check_timestamps;
11446
11447 vim_free(command);
11448
11449 /*
11450 * read the names from the file into memory
11451 */
11452# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011453 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454 fd = mch_fopen((char *)tempname, "r");
11455# else
11456 fd = mch_fopen((char *)tempname, READBIN);
11457# endif
11458
11459 if (fd == NULL)
11460 {
11461 EMSG2(_(e_notopen), tempname);
11462 goto done;
11463 }
11464
11465 fseek(fd, 0L, SEEK_END);
11466 len = ftell(fd); /* get size of temp file */
11467 fseek(fd, 0L, SEEK_SET);
11468
11469 buffer = alloc(len + 1);
11470 if (buffer != NULL)
11471 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11472 fclose(fd);
11473 mch_remove(tempname);
11474 if (buffer == NULL)
11475 goto done;
11476#ifdef VMS
11477 len = i; /* VMS doesn't give us what we asked for... */
11478#endif
11479 if (i != len)
11480 {
11481 EMSG2(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +010011482 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011484 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011485 {
11486 /* Change NUL into SOH, otherwise the string is truncated. */
11487 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011488 if (buffer[i] == NUL)
11489 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011490
Bram Moolenaar162bd912010-07-28 22:29:10 +020011491 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011492 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011493 else
11494 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495
11496done:
11497 vim_free(tempname);
11498 return buffer;
11499}
11500#endif
11501
11502/*
11503 * Free the list of files returned by expand_wildcards() or other expansion
11504 * functions.
11505 */
11506 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011507FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011508{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011509 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011510 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011511 while (count--)
11512 vim_free(files[count]);
11513 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514}
11515
11516/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011517 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518 * Don't do this when still processing a command or a mapping.
11519 * Don't do this when inside a ":normal" command.
11520 */
11521 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011522goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011523{
11524 return (p_im && stuff_empty() && typebuf_typed());
11525}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011526
11527/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011528 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011529 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11530 * - Remove any argument. E.g., "csh -f" -> "csh".
11531 * But don't allow a space in the path, so that this works:
11532 * "/usr/bin/csh --rcfile ~/.cshrc"
11533 * But don't do that for Windows, it's common to have a space in the path.
11534 */
11535 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011536get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011537{
11538 char_u *p;
11539
11540#ifdef WIN3264
11541 p = gettail(p_sh);
11542 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11543#else
11544 p = skiptowhite(p_sh);
11545 if (*p == NUL)
11546 {
11547 /* No white space, use the tail. */
11548 p = vim_strsave(gettail(p_sh));
11549 }
11550 else
11551 {
11552 char_u *p1, *p2;
11553
11554 /* Find the last path separator before the space. */
11555 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011556 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011557 if (vim_ispathsep(*p2))
11558 p1 = p2 + 1;
11559 p = vim_strnsave(p1, (int)(p - p1));
11560 }
11561#endif
11562 return p;
11563}