blob: 34b4134a04c0b07ed7d2c2e083f9d1ae7f440129 [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,
1708 1L, (long)-less_cols);
1709 }
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 Moolenaar1c465442017-03-12 20:10:05 +01002021 while (VIM_ISWHITE(string[0]))
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 {
2036 continue;
2037 }
2038
2039 /*
2040 * We have found a match, stop searching.
2041 */
2042 found_one = TRUE;
2043
2044 if (flags)
2045 *flags = flags_save;
2046 com_flags = flags_save;
2047
2048 break;
2049 }
2050
2051 if (found_one)
2052 {
2053 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
2054 int len1, len2, off;
2055
2056 result = i;
2057 /*
2058 * If this comment nests, continue searching.
2059 */
2060 if (vim_strchr(part_buf, COM_NEST) != NULL)
2061 continue;
2062
2063 lower_check_bound = i;
2064
2065 /* Let's verify whether the comment leader found is a substring
2066 * of other comment leaders. If it is, let's adjust the
2067 * lower_check_bound so that we make sure that we have determined
2068 * the comment leader correctly.
2069 */
2070
Bram Moolenaar1c465442017-03-12 20:10:05 +01002071 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +02002072 ++com_leader;
2073 len1 = (int)STRLEN(com_leader);
2074
2075 for (list = curbuf->b_p_com; *list; )
2076 {
2077 char_u *flags_save = list;
2078
2079 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
2080 if (flags_save == com_flags)
2081 continue;
2082 string = vim_strchr(part_buf2, ':');
2083 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002084 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +02002085 ++string;
2086 len2 = (int)STRLEN(string);
2087 if (len2 == 0)
2088 continue;
2089
2090 /* Now we have to verify whether string ends with a substring
2091 * beginning the com_leader. */
2092 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
2093 {
2094 --off;
2095 if (!STRNCMP(string + off, com_leader, len2 - off))
2096 {
2097 if (i - off < lower_check_bound)
2098 lower_check_bound = i - off;
2099 }
2100 }
2101 }
2102 }
2103 }
2104 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105}
2106#endif
2107
2108/*
2109 * Return the number of window lines occupied by buffer line "lnum".
2110 */
2111 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002112plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113{
2114 return plines_win(curwin, lnum, TRUE);
2115}
2116
2117 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002118plines_win(
2119 win_T *wp,
2120 linenr_T lnum,
2121 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122{
2123#if defined(FEAT_DIFF) || defined(PROTO)
2124 /* Check for filler lines above this buffer line. When folded the result
2125 * is one line anyway. */
2126 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
2127}
2128
2129 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002130plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131{
2132 return plines_win_nofill(curwin, lnum, TRUE);
2133}
2134
2135 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002136plines_win_nofill(
2137 win_T *wp,
2138 linenr_T lnum,
2139 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140{
2141#endif
2142 int lines;
2143
2144 if (!wp->w_p_wrap)
2145 return 1;
2146
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 if (wp->w_width == 0)
2148 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149
2150#ifdef FEAT_FOLDING
2151 /* A folded lines is handled just like an empty line. */
2152 /* NOTE: Caller must handle lines that are MAYBE folded. */
2153 if (lineFolded(wp, lnum) == TRUE)
2154 return 1;
2155#endif
2156
2157 lines = plines_win_nofold(wp, lnum);
2158 if (winheight > 0 && lines > wp->w_height)
2159 return (int)wp->w_height;
2160 return lines;
2161}
2162
2163/*
2164 * Return number of window lines physical line "lnum" will occupy in window
2165 * "wp". Does not care about folding, 'wrap' or 'diff'.
2166 */
2167 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002168plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169{
2170 char_u *s;
2171 long col;
2172 int width;
2173
2174 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2175 if (*s == NUL) /* empty line */
2176 return 1;
2177 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
2178
2179 /*
2180 * If list mode is on, then the '$' at the end of the line may take up one
2181 * extra column.
2182 */
2183 if (wp->w_p_list && lcs_eol != NUL)
2184 col += 1;
2185
2186 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002187 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002189 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 if (width <= 0)
2191 return 32000;
2192 if (col <= width)
2193 return 1;
2194 col -= width;
2195 width += win_col_off2(wp);
2196 return (col + (width - 1)) / width + 1;
2197}
2198
2199/*
2200 * Like plines_win(), but only reports the number of physical screen lines
2201 * used from the start of the line to the given column number.
2202 */
2203 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002204plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205{
2206 long col;
2207 char_u *s;
2208 int lines = 0;
2209 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002210 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211
2212#ifdef FEAT_DIFF
2213 /* Check for filler lines above this buffer line. When folded the result
2214 * is one line anyway. */
2215 lines = diff_check_fill(wp, lnum);
2216#endif
2217
2218 if (!wp->w_p_wrap)
2219 return lines + 1;
2220
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 if (wp->w_width == 0)
2222 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223
Bram Moolenaar597a4222014-06-25 14:39:50 +02002224 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225
2226 col = 0;
2227 while (*s != NUL && --column >= 0)
2228 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02002229 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002230 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 }
2232
2233 /*
2234 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2235 * INSERT mode, then col must be adjusted so that it represents the last
2236 * screen position of the TAB. This only fixes an error when the TAB wraps
2237 * from one screen line to the next (when 'columns' is not a multiple of
2238 * 'ts') -- webb.
2239 */
2240 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02002241 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242
2243 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002244 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002246 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002247 if (width <= 0)
2248 return 9999;
2249
2250 lines += 1;
2251 if (col > width)
2252 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2253 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254}
2255
2256 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002257plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258{
2259 int count = 0;
2260
2261 while (first <= last)
2262 {
2263#ifdef FEAT_FOLDING
2264 int x;
2265
2266 /* Check if there are any really folded lines, but also included lines
2267 * that are maybe folded. */
2268 x = foldedCount(wp, first, NULL);
2269 if (x > 0)
2270 {
2271 ++count; /* count 1 for "+-- folded" line */
2272 first += x;
2273 }
2274 else
2275#endif
2276 {
2277#ifdef FEAT_DIFF
2278 if (first == wp->w_topline)
2279 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2280 else
2281#endif
2282 count += plines_win(wp, first, TRUE);
2283 ++first;
2284 }
2285 }
2286 return (count);
2287}
2288
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289/*
2290 * Insert string "p" at the cursor position. Stops at a NUL byte.
2291 * Handles Replace mode and multi-byte characters.
2292 */
2293 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002294ins_bytes(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295{
2296 ins_bytes_len(p, (int)STRLEN(p));
2297}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299/*
2300 * Insert string "p" with length "len" at the cursor position.
2301 * Handles Replace mode and multi-byte characters.
2302 */
2303 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002304ins_bytes_len(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305{
2306 int i;
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02002307#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 int n;
2309
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002310 if (has_mbyte)
2311 for (i = 0; i < len; i += n)
2312 {
2313 if (enc_utf8)
2314 /* avoid reading past p[len] */
2315 n = utfc_ptr2len_len(p + i, len - i);
2316 else
2317 n = (*mb_ptr2len)(p + i);
2318 ins_char_bytes(p + i, n);
2319 }
2320 else
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02002321#endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002322 for (i = 0; i < len; ++i)
2323 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325
2326/*
2327 * Insert or replace a single character at the cursor position.
2328 * When in REPLACE or VREPLACE mode, replace any existing character.
2329 * Caller must have prepared for undo.
2330 * For multi-byte characters we get the whole character, the caller must
2331 * convert bytes to a character.
2332 */
2333 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002334ins_char(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002336 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar560379d2017-01-21 22:50:00 +01002337 int n = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338
Bram Moolenaar6a8ede92017-01-22 19:49:12 +01002339#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 n = (*mb_char2bytes)(c, buf);
2341
2342 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2343 * Happens for CTRL-Vu9900. */
2344 if (buf[0] == 0)
2345 buf[0] = '\n';
Bram Moolenaar560379d2017-01-21 22:50:00 +01002346#else
2347 buf[0] = c;
2348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349
2350 ins_char_bytes(buf, n);
2351}
2352
2353 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002354ins_char_bytes(char_u *buf, int charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355{
2356 int c = buf[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 int newlen; /* nr of bytes inserted */
2358 int oldlen; /* nr of bytes deleted (0 when not replacing) */
2359 char_u *p;
2360 char_u *newp;
2361 char_u *oldp;
2362 int linelen; /* length of old line including NUL */
2363 colnr_T col;
2364 linenr_T lnum = curwin->w_cursor.lnum;
2365 int i;
2366
2367#ifdef FEAT_VIRTUALEDIT
2368 /* Break tabs if needed. */
2369 if (virtual_active() && curwin->w_cursor.coladd > 0)
2370 coladvance_force(getviscol());
2371#endif
2372
2373 col = curwin->w_cursor.col;
2374 oldp = ml_get(lnum);
2375 linelen = (int)STRLEN(oldp) + 1;
2376
2377 /* The lengths default to the values for when not replacing. */
2378 oldlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 newlen = charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380
2381 if (State & REPLACE_FLAG)
2382 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 if (State & VREPLACE_FLAG)
2384 {
2385 colnr_T new_vcol = 0; /* init for GCC */
2386 colnr_T vcol;
2387 int old_list;
2388#ifndef FEAT_MBYTE
2389 char_u buf[2];
2390#endif
2391
2392 /*
2393 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2394 * Returns the old value of list, so when finished,
2395 * curwin->w_p_list should be set back to this.
2396 */
2397 old_list = curwin->w_p_list;
2398 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2399 curwin->w_p_list = FALSE;
2400
2401 /*
2402 * In virtual replace mode each character may replace one or more
2403 * characters (zero if it's a TAB). Count the number of bytes to
2404 * be deleted to make room for the new character, counting screen
2405 * cells. May result in adding spaces to fill a gap.
2406 */
2407 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2408#ifndef FEAT_MBYTE
2409 buf[0] = c;
2410 buf[1] = NUL;
2411#endif
2412 new_vcol = vcol + chartabsize(buf, vcol);
2413 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2414 {
2415 vcol += chartabsize(oldp + col + oldlen, vcol);
2416 /* Don't need to remove a TAB that takes us to the right
2417 * position. */
2418 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2419 break;
2420#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002421 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422#else
2423 ++oldlen;
2424#endif
2425 /* Deleted a bit too much, insert spaces. */
2426 if (vcol > new_vcol)
2427 newlen += vcol - new_vcol;
2428 }
2429 curwin->w_p_list = old_list;
2430 }
2431 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 if (oldp[col] != NUL)
2433 {
2434 /* normal replace */
2435#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002436 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437#else
2438 oldlen = 1;
2439#endif
2440 }
2441
2442
2443 /* Push the replaced bytes onto the replace stack, so that they can be
2444 * put back when BS is used. The bytes of a multi-byte character are
2445 * done the other way around, so that the first byte is popped off
2446 * first (it tells the byte length of the character). */
2447 replace_push(NUL);
2448 for (i = 0; i < oldlen; ++i)
2449 {
2450#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002451 if (has_mbyte)
2452 i += replace_push_mb(oldp + col + i) - 1;
2453 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002455 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 }
2457 }
2458
2459 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2460 if (newp == NULL)
2461 return;
2462
2463 /* Copy bytes before the cursor. */
2464 if (col > 0)
2465 mch_memmove(newp, oldp, (size_t)col);
2466
2467 /* Copy bytes after the changed character(s). */
2468 p = newp + col;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02002469 if (linelen > col + oldlen)
2470 mch_memmove(p + newlen, oldp + col + oldlen,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 (size_t)(linelen - col - oldlen));
2472
2473 /* Insert or overwrite the new character. */
2474#ifdef FEAT_MBYTE
2475 mch_memmove(p, buf, charlen);
2476 i = charlen;
2477#else
2478 *p = c;
2479 i = 1;
2480#endif
2481
2482 /* Fill with spaces when necessary. */
2483 while (i < newlen)
2484 p[i++] = ' ';
2485
2486 /* Replace the line in the buffer. */
2487 ml_replace(lnum, newp, FALSE);
2488
2489 /* mark the buffer as changed and prepare for displaying */
2490 changed_bytes(lnum, col);
2491
2492 /*
2493 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2494 * show the match for right parens and braces.
2495 */
2496 if (p_sm && (State & INSERT)
2497 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002498#ifdef FEAT_INS_EXPAND
2499 && !ins_compl_active()
2500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002502 {
2503#ifdef FEAT_MBYTE
2504 if (has_mbyte)
2505 showmatch(mb_ptr2char(buf));
2506 else
2507#endif
2508 showmatch(c);
2509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510
2511#ifdef FEAT_RIGHTLEFT
2512 if (!p_ri || (State & REPLACE_FLAG))
2513#endif
2514 {
2515 /* Normal insert: move cursor right */
2516#ifdef FEAT_MBYTE
2517 curwin->w_cursor.col += charlen;
2518#else
2519 ++curwin->w_cursor.col;
2520#endif
2521 }
2522 /*
2523 * TODO: should try to update w_row here, to avoid recomputing it later.
2524 */
2525}
2526
2527/*
2528 * Insert a string at the cursor position.
2529 * Note: Does NOT handle Replace mode.
2530 * Caller must have prepared for undo.
2531 */
2532 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002533ins_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534{
2535 char_u *oldp, *newp;
2536 int newlen = (int)STRLEN(s);
2537 int oldlen;
2538 colnr_T col;
2539 linenr_T lnum = curwin->w_cursor.lnum;
2540
2541#ifdef FEAT_VIRTUALEDIT
2542 if (virtual_active() && curwin->w_cursor.coladd > 0)
2543 coladvance_force(getviscol());
2544#endif
2545
2546 col = curwin->w_cursor.col;
2547 oldp = ml_get(lnum);
2548 oldlen = (int)STRLEN(oldp);
2549
2550 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2551 if (newp == NULL)
2552 return;
2553 if (col > 0)
2554 mch_memmove(newp, oldp, (size_t)col);
2555 mch_memmove(newp + col, s, (size_t)newlen);
2556 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2557 ml_replace(lnum, newp, FALSE);
2558 changed_bytes(lnum, col);
2559 curwin->w_cursor.col += newlen;
2560}
2561
2562/*
2563 * Delete one character under the cursor.
2564 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2565 * Caller must have prepared for undo.
2566 *
2567 * return FAIL for failure, OK otherwise
2568 */
2569 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002570del_char(int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571{
2572#ifdef FEAT_MBYTE
2573 if (has_mbyte)
2574 {
2575 /* Make sure the cursor is at the start of a character. */
2576 mb_adjust_cursor();
2577 if (*ml_get_cursor() == NUL)
2578 return FAIL;
2579 return del_chars(1L, fixpos);
2580 }
2581#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002582 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583}
2584
2585#if defined(FEAT_MBYTE) || defined(PROTO)
2586/*
2587 * Like del_bytes(), but delete characters instead of bytes.
2588 */
2589 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002590del_chars(long count, int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591{
2592 long bytes = 0;
2593 long i;
2594 char_u *p;
2595 int l;
2596
2597 p = ml_get_cursor();
2598 for (i = 0; i < count && *p != NUL; ++i)
2599 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002600 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 bytes += l;
2602 p += l;
2603 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002604 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605}
2606#endif
2607
2608/*
2609 * Delete "count" bytes under the cursor.
2610 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2611 * Caller must have prepared for undo.
2612 *
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002613 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 */
2615 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002616del_bytes(
2617 long count,
2618 int fixpos_arg,
2619 int use_delcombine UNUSED) /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620{
2621 char_u *oldp, *newp;
2622 colnr_T oldlen;
2623 linenr_T lnum = curwin->w_cursor.lnum;
2624 colnr_T col = curwin->w_cursor.col;
2625 int was_alloced;
2626 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002627 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628
2629 oldp = ml_get(lnum);
2630 oldlen = (int)STRLEN(oldp);
2631
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002632 /* Can't do anything when the cursor is on the NUL after the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 if (col >= oldlen)
2634 return FAIL;
2635
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002636 /* If "count" is zero there is nothing to do. */
2637 if (count == 0)
2638 return OK;
2639
2640 /* If "count" is negative the caller must be doing something wrong. */
2641 if (count < 1)
2642 {
2643 IEMSGN("E950: Invalid count for del_bytes(): %ld", count);
2644 return FAIL;
2645 }
2646
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647#ifdef FEAT_MBYTE
2648 /* If 'delcombine' is set and deleting (less than) one character, only
2649 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002650 if (p_deco && use_delcombine && enc_utf8
2651 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002653 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 int n;
2655
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002656 (void)utfc_ptr2char(oldp + col, cc);
2657 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 {
2659 /* Find the last composing char, there can be several. */
2660 n = col;
2661 do
2662 {
2663 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002664 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 n += count;
2666 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2667 fixpos = 0;
2668 }
2669 }
2670#endif
2671
2672 /*
2673 * When count is too big, reduce it.
2674 */
2675 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2676 if (movelen <= 1)
2677 {
2678 /*
2679 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002680 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2681 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002683 if (col > 0 && fixpos && restart_edit == 0
2684#ifdef FEAT_VIRTUALEDIT
2685 && (ve_flags & VE_ONEMORE) == 0
2686#endif
2687 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 {
2689 --curwin->w_cursor.col;
2690#ifdef FEAT_VIRTUALEDIT
2691 curwin->w_cursor.coladd = 0;
2692#endif
2693#ifdef FEAT_MBYTE
2694 if (has_mbyte)
2695 curwin->w_cursor.col -=
2696 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2697#endif
2698 }
2699 count = oldlen - col;
2700 movelen = 1;
2701 }
2702
2703 /*
2704 * If the old line has been allocated the deletion can be done in the
2705 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002706 * Can't do this when using Netbeans, because we would need to invoke
2707 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002708 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002711 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002712 was_alloced = FALSE;
2713 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002715 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 if (was_alloced)
2717 newp = oldp; /* use same allocated memory */
2718 else
2719 { /* need to allocate a new line */
2720 newp = alloc((unsigned)(oldlen + 1 - count));
2721 if (newp == NULL)
2722 return FAIL;
2723 mch_memmove(newp, oldp, (size_t)col);
2724 }
2725 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2726 if (!was_alloced)
2727 ml_replace(lnum, newp, FALSE);
2728
2729 /* mark the buffer as changed and prepare for displaying */
2730 changed_bytes(lnum, curwin->w_cursor.col);
2731
2732 return OK;
2733}
2734
2735/*
2736 * Delete from cursor to end of line.
2737 * Caller must have prepared for undo.
2738 *
2739 * return FAIL for failure, OK otherwise
2740 */
2741 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002742truncate_line(
2743 int fixpos) /* if TRUE fix the cursor position when done */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744{
2745 char_u *newp;
2746 linenr_T lnum = curwin->w_cursor.lnum;
2747 colnr_T col = curwin->w_cursor.col;
2748
2749 if (col == 0)
2750 newp = vim_strsave((char_u *)"");
2751 else
2752 newp = vim_strnsave(ml_get(lnum), col);
2753
2754 if (newp == NULL)
2755 return FAIL;
2756
2757 ml_replace(lnum, newp, FALSE);
2758
2759 /* mark the buffer as changed and prepare for displaying */
2760 changed_bytes(lnum, curwin->w_cursor.col);
2761
2762 /*
2763 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2764 */
2765 if (fixpos && curwin->w_cursor.col > 0)
2766 --curwin->w_cursor.col;
2767
2768 return OK;
2769}
2770
2771/*
2772 * Delete "nlines" lines at the cursor.
2773 * Saves the lines for undo first if "undo" is TRUE.
2774 */
2775 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002776del_lines(
2777 long nlines, /* number of lines to delete */
2778 int undo) /* if TRUE, prepare for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779{
2780 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002781 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782
2783 if (nlines <= 0)
2784 return;
2785
2786 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002787 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 return;
2789
2790 for (n = 0; n < nlines; )
2791 {
2792 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2793 break;
2794
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002795 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 ++n;
2797
2798 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002799 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 break;
2801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002803 /* Correct the cursor position before calling deleted_lines_mark(), it may
2804 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 curwin->w_cursor.col = 0;
2806 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002807
2808 /* adjust marks, mark the buffer as changed and prepare for displaying */
2809 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810}
2811
2812 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002813gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002815 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002817 /* When searching columns is sometimes put at the end of a line. */
2818 if (pos->col == MAXCOL)
2819 return NUL;
2820 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821#ifdef FEAT_MBYTE
2822 if (has_mbyte)
2823 return (*mb_ptr2char)(ptr);
2824#endif
2825 return (int)*ptr;
2826}
2827
2828 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002829gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830{
2831#ifdef FEAT_MBYTE
2832 if (has_mbyte)
2833 return (*mb_ptr2char)(ml_get_cursor());
2834#endif
2835 return (int)*ml_get_cursor();
2836}
2837
2838/*
2839 * Write a character at the current cursor position.
2840 * It is directly written into the block.
2841 */
2842 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002843pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844{
2845 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2846 + curwin->w_cursor.col) = c;
2847}
2848
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849/*
2850 * When extra == 0: Return TRUE if the cursor is before or on the first
2851 * non-blank in the line.
2852 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2853 * the line.
2854 */
2855 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002856inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857{
2858 char_u *ptr;
2859 colnr_T col;
2860
Bram Moolenaar1c465442017-03-12 20:10:05 +01002861 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 ++ptr;
2863 if (col >= curwin->w_cursor.col + extra)
2864 return TRUE;
2865 else
2866 return FALSE;
2867}
2868
2869/*
2870 * Skip to next part of an option argument: Skip space and comma.
2871 */
2872 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002873skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874{
2875 if (*p == ',')
2876 ++p;
2877 while (*p == ' ')
2878 ++p;
2879 return p;
2880}
2881
2882/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002883 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 *
2885 * Most often called through changed_bytes() and changed_lines(), which also
2886 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002887 *
2888 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 */
2890 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002891changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892{
2893#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02002894 if (p_imst == IM_ON_THE_SPOT)
2895 {
2896 /* The text of the preediting area is inserted, but this doesn't
2897 * mean a change of the buffer yet. That is delayed until the
2898 * text is committed. (this means preedit becomes empty) */
2899 if (im_is_preediting() && !xim_changed_while_preediting)
2900 return;
2901 xim_changed_while_preediting = FALSE;
2902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903#endif
2904
2905 if (!curbuf->b_changed)
2906 {
2907 int save_msg_scroll = msg_scroll;
2908
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002909 /* Give a warning about changing a read-only file. This may also
2910 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002912
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 /* Create a swap file if that is wanted.
2914 * Don't do this for "nofile" and "nowrite" buffer types. */
2915 if (curbuf->b_may_swap
2916#ifdef FEAT_QUICKFIX
2917 && !bt_dontwrite(curbuf)
2918#endif
2919 )
2920 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002921 int save_need_wait_return = need_wait_return;
2922
2923 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 ml_open_file(curbuf);
2925
2926 /* The ml_open_file() can cause an ATTENTION message.
2927 * Wait two seconds, to make sure the user reads this unexpected
2928 * message. Since we could be anywhere, call wait_return() now,
2929 * and don't let the emsg() set msg_scroll. */
2930 if (need_wait_return && emsg_silent == 0)
2931 {
2932 out_flush();
2933 ui_delay(2000L, TRUE);
2934 wait_return(TRUE);
2935 msg_scroll = save_msg_scroll;
2936 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002937 else
2938 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002940 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002942 ++CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943}
2944
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002945/*
2946 * Internal part of changed(), no user interaction.
2947 */
2948 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002949changed_int(void)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002950{
2951 curbuf->b_changed = TRUE;
2952 ml_setflags(curbuf);
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002953 check_status(curbuf);
2954 redraw_tabline = TRUE;
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002955#ifdef FEAT_TITLE
2956 need_maketitle = TRUE; /* set window title later */
2957#endif
2958}
2959
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002960static void changedOneline(buf_T *buf, linenr_T lnum);
2961static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
2962static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963
2964/*
2965 * Changed bytes within a single line for the current buffer.
2966 * - marks the windows on this buffer to be redisplayed
2967 * - marks the buffer changed by calling changed()
2968 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002969 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 */
2971 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002972changed_bytes(linenr_T lnum, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002974 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002976
2977#ifdef FEAT_DIFF
2978 /* Diff highlighting in other diff windows may need to be updated too. */
2979 if (curwin->w_p_diff)
2980 {
2981 win_T *wp;
2982 linenr_T wlnum;
2983
Bram Moolenaar29323592016-07-24 22:04:11 +02002984 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002985 if (wp->w_p_diff && wp != curwin)
2986 {
2987 redraw_win_later(wp, VALID);
2988 wlnum = diff_lnum_win(lnum, wp);
2989 if (wlnum > 0)
2990 changedOneline(wp->w_buffer, wlnum);
2991 }
2992 }
2993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994}
2995
2996 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002997changedOneline(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002999 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 {
3001 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003002 if (lnum < buf->b_mod_top)
3003 buf->b_mod_top = lnum;
3004 else if (lnum >= buf->b_mod_bot)
3005 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 }
3007 else
3008 {
3009 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003010 buf->b_mod_set = TRUE;
3011 buf->b_mod_top = lnum;
3012 buf->b_mod_bot = lnum + 1;
3013 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 }
3015}
3016
3017/*
3018 * Appended "count" lines below line "lnum" in the current buffer.
3019 * Must be called AFTER the change and after mark_adjust().
3020 * Takes care of marking the buffer to be redrawn and sets the changed flag.
3021 */
3022 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003023appended_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024{
3025 changed_lines(lnum + 1, 0, lnum + 1, count);
3026}
3027
3028/*
3029 * Like appended_lines(), but adjust marks first.
3030 */
3031 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003032appended_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033{
Bram Moolenaar82faa252016-06-04 20:14:07 +02003034 /* Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003035 * be marks there. But it's still needed in diff mode. */
3036 if (lnum + count < curbuf->b_ml.ml_line_count
3037#ifdef FEAT_DIFF
3038 || curwin->w_p_diff
3039#endif
3040 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02003041 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 changed_lines(lnum + 1, 0, lnum + 1, count);
3043}
3044
3045/*
3046 * Deleted "count" lines at line "lnum" in the current buffer.
3047 * Must be called AFTER the change and after mark_adjust().
3048 * Takes care of marking the buffer to be redrawn and sets the changed flag.
3049 */
3050 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003051deleted_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052{
3053 changed_lines(lnum, 0, lnum + count, -count);
3054}
3055
3056/*
3057 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00003058 * Make sure the cursor is on a valid line before calling, a GUI callback may
3059 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 */
3061 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003062deleted_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063{
3064 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
3065 changed_lines(lnum, 0, lnum + count, -count);
3066}
3067
3068/*
3069 * Changed lines for the current buffer.
3070 * Must be called AFTER the change and after mark_adjust().
3071 * - mark the buffer changed by calling changed()
3072 * - mark the windows on this buffer to be redisplayed
3073 * - invalidate cached values
3074 * "lnum" is the first line that needs displaying, "lnume" the first line
3075 * below the changed lines (BEFORE the change).
3076 * When only inserting lines, "lnum" and "lnume" are equal.
3077 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003078 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 */
3080 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003081changed_lines(
3082 linenr_T lnum, /* first line with change */
3083 colnr_T col, /* column in first line with change */
3084 linenr_T lnume, /* line below last changed line */
3085 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086{
Bram Moolenaardba8a912005-04-24 22:08:39 +00003087 changed_lines_buf(curbuf, lnum, lnume, xtra);
3088
3089#ifdef FEAT_DIFF
Bram Moolenaare3521d92018-09-16 14:10:31 +02003090 if (xtra == 0 && curwin->w_p_diff && !diff_internal())
Bram Moolenaardba8a912005-04-24 22:08:39 +00003091 {
3092 /* When the number of lines doesn't change then mark_adjust() isn't
3093 * called and other diff buffers still need to be marked for
3094 * displaying. */
3095 win_T *wp;
3096 linenr_T wlnum;
3097
Bram Moolenaar29323592016-07-24 22:04:11 +02003098 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00003099 if (wp->w_p_diff && wp != curwin)
3100 {
3101 redraw_win_later(wp, VALID);
3102 wlnum = diff_lnum_win(lnum, wp);
3103 if (wlnum > 0)
3104 changed_lines_buf(wp->w_buffer, wlnum,
3105 lnume - lnum + wlnum, 0L);
3106 }
3107 }
3108#endif
3109
3110 changed_common(lnum, col, lnume, xtra);
3111}
3112
3113 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003114changed_lines_buf(
3115 buf_T *buf,
3116 linenr_T lnum, /* first line with change */
3117 linenr_T lnume, /* line below last changed line */
3118 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003119{
3120 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 {
3122 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003123 if (lnum < buf->b_mod_top)
3124 buf->b_mod_top = lnum;
3125 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 {
3127 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003128 buf->b_mod_bot += xtra;
3129 if (buf->b_mod_bot < lnum)
3130 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00003132 if (lnume + xtra > buf->b_mod_bot)
3133 buf->b_mod_bot = lnume + xtra;
3134 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 }
3136 else
3137 {
3138 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003139 buf->b_mod_set = TRUE;
3140 buf->b_mod_top = lnum;
3141 buf->b_mod_bot = lnume + xtra;
3142 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144}
3145
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003146/*
3147 * Common code for when a change is was made.
3148 * See changed_lines() for the arguments.
3149 * Careful: may trigger autocommands that reload the buffer.
3150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003152changed_common(
3153 linenr_T lnum,
3154 colnr_T col,
3155 linenr_T lnume,
3156 long xtra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157{
3158 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003159 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 int i;
3161#ifdef FEAT_JUMPLIST
3162 int cols;
3163 pos_T *p;
3164 int add;
3165#endif
3166
3167 /* mark the buffer as modified */
3168 changed();
3169
Bram Moolenaare3521d92018-09-16 14:10:31 +02003170#ifdef FEAT_DIFF
3171 if (curwin->w_p_diff && diff_internal())
3172 curtab->tp_diff_update = TRUE;
3173#endif
3174
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 /* set the '. mark */
3176 if (!cmdmod.keepjumps)
3177 {
3178 curbuf->b_last_change.lnum = lnum;
3179 curbuf->b_last_change.col = col;
3180
3181#ifdef FEAT_JUMPLIST
3182 /* Create a new entry if a new undo-able change was started or we
3183 * don't have an entry yet. */
3184 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3185 {
3186 if (curbuf->b_changelistlen == 0)
3187 add = TRUE;
3188 else
3189 {
3190 /* Don't create a new entry when the line number is the same
3191 * as the last one and the column is not too far away. Avoids
3192 * creating many entries for typing "xxxxx". */
3193 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3194 if (p->lnum != lnum)
3195 add = TRUE;
3196 else
3197 {
3198 cols = comp_textwidth(FALSE);
3199 if (cols == 0)
3200 cols = 79;
3201 add = (p->col + cols < col || col + cols < p->col);
3202 }
3203 }
3204 if (add)
3205 {
3206 /* This is the first of a new sequence of undo-able changes
3207 * and it's at some distance of the last change. Use a new
3208 * position in the changelist. */
3209 curbuf->b_new_change = FALSE;
3210
3211 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3212 {
3213 /* changelist is full: remove oldest entry */
3214 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3215 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3216 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003217 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 {
3219 /* Correct position in changelist for other windows on
3220 * this buffer. */
3221 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3222 --wp->w_changelistidx;
3223 }
3224 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003225 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 {
3227 /* For other windows, if the position in the changelist is
3228 * at the end it stays at the end. */
3229 if (wp->w_buffer == curbuf
3230 && wp->w_changelistidx == curbuf->b_changelistlen)
3231 ++wp->w_changelistidx;
3232 }
3233 ++curbuf->b_changelistlen;
3234 }
3235 }
3236 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3237 curbuf->b_last_change;
3238 /* The current window is always after the last change, so that "g,"
3239 * takes you back to it. */
3240 curwin->w_changelistidx = curbuf->b_changelistlen;
3241#endif
3242 }
3243
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003244 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 {
3246 if (wp->w_buffer == curbuf)
3247 {
3248 /* Mark this window to be redrawn later. */
3249 if (wp->w_redr_type < VALID)
3250 wp->w_redr_type = VALID;
3251
3252 /* Check if a change in the buffer has invalidated the cached
3253 * values for the cursor. */
3254#ifdef FEAT_FOLDING
3255 /*
3256 * Update the folds for this window. Can't postpone this, because
3257 * a following operator might work on the whole fold: ">>dd".
3258 */
3259 foldUpdate(wp, lnum, lnume + xtra - 1);
3260
3261 /* The change may cause lines above or below the change to become
3262 * included in a fold. Set lnum/lnume to the first/last line that
3263 * might be displayed differently.
3264 * Set w_cline_folded here as an efficient way to update it when
3265 * inserting lines just above a closed fold. */
3266 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3267 if (wp->w_cursor.lnum == lnum)
3268 wp->w_cline_folded = i;
3269 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3270 if (wp->w_cursor.lnum == lnume)
3271 wp->w_cline_folded = i;
3272
3273 /* If the changed line is in a range of previously folded lines,
3274 * compare with the first line in that range. */
3275 if (wp->w_cursor.lnum <= lnum)
3276 {
3277 i = find_wl_entry(wp, lnum);
3278 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3279 changed_line_abv_curs_win(wp);
3280 }
3281#endif
3282
3283 if (wp->w_cursor.lnum > lnum)
3284 changed_line_abv_curs_win(wp);
3285 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3286 changed_cline_bef_curs_win(wp);
3287 if (wp->w_botline >= lnum)
3288 {
3289 /* Assume that botline doesn't change (inserted lines make
3290 * other lines scroll down below botline). */
3291 approximate_botline_win(wp);
3292 }
3293
3294 /* Check if any w_lines[] entries have become invalid.
3295 * For entries below the change: Correct the lnums for
3296 * inserted/deleted lines. Makes it possible to stop displaying
3297 * after the change. */
3298 for (i = 0; i < wp->w_lines_valid; ++i)
3299 if (wp->w_lines[i].wl_valid)
3300 {
3301 if (wp->w_lines[i].wl_lnum >= lnum)
3302 {
3303 if (wp->w_lines[i].wl_lnum < lnume)
3304 {
3305 /* line included in change */
3306 wp->w_lines[i].wl_valid = FALSE;
3307 }
3308 else if (xtra != 0)
3309 {
3310 /* line below change */
3311 wp->w_lines[i].wl_lnum += xtra;
3312#ifdef FEAT_FOLDING
3313 wp->w_lines[i].wl_lastlnum += xtra;
3314#endif
3315 }
3316 }
3317#ifdef FEAT_FOLDING
3318 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3319 {
3320 /* change somewhere inside this range of folded lines,
3321 * may need to be redrawn */
3322 wp->w_lines[i].wl_valid = FALSE;
3323 }
3324#endif
3325 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003326
3327#ifdef FEAT_FOLDING
3328 /* Take care of side effects for setting w_topline when folds have
3329 * changed. Esp. when the buffer was changed in another window. */
3330 if (hasAnyFolding(wp))
3331 set_topline(wp, wp->w_topline);
3332#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003333 /* relative numbering may require updating more */
3334 if (wp->w_p_rnu)
3335 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 }
3337 }
3338
3339 /* Call update_screen() later, which checks out what needs to be redrawn,
3340 * since it notices b_mod_set and then uses b_mod_*. */
3341 if (must_redraw < VALID)
3342 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003343
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003344 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003345 if (lnum <= curwin->w_cursor.lnum
3346 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003347 last_cursormoved.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348}
3349
3350/*
3351 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3352 */
3353 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003354unchanged(
3355 buf_T *buf,
3356 int ff) /* also reset 'fileformat' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003358 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 {
3360 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003361 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 if (ff)
3363 save_file_ff(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003365 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366#ifdef FEAT_TITLE
3367 need_maketitle = TRUE; /* set window title later */
3368#endif
3369 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003370 ++CHANGEDTICK(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371#ifdef FEAT_NETBEANS_INTG
3372 netbeans_unmodified(buf);
3373#endif
3374}
3375
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376/*
3377 * check_status: called when the status bars for the buffer 'buf'
3378 * need to be updated
3379 */
3380 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003381check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382{
3383 win_T *wp;
3384
Bram Moolenaar29323592016-07-24 22:04:11 +02003385 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 if (wp->w_buffer == buf && wp->w_status_height)
3387 {
3388 wp->w_redr_status = TRUE;
3389 if (must_redraw < VALID)
3390 must_redraw = VALID;
3391 }
3392}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393
3394/*
3395 * If the file is readonly, give a warning message with the first change.
3396 * Don't do this for autocommands.
3397 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003398 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003400 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 */
3402 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003403change_warning(
3404 int col) /* column for message; non-zero when in insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 mode and 'showmode' is on */
3406{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003407 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3408
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 if (curbuf->b_did_warn == FALSE
3410 && curbufIsChanged() == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 && !autocmd_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 && curbuf->b_p_ro)
3413 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003414 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003416 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 if (!curbuf->b_p_ro)
3418 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 /*
3420 * Do what msg() does, but with a column offset if the warning should
3421 * be after the mode message.
3422 */
3423 msg_start();
3424 if (msg_row == Rows - 1)
3425 msg_col = col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003426 msg_source(HL_ATTR(HLF_W));
3427 MSG_PUTS_ATTR(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST);
Bram Moolenaar496c5262009-03-18 14:42:00 +00003428#ifdef FEAT_EVAL
3429 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 msg_clr_eos();
3432 (void)msg_end();
Bram Moolenaare5f2a072017-02-01 22:31:49 +01003433 if (msg_silent == 0 && !silent_mode
3434#ifdef FEAT_EVAL
3435 && time_for_testing != 1
3436#endif
3437 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 {
3439 out_flush();
3440 ui_delay(1000L, TRUE); /* give the user time to think about it */
3441 }
3442 curbuf->b_did_warn = TRUE;
3443 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3444 if (msg_row < Rows - 1)
3445 showmode();
3446 }
3447}
3448
3449/*
3450 * Ask for a reply from the user, a 'y' or a 'n'.
3451 * No other characters are accepted, the message is repeated until a valid
3452 * reply is entered or CTRL-C is hit.
3453 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3454 * from any buffers but directly from the user.
3455 *
3456 * return the 'y' or 'n'
3457 */
3458 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003459ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460{
3461 int r = ' ';
3462 int save_State = State;
3463
3464 if (exiting) /* put terminal in raw mode for this question */
3465 settmode(TMODE_RAW);
3466 ++no_wait_return;
3467#ifdef USE_ON_FLY_SCROLL
3468 dont_scroll = TRUE; /* disallow scrolling here */
3469#endif
3470 State = CONFIRM; /* mouse behaves like with :confirm */
3471#ifdef FEAT_MOUSE
3472 setmouse(); /* disables mouse for xterm */
3473#endif
3474 ++no_mapping;
3475 ++allow_keys; /* no mapping here, but recognize keys */
3476
3477 while (r != 'y' && r != 'n')
3478 {
3479 /* same highlighting as for wait_return */
Bram Moolenaar8820b482017-03-16 17:23:31 +01003480 smsg_attr(HL_ATTR(HLF_R), (char_u *)"%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 if (direct)
3482 r = get_keystroke();
3483 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003484 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 if (r == Ctrl_C || r == ESC)
3486 r = 'n';
3487 msg_putchar(r); /* show what you typed */
3488 out_flush();
3489 }
3490 --no_wait_return;
3491 State = save_State;
3492#ifdef FEAT_MOUSE
3493 setmouse();
3494#endif
3495 --no_mapping;
3496 --allow_keys;
3497
3498 return r;
3499}
3500
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003501#if defined(FEAT_MOUSE) || defined(PROTO)
3502/*
3503 * Return TRUE if "c" is a mouse key.
3504 */
3505 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003506is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003507{
3508 return c == K_LEFTMOUSE
3509 || c == K_LEFTMOUSE_NM
3510 || c == K_LEFTDRAG
3511 || c == K_LEFTRELEASE
3512 || c == K_LEFTRELEASE_NM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003513 || c == K_MOUSEMOVE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003514 || c == K_MIDDLEMOUSE
3515 || c == K_MIDDLEDRAG
3516 || c == K_MIDDLERELEASE
3517 || c == K_RIGHTMOUSE
3518 || c == K_RIGHTDRAG
3519 || c == K_RIGHTRELEASE
3520 || c == K_MOUSEDOWN
3521 || c == K_MOUSEUP
3522 || c == K_MOUSELEFT
3523 || c == K_MOUSERIGHT
3524 || c == K_X1MOUSE
3525 || c == K_X1DRAG
3526 || c == K_X1RELEASE
3527 || c == K_X2MOUSE
3528 || c == K_X2DRAG
3529 || c == K_X2RELEASE;
3530}
3531#endif
3532
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533/*
3534 * Get a key stroke directly from the user.
3535 * Ignores mouse clicks and scrollbar events, except a click for the left
3536 * button (used at the more prompt).
3537 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3538 * Disadvantage: typeahead is ignored.
3539 * Translates the interrupt character for unix to ESC.
3540 */
3541 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003542get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003544 char_u *buf = NULL;
3545 int buflen = 150;
3546 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 int len = 0;
3548 int n;
3549 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003550 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551
3552 mapped_ctrl_c = FALSE; /* mappings are not used here */
3553 for (;;)
3554 {
3555 cursor_on();
3556 out_flush();
3557
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003558 /* Leave some room for check_termcode() to insert a key code into (max
3559 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3560 * bytes. */
3561 maxlen = (buflen - 6 - len) / 3;
3562 if (buf == NULL)
3563 buf = alloc(buflen);
3564 else if (maxlen < 10)
3565 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003566 char_u *t_buf = buf;
3567
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003568 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003569 * escape sequence. */
3570 buflen += 100;
3571 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003572 if (buf == NULL)
3573 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003574 maxlen = (buflen - 6 - len) / 3;
3575 }
3576 if (buf == NULL)
3577 {
3578 do_outofmem_msg((long_u)buflen);
3579 return ESC; /* panic! */
3580 }
3581
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003583 * terminal code to complete. */
3584 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 if (n > 0)
3586 {
3587 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003588 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003590 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003592 else if (len > 0)
3593 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594
Bram Moolenaar4395a712006-09-05 18:57:57 +00003595 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003596 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003597 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003599
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003600 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003601 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003602 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003603 {
3604 /* Redrawing was postponed, do it now. */
3605 update_screen(0);
3606 setcursor(); /* put cursor back where it belongs */
3607 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003608 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003609 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003610 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003612 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 continue;
3614
3615 /* Handle modifier and/or special key code. */
3616 n = buf[0];
3617 if (n == K_SPECIAL)
3618 {
3619 n = TO_SPECIAL(buf[1], buf[2]);
3620 if (buf[1] == KS_MODIFIER
3621 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003622#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003623 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003624#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003625#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 || n == K_VER_SCROLLBAR
3627 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628#endif
3629 )
3630 {
3631 if (buf[1] == KS_MODIFIER)
3632 mod_mask = buf[2];
3633 len -= 3;
3634 if (len > 0)
3635 mch_memmove(buf, buf + 3, (size_t)len);
3636 continue;
3637 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003638 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 }
3640#ifdef FEAT_MBYTE
3641 if (has_mbyte)
3642 {
3643 if (MB_BYTE2LEN(n) > len)
3644 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003645 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 n = (*mb_ptr2char)(buf);
3647 }
3648#endif
3649#ifdef UNIX
3650 if (n == intr_char)
3651 n = ESC;
3652#endif
3653 break;
3654 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003655 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656
3657 mapped_ctrl_c = save_mapped_ctrl_c;
3658 return n;
3659}
3660
3661/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003662 * Get a number from the user.
3663 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 */
3665 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003666get_number(
3667 int colon, /* allow colon to abort */
3668 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669{
3670 int n = 0;
3671 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003672 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003674 if (mouse_used != NULL)
3675 *mouse_used = FALSE;
3676
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 /* When not printing messages, the user won't know what to type, return a
3678 * zero (as if CR was hit). */
3679 if (msg_silent != 0)
3680 return 0;
3681
3682#ifdef USE_ON_FLY_SCROLL
3683 dont_scroll = TRUE; /* disallow scrolling here */
3684#endif
3685 ++no_mapping;
3686 ++allow_keys; /* no mapping here, but recognize keys */
3687 for (;;)
3688 {
3689 windgoto(msg_row, msg_col);
3690 c = safe_vgetc();
3691 if (VIM_ISDIGIT(c))
3692 {
3693 n = n * 10 + c - '0';
3694 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003695 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 }
3697 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3698 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003699 if (typed > 0)
3700 {
3701 MSG_PUTS("\b \b");
3702 --typed;
3703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003706#ifdef FEAT_MOUSE
3707 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3708 {
3709 *mouse_used = TRUE;
3710 n = mouse_row + 1;
3711 break;
3712 }
3713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 else if (n == 0 && c == ':' && colon)
3715 {
3716 stuffcharReadbuff(':');
3717 if (!exmode_active)
3718 cmdline_row = msg_row;
3719 skip_redraw = TRUE; /* skip redraw once */
3720 do_redraw = FALSE;
3721 break;
3722 }
3723 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3724 break;
3725 }
3726 --no_mapping;
3727 --allow_keys;
3728 return n;
3729}
3730
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003731/*
3732 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003733 * When "mouse_used" is not NULL allow using the mouse and in that case return
3734 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003735 */
3736 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003737prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003738{
3739 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003740 int save_cmdline_row;
3741 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003742
3743 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003744 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003745 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003746 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003747 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003748
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003749 // Set the state such that text can be selected/copied/pasted and we still
3750 // get mouse events. redraw_after_callback() will not redraw if cmdline_row
3751 // is zero.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003752 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003753 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003754 save_State = State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003755 State = CMDLINE;
Bram Moolenaar73658312018-04-24 17:41:57 +02003756#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003757 // May show different mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02003758 setmouse();
3759#endif
3760
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003761 i = get_number(TRUE, mouse_used);
3762 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003763 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003764 /* don't call wait_return() now */
3765 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003766 cmdline_row = msg_row - 1;
3767 need_wait_return = FALSE;
3768 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003769 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003770 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003771 else
3772 cmdline_row = save_cmdline_row;
3773 State = save_State;
Bram Moolenaar73658312018-04-24 17:41:57 +02003774#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003775 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02003776 setmouse();
3777#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003778
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003779 return i;
3780}
3781
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003783msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784{
3785 long pn;
3786
3787 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3789 return;
3790
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003791 /* We don't want to overwrite another important message, but do overwrite
3792 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3793 * then "put" reports the last action. */
3794 if (keep_msg != NULL && !keep_msg_more)
3795 return;
3796
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 if (n > 0)
3798 pn = n;
3799 else
3800 pn = -n;
3801
3802 if (pn > p_report)
3803 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003804 if (n > 0)
3805 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3806 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003808 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3809 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003811 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 if (msg(msg_buf))
3813 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003814 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003815 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 }
3817 }
3818}
3819
3820/*
3821 * flush map and typeahead buffers and give a warning for an error
3822 */
3823 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003824beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825{
3826 if (emsg_silent == 0)
3827 {
3828 flush_buffers(FALSE);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003829 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 }
3831}
3832
3833/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003834 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 */
3836 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003837vim_beep(
3838 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003840#ifdef FEAT_EVAL
3841 called_vim_beep = TRUE;
3842#endif
3843
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 if (emsg_silent == 0)
3845 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003846 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3847 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003848#ifdef ELAPSED_FUNC
3849 static int did_init = FALSE;
3850 static ELAPSED_TYPE start_tv;
3851
3852 /* Only beep once per half a second, otherwise a sequence of beeps
3853 * would freeze Vim. */
3854 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
3855 {
3856 did_init = TRUE;
3857 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003859 if (p_vb
3860#ifdef FEAT_GUI
3861 /* While the GUI is starting up the termcap is set for
3862 * the GUI but the output still goes to a terminal. */
3863 && !(gui.in_use && gui.starting)
3864#endif
3865 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003866 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003867 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003868#ifdef FEAT_VTP
3869 /* No restore color information, refresh the screen. */
3870 if (has_vtp_working() != 0
3871# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003872 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003873# endif
3874 )
3875 {
3876 redraw_later(CLEAR);
3877 update_screen(0);
3878 redrawcmd();
3879 }
3880#endif
3881 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003882 else
3883 out_char(BELL);
3884#ifdef ELAPSED_FUNC
3885 }
3886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003888
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003889 /* When 'debug' contains "beep" produce a message. If we are sourcing
3890 * a script or executing a function give the user a hint where the beep
3891 * comes from. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003892 if (vim_strchr(p_debug, 'e') != NULL)
3893 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003894 msg_source(HL_ATTR(HLF_W));
3895 msg_attr((char_u *)_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 }
3898}
3899
3900/*
3901 * To get the "real" home directory:
3902 * - get value of $HOME
3903 * For Unix:
3904 * - go to that directory
3905 * - do mch_dirname() to get the real name of that directory.
3906 * This also works with mounts and links.
3907 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3908 */
3909static char_u *homedir = NULL;
3910
3911 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003912init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913{
3914 char_u *var;
3915
Bram Moolenaar05159a02005-02-26 23:04:13 +00003916 /* In case we are called a second time (when 'encoding' changes). */
Bram Moolenaard23a8232018-02-10 18:45:26 +01003917 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003918
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919#ifdef VMS
3920 var = mch_getenv((char_u *)"SYS$LOGIN");
3921#else
3922 var = mch_getenv((char_u *)"HOME");
3923#endif
3924
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925#ifdef WIN3264
3926 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02003927 * Typically, $HOME is not defined on Windows, unless the user has
3928 * specifically defined it for Vim's sake. However, on Windows NT
3929 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3930 * each user. Try constructing $HOME from these.
3931 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02003932 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02003933 {
3934 char_u *homedrive, *homepath;
3935
3936 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3937 homepath = mch_getenv((char_u *)"HOMEPATH");
3938 if (homepath == NULL || *homepath == NUL)
3939 homepath = (char_u *)"\\";
3940 if (homedrive != NULL
3941 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3942 {
3943 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3944 if (NameBuff[0] != NUL)
3945 var = NameBuff;
3946 }
3947 }
3948
3949 if (var == NULL)
3950 var = mch_getenv((char_u *)"USERPROFILE");
3951
3952 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 * Weird but true: $HOME may contain an indirect reference to another
3954 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3955 * when $HOME is being set.
3956 */
3957 if (var != NULL && *var == '%')
3958 {
3959 char_u *p;
3960 char_u *exp;
3961
3962 p = vim_strchr(var + 1, '%');
3963 if (p != NULL)
3964 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003965 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 exp = mch_getenv(NameBuff);
3967 if (exp != NULL && *exp != NUL
3968 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3969 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003970 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 }
3973 }
3974 }
3975
Bram Moolenaar48340b62017-08-29 22:08:53 +02003976 if (var != NULL && *var == NUL) /* empty is same as not set */
3977 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978
Bram Moolenaar48340b62017-08-29 22:08:53 +02003979# ifdef FEAT_MBYTE
Bram Moolenaar05159a02005-02-26 23:04:13 +00003980 if (enc_utf8 && var != NULL)
3981 {
3982 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003983 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003984
3985 /* Convert from active codepage to UTF-8. Other conversions are
3986 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003987 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003988 if (pp != NULL)
3989 {
3990 homedir = pp;
3991 return;
3992 }
3993 }
3994# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 /*
3997 * Default home dir is C:/
3998 * Best assumption we can make in such a situation.
3999 */
4000 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004001 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004003
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 if (var != NULL)
4005 {
4006#ifdef UNIX
4007 /*
4008 * Change to the directory and get the actual path. This resolves
4009 * links. Don't do it when we can't return.
4010 */
4011 if (mch_dirname(NameBuff, MAXPATHL) == OK
4012 && mch_chdir((char *)NameBuff) == 0)
4013 {
4014 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
4015 var = IObuff;
4016 if (mch_chdir((char *)NameBuff) != 0)
4017 EMSG(_(e_prev_dir));
4018 }
4019#endif
4020 homedir = vim_strsave(var);
4021 }
4022}
4023
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004024#if defined(EXITFREE) || defined(PROTO)
4025 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004026free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004027{
4028 vim_free(homedir);
4029}
Bram Moolenaar24305862012-08-15 14:05:05 +02004030
4031# ifdef FEAT_CMDL_COMPL
4032 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004033free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02004034{
4035 ga_clear_strings(&ga_users);
4036}
4037# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004038#endif
4039
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004041 * Call expand_env() and store the result in an allocated string.
4042 * This is not very memory efficient, this expects the result to be freed
4043 * again soon.
4044 */
4045 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004046expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004047{
4048 return expand_env_save_opt(src, FALSE);
4049}
4050
4051/*
4052 * Idem, but when "one" is TRUE handle the string as one file name, only
4053 * expand "~" at the start.
4054 */
4055 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004056expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004057{
4058 char_u *p;
4059
4060 p = alloc(MAXPATHL);
4061 if (p != NULL)
4062 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
4063 return p;
4064}
4065
4066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 * Expand environment variable with path name.
4068 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004069 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 * If anything fails no expansion is done and dst equals src.
4071 */
4072 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004073expand_env(
4074 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
4075 char_u *dst, /* where to put the result */
4076 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004078 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079}
4080
4081 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004082expand_env_esc(
4083 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
4084 char_u *dst, /* where to put the result */
4085 int dstlen, /* maximum length of the result */
4086 int esc, /* escape spaces in expanded variables */
4087 int one, /* "srcp" is one file name */
4088 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004090 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 char_u *tail;
4092 int c;
4093 char_u *var;
4094 int copy_char;
4095 int mustfree; /* var was allocated, need to free it later */
4096 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004097 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004099 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004100 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004101
4102 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 --dstlen; /* leave one char space for "\," */
4104 while (*src && dstlen > 0)
4105 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004106#ifdef FEAT_EVAL
4107 /* Skip over `=expr`. */
4108 if (src[0] == '`' && src[1] == '=')
4109 {
4110 size_t len;
4111
4112 var = src;
4113 src += 2;
4114 (void)skip_expr(&src);
4115 if (*src == '`')
4116 ++src;
4117 len = src - var;
4118 if (len > (size_t)dstlen)
4119 len = dstlen;
4120 vim_strncpy(dst, var, len);
4121 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02004122 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004123 continue;
4124 }
4125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004127 if ((*src == '$'
4128#ifdef VMS
4129 && at_start
4130#endif
4131 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004132#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 || *src == '%'
4134#endif
4135 || (*src == '~' && at_start))
4136 {
4137 mustfree = FALSE;
4138
4139 /*
4140 * The variable name is copied into dst temporarily, because it may
4141 * be a string in read-only memory and a NUL needs to be appended.
4142 */
4143 if (*src != '~') /* environment var */
4144 {
4145 tail = src + 1;
4146 var = dst;
4147 c = dstlen - 1;
4148
4149#ifdef UNIX
4150 /* Unix has ${var-name} type environment vars */
4151 if (*tail == '{' && !vim_isIDc('{'))
4152 {
4153 tail++; /* ignore '{' */
4154 while (c-- > 0 && *tail && *tail != '}')
4155 *var++ = *tail++;
4156 }
4157 else
4158#endif
4159 {
4160 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004161#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 || (*src == '%' && *tail != '%')
4163#endif
4164 ))
4165 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 }
4168 }
4169
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004170#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171# ifdef UNIX
4172 if (src[1] == '{' && *tail != '}')
4173# else
4174 if (*src == '%' && *tail != '%')
4175# endif
4176 var = NULL;
4177 else
4178 {
4179# ifdef UNIX
4180 if (src[1] == '{')
4181# else
4182 if (*src == '%')
4183#endif
4184 ++tail;
4185#endif
4186 *var = NUL;
4187 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004188#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 }
4190#endif
4191 }
4192 /* home directory */
4193 else if ( src[1] == NUL
4194 || vim_ispathsep(src[1])
4195 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4196 {
4197 var = homedir;
4198 tail = src + 1;
4199 }
4200 else /* user directory */
4201 {
4202#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4203 /*
4204 * Copy ~user to dst[], so we can put a NUL after it.
4205 */
4206 tail = src;
4207 var = dst;
4208 c = dstlen - 1;
4209 while ( c-- > 0
4210 && *tail
4211 && vim_isfilec(*tail)
4212 && !vim_ispathsep(*tail))
4213 *var++ = *tail++;
4214 *var = NUL;
4215# ifdef UNIX
4216 /*
4217 * If the system supports getpwnam(), use it.
4218 * Otherwise, or if getpwnam() fails, the shell is used to
4219 * expand ~user. This is slower and may fail if the shell
4220 * does not support ~user (old versions of /bin/sh).
4221 */
4222# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4223 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004224 /* Note: memory allocated by getpwnam() is never freed.
4225 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01004226 struct passwd *pw = (*dst == NUL)
4227 ? NULL : getpwnam((char *)dst + 1);
4228
4229 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 }
4231 if (var == NULL)
4232# endif
4233 {
4234 expand_T xpc;
4235
4236 ExpandInit(&xpc);
4237 xpc.xp_context = EXPAND_FILES;
4238 var = ExpandOne(&xpc, dst, NULL,
4239 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 mustfree = TRUE;
4241 }
4242
4243# else /* !UNIX, thus VMS */
4244 /*
4245 * USER_HOME is a comma-separated list of
4246 * directories to search for the user account in.
4247 */
4248 {
4249 char_u test[MAXPATHL], paths[MAXPATHL];
4250 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004251 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252
4253 STRCPY(paths, USER_HOME);
4254 next_path = paths;
4255 while (*next_path)
4256 {
4257 for (path = next_path; *next_path && *next_path != ',';
4258 next_path++);
4259 if (*next_path)
4260 *next_path++ = NUL;
4261 STRCPY(test, path);
4262 STRCAT(test, "/");
4263 STRCAT(test, dst + 1);
4264 if (mch_stat(test, &st) == 0)
4265 {
4266 var = alloc(STRLEN(test) + 1);
4267 STRCPY(var, test);
4268 mustfree = TRUE;
4269 break;
4270 }
4271 }
4272 }
4273# endif /* UNIX */
4274#else
4275 /* cannot expand user's home directory, so don't try */
4276 var = NULL;
4277 tail = (char_u *)""; /* for gcc */
4278#endif /* UNIX || VMS */
4279 }
4280
4281#ifdef BACKSLASH_IN_FILENAME
4282 /* If 'shellslash' is set change backslashes to forward slashes.
4283 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4284 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4285 {
4286 char_u *p = vim_strsave(var);
4287
4288 if (p != NULL)
4289 {
4290 if (mustfree)
4291 vim_free(var);
4292 var = p;
4293 mustfree = TRUE;
4294 forward_slash(var);
4295 }
4296 }
4297#endif
4298
4299 /* If "var" contains white space, escape it with a backslash.
4300 * Required for ":e ~/tt" when $HOME includes a space. */
4301 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4302 {
4303 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4304
4305 if (p != NULL)
4306 {
4307 if (mustfree)
4308 vim_free(var);
4309 var = p;
4310 mustfree = TRUE;
4311 }
4312 }
4313
4314 if (var != NULL && *var != NUL
4315 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4316 {
4317 STRCPY(dst, var);
4318 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004319 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 /* if var[] ends in a path separator and tail[] starts
4321 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004322 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4324 && dst[-1] != ':'
4325#endif
4326 && vim_ispathsep(*tail))
4327 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004328 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 src = tail;
4330 copy_char = FALSE;
4331 }
4332 if (mustfree)
4333 vim_free(var);
4334 }
4335
4336 if (copy_char) /* copy at least one char */
4337 {
4338 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004339 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004340 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4341 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 */
4343 at_start = FALSE;
4344 if (src[0] == '\\' && src[1] != NUL)
4345 {
4346 *dst++ = *src++;
4347 --dstlen;
4348 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004349 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02004351 if (dstlen > 0)
4352 {
4353 *dst++ = *src++;
4354 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004355
Bram Moolenaar1c864092017-08-06 18:15:45 +02004356 if (startstr != NULL && src - startstr_len >= srcp
4357 && STRNCMP(src - startstr_len, startstr,
4358 startstr_len) == 0)
4359 at_start = TRUE;
4360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02004362
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 }
4364 *dst = NUL;
4365}
4366
4367/*
4368 * Vim's version of getenv().
4369 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004370 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004371 * "mustfree" is set to TRUE when returned is allocated, it must be
4372 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 */
4374 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004375vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376{
4377 char_u *p;
4378 char_u *pend;
4379 int vimruntime;
4380
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004381#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 /* use "C:/" when $HOME is not set */
4383 if (STRCMP(name, "HOME") == 0)
4384 return homedir;
4385#endif
4386
4387 p = mch_getenv(name);
4388 if (p != NULL && *p == NUL) /* empty is the same as not set */
4389 p = NULL;
4390
4391 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004392 {
4393#if defined(FEAT_MBYTE) && defined(WIN3264)
4394 if (enc_utf8)
4395 {
4396 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004397 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004398
4399 /* Convert from active codepage to UTF-8. Other conversions are
4400 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004401 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004402 if (pp != NULL)
4403 {
4404 p = pp;
4405 *mustfree = TRUE;
4406 }
4407 }
4408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411
4412 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4413 if (!vimruntime && STRCMP(name, "VIM") != 0)
4414 return NULL;
4415
4416 /*
4417 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4418 * Don't do this when default_vimruntime_dir is non-empty.
4419 */
4420 if (vimruntime
4421#ifdef HAVE_PATHDEF
4422 && *default_vimruntime_dir == NUL
4423#endif
4424 )
4425 {
4426 p = mch_getenv((char_u *)"VIM");
4427 if (p != NULL && *p == NUL) /* empty is the same as not set */
4428 p = NULL;
4429 if (p != NULL)
4430 {
4431 p = vim_version_dir(p);
4432 if (p != NULL)
4433 *mustfree = TRUE;
4434 else
4435 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004436
4437#if defined(FEAT_MBYTE) && defined(WIN3264)
4438 if (enc_utf8)
4439 {
4440 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004441 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004442
4443 /* Convert from active codepage to UTF-8. Other conversions
4444 * are not done, because they would fail for non-ASCII
4445 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004446 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004447 if (pp != NULL)
4448 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004449 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004450 vim_free(p);
4451 p = pp;
4452 *mustfree = TRUE;
4453 }
4454 }
4455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 }
4457 }
4458
4459 /*
4460 * When expanding $VIM or $VIMRUNTIME fails, try using:
4461 * - the directory name from 'helpfile' (unless it contains '$')
4462 * - the executable name from argv[0]
4463 */
4464 if (p == NULL)
4465 {
4466 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4467 p = p_hf;
4468#ifdef USE_EXE_NAME
4469 /*
4470 * Use the name of the executable, obtained from argv[0].
4471 */
4472 else
4473 p = exe_name;
4474#endif
4475 if (p != NULL)
4476 {
4477 /* remove the file name */
4478 pend = gettail(p);
4479
4480 /* remove "doc/" from 'helpfile', if present */
4481 if (p == p_hf)
4482 pend = remove_tail(p, pend, (char_u *)"doc");
4483
4484#ifdef USE_EXE_NAME
4485# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004486 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 if (p == exe_name)
4488 {
4489 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004490 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004492 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4493 if (pend1 != pend)
4494 {
4495 pnew = alloc((unsigned)(pend1 - p) + 15);
4496 if (pnew != NULL)
4497 {
4498 STRNCPY(pnew, p, (pend1 - p));
4499 STRCPY(pnew + (pend1 - p), "Resources/vim");
4500 p = pnew;
4501 pend = p + STRLEN(p);
4502 }
4503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 }
4505# endif
4506 /* remove "src/" from exe_name, if present */
4507 if (p == exe_name)
4508 pend = remove_tail(p, pend, (char_u *)"src");
4509#endif
4510
4511 /* for $VIM, remove "runtime/" or "vim54/", if present */
4512 if (!vimruntime)
4513 {
4514 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4515 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4516 }
4517
4518 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004519 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004522#ifdef MACOS_X
4523 if (p == exe_name || p == p_hf)
4524#endif
4525 /* check that the result is a directory name */
4526 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527
4528 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01004529 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 else
4531 {
4532#ifdef USE_EXE_NAME
4533 /* may add "/vim54" or "/runtime" if it exists */
4534 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4535 {
4536 vim_free(p);
4537 p = pend;
4538 }
4539#endif
4540 *mustfree = TRUE;
4541 }
4542 }
4543 }
4544
4545#ifdef HAVE_PATHDEF
4546 /* When there is a pathdef.c file we can use default_vim_dir and
4547 * default_vimruntime_dir */
4548 if (p == NULL)
4549 {
4550 /* Only use default_vimruntime_dir when it is not empty */
4551 if (vimruntime && *default_vimruntime_dir != NUL)
4552 {
4553 p = default_vimruntime_dir;
4554 *mustfree = FALSE;
4555 }
4556 else if (*default_vim_dir != NUL)
4557 {
4558 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4559 *mustfree = TRUE;
4560 else
4561 {
4562 p = default_vim_dir;
4563 *mustfree = FALSE;
4564 }
4565 }
4566 }
4567#endif
4568
4569 /*
4570 * Set the environment variable, so that the new value can be found fast
4571 * next time, and others can also use it (e.g. Perl).
4572 */
4573 if (p != NULL)
4574 {
4575 if (vimruntime)
4576 {
4577 vim_setenv((char_u *)"VIMRUNTIME", p);
4578 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 }
4580 else
4581 {
4582 vim_setenv((char_u *)"VIM", p);
4583 didset_vim = TRUE;
4584 }
4585 }
4586 return p;
4587}
4588
4589/*
4590 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4591 * Return NULL if not, return its name in allocated memory otherwise.
4592 */
4593 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004594vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595{
4596 char_u *p;
4597
4598 if (vimdir == NULL || *vimdir == NUL)
4599 return NULL;
4600 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4601 if (p != NULL && mch_isdir(p))
4602 return p;
4603 vim_free(p);
4604 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4605 if (p != NULL && mch_isdir(p))
4606 return p;
4607 vim_free(p);
4608 return NULL;
4609}
4610
4611/*
4612 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4613 * the length of "name/". Otherwise return "pend".
4614 */
4615 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004616remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617{
4618 int len = (int)STRLEN(name) + 1;
4619 char_u *newend = pend - len;
4620
4621 if (newend >= p
4622 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004623 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 return newend;
4625 return pend;
4626}
4627
Bram Moolenaar137374f2018-05-13 15:59:50 +02004628 void
4629vim_unsetenv(char_u *var)
4630{
4631#ifdef HAVE_UNSETENV
4632 unsetenv((char *)var);
4633#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02004634 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02004635#endif
4636}
4637
4638
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 * Our portable version of setenv.
4641 */
4642 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004643vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644{
4645#ifdef HAVE_SETENV
4646 mch_setenv((char *)name, (char *)val, 1);
4647#else
4648 char_u *envbuf;
4649
4650 /*
4651 * Putenv does not copy the string, it has to remain
4652 * valid. The allocated memory will never be freed.
4653 */
4654 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4655 if (envbuf != NULL)
4656 {
4657 sprintf((char *)envbuf, "%s=%s", name, val);
4658 putenv((char *)envbuf);
4659 }
4660#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004661#ifdef FEAT_GETTEXT
4662 /*
4663 * When setting $VIMRUNTIME adjust the directory to find message
4664 * translations to $VIMRUNTIME/lang.
4665 */
4666 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4667 {
4668 char_u *buf = concat_str(val, (char_u *)"/lang");
4669
4670 if (buf != NULL)
4671 {
4672 bindtextdomain(VIMPACKAGE, (char *)buf);
4673 vim_free(buf);
4674 }
4675 }
4676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677}
4678
4679#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4680/*
4681 * Function given to ExpandGeneric() to obtain an environment variable name.
4682 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004684get_env_name(
4685 expand_T *xp UNUSED,
4686 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687{
Bram Moolenaard0573012017-10-28 21:11:06 +02004688# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02004690 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 */
4692 return NULL;
4693# else
4694# ifndef __WIN32__
4695 /* Borland C++ 5.2 has this in a header file. */
4696 extern char **environ;
4697# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004698# define ENVNAMELEN 100
4699 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 char_u *str;
4701 int n;
4702
4703 str = (char_u *)environ[idx];
4704 if (str == NULL)
4705 return NULL;
4706
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004707 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 {
4709 if (str[n] == '=' || str[n] == NUL)
4710 break;
4711 name[n] = str[n];
4712 }
4713 name[n] = NUL;
4714 return name;
4715# endif
4716}
Bram Moolenaar24305862012-08-15 14:05:05 +02004717
4718/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004719 * Add a user name to the list of users in ga_users.
4720 * Do nothing if user name is NULL or empty.
4721 */
4722 static void
4723add_user(char_u *user, int need_copy)
4724{
4725 char_u *user_copy = (user != NULL && need_copy)
4726 ? vim_strsave(user) : user;
4727
4728 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
4729 {
4730 if (need_copy)
4731 vim_free(user);
4732 return;
4733 }
4734 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
4735}
4736
4737/*
Bram Moolenaar24305862012-08-15 14:05:05 +02004738 * Find all user names for user completion.
4739 * Done only once and then cached.
4740 */
4741 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004742init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004743{
Bram Moolenaar24305862012-08-15 14:05:05 +02004744 static int lazy_init_done = FALSE;
4745
4746 if (lazy_init_done)
4747 return;
4748
4749 lazy_init_done = TRUE;
4750 ga_init2(&ga_users, sizeof(char_u *), 20);
4751
4752# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4753 {
Bram Moolenaar24305862012-08-15 14:05:05 +02004754 struct passwd* pw;
4755
4756 setpwent();
4757 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004758 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02004759 endpwent();
4760 }
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004761# elif defined(WIN3264)
4762 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004763 DWORD nusers = 0, ntotal = 0, i;
4764 PUSER_INFO_0 uinfo;
4765
4766 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
4767 &nusers, &ntotal, NULL) == NERR_Success)
4768 {
4769 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004770 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004771
4772 NetApiBufferFree(uinfo);
4773 }
4774 }
Bram Moolenaar24305862012-08-15 14:05:05 +02004775# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004776# if defined(HAVE_GETPWNAM)
4777 {
4778 char_u *user_env = mch_getenv((char_u *)"USER");
4779
4780 // The $USER environment variable may be a valid remote user name (NIS,
4781 // LDAP) not already listed by getpwent(), as getpwent() only lists
4782 // local user names. If $USER is not already listed, check whether it
4783 // is a valid remote user name using getpwnam() and if it is, add it to
4784 // the list of user names.
4785
4786 if (user_env != NULL && *user_env != NUL)
4787 {
4788 int i;
4789
4790 for (i = 0; i < ga_users.ga_len; i++)
4791 {
4792 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
4793
4794 if (STRCMP(local_user, user_env) == 0)
4795 break;
4796 }
4797
4798 if (i == ga_users.ga_len)
4799 {
4800 struct passwd *pw = getpwnam((char *)user_env);
4801
4802 if (pw != NULL)
4803 add_user((char_u *)pw->pw_name, TRUE);
4804 }
4805 }
4806 }
4807# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02004808}
4809
4810/*
4811 * Function given to ExpandGeneric() to obtain an user names.
4812 */
4813 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004814get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004815{
4816 init_users();
4817 if (idx < ga_users.ga_len)
4818 return ((char_u **)ga_users.ga_data)[idx];
4819 return NULL;
4820}
4821
4822/*
4823 * Check whether name matches a user name. Return:
4824 * 0 if name does not match any user name.
4825 * 1 if name partially matches the beginning of a user name.
4826 * 2 is name fully matches a user name.
4827 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02004828 int
4829match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004830{
4831 int i;
4832 int n = (int)STRLEN(name);
4833 int result = 0;
4834
4835 init_users();
4836 for (i = 0; i < ga_users.ga_len; i++)
4837 {
4838 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4839 return 2; /* full match */
4840 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4841 result = 1; /* partial match */
4842 }
4843 return result;
4844}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845#endif
4846
4847/*
4848 * Replace home directory by "~" in each space or comma separated file name in
4849 * 'src'.
4850 * If anything fails (except when out of space) dst equals src.
4851 */
4852 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004853home_replace(
4854 buf_T *buf, /* when not NULL, check for help files */
4855 char_u *src, /* input file name */
4856 char_u *dst, /* where to put the result */
4857 int dstlen, /* maximum length of the result */
4858 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 spaces and commas in the file name. */
4860{
4861 size_t dirlen = 0, envlen = 0;
4862 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004863 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 char_u *p;
4865
4866 if (src == NULL)
4867 {
4868 *dst = NUL;
4869 return;
4870 }
4871
4872 /*
4873 * If the file is a help file, remove the path completely.
4874 */
4875 if (buf != NULL && buf->b_help)
4876 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02004877 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 return;
4879 }
4880
4881 /*
4882 * We check both the value of the $HOME environment variable and the
4883 * "real" home directory.
4884 */
4885 if (homedir != NULL)
4886 dirlen = STRLEN(homedir);
4887
4888#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004889 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004891 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4892#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004893#ifdef WIN3264
4894 if (homedir_env == NULL)
4895 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
4896#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004897 /* Empty is the same as not set. */
4898 if (homedir_env != NULL && *homedir_env == NUL)
4899 homedir_env = NULL;
4900
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004901#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004902 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004903 {
4904 int usedlen = 0;
4905 int flen;
4906 char_u *fbuf = NULL;
4907
4908 flen = (int)STRLEN(homedir_env);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004909 (void)modify_fname((char_u *)":p", FALSE, &usedlen,
Bram Moolenaard12f8112012-06-20 17:56:09 +02004910 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004911 flen = (int)STRLEN(homedir_env);
4912 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4913 /* Remove the trailing / that is added to a directory. */
4914 homedir_env[flen - 1] = NUL;
4915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916#endif
4917
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 if (homedir_env != NULL)
4919 envlen = STRLEN(homedir_env);
4920
4921 if (!one)
4922 src = skipwhite(src);
4923 while (*src && dstlen > 0)
4924 {
4925 /*
4926 * Here we are at the beginning of a file name.
4927 * First, check to see if the beginning of the file name matches
4928 * $HOME or the "real" home directory. Check that there is a '/'
4929 * after the match (so that if e.g. the file is "/home/pieter/bla",
4930 * and the home directory is "/home/piet", the file does not end up
4931 * as "~er/bla" (which would seem to indicate the file "bla" in user
4932 * er's home directory)).
4933 */
4934 p = homedir;
4935 len = dirlen;
4936 for (;;)
4937 {
4938 if ( len
4939 && fnamencmp(src, p, len) == 0
4940 && (vim_ispathsep(src[len])
4941 || (!one && (src[len] == ',' || src[len] == ' '))
4942 || src[len] == NUL))
4943 {
4944 src += len;
4945 if (--dstlen > 0)
4946 *dst++ = '~';
4947
4948 /*
4949 * If it's just the home directory, add "/".
4950 */
4951 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4952 *dst++ = '/';
4953 break;
4954 }
4955 if (p == homedir_env)
4956 break;
4957 p = homedir_env;
4958 len = envlen;
4959 }
4960
4961 /* if (!one) skip to separator: space or comma */
4962 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4963 *dst++ = *src++;
4964 /* skip separator */
4965 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4966 *dst++ = *src++;
4967 }
4968 /* if (dstlen == 0) out of space, what to do??? */
4969
4970 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004971
4972 if (homedir_env != homedir_env_orig)
4973 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974}
4975
4976/*
4977 * Like home_replace, store the replaced string in allocated memory.
4978 * When something fails, NULL is returned.
4979 */
4980 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004981home_replace_save(
4982 buf_T *buf, /* when not NULL, check for help files */
4983 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984{
4985 char_u *dst;
4986 unsigned len;
4987
4988 len = 3; /* space for "~/" and trailing NUL */
4989 if (src != NULL) /* just in case */
4990 len += (unsigned)STRLEN(src);
4991 dst = alloc(len);
4992 if (dst != NULL)
4993 home_replace(buf, src, dst, len, TRUE);
4994 return dst;
4995}
4996
4997/*
4998 * Compare two file names and return:
4999 * FPC_SAME if they both exist and are the same file.
5000 * FPC_SAMEX if they both don't exist and have the same file name.
5001 * FPC_DIFF if they both exist and are different files.
5002 * FPC_NOTX if they both don't exist.
5003 * FPC_DIFFX if one of them doesn't exist.
5004 * For the first name environment variables are expanded
5005 */
5006 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005007fullpathcmp(
5008 char_u *s1,
5009 char_u *s2,
5010 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011{
5012#ifdef UNIX
5013 char_u exp1[MAXPATHL];
5014 char_u full1[MAXPATHL];
5015 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02005016 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 int r1, r2;
5018
5019 expand_env(s1, exp1, MAXPATHL);
5020 r1 = mch_stat((char *)exp1, &st1);
5021 r2 = mch_stat((char *)s2, &st2);
5022 if (r1 != 0 && r2 != 0)
5023 {
5024 /* if mch_stat() doesn't work, may compare the names */
5025 if (checkname)
5026 {
5027 if (fnamecmp(exp1, s2) == 0)
5028 return FPC_SAMEX;
5029 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
5030 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
5031 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
5032 return FPC_SAMEX;
5033 }
5034 return FPC_NOTX;
5035 }
5036 if (r1 != 0 || r2 != 0)
5037 return FPC_DIFFX;
5038 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
5039 return FPC_SAME;
5040 return FPC_DIFF;
5041#else
5042 char_u *exp1; /* expanded s1 */
5043 char_u *full1; /* full path of s1 */
5044 char_u *full2; /* full path of s2 */
5045 int retval = FPC_DIFF;
5046 int r1, r2;
5047
5048 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
5049 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
5050 {
5051 full1 = exp1 + MAXPATHL;
5052 full2 = full1 + MAXPATHL;
5053
5054 expand_env(s1, exp1, MAXPATHL);
5055 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
5056 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
5057
5058 /* If vim_FullName() fails, the file probably doesn't exist. */
5059 if (r1 != OK && r2 != OK)
5060 {
5061 if (checkname && fnamecmp(exp1, s2) == 0)
5062 retval = FPC_SAMEX;
5063 else
5064 retval = FPC_NOTX;
5065 }
5066 else if (r1 != OK || r2 != OK)
5067 retval = FPC_DIFFX;
5068 else if (fnamecmp(full1, full2))
5069 retval = FPC_DIFF;
5070 else
5071 retval = FPC_SAME;
5072 vim_free(exp1);
5073 }
5074 return retval;
5075#endif
5076}
5077
5078/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005079 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02005080 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005081 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 */
5083 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005084gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085{
5086 char_u *p1, *p2;
5087
5088 if (fname == NULL)
5089 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01005090 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01005092 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005094 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 }
5096 return p1;
5097}
5098
Bram Moolenaar31710262010-08-13 13:36:15 +02005099#if defined(FEAT_SEARCHPATH)
Bram Moolenaar31710262010-08-13 13:36:15 +02005100/*
5101 * Return the end of the directory name, on the first path
5102 * separator:
5103 * "/path/file", "/path/dir/", "/path//dir", "/file"
5104 * ^ ^ ^ ^
5105 */
5106 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005107gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02005108{
5109 char_u *dir_end = fname;
5110 char_u *next_dir_end = fname;
5111 int look_for_sep = TRUE;
5112 char_u *p;
5113
5114 for (p = fname; *p != NUL; )
5115 {
5116 if (vim_ispathsep(*p))
5117 {
5118 if (look_for_sep)
5119 {
5120 next_dir_end = p;
5121 look_for_sep = FALSE;
5122 }
5123 }
5124 else
5125 {
5126 if (!look_for_sep)
5127 dir_end = next_dir_end;
5128 look_for_sep = TRUE;
5129 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005130 MB_PTR_ADV(p);
Bram Moolenaar31710262010-08-13 13:36:15 +02005131 }
5132 return dir_end;
5133}
5134#endif
5135
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005137 * Get pointer to tail of "fname", including path separators. Putting a NUL
5138 * here leaves the directory name. Takes care of "c:/" and "//".
5139 * Always returns a valid pointer.
5140 */
5141 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005142gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005143{
5144 char_u *p;
5145 char_u *t;
5146
5147 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
5148 t = gettail(fname);
5149 while (t > p && after_pathsep(fname, t))
5150 --t;
5151#ifdef VMS
5152 /* path separator is part of the path */
5153 ++t;
5154#endif
5155 return t;
5156}
5157
5158/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 * get the next path component (just after the next path separator).
5160 */
5161 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005162getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163{
5164 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005165 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 if (*fname)
5167 ++fname;
5168 return fname;
5169}
5170
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171/*
5172 * Get a pointer to one character past the head of a path name.
5173 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
5174 * If there is no head, path is returned.
5175 */
5176 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005177get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178{
5179 char_u *retval;
5180
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005181#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 /* may skip "c:" */
5183 if (isalpha(path[0]) && path[1] == ':')
5184 retval = path + 2;
5185 else
5186 retval = path;
5187#else
5188# if defined(AMIGA)
5189 /* may skip "label:" */
5190 retval = vim_strchr(path, ':');
5191 if (retval == NULL)
5192 retval = path;
5193# else /* Unix */
5194 retval = path;
5195# endif
5196#endif
5197
5198 while (vim_ispathsep(*retval))
5199 ++retval;
5200
5201 return retval;
5202}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203
5204/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01005205 * Return TRUE if 'c' is a path separator.
5206 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 */
5208 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005209vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210{
Bram Moolenaare60acc12011-05-10 16:41:25 +02005211#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02005213#else
5214# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005216# else
5217# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
5219 return (c == ':' || c == '[' || c == ']' || c == '/'
5220 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02005221# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005223# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02005225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226}
5227
Bram Moolenaar69c35002013-11-04 02:54:12 +01005228/*
5229 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
5230 */
5231 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005232vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01005233{
5234 return vim_ispathsep(c)
5235#ifdef BACKSLASH_IN_FILENAME
5236 && c != ':'
5237#endif
5238 ;
5239}
5240
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5242/*
5243 * return TRUE if 'c' is a path list separator.
5244 */
5245 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005246vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247{
5248#ifdef UNIX
5249 return (c == ':');
5250#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005251 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252#endif
5253}
5254#endif
5255
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005256/*
5257 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5258 * It's done in-place.
5259 */
5260 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005261shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005262{
5263 char_u *tail, *s, *d;
5264 int skip = FALSE;
5265
5266 tail = gettail(str);
5267 d = str;
5268 for (s = str; ; ++s)
5269 {
5270 if (s >= tail) /* copy the whole tail */
5271 {
5272 *d++ = *s;
5273 if (*s == NUL)
5274 break;
5275 }
5276 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5277 {
5278 *d++ = *s;
5279 skip = FALSE;
5280 }
5281 else if (!skip)
5282 {
5283 *d++ = *s; /* copy next char */
5284 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5285 skip = TRUE;
5286# ifdef FEAT_MBYTE
5287 if (has_mbyte)
5288 {
5289 int l = mb_ptr2len(s);
5290
5291 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005292 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005293 }
5294# endif
5295 }
5296 }
5297}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005298
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005299/*
5300 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5301 * Also returns TRUE if there is no directory name.
5302 * "fname" must be writable!.
5303 */
5304 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005305dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005306{
5307 char_u *p;
5308 int c;
5309 int retval;
5310
5311 p = gettail_sep(fname);
5312 if (p == fname)
5313 return TRUE;
5314 c = *p;
5315 *p = NUL;
5316 retval = mch_isdir(fname);
5317 *p = c;
5318 return retval;
5319}
5320
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005322 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5323 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 */
5325 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005326vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005328#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005330#else
5331 if (p_fic)
5332 return MB_STRICMP(x, y);
5333 return STRCMP(x, y);
5334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335}
5336
5337 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005338vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005340#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005341 char_u *px = x;
5342 char_u *py = y;
5343 int cx = NUL;
5344 int cy = NUL;
5345
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005346 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005348 cx = PTR2CHAR(px);
5349 cy = PTR2CHAR(py);
5350 if (cx == NUL || cy == NUL
5351 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5352 && !(cx == '/' && cy == '\\')
5353 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005355 len -= MB_PTR2LEN(px);
5356 px += MB_PTR2LEN(px);
5357 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 }
5359 if (len == 0)
5360 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005361 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005362#else
5363 if (p_fic)
5364 return MB_STRNICMP(x, y, len);
5365 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005367}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368
5369/*
5370 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005371 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 */
5373 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005374concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375{
5376 char_u *dest;
5377
5378 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5379 if (dest != NULL)
5380 {
5381 STRCPY(dest, fname1);
5382 if (sep)
5383 add_pathsep(dest);
5384 STRCAT(dest, fname2);
5385 }
5386 return dest;
5387}
5388
Bram Moolenaard6754642005-01-17 22:18:45 +00005389/*
5390 * Concatenate two strings and return the result in allocated memory.
5391 * Returns NULL when out of memory.
5392 */
5393 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005394concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005395{
5396 char_u *dest;
5397 size_t l = STRLEN(str1);
5398
5399 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5400 if (dest != NULL)
5401 {
5402 STRCPY(dest, str1);
5403 STRCPY(dest + l, str2);
5404 }
5405 return dest;
5406}
Bram Moolenaard6754642005-01-17 22:18:45 +00005407
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408/*
5409 * Add a path separator to a file name, unless it already ends in a path
5410 * separator.
5411 */
5412 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005413add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005415 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 STRCAT(p, PATHSEPSTR);
5417}
5418
5419/*
5420 * FullName_save - Make an allocated copy of a full file name.
5421 * Returns NULL when out of memory.
5422 */
5423 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005424FullName_save(
5425 char_u *fname,
5426 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005427 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428{
5429 char_u *buf;
5430 char_u *new_fname = NULL;
5431
5432 if (fname == NULL)
5433 return NULL;
5434
5435 buf = alloc((unsigned)MAXPATHL);
5436 if (buf != NULL)
5437 {
5438 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5439 new_fname = vim_strsave(buf);
5440 else
5441 new_fname = vim_strsave(fname);
5442 vim_free(buf);
5443 }
5444 return new_fname;
5445}
5446
5447#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5448
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005449static char_u *skip_string(char_u *p);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005450static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451
5452/*
5453 * Find the start of a comment, not knowing if we are in a comment right now.
5454 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005455 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005457 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005458ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005459{
5460 return find_start_comment(curbuf->b_ind_maxcomment);
5461}
5462
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005464find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465{
5466 pos_T *pos;
5467 char_u *line;
5468 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005469 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005471 for (;;)
5472 {
5473 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5474 if (pos == NULL)
5475 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005477 /*
5478 * Check if the comment start we found is inside a string.
5479 * If it is then restrict the search to below this line and try again.
5480 */
5481 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005482 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005483 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005484 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005485 break;
5486 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5487 if (cur_maxcomment <= 0)
5488 {
5489 pos = NULL;
5490 break;
5491 }
5492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 return pos;
5494}
5495
5496/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005497 * Find the start of a comment or raw string, not knowing if we are in a
5498 * comment or raw string right now.
5499 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaardde81312017-08-26 17:49:01 +02005500 * If is_raw is given and returns start of raw_string, sets it to true.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005501 * Return NULL when not inside a comment or raw string.
5502 * "CORS" -> Comment Or Raw String
5503 */
5504 static pos_T *
Bram Moolenaardde81312017-08-26 17:49:01 +02005505ind_find_start_CORS(linenr_T *is_raw) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005506{
Bram Moolenaar089af182015-10-07 11:41:49 +02005507 static pos_T comment_pos_copy;
5508 pos_T *comment_pos;
5509 pos_T *rs_pos;
5510
5511 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5512 if (comment_pos != NULL)
5513 {
5514 /* Need to make a copy of the static pos in findmatchlimit(),
5515 * calling find_start_rawstring() may change it. */
5516 comment_pos_copy = *comment_pos;
5517 comment_pos = &comment_pos_copy;
5518 }
5519 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005520
5521 /* If comment_pos is before rs_pos the raw string is inside the comment.
5522 * If rs_pos is before comment_pos the comment is inside the raw string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005523 if (comment_pos == NULL || (rs_pos != NULL
5524 && LT_POS(*rs_pos, *comment_pos)))
Bram Moolenaardde81312017-08-26 17:49:01 +02005525 {
5526 if (is_raw != NULL && rs_pos != NULL)
5527 *is_raw = rs_pos->lnum;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005528 return rs_pos;
Bram Moolenaardde81312017-08-26 17:49:01 +02005529 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005530 return comment_pos;
5531}
5532
5533/*
5534 * Find the start of a raw string, not knowing if we are in one right now.
5535 * Search starts at w_cursor.lnum and goes backwards.
5536 * Return NULL when not inside a raw string.
5537 */
5538 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005539find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005540{
5541 pos_T *pos;
5542 char_u *line;
5543 char_u *p;
5544 int cur_maxcomment = ind_maxcomment;
5545
5546 for (;;)
5547 {
5548 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5549 if (pos == NULL)
5550 break;
5551
5552 /*
5553 * Check if the raw string start we found is inside a string.
5554 * If it is then restrict the search to below this line and try again.
5555 */
5556 line = ml_get(pos->lnum);
5557 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5558 p = skip_string(p);
5559 if ((colnr_T)(p - line) <= pos->col)
5560 break;
5561 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5562 if (cur_maxcomment <= 0)
5563 {
5564 pos = NULL;
5565 break;
5566 }
5567 }
5568 return pos;
5569}
5570
5571/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 * Skip to the end of a "string" and a 'c' character.
5573 * If there is no string or character, return argument unmodified.
5574 */
5575 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005576skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577{
5578 int i;
5579
5580 /*
5581 * We loop, because strings may be concatenated: "date""time".
5582 */
5583 for ( ; ; ++p)
5584 {
5585 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5586 {
5587 if (!p[1]) /* ' at end of line */
5588 break;
5589 i = 2;
5590 if (p[1] == '\\') /* '\n' or '\000' */
5591 {
5592 ++i;
5593 while (vim_isdigit(p[i - 1])) /* '\000' */
5594 ++i;
5595 }
5596 if (p[i] == '\'') /* check for trailing ' */
5597 {
5598 p += i;
5599 continue;
5600 }
5601 }
5602 else if (p[0] == '"') /* start of string */
5603 {
5604 for (++p; p[0]; ++p)
5605 {
5606 if (p[0] == '\\' && p[1] != NUL)
5607 ++p;
5608 else if (p[0] == '"') /* end of string */
5609 break;
5610 }
5611 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005612 continue; /* continue for another string */
5613 }
5614 else if (p[0] == 'R' && p[1] == '"')
5615 {
5616 /* Raw string: R"[delim](...)[delim]" */
5617 char_u *delim = p + 2;
5618 char_u *paren = vim_strchr(delim, '(');
5619
5620 if (paren != NULL)
5621 {
5622 size_t delim_len = paren - delim;
5623
5624 for (p += 3; *p; ++p)
5625 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5626 && p[delim_len + 1] == '"')
5627 {
5628 p += delim_len + 1;
5629 break;
5630 }
5631 if (p[0] == '"')
5632 continue; /* continue for another string */
5633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 }
5635 break; /* no string found */
5636 }
5637 if (!*p)
5638 --p; /* backup from NUL */
5639 return p;
5640}
5641#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5642
5643#if defined(FEAT_CINDENT) || defined(PROTO)
5644
5645/*
5646 * Do C or expression indenting on the current line.
5647 */
5648 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005649do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650{
5651# ifdef FEAT_EVAL
5652 if (*curbuf->b_p_inde != NUL)
5653 fixthisline(get_expr_indent);
5654 else
5655# endif
5656 fixthisline(get_c_indent);
5657}
5658
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005659/* Find result cache for cpp_baseclass */
5660typedef struct {
5661 int found;
5662 lpos_T lpos;
5663} cpp_baseclass_cache_T;
5664
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665/*
5666 * Functions for C-indenting.
5667 * Most of this originally comes from Eric Fischer.
5668 */
5669/*
5670 * Below "XXX" means that this function may unlock the current line.
5671 */
5672
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005673static int cin_isdefault(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005674static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005675static int cin_iscomment(char_u *);
5676static int cin_islinecomment(char_u *);
5677static int cin_isterminated(char_u *, int, int);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005678static int cin_iselse(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005679static int cin_ends_in(char_u *, char_u *, char_u *);
5680static int cin_starts_with(char_u *s, char *word);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005681static pos_T *find_match_paren(int);
5682static pos_T *find_match_char(int c, int ind_maxparen);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005683static int find_last_paren(char_u *l, int start, int end);
5684static int find_match(int lookfor, linenr_T ourscope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685
5686/*
5687 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005688 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 */
5690 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005691cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692{
5693 while (*s)
5694 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005695 char_u *prev_s = s;
5696
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005698
5699 /* Perl/shell # comment comment continues until eol. Require a space
5700 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005701 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005702 {
5703 s += STRLEN(s);
5704 break;
5705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 if (*s != '/')
5707 break;
5708 ++s;
5709 if (*s == '/') /* slash-slash comment continues till eol */
5710 {
5711 s += STRLEN(s);
5712 break;
5713 }
5714 if (*s != '*')
5715 break;
5716 for (++s; *s; ++s) /* skip slash-star comment */
5717 if (s[0] == '*' && s[1] == '/')
5718 {
5719 s += 2;
5720 break;
5721 }
5722 }
5723 return s;
5724}
5725
5726/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005727 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 * not considered code.
5729 */
5730 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005731cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732{
5733 return *cin_skipcomment(s) == NUL;
5734}
5735
5736/*
5737 * Check previous lines for a "//" line comment, skipping over blank lines.
5738 */
5739 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005740find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741{
5742 static pos_T pos;
5743 char_u *line;
5744 char_u *p;
5745
5746 pos = curwin->w_cursor;
5747 while (--pos.lnum > 0)
5748 {
5749 line = ml_get(pos.lnum);
5750 p = skipwhite(line);
5751 if (cin_islinecomment(p))
5752 {
5753 pos.col = (int)(p - line);
5754 return &pos;
5755 }
5756 if (*p != NUL)
5757 break;
5758 }
5759 return NULL;
5760}
5761
5762/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005763 * Return TRUE if "text" starts with "key:".
5764 */
5765 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005766cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005767{
5768 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005769 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005770
5771 if (*s == '\'' || *s == '"')
5772 {
5773 /* can be 'key': or "key": */
5774 quote = *s;
5775 ++s;
5776 }
5777 if (!vim_isIDc(*s)) /* need at least one ID character */
5778 return FALSE;
5779
5780 while (vim_isIDc(*s))
5781 ++s;
5782 if (*s == quote)
5783 ++s;
5784
5785 s = cin_skipcomment(s);
5786
5787 /* "::" is not a label, it's C++ */
5788 return (*s == ':' && s[1] != ':');
5789}
5790
5791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005793 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 */
5795 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005796cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797{
5798 if (!vim_isIDc(**s)) /* need at least one ID character */
5799 return FALSE;
5800
5801 while (vim_isIDc(**s))
5802 (*s)++;
5803
5804 *s = cin_skipcomment(*s);
5805
5806 /* "::" is not a label, it's C++ */
5807 return (**s == ':' && *++*s != ':');
5808}
5809
5810/*
5811 * Recognize a label: "label:".
5812 * Note: curwin->w_cursor must be where we are looking for the label.
5813 */
5814 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005815cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816{
5817 char_u *s;
5818
5819 s = cin_skipcomment(ml_get_curline());
5820
5821 /*
5822 * Exclude "default" from labels, since it should be indented
5823 * like a switch label. Same for C++ scope declarations.
5824 */
5825 if (cin_isdefault(s))
5826 return FALSE;
5827 if (cin_isscopedecl(s))
5828 return FALSE;
5829
5830 if (cin_islabel_skip(&s))
5831 {
5832 /*
5833 * Only accept a label if the previous line is terminated or is a case
5834 * label.
5835 */
5836 pos_T cursor_save;
5837 pos_T *trypos;
5838 char_u *line;
5839
5840 cursor_save = curwin->w_cursor;
5841 while (curwin->w_cursor.lnum > 1)
5842 {
5843 --curwin->w_cursor.lnum;
5844
5845 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005846 * If we're in a comment or raw string now, skip to the start of
5847 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 */
5849 curwin->w_cursor.col = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02005850 if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 curwin->w_cursor = *trypos;
5852
5853 line = ml_get_curline();
5854 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5855 continue;
5856 if (*(line = cin_skipcomment(line)) == NUL)
5857 continue;
5858
5859 curwin->w_cursor = cursor_save;
5860 if (cin_isterminated(line, TRUE, FALSE)
5861 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005862 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 || (cin_islabel_skip(&line) && cin_nocode(line)))
5864 return TRUE;
5865 return FALSE;
5866 }
5867 curwin->w_cursor = cursor_save;
5868 return TRUE; /* label at start of file??? */
5869 }
5870 return FALSE;
5871}
5872
5873/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005874 * Recognize structure initialization and enumerations:
5875 * "[typedef] [static|public|protected|private] enum"
5876 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 */
5878 static int
5879cin_isinit(void)
5880{
5881 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005882 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883
5884 s = cin_skipcomment(ml_get_curline());
5885
Bram Moolenaar75342212013-03-07 13:13:52 +01005886 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 s = cin_skipcomment(s + 7);
5888
Bram Moolenaar75342212013-03-07 13:13:52 +01005889 for (;;)
5890 {
5891 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005892
Bram Moolenaar75342212013-03-07 13:13:52 +01005893 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5894 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005895 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005896 if (cin_starts_with(s, skip[i]))
5897 {
5898 s = cin_skipcomment(s + l);
5899 l = 0;
5900 break;
5901 }
5902 }
5903 if (l != 0)
5904 break;
5905 }
5906
5907 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 return TRUE;
5909
5910 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5911 return TRUE;
5912
5913 return FALSE;
5914}
5915
5916/*
5917 * Recognize a switch label: "case .*:" or "default:".
5918 */
5919 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005920cin_iscase(
5921 char_u *s,
5922 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923{
5924 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005925 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 {
5927 for (s += 4; *s; ++s)
5928 {
5929 s = cin_skipcomment(s);
5930 if (*s == ':')
5931 {
5932 if (s[1] == ':') /* skip over "::" for C++ */
5933 ++s;
5934 else
5935 return TRUE;
5936 }
5937 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005938 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5940 return FALSE; /* stop at comment */
5941 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005942 {
5943 /* JS etc. */
5944 if (strict)
5945 return FALSE; /* stop at string */
5946 else
5947 return TRUE;
5948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 }
5950 return FALSE;
5951 }
5952
5953 if (cin_isdefault(s))
5954 return TRUE;
5955 return FALSE;
5956}
5957
5958/*
5959 * Recognize a "default" switch label.
5960 */
5961 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005962cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963{
5964 return (STRNCMP(s, "default", 7) == 0
5965 && *(s = cin_skipcomment(s + 7)) == ':'
5966 && s[1] != ':');
5967}
5968
5969/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005970 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 */
5972 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005973cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974{
5975 int i;
5976
5977 s = cin_skipcomment(s);
5978 if (STRNCMP(s, "public", 6) == 0)
5979 i = 6;
5980 else if (STRNCMP(s, "protected", 9) == 0)
5981 i = 9;
5982 else if (STRNCMP(s, "private", 7) == 0)
5983 i = 7;
5984 else
5985 return FALSE;
5986 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5987}
5988
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005989/* Maximum number of lines to search back for a "namespace" line. */
5990#define FIND_NAMESPACE_LIM 20
5991
5992/*
5993 * Recognize a "namespace" scope declaration.
5994 */
5995 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005996cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005997{
5998 char_u *p;
5999 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006000 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006001
6002 s = cin_skipcomment(s);
6003 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
6004 {
6005 p = cin_skipcomment(skipwhite(s + 9));
6006 while (*p != NUL)
6007 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006008 if (VIM_ISWHITE(*p))
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006009 {
6010 has_name = TRUE; /* found end of a name */
6011 p = cin_skipcomment(skipwhite(p));
6012 }
6013 else if (*p == '{')
6014 {
6015 break;
6016 }
6017 else if (vim_iswordc(*p))
6018 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006019 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006020 if (has_name)
6021 return FALSE; /* word character after skipping past name */
6022 ++p;
6023 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006024 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
6025 {
6026 if (!has_name_start || has_name)
6027 return FALSE;
6028 /* C++ 17 nested namespace */
6029 p += 3;
6030 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006031 else
6032 {
6033 return FALSE;
6034 }
6035 }
6036 return TRUE;
6037 }
6038 return FALSE;
6039}
6040
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041/*
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006042 * Recognize a `extern "C"` or `extern "C++"` linkage specifications.
6043 */
6044 static int
6045cin_is_cpp_extern_c(char_u *s)
6046{
6047 char_u *p;
6048 int has_string_literal = FALSE;
6049
6050 s = cin_skipcomment(s);
6051 if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
6052 {
6053 p = cin_skipcomment(skipwhite(s + 6));
6054 while (*p != NUL)
6055 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006056 if (VIM_ISWHITE(*p))
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006057 {
6058 p = cin_skipcomment(skipwhite(p));
6059 }
6060 else if (*p == '{')
6061 {
6062 break;
6063 }
6064 else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
6065 {
6066 if (has_string_literal)
6067 return FALSE;
6068 has_string_literal = TRUE;
6069 p += 3;
6070 }
6071 else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
6072 && p[4] == '"')
6073 {
6074 if (has_string_literal)
6075 return FALSE;
6076 has_string_literal = TRUE;
6077 p += 5;
6078 }
6079 else
6080 {
6081 return FALSE;
6082 }
6083 }
6084 return has_string_literal ? TRUE : FALSE;
6085 }
6086 return FALSE;
6087}
6088
6089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 * Return a pointer to the first non-empty non-comment character after a ':'.
6091 * Return NULL if not found.
6092 * case 234: a = b;
6093 * ^
6094 */
6095 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006096after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097{
6098 for ( ; *l; ++l)
6099 {
6100 if (*l == ':')
6101 {
6102 if (l[1] == ':') /* skip over "::" for C++ */
6103 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006104 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 break;
6106 }
6107 else if (*l == '\'' && l[1] && l[2] == '\'')
6108 l += 2; /* skip over 'x' */
6109 }
6110 if (*l == NUL)
6111 return NULL;
6112 l = cin_skipcomment(l + 1);
6113 if (*l == NUL)
6114 return NULL;
6115 return l;
6116}
6117
6118/*
6119 * Get indent of line "lnum", skipping a label.
6120 * Return 0 if there is nothing after the label.
6121 */
6122 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006123get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124{
6125 char_u *l;
6126 pos_T fp;
6127 colnr_T col;
6128 char_u *p;
6129
6130 l = ml_get(lnum);
6131 p = after_label(l);
6132 if (p == NULL)
6133 return 0;
6134
6135 fp.col = (colnr_T)(p - l);
6136 fp.lnum = lnum;
6137 getvcol(curwin, &fp, &col, NULL, NULL);
6138 return (int)col;
6139}
6140
6141/*
6142 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006143 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 * label: if (asdf && asdfasdf)
6145 * ^
6146 */
6147 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006148skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149{
6150 char_u *l;
6151 int amount;
6152 pos_T cursor_save;
6153
6154 cursor_save = curwin->w_cursor;
6155 curwin->w_cursor.lnum = lnum;
6156 l = ml_get_curline();
6157 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006158 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 {
6160 amount = get_indent_nolabel(lnum);
6161 l = after_label(ml_get_curline());
6162 if (l == NULL) /* just in case */
6163 l = ml_get_curline();
6164 }
6165 else
6166 {
6167 amount = get_indent();
6168 l = ml_get_curline();
6169 }
6170 *pp = l;
6171
6172 curwin->w_cursor = cursor_save;
6173 return amount;
6174}
6175
6176/*
6177 * Return the indent of the first variable name after a type in a declaration.
6178 * int a, indent of "a"
6179 * static struct foo b, indent of "b"
6180 * enum bla c, indent of "c"
6181 * Returns zero when it doesn't look like a declaration.
6182 */
6183 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006184cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185{
6186 char_u *line, *p, *s;
6187 int len;
6188 pos_T fp;
6189 colnr_T col;
6190
6191 line = ml_get_curline();
6192 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006193 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 if (len == 6 && STRNCMP(p, "static", 6) == 0)
6195 {
6196 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006197 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 }
6199 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
6200 p = skipwhite(p + 6);
6201 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
6202 p = skipwhite(p + 4);
6203 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
6204 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
6205 {
6206 s = skipwhite(p + len);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006207 if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3]))
6208 || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4]))
6209 || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5]))
6210 || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 p = s;
6212 }
6213 for (len = 0; vim_isIDc(p[len]); ++len)
6214 ;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006215 if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 return 0;
6217
6218 p = skipwhite(p + len);
6219 fp.lnum = curwin->w_cursor.lnum;
6220 fp.col = (colnr_T)(p - line);
6221 getvcol(curwin, &fp, &col, NULL, NULL);
6222 return (int)col;
6223}
6224
6225/*
6226 * Return the indent of the first non-blank after an equal sign.
6227 * char *foo = "here";
6228 * Return zero if no (useful) equal sign found.
6229 * Return -1 if the line above "lnum" ends in a backslash.
6230 * foo = "asdf\
6231 * asdf\
6232 * here";
6233 */
6234 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006235cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236{
6237 char_u *line;
6238 char_u *s;
6239 colnr_T col;
6240 pos_T fp;
6241
6242 if (lnum > 1)
6243 {
6244 line = ml_get(lnum - 1);
6245 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6246 return -1;
6247 }
6248
6249 line = s = ml_get(lnum);
6250 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
6251 {
6252 if (cin_iscomment(s)) /* ignore comments */
6253 s = cin_skipcomment(s);
6254 else
6255 ++s;
6256 }
6257 if (*s != '=')
6258 return 0;
6259
6260 s = skipwhite(s + 1);
6261 if (cin_nocode(s))
6262 return 0;
6263
6264 if (*s == '"') /* nice alignment for continued strings */
6265 ++s;
6266
6267 fp.lnum = lnum;
6268 fp.col = (colnr_T)(s - line);
6269 getvcol(curwin, &fp, &col, NULL, NULL);
6270 return (int)col;
6271}
6272
6273/*
6274 * Recognize a preprocessor statement: Any line that starts with '#'.
6275 */
6276 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006277cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006279 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 return TRUE;
6281 return FALSE;
6282}
6283
6284/*
6285 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6286 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6287 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006288 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 */
6290 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006291cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292{
6293 char_u *line = *pp;
6294 linenr_T lnum = *lnump;
6295 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006296 int candidate_amount = *amount;
6297
6298 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6299 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006301 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 {
6303 if (cin_ispreproc(line))
6304 {
6305 retval = TRUE;
6306 *lnump = lnum;
6307 break;
6308 }
6309 if (lnum == 1)
6310 break;
6311 line = ml_get(--lnum);
6312 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6313 break;
6314 }
6315
6316 if (lnum != *lnump)
6317 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006318 if (retval)
6319 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 return retval;
6321}
6322
6323/*
6324 * Recognize the start of a C or C++ comment.
6325 */
6326 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006327cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328{
6329 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6330}
6331
6332/*
6333 * Recognize the start of a "//" comment.
6334 */
6335 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006336cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337{
6338 return (p[0] == '/' && p[1] == '/');
6339}
6340
6341/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006342 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6343 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006345 * If a line begins with an "else", only consider it terminated if no unmatched
6346 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 * Return the character terminating the line (ending char's have precedence if
6348 * both apply in order to determine initializations).
6349 */
6350 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006351cin_isterminated(
6352 char_u *s,
6353 int incl_open, /* include '{' at the end as terminator */
6354 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006356 char_u found_start = 0;
6357 unsigned n_open = 0;
6358 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359
6360 s = cin_skipcomment(s);
6361
6362 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6363 found_start = *s;
6364
Bram Moolenaar496f9512011-05-19 16:35:09 +02006365 if (!found_start)
6366 is_else = cin_iselse(s);
6367
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 while (*s)
6369 {
6370 /* skip over comments, "" strings and 'c'haracters */
6371 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006372 if (*s == '}' && n_open > 0)
6373 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006374 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006375 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 && cin_nocode(s + 1))
6377 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006378 else if (*s == '{')
6379 {
6380 if (incl_open && cin_nocode(s + 1))
6381 return *s;
6382 else
6383 ++n_open;
6384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385
6386 if (*s)
6387 s++;
6388 }
6389 return found_start;
6390}
6391
6392/*
6393 * Recognize the basic picture of a function declaration -- it needs to
6394 * have an open paren somewhere and a close paren at the end of the line and
6395 * no semicolons anywhere.
6396 * When a line ends in a comma we continue looking in the next line.
6397 * "sp" points to a string with the line. When looking at other lines it must
6398 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006399 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006400 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 */
6402 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006403cin_isfuncdecl(
6404 char_u **sp,
6405 linenr_T first_lnum,
6406 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407{
6408 char_u *s;
6409 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006410 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006412 pos_T *trypos;
6413 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414
6415 if (sp == NULL)
6416 s = ml_get(lnum);
6417 else
6418 s = *sp;
6419
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006420 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006421 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006422 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006423 {
6424 lnum = trypos->lnum;
6425 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006426 {
6427 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006428 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006429 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006430
6431 s = ml_get(lnum);
6432 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006433 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006434
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006435 /* Ignore line starting with #. */
6436 if (cin_ispreproc(s))
6437 return FALSE;
6438
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6440 {
6441 if (cin_iscomment(s)) /* ignore comments */
6442 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006443 else if (*s == ':')
6444 {
6445 if (*(s + 1) == ':')
6446 s += 2;
6447 else
6448 /* To avoid a mistake in the following situation:
6449 * A::A(int a, int b)
6450 * : a(0) // <--not a function decl
6451 * , b(0)
6452 * {...
6453 */
6454 return FALSE;
6455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 else
6457 ++s;
6458 }
6459 if (*s != '(')
6460 return FALSE; /* ';', ' or " before any () or no '(' */
6461
6462 while (*s && *s != ';' && *s != '\'' && *s != '"')
6463 {
6464 if (*s == ')' && cin_nocode(s + 1))
6465 {
6466 /* ')' at the end: may have found a match
6467 * Check for he previous line not to end in a backslash:
6468 * #if defined(x) && \
6469 * defined(y)
6470 */
6471 lnum = first_lnum - 1;
6472 s = ml_get(lnum);
6473 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6474 retval = TRUE;
6475 goto done;
6476 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006477 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006479 int comma = (*s == ',');
6480
6481 /* ',' at the end: continue looking in the next line.
6482 * At the end: check for ',' in the next line, for this style:
6483 * func(arg1
6484 * , arg2) */
6485 for (;;)
6486 {
6487 if (lnum >= curbuf->b_ml.ml_line_count)
6488 break;
6489 s = ml_get(++lnum);
6490 if (!cin_ispreproc(s))
6491 break;
6492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 if (lnum >= curbuf->b_ml.ml_line_count)
6494 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006495 /* Require a comma at end of the line or a comma or ')' at the
6496 * start of next line. */
6497 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006498 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006499 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006500 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 }
6502 else if (cin_iscomment(s)) /* ignore comments */
6503 s = cin_skipcomment(s);
6504 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006505 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006507 just_started = FALSE;
6508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 }
6510
6511done:
6512 if (lnum != first_lnum && sp != NULL)
6513 *sp = ml_get(first_lnum);
6514
6515 return retval;
6516}
6517
6518 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006519cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006521 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522}
6523
6524 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006525cin_iselse(
6526 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527{
6528 if (*p == '}') /* accept "} else" */
6529 p = cin_skipcomment(p + 1);
6530 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6531}
6532
6533 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006534cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535{
6536 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6537}
6538
6539/*
6540 * Check if this is a "while" that should have a matching "do".
6541 * We only accept a "while (condition) ;", with only white space between the
6542 * ')' and ';'. The condition may be spread over several lines.
6543 */
6544 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006545cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546{
6547 pos_T cursor_save;
6548 pos_T *trypos;
6549 int retval = FALSE;
6550
6551 p = cin_skipcomment(p);
6552 if (*p == '}') /* accept "} while (cond);" */
6553 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006554 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 {
6556 cursor_save = curwin->w_cursor;
6557 curwin->w_cursor.lnum = lnum;
6558 curwin->w_cursor.col = 0;
6559 p = ml_get_curline();
6560 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6561 {
6562 ++p;
6563 ++curwin->w_cursor.col;
6564 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006565 if ((trypos = findmatchlimit(NULL, 0, 0,
6566 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6568 retval = TRUE;
6569 curwin->w_cursor = cursor_save;
6570 }
6571 return retval;
6572}
6573
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006574/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006575 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006576 * Return 0 if there is none.
6577 * Otherwise return !0 and update "*poffset" to point to the place where the
6578 * string was found.
6579 */
6580 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006581cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006582{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006583 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006584
6585 if (offset-- < 2)
6586 return 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006587 while (offset > 2 && VIM_ISWHITE(line[offset]))
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006588 --offset;
6589
6590 offset -= 1;
6591 if (!STRNCMP(line + offset, "if", 2))
6592 goto probablyFound;
6593
6594 if (offset >= 1)
6595 {
6596 offset -= 1;
6597 if (!STRNCMP(line + offset, "for", 3))
6598 goto probablyFound;
6599
6600 if (offset >= 2)
6601 {
6602 offset -= 2;
6603 if (!STRNCMP(line + offset, "while", 5))
6604 goto probablyFound;
6605 }
6606 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006607 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006608
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006609probablyFound:
6610 if (!offset || !vim_isIDc(line[offset - 1]))
6611 {
6612 *poffset = offset;
6613 return 1;
6614 }
6615 return 0;
6616}
6617
6618/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006619 * Return TRUE if we are at the end of a do-while.
6620 * do
6621 * nothing;
6622 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006623 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006624 * Adjust the cursor to the line with "while".
6625 */
6626 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006627cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006628{
6629 char_u *line;
6630 char_u *p;
6631 char_u *s;
6632 pos_T *trypos;
6633 int i;
6634
6635 if (terminated != ';') /* there must be a ';' at the end */
6636 return FALSE;
6637
6638 p = line = ml_get_curline();
6639 while (*p != NUL)
6640 {
6641 p = cin_skipcomment(p);
6642 if (*p == ')')
6643 {
6644 s = skipwhite(p + 1);
6645 if (*s == ';' && cin_nocode(s + 1))
6646 {
6647 /* Found ");" at end of the line, now check there is "while"
6648 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006649 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006650 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006651 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006652 if (trypos != NULL)
6653 {
6654 s = cin_skipcomment(ml_get(trypos->lnum));
6655 if (*s == '}') /* accept "} while (cond);" */
6656 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006657 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006658 {
6659 curwin->w_cursor.lnum = trypos->lnum;
6660 return TRUE;
6661 }
6662 }
6663
6664 /* Searching may have made "line" invalid, get it again. */
6665 line = ml_get_curline();
6666 p = line + i;
6667 }
6668 }
6669 if (*p != NUL)
6670 ++p;
6671 }
6672 return FALSE;
6673}
6674
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006676cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677{
6678 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6679}
6680
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006681/*
6682 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 * constructor-initialization. eg:
6684 *
6685 * class MyClass :
6686 * baseClass <-- here
6687 * class MyClass : public baseClass,
6688 * anotherBaseClass <-- here (should probably lineup ??)
6689 * MyClass::MyClass(...) :
6690 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006691 *
6692 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 */
6694 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006695cin_is_cpp_baseclass(
6696 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006698 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 char_u *s;
6700 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006701 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006702 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006704 if (pos->lnum <= lnum)
6705 return cached->found; /* Use the cached result */
6706
6707 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006709 s = skipwhite(line);
6710 if (*s == '#') /* skip #define FOO x ? (x) : x */
6711 return FALSE;
6712 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 if (*s == NUL)
6714 return FALSE;
6715
6716 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6717
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006718 /* Search for a line starting with '#', empty, ending in ';' or containing
6719 * '{' or '}' and start below it. This handles the following situations:
6720 * a = cond ?
6721 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006722 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006723 * func::foo()
6724 * : something
6725 * {}
6726 * Foo::Foo (int one, int two)
6727 * : something(4),
6728 * somethingelse(3)
6729 * {}
6730 */
6731 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006733 line = ml_get(lnum - 1);
6734 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006735 if (*s == '#' || *s == NUL)
6736 break;
6737 while (*s != NUL)
6738 {
6739 s = cin_skipcomment(s);
6740 if (*s == '{' || *s == '}'
6741 || (*s == ';' && cin_nocode(s + 1)))
6742 break;
6743 if (*s != NUL)
6744 ++s;
6745 }
6746 if (*s != NUL)
6747 break;
6748 --lnum;
6749 }
6750
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006751 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006752 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006753 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006754 for (;;)
6755 {
6756 if (*s == NUL)
6757 {
6758 if (lnum == curwin->w_cursor.lnum)
6759 break;
6760 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006761 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006762 s = line;
6763 }
6764 if (s == line)
6765 {
6766 /* don't recognize "case (foo):" as a baseclass */
6767 if (cin_iscase(s, FALSE))
6768 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006769 s = cin_skipcomment(line);
6770 if (*s == NUL)
6771 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006772 }
6773
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006774 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006775 s = skip_string(s) + 1;
6776 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 {
6778 if (s[1] == ':')
6779 {
6780 /* skip double colon. It can't be a constructor
6781 * initialization any more */
6782 lookfor_ctor_init = FALSE;
6783 s = cin_skipcomment(s + 2);
6784 }
6785 else if (lookfor_ctor_init || class_or_struct)
6786 {
6787 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006788 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 cpp_base_class = TRUE;
6790 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006791 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 s = cin_skipcomment(s + 1);
6793 }
6794 else
6795 s = cin_skipcomment(s + 1);
6796 }
6797 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6798 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6799 {
6800 class_or_struct = TRUE;
6801 lookfor_ctor_init = FALSE;
6802
6803 if (*s == 'c')
6804 s = cin_skipcomment(s + 5);
6805 else
6806 s = cin_skipcomment(s + 6);
6807 }
6808 else
6809 {
6810 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6811 {
6812 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6813 }
6814 else if (s[0] == ')')
6815 {
6816 /* Constructor-initialization is assumed if we come across
6817 * something like "):" */
6818 class_or_struct = FALSE;
6819 lookfor_ctor_init = TRUE;
6820 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006821 else if (s[0] == '?')
6822 {
6823 /* Avoid seeing '() :' after '?' as constructor init. */
6824 return FALSE;
6825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 else if (!vim_isIDc(s[0]))
6827 {
6828 /* if it is not an identifier, we are wrong */
6829 class_or_struct = FALSE;
6830 lookfor_ctor_init = FALSE;
6831 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006832 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 {
6834 /* it can't be a constructor-initialization any more */
6835 lookfor_ctor_init = FALSE;
6836
6837 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006838 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006839 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 }
6841
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006842 /* When the line ends in a comma don't align with it. */
6843 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006844 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006845
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 s = cin_skipcomment(s + 1);
6847 }
6848 }
6849
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006850 cached->found = cpp_base_class;
6851 if (cpp_base_class)
6852 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 return cpp_base_class;
6854}
6855
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006856 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006857get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006858{
6859 int amount;
6860 colnr_T vcol;
6861 pos_T *trypos;
6862
6863 if (col == 0)
6864 {
6865 amount = get_indent();
6866 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006867 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006868 amount = get_indent_lnum(trypos->lnum); /* XXX */
6869 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006870 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006871 }
6872 else
6873 {
6874 curwin->w_cursor.col = col;
6875 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6876 amount = (int)vcol;
6877 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006878 if (amount < curbuf->b_ind_cpp_baseclass)
6879 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006880 return amount;
6881}
6882
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883/*
6884 * Return TRUE if string "s" ends with the string "find", possibly followed by
6885 * white space and comments. Skip strings and comments.
6886 * Ignore "ignore" after "find" if it's not NULL.
6887 */
6888 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006889cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890{
6891 char_u *p = s;
6892 char_u *r;
6893 int len = (int)STRLEN(find);
6894
6895 while (*p != NUL)
6896 {
6897 p = cin_skipcomment(p);
6898 if (STRNCMP(p, find, len) == 0)
6899 {
6900 r = skipwhite(p + len);
6901 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6902 r = skipwhite(r + STRLEN(ignore));
6903 if (cin_nocode(r))
6904 return TRUE;
6905 }
6906 if (*p != NUL)
6907 ++p;
6908 }
6909 return FALSE;
6910}
6911
6912/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006913 * Return TRUE when "s" starts with "word" and then a non-ID character.
6914 */
6915 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006916cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006917{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006918 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006919
6920 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6921}
6922
6923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 * Skip strings, chars and comments until at or past "trypos".
6925 * Return the column found.
6926 */
6927 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006928cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929{
6930 char_u *line;
6931 char_u *p;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006932 char_u *new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933
6934 p = line = ml_get(trypos->lnum);
6935 while (*p && (colnr_T)(p - line) < trypos->col)
6936 {
6937 if (cin_iscomment(p))
6938 p = cin_skipcomment(p);
6939 else
6940 {
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006941 new_p = skip_string(p);
6942 if (new_p == p)
6943 ++p;
6944 else
6945 p = new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 }
6947 }
6948 return (int)(p - line);
6949}
6950
6951/*
6952 * Find the '{' at the start of the block we are in.
6953 * Return NULL if no match found.
6954 * Ignore a '{' that is in a comment, makes indenting the next three lines
6955 * work. */
6956/* foo() */
6957/* { */
6958/* } */
6959
6960 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006961find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962{
6963 pos_T cursor_save;
6964 pos_T *trypos;
6965 pos_T *pos;
6966 static pos_T pos_copy;
6967
6968 cursor_save = curwin->w_cursor;
6969 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6970 {
6971 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6972 trypos = &pos_copy;
6973 curwin->w_cursor = *trypos;
6974 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006975 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaardde81312017-08-26 17:49:01 +02006977 && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 break;
6979 if (pos != NULL)
6980 curwin->w_cursor.lnum = pos->lnum;
6981 }
6982 curwin->w_cursor = cursor_save;
6983 return trypos;
6984}
6985
6986/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006987 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006988 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 */
6990 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006991find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006993 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006994}
6995
6996 static pos_T *
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02006997find_match_char(int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006998{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 pos_T cursor_save;
7000 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007001 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007002 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003
7004 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007005 ind_maxp_wk = ind_maxparen;
7006retry:
7007 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 {
7009 /* check if the ( is in a // comment */
7010 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01007011 {
7012 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
7013 if (ind_maxp_wk > 0)
7014 {
7015 curwin->w_cursor = *trypos;
7016 curwin->w_cursor.col = 0; /* XXX */
7017 goto retry;
7018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 else
7022 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01007023 pos_T *trypos_wk;
7024
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 pos_copy = *trypos; /* copy trypos, findmatch will change it */
7026 trypos = &pos_copy;
7027 curwin->w_cursor = *trypos;
Bram Moolenaardde81312017-08-26 17:49:01 +02007028 if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01007029 {
7030 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
7031 - trypos_wk->lnum);
7032 if (ind_maxp_wk > 0)
7033 {
7034 curwin->w_cursor = *trypos_wk;
7035 goto retry;
7036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 }
7040 }
7041 curwin->w_cursor = cursor_save;
7042 return trypos;
7043}
7044
7045/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02007046 * Find the matching '(', ignoring it if it is in a comment or before an
7047 * unmatched {.
7048 * Return NULL if no match found.
7049 */
7050 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007051find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02007052{
7053 pos_T *trypos = find_match_paren(ind_maxparen);
7054
7055 if (trypos != NULL)
7056 {
7057 pos_T *tryposBrace = find_start_brace();
7058
7059 /* If both an unmatched '(' and '{' is found. Ignore the '('
7060 * position if the '{' is further down. */
7061 if (tryposBrace != NULL
7062 && (trypos->lnum != tryposBrace->lnum
7063 ? trypos->lnum < tryposBrace->lnum
7064 : trypos->col < tryposBrace->col))
7065 trypos = NULL;
7066 }
7067 return trypos;
7068}
7069
7070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 * Return ind_maxparen corrected for the difference in line number between the
7072 * cursor position and "startpos". This makes sure that searching for a
7073 * matching paren above the cursor line doesn't find a match because of
7074 * looking a few lines further.
7075 */
7076 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007077corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078{
7079 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
7080
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007081 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
7082 return curbuf->b_ind_maxparen - (int)n;
7083 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084}
7085
7086/*
7087 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007088 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 */
7090 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007091find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092{
7093 int i;
7094 int retval = FALSE;
7095 int open_count = 0;
7096
7097 curwin->w_cursor.col = 0; /* default is start of line */
7098
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007099 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 {
7101 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
7102 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
7103 if (l[i] == start)
7104 ++open_count;
7105 else if (l[i] == end)
7106 {
7107 if (open_count > 0)
7108 --open_count;
7109 else
7110 {
7111 curwin->w_cursor.col = i;
7112 retval = TRUE;
7113 }
7114 }
7115 }
7116 return retval;
7117}
7118
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007119/*
7120 * Parse 'cinoptions' and set the values in "curbuf".
7121 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
7122 */
7123 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007124parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007125{
7126 char_u *p;
7127 char_u *l;
7128 char_u *digits;
7129 int n;
7130 int divider;
7131 int fraction = 0;
7132 int sw = (int)get_sw_value(buf);
7133
7134 /*
7135 * Set the default values.
7136 */
7137 /* Spaces from a block's opening brace the prevailing indent for that
7138 * block should be. */
7139 buf->b_ind_level = sw;
7140
7141 /* Spaces from the edge of the line an open brace that's at the end of a
7142 * line is imagined to be. */
7143 buf->b_ind_open_imag = 0;
7144
7145 /* Spaces from the prevailing indent for a line that is not preceded by
7146 * an opening brace. */
7147 buf->b_ind_no_brace = 0;
7148
7149 /* Column where the first { of a function should be located }. */
7150 buf->b_ind_first_open = 0;
7151
7152 /* Spaces from the prevailing indent a leftmost open brace should be
7153 * located. */
7154 buf->b_ind_open_extra = 0;
7155
7156 /* Spaces from the matching open brace (real location for one at the left
7157 * edge; imaginary location from one that ends a line) the matching close
7158 * brace should be located. */
7159 buf->b_ind_close_extra = 0;
7160
7161 /* Spaces from the edge of the line an open brace sitting in the leftmost
7162 * column is imagined to be. */
7163 buf->b_ind_open_left_imag = 0;
7164
7165 /* Spaces jump labels should be shifted to the left if N is non-negative,
7166 * otherwise the jump label will be put to column 1. */
7167 buf->b_ind_jump_label = -1;
7168
7169 /* Spaces from the switch() indent a "case xx" label should be located. */
7170 buf->b_ind_case = sw;
7171
7172 /* Spaces from the "case xx:" code after a switch() should be located. */
7173 buf->b_ind_case_code = sw;
7174
7175 /* Lineup break at end of case in switch() with case label. */
7176 buf->b_ind_case_break = 0;
7177
7178 /* Spaces from the class declaration indent a scope declaration label
7179 * should be located. */
7180 buf->b_ind_scopedecl = sw;
7181
7182 /* Spaces from the scope declaration label code should be located. */
7183 buf->b_ind_scopedecl_code = sw;
7184
7185 /* Amount K&R-style parameters should be indented. */
7186 buf->b_ind_param = sw;
7187
7188 /* Amount a function type spec should be indented. */
7189 buf->b_ind_func_type = sw;
7190
7191 /* Amount a cpp base class declaration or constructor initialization
7192 * should be indented. */
7193 buf->b_ind_cpp_baseclass = sw;
7194
7195 /* additional spaces beyond the prevailing indent a continuation line
7196 * should be located. */
7197 buf->b_ind_continuation = sw;
7198
7199 /* Spaces from the indent of the line with an unclosed parentheses. */
7200 buf->b_ind_unclosed = sw * 2;
7201
7202 /* Spaces from the indent of the line with an unclosed parentheses, which
7203 * itself is also unclosed. */
7204 buf->b_ind_unclosed2 = sw;
7205
7206 /* Suppress ignoring spaces from the indent of a line starting with an
7207 * unclosed parentheses. */
7208 buf->b_ind_unclosed_noignore = 0;
7209
7210 /* If the opening paren is the last nonwhite character on the line, and
7211 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
7212 * context (for very long lines). */
7213 buf->b_ind_unclosed_wrapped = 0;
7214
7215 /* Suppress ignoring white space when lining up with the character after
7216 * an unclosed parentheses. */
7217 buf->b_ind_unclosed_whiteok = 0;
7218
7219 /* Indent a closing parentheses under the line start of the matching
7220 * opening parentheses. */
7221 buf->b_ind_matching_paren = 0;
7222
7223 /* Indent a closing parentheses under the previous line. */
7224 buf->b_ind_paren_prev = 0;
7225
7226 /* Extra indent for comments. */
7227 buf->b_ind_comment = 0;
7228
7229 /* Spaces from the comment opener when there is nothing after it. */
7230 buf->b_ind_in_comment = 3;
7231
7232 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
7233 * after the comment opener. */
7234 buf->b_ind_in_comment2 = 0;
7235
7236 /* Max lines to search for an open paren. */
7237 buf->b_ind_maxparen = 20;
7238
7239 /* Max lines to search for an open comment. */
7240 buf->b_ind_maxcomment = 70;
7241
7242 /* Handle braces for java code. */
7243 buf->b_ind_java = 0;
7244
7245 /* Not to confuse JS object properties with labels. */
7246 buf->b_ind_js = 0;
7247
7248 /* Handle blocked cases correctly. */
7249 buf->b_ind_keep_case_label = 0;
7250
7251 /* Handle C++ namespace. */
7252 buf->b_ind_cpp_namespace = 0;
7253
7254 /* Handle continuation lines containing conditions of if(), for() and
7255 * while(). */
7256 buf->b_ind_if_for_while = 0;
7257
Bram Moolenaar6b643942017-03-05 19:44:06 +01007258 /* indentation for # comments */
7259 buf->b_ind_hash_comment = 0;
7260
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007261 /* Handle C++ extern "C" or "C++" */
7262 buf->b_ind_cpp_extern_c = 0;
7263
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007264 for (p = buf->b_p_cino; *p; )
7265 {
7266 l = p++;
7267 if (*p == '-')
7268 ++p;
7269 digits = p; /* remember where the digits start */
7270 n = getdigits(&p);
7271 divider = 0;
7272 if (*p == '.') /* ".5s" means a fraction */
7273 {
7274 fraction = atol((char *)++p);
7275 while (VIM_ISDIGIT(*p))
7276 {
7277 ++p;
7278 if (divider)
7279 divider *= 10;
7280 else
7281 divider = 10;
7282 }
7283 }
7284 if (*p == 's') /* "2s" means two times 'shiftwidth' */
7285 {
7286 if (p == digits)
7287 n = sw; /* just "s" is one 'shiftwidth' */
7288 else
7289 {
7290 n *= sw;
7291 if (divider)
7292 n += (sw * fraction + divider / 2) / divider;
7293 }
7294 ++p;
7295 }
7296 if (l[1] == '-')
7297 n = -n;
7298
7299 /* When adding an entry here, also update the default 'cinoptions' in
7300 * doc/indent.txt, and add explanation for it! */
7301 switch (*l)
7302 {
7303 case '>': buf->b_ind_level = n; break;
7304 case 'e': buf->b_ind_open_imag = n; break;
7305 case 'n': buf->b_ind_no_brace = n; break;
7306 case 'f': buf->b_ind_first_open = n; break;
7307 case '{': buf->b_ind_open_extra = n; break;
7308 case '}': buf->b_ind_close_extra = n; break;
7309 case '^': buf->b_ind_open_left_imag = n; break;
7310 case 'L': buf->b_ind_jump_label = n; break;
7311 case ':': buf->b_ind_case = n; break;
7312 case '=': buf->b_ind_case_code = n; break;
7313 case 'b': buf->b_ind_case_break = n; break;
7314 case 'p': buf->b_ind_param = n; break;
7315 case 't': buf->b_ind_func_type = n; break;
7316 case '/': buf->b_ind_comment = n; break;
7317 case 'c': buf->b_ind_in_comment = n; break;
7318 case 'C': buf->b_ind_in_comment2 = n; break;
7319 case 'i': buf->b_ind_cpp_baseclass = n; break;
7320 case '+': buf->b_ind_continuation = n; break;
7321 case '(': buf->b_ind_unclosed = n; break;
7322 case 'u': buf->b_ind_unclosed2 = n; break;
7323 case 'U': buf->b_ind_unclosed_noignore = n; break;
7324 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7325 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7326 case 'm': buf->b_ind_matching_paren = n; break;
7327 case 'M': buf->b_ind_paren_prev = n; break;
7328 case ')': buf->b_ind_maxparen = n; break;
7329 case '*': buf->b_ind_maxcomment = n; break;
7330 case 'g': buf->b_ind_scopedecl = n; break;
7331 case 'h': buf->b_ind_scopedecl_code = n; break;
7332 case 'j': buf->b_ind_java = n; break;
7333 case 'J': buf->b_ind_js = n; break;
7334 case 'l': buf->b_ind_keep_case_label = n; break;
7335 case '#': buf->b_ind_hash_comment = n; break;
7336 case 'N': buf->b_ind_cpp_namespace = n; break;
7337 case 'k': buf->b_ind_if_for_while = n; break;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007338 case 'E': buf->b_ind_cpp_extern_c = n; break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007339 }
7340 if (*p == ',')
7341 ++p;
7342 }
7343}
7344
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007345/*
7346 * Return the desired indent for C code.
7347 * Return -1 if the indent should be left alone (inside a raw string).
7348 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007350get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 pos_T cur_curpos;
7353 int amount;
7354 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007355 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 colnr_T col;
7357 char_u *theline;
7358 char_u *linecopy;
7359 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007360 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007362 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 pos_T our_paren_pos;
7364 char_u *start;
7365 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007366#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367#define BRACE_AT_START 2 /* '{' is at start of line */
7368#define BRACE_AT_END 3 /* '{' is at end of line */
7369 linenr_T ourscope;
7370 char_u *l;
7371 char_u *look;
7372 char_u terminated;
7373 int lookfor;
7374#define LOOKFOR_INITIAL 0
7375#define LOOKFOR_IF 1
7376#define LOOKFOR_DO 2
7377#define LOOKFOR_CASE 3
7378#define LOOKFOR_ANY 4
7379#define LOOKFOR_TERM 5
7380#define LOOKFOR_UNTERM 6
7381#define LOOKFOR_SCOPEDECL 7
7382#define LOOKFOR_NOBREAK 8
7383#define LOOKFOR_CPP_BASECLASS 9
7384#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007385#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007386#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387
7388 int whilelevel;
7389 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 int n;
7391 int iscase;
7392 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007393 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007395 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007396 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007397 int js_cur_has_key = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02007398 linenr_T raw_string_start = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007399 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007401 /* make a copy, value is changed below */
7402 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403
7404 /* remember where the cursor was when we started */
7405 cur_curpos = curwin->w_cursor;
7406
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007407 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007408 if (cur_curpos.lnum == 1)
7409 return 0;
7410
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 /* Get a copy of the current contents of the line.
7412 * This is required, because only the most recent line obtained with
7413 * ml_get is valid! */
7414 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7415 if (linecopy == NULL)
7416 return 0;
7417
7418 /*
7419 * In insert mode and the cursor is on a ')' truncate the line at the
7420 * cursor position. We don't want to line up with the matching '(' when
7421 * inserting new stuff.
7422 * For unknown reasons the cursor might be past the end of the line, thus
7423 * check for that.
7424 */
7425 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007426 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 && linecopy[curwin->w_cursor.col] == ')')
7428 linecopy[curwin->w_cursor.col] = NUL;
7429
7430 theline = skipwhite(linecopy);
7431
7432 /* move the cursor to the start of the line */
7433
7434 curwin->w_cursor.col = 0;
7435
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007436 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007437
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007439 * If we are inside a raw string don't change the indent.
7440 * Ignore a raw string inside a comment.
7441 */
7442 comment_pos = ind_find_start_comment();
7443 if (comment_pos != NULL)
7444 {
7445 /* findmatchlimit() static pos is overwritten, make a copy */
7446 tryposCopy = *comment_pos;
7447 comment_pos = &tryposCopy;
7448 }
7449 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007450 if (trypos != NULL && (comment_pos == NULL
7451 || LT_POS(*trypos, *comment_pos)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007452 {
7453 amount = -1;
7454 goto laterend;
7455 }
7456
7457 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 * #defines and so on always go at the left when included in 'cinkeys'.
7459 */
7460 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007461 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007462 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007463 goto theend;
7464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465
7466 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007467 * Is it a non-case label? Then that goes at the left margin too unless:
7468 * - JS flag is set.
7469 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007471 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007472 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 {
7474 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007475 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 }
7477
7478 /*
7479 * If we're inside a "//" comment and there is a "//" comment in a
7480 * previous line, lineup with that one.
7481 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007482 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 && (trypos = find_line_comment()) != NULL) /* XXX */
7484 {
7485 /* find how indented the line beginning the comment is */
7486 getvcol(curwin, trypos, &col, NULL, NULL);
7487 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007488 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 }
7490
7491 /*
7492 * If we're inside a comment and not looking at the start of the
7493 * comment, try using the 'comments' option.
7494 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007495 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 {
7497 int lead_start_len = 2;
7498 int lead_middle_len = 1;
7499 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7500 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7501 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7502 char_u *p;
7503 int start_align = 0;
7504 int start_off = 0;
7505 int done = FALSE;
7506
7507 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007508 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007510 *lead_start = NUL;
7511 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512
7513 p = curbuf->b_p_com;
7514 while (*p != NUL)
7515 {
7516 int align = 0;
7517 int off = 0;
7518 int what = 0;
7519
7520 while (*p != NUL && *p != ':')
7521 {
7522 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7523 what = *p++;
7524 else if (*p == COM_LEFT || *p == COM_RIGHT)
7525 align = *p++;
7526 else if (VIM_ISDIGIT(*p) || *p == '-')
7527 off = getdigits(&p);
7528 else
7529 ++p;
7530 }
7531
7532 if (*p == ':')
7533 ++p;
7534 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7535 if (what == COM_START)
7536 {
7537 STRCPY(lead_start, lead_end);
7538 lead_start_len = (int)STRLEN(lead_start);
7539 start_off = off;
7540 start_align = align;
7541 }
7542 else if (what == COM_MIDDLE)
7543 {
7544 STRCPY(lead_middle, lead_end);
7545 lead_middle_len = (int)STRLEN(lead_middle);
7546 }
7547 else if (what == COM_END)
7548 {
7549 /* If our line starts with the middle comment string, line it
7550 * up with the comment opener per the 'comments' option. */
7551 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7552 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7553 {
7554 done = TRUE;
7555 if (curwin->w_cursor.lnum > 1)
7556 {
7557 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007558 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 * the middle comment string matches in the previous
7560 * line, use the indent of that line. XXX */
7561 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7562 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7563 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7564 else if (STRNCMP(look, lead_middle,
7565 lead_middle_len) == 0)
7566 {
7567 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7568 break;
7569 }
7570 /* If the start comment string doesn't match with the
7571 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007572 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 lead_start, lead_start_len) != 0)
7574 continue;
7575 }
7576 if (start_off != 0)
7577 amount += start_off;
7578 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007579 amount += vim_strsize(lead_start)
7580 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 break;
7582 }
7583
7584 /* If our line starts with the end comment string, line it up
7585 * with the middle comment */
7586 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7587 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7588 {
7589 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7590 /* XXX */
7591 if (off != 0)
7592 amount += off;
7593 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007594 amount += vim_strsize(lead_start)
7595 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 done = TRUE;
7597 break;
7598 }
7599 }
7600 }
7601
7602 /* If our line starts with an asterisk, line up with the
7603 * asterisk in the comment opener; otherwise, line up
7604 * with the first character of the comment text.
7605 */
7606 if (done)
7607 ;
7608 else if (theline[0] == '*')
7609 amount += 1;
7610 else
7611 {
7612 /*
7613 * If we are more than one line away from the comment opener, take
7614 * the indent of the previous non-empty line. If 'cino' has "CO"
7615 * and we are just below the comment opener and there are any
7616 * white characters after it line up with the text after it;
7617 * otherwise, add the amount specified by "c" in 'cino'
7618 */
7619 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007620 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 {
7622 if (linewhite(lnum)) /* skip blank lines */
7623 continue;
7624 amount = get_indent_lnum(lnum); /* XXX */
7625 break;
7626 }
7627 if (amount == -1) /* use the comment opener */
7628 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007629 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007631 start = ml_get(comment_pos->lnum);
7632 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007634 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007636 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007638 if (curbuf->b_ind_in_comment2 || *look == NUL)
7639 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 }
7641 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007642 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 }
7644
7645 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007646 * Are we looking at a ']' that has a match?
7647 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007648 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007649 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7650 {
7651 /* align with the line containing the '['. */
7652 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007653 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007654 }
7655
7656 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 * Are we inside parentheses or braces?
7658 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007659 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007660 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007661 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 || trypos != NULL)
7663 {
7664 if (trypos != NULL && tryposBrace != NULL)
7665 {
7666 /* Both an unmatched '(' and '{' is found. Use the one which is
7667 * closer to the current cursor position, set the other to NULL. */
7668 if (trypos->lnum != tryposBrace->lnum
7669 ? trypos->lnum < tryposBrace->lnum
7670 : trypos->col < tryposBrace->col)
7671 trypos = NULL;
7672 else
7673 tryposBrace = NULL;
7674 }
7675
7676 if (trypos != NULL)
7677 {
7678 /*
7679 * If the matching paren is more than one line away, use the indent of
7680 * a previous non-empty line that matches the same paren.
7681 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007682 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007684 /* Line up with the start of the matching paren line. */
7685 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7686 }
7687 else
7688 {
7689 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007690 our_paren_pos = *trypos;
7691 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007693 l = skipwhite(ml_get(lnum));
7694 if (cin_nocode(l)) /* skip comment lines */
7695 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007696 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007697 continue; /* ignore #define, #if, etc. */
7698 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007700 /* Skip a comment or raw string. XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02007701 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007702 {
7703 lnum = trypos->lnum + 1;
7704 continue;
7705 }
7706
7707 /* XXX */
7708 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007709 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007710 && trypos->lnum == our_paren_pos.lnum
7711 && trypos->col == our_paren_pos.col)
7712 {
7713 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007715 if (theline[0] == ')')
7716 {
7717 if (our_paren_pos.lnum != lnum
7718 && cur_amount > amount)
7719 cur_amount = amount;
7720 amount = -1;
7721 }
7722 break;
7723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 }
7725 }
7726
7727 /*
7728 * Line up with line where the matching paren is. XXX
7729 * If the line starts with a '(' or the indent for unclosed
7730 * parentheses is zero, line up with the unclosed parentheses.
7731 */
7732 if (amount == -1)
7733 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007734 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007735 int is_if_for_while = 0;
7736
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007737 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007738 {
7739 /* Look for the outermost opening parenthesis on this line
7740 * and check whether it belongs to an "if", "for" or "while". */
7741
7742 pos_T cursor_save = curwin->w_cursor;
7743 pos_T outermost;
7744 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007745
7746 trypos = &our_paren_pos;
7747 do {
7748 outermost = *trypos;
7749 curwin->w_cursor.lnum = outermost.lnum;
7750 curwin->w_cursor.col = outermost.col;
7751
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007752 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007753 } while (trypos && trypos->lnum == outermost.lnum);
7754
7755 curwin->w_cursor = cursor_save;
7756
7757 line = ml_get(outermost.lnum);
7758
7759 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007760 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007761 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007762
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007763 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007764 look = skipwhite(look);
7765 if (*look == '(')
7766 {
7767 linenr_T save_lnum = curwin->w_cursor.lnum;
7768 char_u *line;
7769 int look_col;
7770
7771 /* Ignore a '(' in front of the line that has a match before
7772 * our matching '('. */
7773 curwin->w_cursor.lnum = our_paren_pos.lnum;
7774 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007775 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007776 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007777 if ((trypos = findmatchlimit(NULL, ')', 0,
7778 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007779 != NULL
7780 && trypos->lnum == our_paren_pos.lnum
7781 && trypos->col < our_paren_pos.col)
7782 ignore_paren_col = trypos->col + 1;
7783
7784 curwin->w_cursor.lnum = save_lnum;
7785 look = ml_get(our_paren_pos.lnum) + look_col;
7786 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007787 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7788 && is_if_for_while == 0)
7789 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007790 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 {
7792 /*
7793 * If we're looking at a close paren, line up right there;
7794 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007795 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 * the last nonwhite character of the line, use either the
7797 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007798 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 * lines).
7800 */
7801 if (theline[0] != ')')
7802 {
7803 cur_amount = MAXCOL;
7804 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007805 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 && cin_ends_in(l, (char_u *)"(", NULL))
7807 {
7808 /* look for opening unmatched paren, indent one level
7809 * for each additional level */
7810 n = 1;
7811 for (col = 0; col < our_paren_pos.col; ++col)
7812 {
7813 switch (l[col])
7814 {
7815 case '(':
7816 case '{': ++n;
7817 break;
7818
7819 case ')':
7820 case '}': if (n > 1)
7821 --n;
7822 break;
7823 }
7824 }
7825
7826 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007827 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007829 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 our_paren_pos.col++;
7831 else
7832 {
7833 col = our_paren_pos.col + 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01007834 while (VIM_ISWHITE(l[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 col++;
7836 if (l[col] != NUL) /* In case of trailing space */
7837 our_paren_pos.col = col;
7838 else
7839 our_paren_pos.col++;
7840 }
7841 }
7842
7843 /*
7844 * Find how indented the paren is, or the character after it
7845 * if we did the above "if".
7846 */
7847 if (our_paren_pos.col > 0)
7848 {
7849 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7850 if (cur_amount > (int)col)
7851 cur_amount = col;
7852 }
7853 }
7854
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007855 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 {
7857 /* Line up with the start of the matching paren line. */
7858 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007859 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7860 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007861 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 {
7863 if (cur_amount != MAXCOL)
7864 amount = cur_amount;
7865 }
7866 else
7867 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007868 /* Add b_ind_unclosed2 for each '(' before our matching one,
7869 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007871 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 {
7873 --our_paren_pos.col;
7874 switch (*ml_get_pos(&our_paren_pos))
7875 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007876 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 col = our_paren_pos.col;
7878 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007879 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 col = MAXCOL;
7881 break;
7882 }
7883 }
7884
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007885 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 * braces */
7887 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007888 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 else
7890 {
7891 curwin->w_cursor.lnum = our_paren_pos.lnum;
7892 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007893 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7894 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007895 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007897 {
7898 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007899 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007900 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007901 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 }
7904 /*
7905 * For a line starting with ')' use the minimum of the two
7906 * positions, to avoid giving it more indent than the previous
7907 * lines:
7908 * func_long_name( if (x
7909 * arg && yy
7910 * ) ^ not here ) ^ not here
7911 */
7912 if (cur_amount < amount)
7913 amount = cur_amount;
7914 }
7915 }
7916
7917 /* add extra indent for a comment */
7918 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007919 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 else
7922 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007923 /*
7924 * We are inside braces, there is a { before this line at the position
7925 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007926 * Make a copy of tryposBrace, it may point to pos_copy inside
7927 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007928 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007929 tryposCopy = *tryposBrace;
7930 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 ourscope = trypos->lnum;
7933 start = ml_get(ourscope);
7934
7935 /*
7936 * Now figure out how indented the line is in general.
7937 * If the brace was at the start of the line, we use that;
7938 * otherwise, check out the indentation of the line as
7939 * a whole and then add the "imaginary indent" to that.
7940 */
7941 look = skipwhite(start);
7942 if (*look == '{')
7943 {
7944 getvcol(curwin, trypos, &col, NULL, NULL);
7945 amount = col;
7946 if (*start == '{')
7947 start_brace = BRACE_IN_COL0;
7948 else
7949 start_brace = BRACE_AT_START;
7950 }
7951 else
7952 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007953 /* That opening brace might have been on a continuation
7954 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 curwin->w_cursor.lnum = ourscope;
7956
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007957 /* Position the cursor over the rightmost paren, so that
7958 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 lnum = ourscope;
7960 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007961 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7962 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 lnum = trypos->lnum;
7964
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007965 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 * case 1: if (asdf &&
7967 * ldfd) {
7968 * }
7969 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007970 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7971 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007973 else if (curbuf->b_ind_js)
7974 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007976 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977
7978 start_brace = BRACE_AT_END;
7979 }
7980
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007981 /* For Javascript check if the line starts with "key:". */
7982 if (curbuf->b_ind_js)
7983 js_cur_has_key = cin_has_js_key(theline);
7984
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007986 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 * we want to be. otherwise, add the amount of room
7988 * that an indent is supposed to be.
7989 */
7990 if (theline[0] == '}')
7991 {
7992 /*
7993 * they may want closing braces to line up with something
7994 * other than the open brace. indulge them, if so.
7995 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007996 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 }
7998 else
7999 {
8000 /*
8001 * If we're looking at an "else", try to find an "if"
8002 * to match it with.
8003 * If we're looking at a "while", try to find a "do"
8004 * to match it with.
8005 */
8006 lookfor = LOOKFOR_INITIAL;
8007 if (cin_iselse(theline))
8008 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008009 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 lookfor = LOOKFOR_DO;
8011 if (lookfor != LOOKFOR_INITIAL)
8012 {
8013 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008014 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 {
8016 amount = get_indent(); /* XXX */
8017 goto theend;
8018 }
8019 }
8020
8021 /*
8022 * We get here if we are not on an "while-of-do" or "else" (or
8023 * failed to find a matching "if").
8024 * Search backwards for something to line up with.
8025 * First set amount for when we don't find anything.
8026 */
8027
8028 /*
8029 * if the '{' is _really_ at the left margin, use the imaginary
8030 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008031 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 */
8033
8034 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
8035 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008036 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008037 lookfor_cpp_namespace = TRUE;
8038 }
8039 else if (start_brace == BRACE_AT_START &&
8040 lookfor_cpp_namespace) /* '{' is at start */
8041 {
8042
8043 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 }
8045 else
8046 {
8047 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008048 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008049 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008050
8051 l = skipwhite(ml_get_curline());
8052 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008053 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008054 else if (cin_is_cpp_extern_c(l))
8055 amount += curbuf->b_ind_cpp_extern_c;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 else
8058 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008059 /* Compensate for adding b_ind_open_extra later. */
8060 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 if (amount < 0)
8062 amount = 0;
8063 }
8064 }
8065
8066 lookfor_break = FALSE;
8067
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008068 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 {
8070 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008071 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 }
8073 else if (cin_isscopedecl(theline)) /* private:, ... */
8074 {
8075 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008076 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 }
8078 else
8079 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008080 if (curbuf->b_ind_case_break && cin_isbreak(theline))
8081 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 lookfor_break = TRUE;
8083
8084 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008085 /* b_ind_level from start of block */
8086 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 }
8088 scope_amount = amount;
8089 whilelevel = 0;
8090
8091 /*
8092 * Search backwards. If we find something we recognize, line up
8093 * with that.
8094 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008095 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 * the usual amount relative to the conditional
8097 * that opens the block.
8098 */
8099 curwin->w_cursor = cur_curpos;
8100 for (;;)
8101 {
8102 curwin->w_cursor.lnum--;
8103 curwin->w_cursor.col = 0;
8104
8105 /*
8106 * If we went all the way back to the start of our scope, line
8107 * up with it.
8108 */
8109 if (curwin->w_cursor.lnum <= ourscope)
8110 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008111 /* We reached end of scope:
8112 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008114 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 * don't add ind_continuation, otherwise it is a variable
8116 * declaration:
8117 * int x,
8118 * here; <-- add ind_continuation
8119 */
8120 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8121 {
8122 if (curwin->w_cursor.lnum == 0
8123 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008124 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008126 /* nothing found (abuse curbuf->b_ind_maxparen as
8127 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 * initialization) */
8129 if (cont_amount > 0)
8130 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008131 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 amount += ind_continuation;
8133 break;
8134 }
8135
8136 l = ml_get_curline();
8137
8138 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008139 * If we're in a comment or raw string now, skip to
8140 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 */
Bram Moolenaardde81312017-08-26 17:49:01 +02008142 trypos = ind_find_start_CORS(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 if (trypos != NULL)
8144 {
8145 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008146 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 continue;
8148 }
8149
8150 /*
8151 * Skip preprocessor directives and blank lines.
8152 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008153 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8154 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 continue;
8156
8157 if (cin_nocode(l))
8158 continue;
8159
8160 terminated = cin_isterminated(l, FALSE, TRUE);
8161
8162 /*
8163 * If we are at top level and the line looks like a
8164 * function declaration, we are done
8165 * (it's a variable declaration).
8166 */
8167 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008168 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 {
8170 /* if the line is terminated with another ','
8171 * it is a continued variable initialization.
8172 * don't add extra indent.
8173 * TODO: does not work, if a function
8174 * declaration is split over multiple lines:
8175 * cin_isfuncdecl returns FALSE then.
8176 */
8177 if (terminated == ',')
8178 break;
8179
8180 /* if it es a enum declaration or an assignment,
8181 * we are done.
8182 */
8183 if (terminated != ';' && cin_isinit())
8184 break;
8185
8186 /* nothing useful found */
8187 if (terminated == 0 || terminated == '{')
8188 continue;
8189 }
8190
8191 if (terminated != ';')
8192 {
8193 /* Skip parens and braces. Position the cursor
8194 * over the rightmost paren, so that matching it
8195 * will take us back to the start of the line.
8196 */ /* XXX */
8197 trypos = NULL;
8198 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008199 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008200 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201
8202 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008203 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204
8205 if (trypos != NULL)
8206 {
8207 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008208 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 continue;
8210 }
8211 }
8212
8213 /* it's a variable declaration, add indentation
8214 * like in
8215 * int a,
8216 * b;
8217 */
8218 if (cont_amount > 0)
8219 amount = cont_amount;
8220 else
8221 amount += ind_continuation;
8222 }
8223 else if (lookfor == LOOKFOR_UNTERM)
8224 {
8225 if (cont_amount > 0)
8226 amount = cont_amount;
8227 else
8228 amount += ind_continuation;
8229 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008230 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008231 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02008232 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008233 && lookfor != LOOKFOR_CPP_BASECLASS
8234 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02008235 {
8236 amount = scope_amount;
8237 if (theline[0] == '{')
8238 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008239 amount += curbuf->b_ind_open_extra;
8240 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008241 }
8242 }
8243
8244 if (lookfor_cpp_namespace)
8245 {
8246 /*
8247 * Looking for C++ namespace, need to look further
8248 * back.
8249 */
8250 if (curwin->w_cursor.lnum == ourscope)
8251 continue;
8252
8253 if (curwin->w_cursor.lnum == 0
8254 || curwin->w_cursor.lnum
8255 < ourscope - FIND_NAMESPACE_LIM)
8256 break;
8257
8258 l = ml_get_curline();
8259
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008260 /* If we're in a comment or raw string now, skip
8261 * to the start of it. */
Bram Moolenaardde81312017-08-26 17:49:01 +02008262 trypos = ind_find_start_CORS(NULL);
Bram Moolenaare79d1532011-10-04 18:03:47 +02008263 if (trypos != NULL)
8264 {
8265 curwin->w_cursor.lnum = trypos->lnum + 1;
8266 curwin->w_cursor.col = 0;
8267 continue;
8268 }
8269
8270 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008271 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8272 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02008273 continue;
8274
8275 /* Finally the actual check for "namespace". */
8276 if (cin_is_cpp_namespace(l))
8277 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008278 amount += curbuf->b_ind_cpp_namespace
8279 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008280 break;
8281 }
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008282 else if (cin_is_cpp_extern_c(l))
8283 {
8284 amount += curbuf->b_ind_cpp_extern_c
8285 - added_to_amount;
8286 break;
8287 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008288
8289 if (cin_nocode(l))
8290 continue;
8291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 }
8293 break;
8294 }
8295
8296 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008297 * If we're in a comment or raw string now, skip to the start
8298 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008300 if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 {
8302 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008303 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 continue;
8305 }
8306
8307 l = ml_get_curline();
8308
8309 /*
8310 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008311 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008313 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 if (iscase || cin_isscopedecl(l))
8315 {
8316 /* we are only looking for cpp base class
8317 * declaration/initialization any longer */
8318 if (lookfor == LOOKFOR_CPP_BASECLASS)
8319 break;
8320
8321 /* When looking for a "do" we are not interested in
8322 * labels. */
8323 if (whilelevel > 0)
8324 continue;
8325
8326 /*
8327 * case xx:
8328 * c = 99 + <- this indent plus continuation
8329 *-> here;
8330 */
8331 if (lookfor == LOOKFOR_UNTERM
8332 || lookfor == LOOKFOR_ENUM_OR_INIT)
8333 {
8334 if (cont_amount > 0)
8335 amount = cont_amount;
8336 else
8337 amount += ind_continuation;
8338 break;
8339 }
8340
8341 /*
8342 * case xx: <- line up with this case
8343 * x = 333;
8344 * case yy:
8345 */
8346 if ( (iscase && lookfor == LOOKFOR_CASE)
8347 || (iscase && lookfor_break)
8348 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8349 {
8350 /*
8351 * Check that this case label is not for another
8352 * switch()
8353 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008354 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008355 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 {
8357 amount = get_indent(); /* XXX */
8358 break;
8359 }
8360 continue;
8361 }
8362
8363 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8364
8365 /*
8366 * case xx: if (cond) <- line up with this if
8367 * y = y + 1;
8368 * -> s = 99;
8369 *
8370 * case xx:
8371 * if (cond) <- line up with this line
8372 * y = y + 1;
8373 * -> s = 99;
8374 */
8375 if (lookfor == LOOKFOR_TERM)
8376 {
8377 if (n)
8378 amount = n;
8379
8380 if (!lookfor_break)
8381 break;
8382 }
8383
8384 /*
8385 * case xx: x = x + 1; <- line up with this x
8386 * -> y = y + 1;
8387 *
8388 * case xx: if (cond) <- line up with this if
8389 * -> y = y + 1;
8390 */
8391 if (n)
8392 {
8393 amount = n;
8394 l = after_label(ml_get_curline());
8395 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008396 {
8397 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008398 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008399 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008400 amount += curbuf->b_ind_level
8401 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 break;
8404 }
8405
8406 /*
8407 * Try to get the indent of a statement before the switch
8408 * label. If nothing is found, line up relative to the
8409 * switch label.
8410 * break; <- may line up with this line
8411 * case xx:
8412 * -> y = 1;
8413 */
8414 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008415 ? curbuf->b_ind_case_code
8416 : curbuf->b_ind_scopedecl_code);
8417 lookfor = curbuf->b_ind_case_break
8418 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 continue;
8420 }
8421
8422 /*
8423 * Looking for a switch() label or C++ scope declaration,
8424 * ignore other lines, skip {}-blocks.
8425 */
8426 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8427 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008428 if (find_last_paren(l, '{', '}')
8429 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008430 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008432 curwin->w_cursor.col = 0;
8433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 continue;
8435 }
8436
8437 /*
8438 * Ignore jump labels with nothing after them.
8439 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008440 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 {
8442 l = after_label(ml_get_curline());
8443 if (l == NULL || cin_nocode(l))
8444 continue;
8445 }
8446
8447 /*
8448 * Ignore #defines, #if, etc.
8449 * Ignore comment and empty lines.
8450 * (need to get the line again, cin_islabel() may have
8451 * unlocked it)
8452 */
8453 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008454 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 || cin_nocode(l))
8456 continue;
8457
8458 /*
8459 * Are we at the start of a cpp base class declaration or
8460 * constructor initialization?
8461 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008462 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008463 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008464 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008465 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008466 l = ml_get_curline();
8467 }
8468 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 {
8470 if (lookfor == LOOKFOR_UNTERM)
8471 {
8472 if (cont_amount > 0)
8473 amount = cont_amount;
8474 else
8475 amount += ind_continuation;
8476 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008477 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008479 /* Need to find start of the declaration. */
8480 lookfor = LOOKFOR_UNTERM;
8481 ind_continuation = 0;
8482 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 }
8484 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008485 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008486 amount = get_baseclass_amount(
8487 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 break;
8489 }
8490 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8491 {
8492 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008493 * declaration or initialization before the opening brace.
8494 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 if (cin_isterminated(l, TRUE, FALSE))
8496 break;
8497 else
8498 continue;
8499 }
8500
8501 /*
8502 * What happens next depends on the line being terminated.
8503 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008504 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 * 123,
8506 * sizeof
8507 * here
8508 * Otherwise check whether it is a enumeration or structure
8509 * initialisation (not indented) or a variable declaration
8510 * (indented).
8511 */
8512 terminated = cin_isterminated(l, FALSE, TRUE);
8513
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008514 if (js_cur_has_key)
8515 {
8516 js_cur_has_key = 0; /* only check the first line */
8517 if (curbuf->b_ind_js && terminated == ',')
8518 {
8519 /* For Javascript we might be inside an object:
8520 * key: something, <- align with this
8521 * key: something
8522 * or:
8523 * key: something + <- align with this
8524 * something,
8525 * key: something
8526 */
8527 lookfor = LOOKFOR_JS_KEY;
8528 }
8529 }
8530 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8531 {
8532 amount = get_indent();
8533 break;
8534 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008535 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008536 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008537 if (tryposBrace != NULL && tryposBrace->lnum
8538 >= curwin->w_cursor.lnum)
8539 break;
8540 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008541 /* line below current line is the one that starts a
8542 * (possibly broken) line ending in a comma. */
8543 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008544 else
8545 {
8546 amount = get_indent();
8547 if (curwin->w_cursor.lnum - 1 == ourscope)
8548 /* line above is start of the scope, thus current
8549 * line is the one that stars a (possibly broken)
8550 * line ending in a comma. */
8551 break;
8552 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008553 }
8554
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8556 && terminated == ','))
8557 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008558 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8559 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008560 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 /*
8562 * if we're in the middle of a paren thing,
8563 * go back to the line that starts it so
8564 * we can get the right prevailing indent
8565 * if ( foo &&
8566 * bar )
8567 */
8568 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008569 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008571 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 */
8573 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008574 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008575 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8576 || (trypos->lnum == tryposBrace->lnum
8577 && trypos->col < tryposBrace->col)))
8578 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579
8580 /*
8581 * If we are looking for ',', we also look for matching
8582 * braces.
8583 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008584 if (trypos == NULL && terminated == ','
8585 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008586 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587
8588 if (trypos != NULL)
8589 {
8590 /*
8591 * Check if we are on a case label now. This is
8592 * handled above.
8593 * case xx: if ( asdf &&
8594 * asdf)
8595 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008596 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008598 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 {
8600 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008601 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 continue;
8603 }
8604 }
8605
8606 /*
8607 * Skip over continuation lines to find the one to get the
8608 * indent from
8609 * char *usethis = "bla\
8610 * bla",
8611 * here;
8612 */
8613 if (terminated == ',')
8614 {
8615 while (curwin->w_cursor.lnum > 1)
8616 {
8617 l = ml_get(curwin->w_cursor.lnum - 1);
8618 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8619 break;
8620 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008621 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 }
8623 }
8624
8625 /*
8626 * Get indent and pointer to text for current line,
8627 * ignoring any jump label. XXX
8628 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008629 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008630 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008631 else
8632 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 /*
8634 * If this is just above the line we are indenting, and it
8635 * starts with a '{', line it up with this line.
8636 * while (not)
8637 * -> {
8638 * }
8639 */
8640 if (terminated != ',' && lookfor != LOOKFOR_TERM
8641 && theline[0] == '{')
8642 {
8643 amount = cur_amount;
8644 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008645 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 * doesn't start with a '{', which must have a match
8647 * in the same line (scope is the same). Probably:
8648 * { 1, 2 },
8649 * -> { 3, 4 }
8650 */
8651 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008652 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008654 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 {
8656 /* have to look back, whether it is a cpp base
8657 * class declaration or initialization */
8658 lookfor = LOOKFOR_CPP_BASECLASS;
8659 continue;
8660 }
8661 break;
8662 }
8663
8664 /*
8665 * Check if we are after an "if", "while", etc.
8666 * Also allow " } else".
8667 */
8668 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8669 {
8670 /*
8671 * Found an unterminated line after an if (), line up
8672 * with the last one.
8673 * if (cond)
8674 * 100 +
8675 * -> here;
8676 */
8677 if (lookfor == LOOKFOR_UNTERM
8678 || lookfor == LOOKFOR_ENUM_OR_INIT)
8679 {
8680 if (cont_amount > 0)
8681 amount = cont_amount;
8682 else
8683 amount += ind_continuation;
8684 break;
8685 }
8686
8687 /*
8688 * If this is just above the line we are indenting, we
8689 * are finished.
8690 * while (not)
8691 * -> here;
8692 * Otherwise this indent can be used when the line
8693 * before this is terminated.
8694 * yyy;
8695 * if (stat)
8696 * while (not)
8697 * xxx;
8698 * -> here;
8699 */
8700 amount = cur_amount;
8701 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008702 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 if (lookfor != LOOKFOR_TERM)
8704 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008705 amount += curbuf->b_ind_level
8706 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 break;
8708 }
8709
8710 /*
8711 * Special trick: when expecting the while () after a
8712 * do, line up with the while()
8713 * do
8714 * x = 1;
8715 * -> here
8716 */
8717 l = skipwhite(ml_get_curline());
8718 if (cin_isdo(l))
8719 {
8720 if (whilelevel == 0)
8721 break;
8722 --whilelevel;
8723 }
8724
8725 /*
8726 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008727 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 * Need to use the scope of this "else". XXX
8729 * If whilelevel != 0 continue looking for a "do {".
8730 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008731 if (cin_iselse(l) && whilelevel == 0)
8732 {
8733 /* If we're looking at "} else", let's make sure we
8734 * find the opening brace of the enclosing scope,
8735 * not the one from "if () {". */
8736 if (*l == '}')
8737 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008738 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008739
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008740 if ((trypos = find_start_brace()) == NULL
8741 || find_match(LOOKFOR_IF, trypos->lnum)
8742 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008743 break;
8744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 }
8746
8747 /*
8748 * If we're below an unterminated line that is not an
8749 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008750 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 * the line before this one.
8752 */
8753 else
8754 {
8755 /*
8756 * Found two unterminated lines on a row, line up with
8757 * the last one.
8758 * c = 99 +
8759 * 100 +
8760 * -> here;
8761 */
8762 if (lookfor == LOOKFOR_UNTERM)
8763 {
8764 /* When line ends in a comma add extra indent */
8765 if (terminated == ',')
8766 amount += ind_continuation;
8767 break;
8768 }
8769
8770 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8771 {
8772 /* Found two lines ending in ',', lineup with the
8773 * lowest one, but check for cpp base class
8774 * declaration/initialization, if it is an
8775 * opening brace or we are looking just for
8776 * enumerations/initializations. */
8777 if (terminated == ',')
8778 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008779 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 break;
8781
8782 lookfor = LOOKFOR_CPP_BASECLASS;
8783 continue;
8784 }
8785
8786 /* Ignore unterminated lines in between, but
8787 * reduce indent. */
8788 if (amount > cur_amount)
8789 amount = cur_amount;
8790 }
8791 else
8792 {
8793 /*
8794 * Found first unterminated line on a row, may
8795 * line up with this line, remember its indent
8796 * 100 +
8797 * -> here;
8798 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008799 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008801
8802 n = (int)STRLEN(l);
8803 if (terminated == ',' && (*skipwhite(l) == ']'
8804 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008805 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806
8807 /*
8808 * If previous line ends in ',', check whether we
8809 * are in an initialization or enum
8810 * struct xxx =
8811 * {
8812 * sizeof a,
8813 * 124 };
8814 * or a normal possible continuation line.
8815 * but only, of no other statement has been found
8816 * yet.
8817 */
8818 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8819 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008820 if (curbuf->b_ind_js)
8821 {
8822 /* Search for a line ending in a comma
8823 * and line up with the line below it
8824 * (could be the current line).
8825 * some = [
8826 * 1, <- line up here
8827 * 2,
8828 * some = [
8829 * 3 + <- line up here
8830 * 4 *
8831 * 5,
8832 * 6,
8833 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008834 if (cin_iscomment(skipwhite(l)))
8835 break;
8836 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008837 trypos = find_match_char('[',
8838 curbuf->b_ind_maxparen);
8839 if (trypos != NULL)
8840 {
8841 if (trypos->lnum
8842 == curwin->w_cursor.lnum - 1)
8843 {
8844 /* Current line is first inside
8845 * [], line up with it. */
8846 break;
8847 }
8848 ourscope = trypos->lnum;
8849 }
8850 }
8851 else
8852 {
8853 lookfor = LOOKFOR_ENUM_OR_INIT;
8854 cont_amount = cin_first_id_amount();
8855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 }
8857 else
8858 {
8859 if (lookfor == LOOKFOR_INITIAL
8860 && *l != NUL
8861 && l[STRLEN(l) - 1] == '\\')
8862 /* XXX */
8863 cont_amount = cin_get_equal_amount(
8864 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008865 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008866 && lookfor != LOOKFOR_JS_KEY
Bram Moolenaardde81312017-08-26 17:49:01 +02008867 && lookfor != LOOKFOR_COMMA
8868 && raw_string_start != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 lookfor = LOOKFOR_UNTERM;
8870 }
8871 }
8872 }
8873 }
8874
8875 /*
8876 * Check if we are after a while (cond);
8877 * If so: Ignore until the matching "do".
8878 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008879 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 {
8881 /*
8882 * Found an unterminated line after a while ();, line up
8883 * with the last one.
8884 * while (cond);
8885 * 100 + <- line up with this one
8886 * -> here;
8887 */
8888 if (lookfor == LOOKFOR_UNTERM
8889 || lookfor == LOOKFOR_ENUM_OR_INIT)
8890 {
8891 if (cont_amount > 0)
8892 amount = cont_amount;
8893 else
8894 amount += ind_continuation;
8895 break;
8896 }
8897
8898 if (whilelevel == 0)
8899 {
8900 lookfor = LOOKFOR_TERM;
8901 amount = get_indent(); /* XXX */
8902 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008903 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 }
8905 ++whilelevel;
8906 }
8907
8908 /*
8909 * We are after a "normal" statement.
8910 * If we had another statement we can stop now and use the
8911 * indent of that other statement.
8912 * Otherwise the indent of the current statement may be used,
8913 * search backwards for the next "normal" statement.
8914 */
8915 else
8916 {
8917 /*
8918 * Skip single break line, if before a switch label. It
8919 * may be lined up with the case label.
8920 */
8921 if (lookfor == LOOKFOR_NOBREAK
8922 && cin_isbreak(skipwhite(ml_get_curline())))
8923 {
8924 lookfor = LOOKFOR_ANY;
8925 continue;
8926 }
8927
8928 /*
8929 * Handle "do {" line.
8930 */
8931 if (whilelevel > 0)
8932 {
8933 l = cin_skipcomment(ml_get_curline());
8934 if (cin_isdo(l))
8935 {
8936 amount = get_indent(); /* XXX */
8937 --whilelevel;
8938 continue;
8939 }
8940 }
8941
8942 /*
8943 * Found a terminated line above an unterminated line. Add
8944 * the amount for a continuation line.
8945 * x = 1;
8946 * y = foo +
8947 * -> here;
8948 * or
8949 * int x = 1;
8950 * int foo,
8951 * -> here;
8952 */
8953 if (lookfor == LOOKFOR_UNTERM
8954 || lookfor == LOOKFOR_ENUM_OR_INIT)
8955 {
8956 if (cont_amount > 0)
8957 amount = cont_amount;
8958 else
8959 amount += ind_continuation;
8960 break;
8961 }
8962
8963 /*
8964 * Found a terminated line above a terminated line or "if"
8965 * etc. line. Use the amount of the line below us.
8966 * x = 1; x = 1;
8967 * if (asdf) y = 2;
8968 * while (asdf) ->here;
8969 * here;
8970 * ->foo;
8971 */
8972 if (lookfor == LOOKFOR_TERM)
8973 {
8974 if (!lookfor_break && whilelevel == 0)
8975 break;
8976 }
8977
8978 /*
8979 * First line above the one we're indenting is terminated.
8980 * To know what needs to be done look further backward for
8981 * a terminated line.
8982 */
8983 else
8984 {
8985 /*
8986 * position the cursor over the rightmost paren, so
8987 * that matching it will take us back to the start of
8988 * the line. Helps for:
8989 * func(asdr,
8990 * asdfasdf);
8991 * here;
8992 */
8993term_again:
8994 l = ml_get_curline();
8995 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008996 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008997 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 {
8999 /*
9000 * Check if we are on a case label now. This is
9001 * handled above.
9002 * case xx: if ( asdf &&
9003 * asdf)
9004 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009005 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02009007 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 {
9009 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009010 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 continue;
9012 }
9013 }
9014
9015 /* When aligning with the case statement, don't align
9016 * with a statement after it.
9017 * case 1: { <-- don't use this { position
9018 * stat;
9019 * }
9020 * case 2:
9021 * stat;
9022 * }
9023 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009024 iscase = (curbuf->b_ind_keep_case_label
9025 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026
9027 /*
9028 * Get indent and pointer to text for current line,
9029 * ignoring any jump label.
9030 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009031 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032
9033 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009034 amount += curbuf->b_ind_open_extra;
9035 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00009036 l = skipwhite(l);
9037 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009038 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
9040
9041 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009042 * When a terminated line starts with "else" skip to
9043 * the matching "if":
9044 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009045 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009046 * Need to use the scope of this "else". XXX
9047 * If whilelevel != 0 continue looking for a "do {".
9048 */
9049 if (lookfor == LOOKFOR_TERM
9050 && *l != '}'
9051 && cin_iselse(l)
9052 && whilelevel == 0)
9053 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009054 if ((trypos = find_start_brace()) == NULL
9055 || find_match(LOOKFOR_IF, trypos->lnum)
9056 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009057 break;
9058 continue;
9059 }
9060
9061 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 * If we're at the end of a block, skip to the start of
9063 * that block.
9064 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01009065 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009066 if (find_last_paren(l, '{', '}') /* XXX */
9067 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009069 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 /* if not "else {" check for terminated again */
9071 /* but skip block for "} else {" */
9072 l = cin_skipcomment(ml_get_curline());
9073 if (*l == '}' || !cin_iselse(l))
9074 goto term_again;
9075 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009076 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 }
9078 }
9079 }
9080 }
9081 }
9082 }
9083
9084 /* add extra indent for a comment */
9085 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009086 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02009087
9088 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009089 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
9090 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009091
9092 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009094
9095 /*
9096 * ok -- we're not inside any sort of structure at all!
9097 *
9098 * This means we're at the top level, and everything should
9099 * basically just match where the previous line is, except
9100 * for the lines immediately following a function declaration,
9101 * which are K&R-style parameters and need to be indented.
9102 *
9103 * if our line starts with an open brace, forget about any
9104 * prevailing indent and make sure it looks like the start
9105 * of a function
9106 */
9107
9108 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009110 amount = curbuf->b_ind_first_open;
9111 goto theend;
9112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009114 /*
9115 * If the NEXT line is a function declaration, the current
9116 * line needs to be indented as a function type spec.
9117 * Don't do this if the current line looks like a comment or if the
9118 * current line is terminated, ie. ends in ';', or if the current line
9119 * contains { or }: "void f() {\n if (1)"
9120 */
9121 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
9122 && !cin_nocode(theline)
9123 && vim_strchr(theline, '{') == NULL
9124 && vim_strchr(theline, '}') == NULL
9125 && !cin_ends_in(theline, (char_u *)":", NULL)
9126 && !cin_ends_in(theline, (char_u *)",", NULL)
9127 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
9128 cur_curpos.lnum + 1)
9129 && !cin_isterminated(theline, FALSE, TRUE))
9130 {
9131 amount = curbuf->b_ind_func_type;
9132 goto theend;
9133 }
9134
9135 /* search backwards until we find something we recognize */
9136 amount = 0;
9137 curwin->w_cursor = cur_curpos;
9138 while (curwin->w_cursor.lnum > 1)
9139 {
9140 curwin->w_cursor.lnum--;
9141 curwin->w_cursor.col = 0;
9142
9143 l = ml_get_curline();
9144
9145 /*
9146 * If we're in a comment or raw string now, skip to the start
9147 * of it.
9148 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02009149 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009151 curwin->w_cursor.lnum = trypos->lnum + 1;
9152 curwin->w_cursor.col = 0;
9153 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 }
9155
9156 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009157 * Are we at the start of a cpp base class declaration or
9158 * constructor initialization?
9159 */ /* XXX */
9160 n = FALSE;
9161 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009163 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
9164 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009166 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009168 /* XXX */
9169 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
9170 break;
9171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009173 /*
9174 * Skip preprocessor directives and blank lines.
9175 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009176 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009177 continue;
9178
9179 if (cin_nocode(l))
9180 continue;
9181
9182 /*
9183 * If the previous line ends in ',', use one level of
9184 * indentation:
9185 * int foo,
9186 * bar;
9187 * do this before checking for '}' in case of eg.
9188 * enum foobar
9189 * {
9190 * ...
9191 * } foo,
9192 * bar;
9193 */
9194 n = 0;
9195 if (cin_ends_in(l, (char_u *)",", NULL)
9196 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
9197 {
9198 /* take us back to opening paren */
9199 if (find_last_paren(l, '(', ')')
9200 && (trypos = find_match_paren(
9201 curbuf->b_ind_maxparen)) != NULL)
9202 curwin->w_cursor = *trypos;
9203
9204 /* For a line ending in ',' that is a continuation line go
9205 * back to the first line with a backslash:
9206 * char *foo = "bla\
9207 * bla",
9208 * here;
9209 */
9210 while (n == 0 && curwin->w_cursor.lnum > 1)
9211 {
9212 l = ml_get(curwin->w_cursor.lnum - 1);
9213 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
9214 break;
9215 --curwin->w_cursor.lnum;
9216 curwin->w_cursor.col = 0;
9217 }
9218
9219 amount = get_indent(); /* XXX */
9220
9221 if (amount == 0)
9222 amount = cin_first_id_amount();
9223 if (amount == 0)
9224 amount = ind_continuation;
9225 break;
9226 }
9227
9228 /*
9229 * If the line looks like a function declaration, and we're
9230 * not in a comment, put it the left margin.
9231 */
9232 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
9233 break;
9234 l = ml_get_curline();
9235
9236 /*
9237 * Finding the closing '}' of a previous function. Put
9238 * current line at the left margin. For when 'cino' has "fs".
9239 */
9240 if (*skipwhite(l) == '}')
9241 break;
9242
9243 /* (matching {)
9244 * If the previous line ends on '};' (maybe followed by
9245 * comments) align at column 0. For example:
9246 * char *string_array[] = { "foo",
9247 * / * x * / "b};ar" }; / * foobar * /
9248 */
9249 if (cin_ends_in(l, (char_u *)"};", NULL))
9250 break;
9251
9252 /*
9253 * If the previous line ends on '[' we are probably in an
9254 * array constant:
9255 * something = [
9256 * 234, <- extra indent
9257 */
9258 if (cin_ends_in(l, (char_u *)"[", NULL))
9259 {
9260 amount = get_indent() + ind_continuation;
9261 break;
9262 }
9263
9264 /*
9265 * Find a line only has a semicolon that belongs to a previous
9266 * line ending in '}', e.g. before an #endif. Don't increase
9267 * indent then.
9268 */
9269 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
9270 {
9271 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272
9273 while (curwin->w_cursor.lnum > 1)
9274 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009275 look = ml_get(--curwin->w_cursor.lnum);
9276 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009277 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009279 }
9280 if (curwin->w_cursor.lnum > 0
9281 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009284 curwin->w_cursor = curpos_save;
9285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009287 /*
9288 * If the PREVIOUS line is a function declaration, the current
9289 * line (and the ones that follow) needs to be indented as
9290 * parameters.
9291 */
9292 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
9293 {
9294 amount = curbuf->b_ind_param;
9295 break;
9296 }
9297
9298 /*
9299 * If the previous line ends in ';' and the line before the
9300 * previous line ends in ',' or '\', ident to column zero:
9301 * int foo,
9302 * bar;
9303 * indent_to_0 here;
9304 */
9305 if (cin_ends_in(l, (char_u *)";", NULL))
9306 {
9307 l = ml_get(curwin->w_cursor.lnum - 1);
9308 if (cin_ends_in(l, (char_u *)",", NULL)
9309 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9310 break;
9311 l = ml_get_curline();
9312 }
9313
9314 /*
9315 * Doesn't look like anything interesting -- so just
9316 * use the indent of this line.
9317 *
9318 * Position the cursor over the rightmost paren, so that
9319 * matching it will take us back to the start of the line.
9320 */
9321 find_last_paren(l, '(', ')');
9322
9323 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9324 curwin->w_cursor = *trypos;
9325 amount = get_indent(); /* XXX */
9326 break;
9327 }
9328
9329 /* add extra indent for a comment */
9330 if (cin_iscomment(theline))
9331 amount += curbuf->b_ind_comment;
9332
9333 /* add extra indent if the previous line ended in a backslash:
9334 * "asdfasdf\
9335 * here";
9336 * char *foo = "asdf\
9337 * here";
9338 */
9339 if (cur_curpos.lnum > 1)
9340 {
9341 l = ml_get(cur_curpos.lnum - 1);
9342 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9343 {
9344 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9345 if (cur_amount > 0)
9346 amount = cur_amount;
9347 else if (cur_amount == 0)
9348 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349 }
9350 }
9351
9352theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009353 if (amount < 0)
9354 amount = 0;
9355
9356laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 /* put the cursor back where it belongs */
9358 curwin->w_cursor = cur_curpos;
9359
9360 vim_free(linecopy);
9361
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 return amount;
9363}
9364
9365 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009366find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367{
9368 char_u *look;
9369 pos_T *theirscope;
9370 char_u *mightbeif;
9371 int elselevel;
9372 int whilelevel;
9373
9374 if (lookfor == LOOKFOR_IF)
9375 {
9376 elselevel = 1;
9377 whilelevel = 0;
9378 }
9379 else
9380 {
9381 elselevel = 0;
9382 whilelevel = 1;
9383 }
9384
9385 curwin->w_cursor.col = 0;
9386
9387 while (curwin->w_cursor.lnum > ourscope + 1)
9388 {
9389 curwin->w_cursor.lnum--;
9390 curwin->w_cursor.col = 0;
9391
9392 look = cin_skipcomment(ml_get_curline());
9393 if (cin_iselse(look)
9394 || cin_isif(look)
9395 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009396 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 {
9398 /*
9399 * if we've gone outside the braces entirely,
9400 * we must be out of scope...
9401 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009402 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 if (theirscope == NULL)
9404 break;
9405
9406 /*
9407 * and if the brace enclosing this is further
9408 * back than the one enclosing the else, we're
9409 * out of luck too.
9410 */
9411 if (theirscope->lnum < ourscope)
9412 break;
9413
9414 /*
9415 * and if they're enclosed in a *deeper* brace,
9416 * then we can ignore it because it's in a
9417 * different scope...
9418 */
9419 if (theirscope->lnum > ourscope)
9420 continue;
9421
9422 /*
9423 * if it was an "else" (that's not an "else if")
9424 * then we need to go back to another if, so
9425 * increment elselevel
9426 */
9427 look = cin_skipcomment(ml_get_curline());
9428 if (cin_iselse(look))
9429 {
9430 mightbeif = cin_skipcomment(look + 4);
9431 if (!cin_isif(mightbeif))
9432 ++elselevel;
9433 continue;
9434 }
9435
9436 /*
9437 * if it was a "while" then we need to go back to
9438 * another "do", so increment whilelevel. XXX
9439 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009440 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 {
9442 ++whilelevel;
9443 continue;
9444 }
9445
9446 /* If it's an "if" decrement elselevel */
9447 look = cin_skipcomment(ml_get_curline());
9448 if (cin_isif(look))
9449 {
9450 elselevel--;
9451 /*
9452 * When looking for an "if" ignore "while"s that
9453 * get in the way.
9454 */
9455 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9456 whilelevel = 0;
9457 }
9458
9459 /* If it's a "do" decrement whilelevel */
9460 if (cin_isdo(look))
9461 whilelevel--;
9462
9463 /*
9464 * if we've used up all the elses, then
9465 * this must be the if that we want!
9466 * match the indent level of that if.
9467 */
9468 if (elselevel <= 0 && whilelevel <= 0)
9469 {
9470 return OK;
9471 }
9472 }
9473 }
9474 return FAIL;
9475}
9476
9477# if defined(FEAT_EVAL) || defined(PROTO)
9478/*
9479 * Get indent level from 'indentexpr'.
9480 */
9481 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009482get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483{
Bram Moolenaar97db5542017-04-21 23:18:26 +02009484 int indent = -1;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009485 char_u *inde_copy;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009486 pos_T save_pos;
9487 colnr_T save_curswant;
9488 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009490 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9491 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009493 /* Save and restore cursor position and curswant, in case it was changed
9494 * via :normal commands */
9495 save_pos = curwin->w_cursor;
9496 save_curswant = curwin->w_curswant;
9497 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009499 if (use_sandbox)
9500 ++sandbox;
9501 ++textlock;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009502
9503 /* Need to make a copy, the 'indentexpr' option could be changed while
9504 * evaluating it. */
9505 inde_copy = vim_strsave(curbuf->b_p_inde);
9506 if (inde_copy != NULL)
9507 {
9508 indent = (int)eval_to_number(inde_copy);
9509 vim_free(inde_copy);
9510 }
9511
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009512 if (use_sandbox)
9513 --sandbox;
9514 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515
9516 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9517 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9518 * command. */
9519 save_State = State;
9520 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009521 curwin->w_cursor = save_pos;
9522 curwin->w_curswant = save_curswant;
9523 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 check_cursor();
9525 State = save_State;
9526
9527 /* If there is an error, just keep the current indent. */
9528 if (indent < 0)
9529 indent = get_indent();
9530
9531 return indent;
9532}
9533# endif
9534
9535#endif /* FEAT_CINDENT */
9536
9537#if defined(FEAT_LISP) || defined(PROTO)
9538
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009540lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541{
9542 char_u buf[LSIZE];
9543 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009544 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545
9546 while (*word != NUL)
9547 {
9548 (void)copy_option_part(&word, buf, LSIZE, ",");
9549 len = (int)STRLEN(buf);
9550 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9551 return TRUE;
9552 }
9553 return FALSE;
9554}
9555
9556/*
9557 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9558 * The incompatible newer method is quite a bit better at indenting
9559 * code in lisp-like languages than the traditional one; it's still
9560 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9561 *
9562 * TODO:
9563 * Findmatch() should be adapted for lisp, also to make showmatch
9564 * work correctly: now (v5.3) it seems all C/C++ oriented:
9565 * - it does not recognize the #\( and #\) notations as character literals
9566 * - it doesn't know about comments starting with a semicolon
9567 * - it incorrectly interprets '(' as a character literal
9568 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009569 * Update from Sergey Khorev:
9570 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 */
9572 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009573get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009575 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 int amount;
9577 char_u *that;
9578 colnr_T col;
9579 colnr_T firsttry;
9580 int parencount, quotecount;
9581 int vi_lisp;
9582
9583 /* Set vi_lisp to use the vi-compatible method */
9584 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9585
9586 realpos = curwin->w_cursor;
9587 curwin->w_cursor.col = 0;
9588
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009589 if ((pos = findmatch(NULL, '(')) == NULL)
9590 pos = findmatch(NULL, '[');
9591 else
9592 {
9593 paren = *pos;
9594 pos = findmatch(NULL, '[');
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009595 if (pos == NULL || LT_POSP(pos, &paren))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009596 pos = &paren;
9597 }
9598 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 {
9600 /* Extra trick: Take the indent of the first previous non-white
9601 * line that is at the same () level. */
9602 amount = -1;
9603 parencount = 0;
9604
9605 while (--curwin->w_cursor.lnum >= pos->lnum)
9606 {
9607 if (linewhite(curwin->w_cursor.lnum))
9608 continue;
9609 for (that = ml_get_curline(); *that != NUL; ++that)
9610 {
9611 if (*that == ';')
9612 {
9613 while (*(that + 1) != NUL)
9614 ++that;
9615 continue;
9616 }
9617 if (*that == '\\')
9618 {
9619 if (*(that + 1) != NUL)
9620 ++that;
9621 continue;
9622 }
9623 if (*that == '"' && *(that + 1) != NUL)
9624 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009625 while (*++that && *that != '"')
9626 {
9627 /* skipping escaped characters in the string */
9628 if (*that == '\\')
9629 {
9630 if (*++that == NUL)
9631 break;
9632 if (that[1] == NUL)
9633 {
9634 ++that;
9635 break;
9636 }
9637 }
9638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009640 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009642 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643 --parencount;
9644 }
9645 if (parencount == 0)
9646 {
9647 amount = get_indent();
9648 break;
9649 }
9650 }
9651
9652 if (amount == -1)
9653 {
9654 curwin->w_cursor.lnum = pos->lnum;
9655 curwin->w_cursor.col = pos->col;
9656 col = pos->col;
9657
9658 that = ml_get_curline();
9659
9660 if (vi_lisp && get_indent() == 0)
9661 amount = 2;
9662 else
9663 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009664 char_u *line = that;
9665
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 amount = 0;
9667 while (*that && col)
9668 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009669 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 col--;
9671 }
9672
9673 /*
9674 * Some keywords require "body" indenting rules (the
9675 * non-standard-lisp ones are Scheme special forms):
9676 *
9677 * (let ((a 1)) instead (let ((a 1))
9678 * (...)) of (...))
9679 */
9680
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009681 if (!vi_lisp && (*that == '(' || *that == '[')
9682 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 amount += 2;
9684 else
9685 {
9686 that++;
9687 amount++;
9688 firsttry = amount;
9689
Bram Moolenaar1c465442017-03-12 20:10:05 +01009690 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009692 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 ++that;
9694 }
9695
9696 if (*that && *that != ';') /* not a comment line */
9697 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009698 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009700 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 firsttry++;
9702
9703 parencount = 0;
9704 quotecount = 0;
9705
9706 if (vi_lisp
9707 || (*that != '"'
9708 && *that != '\''
9709 && *that != '#'
9710 && (*that < '0' || *that > '9')))
9711 {
9712 while (*that
Bram Moolenaar1c465442017-03-12 20:10:05 +01009713 && (!VIM_ISWHITE(*that)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 || quotecount
9715 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009716 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 && !quotecount
9718 && !parencount
9719 && vi_lisp)))
9720 {
9721 if (*that == '"')
9722 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009723 if ((*that == '(' || *that == '[')
9724 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009726 if ((*that == ')' || *that == ']')
9727 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 --parencount;
9729 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009730 amount += lbr_chartabsize_adv(
9731 line, &that, (colnr_T)amount);
9732 amount += lbr_chartabsize_adv(
9733 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 }
9735 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01009736 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009738 amount += lbr_chartabsize(
9739 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 that++;
9741 }
9742 if (!*that || *that == ';')
9743 amount = firsttry;
9744 }
9745 }
9746 }
9747 }
9748 }
9749 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009750 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751
9752 curwin->w_cursor = realpos;
9753
9754 return amount;
9755}
9756#endif /* FEAT_LISP */
9757
9758 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009759prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009761#if defined(SIGHUP) && defined(SIG_IGN)
9762 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9763 * makes Vim exit and then handling SIGHUP causes various reentrance
9764 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009765 signal(SIGHUP, SIG_IGN);
9766#endif
9767
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768#ifdef FEAT_GUI
9769 if (gui.in_use)
9770 {
9771 gui.dying = TRUE;
9772 out_trash(); /* trash any pending output */
9773 }
9774 else
9775#endif
9776 {
9777 windgoto((int)Rows - 1, 0);
9778
9779 /*
9780 * Switch terminal mode back now, so messages end up on the "normal"
9781 * screen (if there are two screens).
9782 */
9783 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009784 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 out_flush();
9786 }
9787}
9788
9789/*
9790 * Preserve files and exit.
9791 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009792 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9793 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 */
9795 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009796preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797{
9798 buf_T *buf;
9799
9800 prepare_to_exit();
9801
Bram Moolenaar4770d092006-01-12 23:22:24 +00009802 /* Setting this will prevent free() calls. That avoids calling free()
9803 * recursively when free() was invoked with a bad pointer. */
9804 really_exiting = TRUE;
9805
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 out_str(IObuff);
9807 screen_start(); /* don't know where cursor is now */
9808 out_flush();
9809
9810 ml_close_notmod(); /* close all not-modified buffers */
9811
Bram Moolenaar29323592016-07-24 22:04:11 +02009812 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 {
9814 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9815 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009816 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 screen_start(); /* don't know where cursor is now */
9818 out_flush();
9819 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9820 break;
9821 }
9822 }
9823
9824 ml_close_all(FALSE); /* close all memfiles, without deleting */
9825
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009826 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827
9828 getout(1);
9829}
9830
9831/*
9832 * return TRUE if "fname" exists.
9833 */
9834 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009835vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009837 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838
9839 if (mch_stat((char *)fname, &st))
9840 return FALSE;
9841 return TRUE;
9842}
9843
9844/*
9845 * Check for CTRL-C pressed, but only once in a while.
9846 * Should be used instead of ui_breakcheck() for functions that check for
9847 * each line in the file. Calling ui_breakcheck() each time takes too much
9848 * time, because it can be a system call.
9849 */
9850
9851#ifndef BREAKCHECK_SKIP
9852# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9853# define BREAKCHECK_SKIP 200
9854# else
9855# define BREAKCHECK_SKIP 32
9856# endif
9857#endif
9858
9859static int breakcheck_count = 0;
9860
9861 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009862line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863{
9864 if (++breakcheck_count >= BREAKCHECK_SKIP)
9865 {
9866 breakcheck_count = 0;
9867 ui_breakcheck();
9868 }
9869}
9870
9871/*
9872 * Like line_breakcheck() but check 10 times less often.
9873 */
9874 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009875fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876{
9877 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9878 {
9879 breakcheck_count = 0;
9880 ui_breakcheck();
9881 }
9882}
9883
9884/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009885 * Invoke expand_wildcards() for one pattern.
9886 * Expand items like "%:h" before the expansion.
9887 * Returns OK or FAIL.
9888 */
9889 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009890expand_wildcards_eval(
9891 char_u **pat, /* pointer to input pattern */
9892 int *num_file, /* resulting number of files */
9893 char_u ***file, /* array of resulting files */
9894 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009895{
9896 int ret = FAIL;
9897 char_u *eval_pat = NULL;
9898 char_u *exp_pat = *pat;
9899 char_u *ignored_msg;
9900 int usedlen;
9901
9902 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9903 {
9904 ++emsg_off;
9905 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9906 NULL, &ignored_msg, NULL);
9907 --emsg_off;
9908 if (eval_pat != NULL)
9909 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9910 }
9911
9912 if (exp_pat != NULL)
9913 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9914
9915 if (eval_pat != NULL)
9916 {
9917 vim_free(exp_pat);
9918 vim_free(eval_pat);
9919 }
9920
9921 return ret;
9922}
9923
9924/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9926 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009927 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928 */
9929 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009930expand_wildcards(
9931 int num_pat, /* number of input patterns */
9932 char_u **pat, /* array of input patterns */
9933 int *num_files, /* resulting number of files */
9934 char_u ***files, /* array of resulting files */
9935 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936{
9937 int retval;
9938 int i, j;
9939 char_u *p;
9940 int non_suf_match; /* number without matching suffix */
9941
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009942 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943
9944 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009945 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 return retval;
9947
9948#ifdef FEAT_WILDIGN
9949 /*
9950 * Remove names that match 'wildignore'.
9951 */
9952 if (*p_wig)
9953 {
9954 char_u *ffname;
9955
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009956 /* check all files in (*files)[] */
9957 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009959 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 if (ffname == NULL) /* out of memory */
9961 break;
9962# ifdef VMS
9963 vms_remove_version(ffname);
9964# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009965 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +01009967 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009968 vim_free((*files)[i]);
9969 for (j = i; j + 1 < *num_files; ++j)
9970 (*files)[j] = (*files)[j + 1];
9971 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 --i;
9973 }
9974 vim_free(ffname);
9975 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009976
9977 /* If the number of matches is now zero, we fail. */
9978 if (*num_files == 0)
9979 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01009980 VIM_CLEAR(*files);
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009981 return FAIL;
9982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983 }
9984#endif
9985
9986 /*
9987 * Move the names where 'suffixes' match to the end.
9988 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009989 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 {
9991 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009992 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009994 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995 {
9996 /*
9997 * Move the name without matching suffix to the front
9998 * of the list.
9999 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010000 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010002 (*files)[j] = (*files)[j - 1];
10003 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 }
10005 }
10006 }
10007
10008 return retval;
10009}
10010
10011/*
10012 * Return TRUE if "fname" matches with an entry in 'suffixes'.
10013 */
10014 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010015match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016{
10017 int fnamelen, setsuflen;
10018 char_u *setsuf;
10019#define MAXSUFLEN 30 /* maximum length of a file suffix */
10020 char_u suf_buf[MAXSUFLEN];
10021
10022 fnamelen = (int)STRLEN(fname);
10023 setsuflen = 0;
10024 for (setsuf = p_su; *setsuf; )
10025 {
10026 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +000010027 if (setsuflen == 0)
10028 {
10029 char_u *tail = gettail(fname);
10030
10031 /* empty entry: match name without a '.' */
10032 if (vim_strchr(tail, '.') == NULL)
10033 {
10034 setsuflen = 1;
10035 break;
10036 }
10037 }
10038 else
10039 {
10040 if (fnamelen >= setsuflen
10041 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
10042 (size_t)setsuflen) == 0)
10043 break;
10044 setsuflen = 0;
10045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 }
10047 return (setsuflen != 0);
10048}
10049
10050#if !defined(NO_EXPANDPATH) || defined(PROTO)
10051
10052# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010053static int vim_backtick(char_u *p);
10054static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055# endif
10056
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010057# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058/*
10059 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
10060 * it's shared between these systems.
10061 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010062# if defined(PROTO)
10063# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064# else
10065# ifdef __BORLANDC__
10066# define _cdecl _RTLENTRYF
10067# endif
10068# endif
10069
10070/*
10071 * comparison function for qsort in dos_expandpath()
10072 */
10073 static int _cdecl
10074pstrcmp(const void *a, const void *b)
10075{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010076 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077}
10078
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079/*
Bram Moolenaar231334e2005-07-25 20:46:57 +000010080 * Recursively expand one path component into all matching files and/or
10081 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 * Return the number of matches found.
10083 * "path" has backslashes before chars that are not to be expanded, starting
10084 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +000010085 * Return the number of matches found.
10086 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 */
10088 static int
10089dos_expandpath(
10090 garray_T *gap,
10091 char_u *path,
10092 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +000010093 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +000010094 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010096 char_u *buf;
10097 char_u *path_end;
10098 char_u *p, *s, *e;
10099 int start_len = gap->ga_len;
10100 char_u *pat;
10101 regmatch_T regmatch;
10102 int starts_with_dot;
10103 int matches;
10104 int len;
10105 int starstar = FALSE;
10106 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 WIN32_FIND_DATA fb;
10108 HANDLE hFind = (HANDLE)0;
10109# ifdef FEAT_MBYTE
10110 WIN32_FIND_DATAW wfb;
10111 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
10112# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010114 int ok;
10115
10116 /* Expanding "**" may take a long time, check for CTRL-C. */
10117 if (stardepth > 0)
10118 {
10119 ui_breakcheck();
10120 if (got_int)
10121 return 0;
10122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123
Bram Moolenaar7314efd2015-10-31 15:32:52 +010010124 /* Make room for file name. When doing encoding conversion the actual
10125 * length may be quite a bit longer, thus use the maximum possible length. */
10126 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 if (buf == NULL)
10128 return 0;
10129
10130 /*
10131 * Find the first part in the path name that contains a wildcard or a ~1.
10132 * Copy it into buf, including the preceding characters.
10133 */
10134 p = buf;
10135 s = buf;
10136 e = NULL;
10137 path_end = path;
10138 while (*path_end != NUL)
10139 {
10140 /* May ignore a wildcard that has a backslash before it; it will
10141 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10142 if (path_end >= path + wildoff && rem_backslash(path_end))
10143 *p++ = *path_end++;
10144 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
10145 {
10146 if (e != NULL)
10147 break;
10148 s = p + 1;
10149 }
10150 else if (path_end >= path + wildoff
10151 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
10152 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +010010153# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154 if (has_mbyte)
10155 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010156 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157 STRNCPY(p, path_end, len);
10158 p += len;
10159 path_end += len;
10160 }
10161 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +010010162# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 *p++ = *path_end++;
10164 }
10165 e = p;
10166 *e = NUL;
10167
10168 /* now we have one wildcard component between s and e */
10169 /* Remove backslashes between "wildoff" and the start of the wildcard
10170 * component. */
10171 for (p = buf + wildoff; p < s; ++p)
10172 if (rem_backslash(p))
10173 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010174 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175 --e;
10176 --s;
10177 }
10178
Bram Moolenaar231334e2005-07-25 20:46:57 +000010179 /* Check for "**" between "s" and "e". */
10180 for (p = s; p < e; ++p)
10181 if (p[0] == '*' && p[1] == '*')
10182 starstar = TRUE;
10183
Bram Moolenaard82103e2016-01-17 17:04:05 +010010184 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10186 if (pat == NULL)
10187 {
10188 vim_free(buf);
10189 return 0;
10190 }
10191
10192 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010193 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010194 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195 regmatch.rm_ic = TRUE; /* Always ignore case */
10196 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010197 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010198 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199 vim_free(pat);
10200
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010201 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 {
10203 vim_free(buf);
10204 return 0;
10205 }
10206
10207 /* remember the pattern or file name being looked for */
10208 matchname = vim_strsave(s);
10209
Bram Moolenaar231334e2005-07-25 20:46:57 +000010210 /* If "**" is by itself, this is the first time we encounter it and more
10211 * is following then find matches without any directory. */
10212 if (!didstar && stardepth < 100 && starstar && e - s == 2
10213 && *path_end == '/')
10214 {
10215 STRCPY(s, path_end + 1);
10216 ++stardepth;
10217 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10218 --stardepth;
10219 }
10220
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 /* Scan all files in the directory with "dir/ *.*" */
10222 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223# ifdef FEAT_MBYTE
10224 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
10225 {
10226 /* The active codepage differs from 'encoding'. Attempt using the
10227 * wide function. If it fails because it is not implemented fall back
10228 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010229 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 if (wn != NULL)
10231 {
10232 hFind = FindFirstFileW(wn, &wfb);
10233 if (hFind == INVALID_HANDLE_VALUE
10234 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
Bram Moolenaard23a8232018-02-10 18:45:26 +010010235 VIM_CLEAR(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236 }
10237 }
10238
10239 if (wn == NULL)
10240# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010241 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010243
10244 while (ok)
10245 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246# ifdef FEAT_MBYTE
10247 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010248 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249 else
10250# endif
10251 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 /* Ignore entries starting with a dot, unless when asked for. Accept
10253 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010254 if ((p[0] != '.' || starts_with_dot
10255 || ((flags & EW_DODOT)
10256 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010258 || (regmatch.regprog != NULL
10259 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010260 || ((flags & EW_NOTWILD)
10261 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010262 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010265
10266 if (starstar && stardepth < 100)
10267 {
10268 /* For "**" in the pattern first go deeper in the tree to
10269 * find matches. */
10270 STRCPY(buf + len, "/**");
10271 STRCPY(buf + len + 3, path_end);
10272 ++stardepth;
10273 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
10274 --stardepth;
10275 }
10276
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277 STRCPY(buf + len, path_end);
10278 if (mch_has_exp_wildcard(path_end))
10279 {
10280 /* need to expand another component of the path */
10281 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010282 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283 }
10284 else
10285 {
10286 /* no more wildcards, check if there is a match */
10287 /* remove backslashes for the remaining components only */
10288 if (*path_end != 0)
10289 backslash_halve(buf + len + 1);
10290 if (mch_getperm(buf) >= 0) /* add existing file */
10291 addfile(gap, buf, flags);
10292 }
10293 }
10294
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295# ifdef FEAT_MBYTE
10296 if (wn != NULL)
10297 {
10298 vim_free(p);
10299 ok = FindNextFileW(hFind, &wfb);
10300 }
10301 else
10302# endif
10303 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304
10305 /* If no more matches and no match was used, try expanding the name
10306 * itself. Finds the long name of a short filename. */
10307 if (!ok && matchname != NULL && gap->ga_len == start_len)
10308 {
10309 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 FindClose(hFind);
10311# ifdef FEAT_MBYTE
10312 if (wn != NULL)
10313 {
10314 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010315 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 if (wn != NULL)
10317 hFind = FindFirstFileW(wn, &wfb);
10318 }
10319 if (wn == NULL)
10320# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010321 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaard23a8232018-02-10 18:45:26 +010010323 VIM_CLEAR(matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324 }
10325 }
10326
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327 FindClose(hFind);
10328# ifdef FEAT_MBYTE
10329 vim_free(wn);
10330# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010332 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 vim_free(matchname);
10334
10335 matches = gap->ga_len - start_len;
10336 if (matches > 0)
10337 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10338 sizeof(char_u *), pstrcmp);
10339 return matches;
10340}
10341
10342 int
10343mch_expandpath(
10344 garray_T *gap,
10345 char_u *path,
10346 int flags) /* EW_* flags */
10347{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010348 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010350# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351
Bram Moolenaar231334e2005-07-25 20:46:57 +000010352#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10353 || defined(PROTO)
10354/*
10355 * Unix style wildcard expansion code.
10356 * It's here because it's used both for Unix and Mac.
10357 */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010358 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010359pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010360{
10361 return (pathcmp(*(char **)a, *(char **)b, -1));
10362}
10363
10364/*
10365 * Recursively expand one path component into all matching files and/or
10366 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10367 * "path" has backslashes before chars that are not to be expanded, starting
10368 * at "path + wildoff".
10369 * Return the number of matches found.
10370 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10371 */
10372 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010373unix_expandpath(
10374 garray_T *gap,
10375 char_u *path,
10376 int wildoff,
10377 int flags, /* EW_* flags */
10378 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010379{
10380 char_u *buf;
10381 char_u *path_end;
10382 char_u *p, *s, *e;
10383 int start_len = gap->ga_len;
10384 char_u *pat;
10385 regmatch_T regmatch;
10386 int starts_with_dot;
10387 int matches;
10388 int len;
10389 int starstar = FALSE;
10390 static int stardepth = 0; /* depth for "**" expansion */
10391
10392 DIR *dirp;
10393 struct dirent *dp;
10394
10395 /* Expanding "**" may take a long time, check for CTRL-C. */
10396 if (stardepth > 0)
10397 {
10398 ui_breakcheck();
10399 if (got_int)
10400 return 0;
10401 }
10402
10403 /* make room for file name */
10404 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10405 if (buf == NULL)
10406 return 0;
10407
10408 /*
10409 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010410 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010411 * Copy it into "buf", including the preceding characters.
10412 */
10413 p = buf;
10414 s = buf;
10415 e = NULL;
10416 path_end = path;
10417 while (*path_end != NUL)
10418 {
10419 /* May ignore a wildcard that has a backslash before it; it will
10420 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10421 if (path_end >= path + wildoff && rem_backslash(path_end))
10422 *p++ = *path_end++;
10423 else if (*path_end == '/')
10424 {
10425 if (e != NULL)
10426 break;
10427 s = p + 1;
10428 }
10429 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010430 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010431 || (!p_fic && (flags & EW_ICASE)
10432 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010433 e = p;
10434#ifdef FEAT_MBYTE
10435 if (has_mbyte)
10436 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010437 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010438 STRNCPY(p, path_end, len);
10439 p += len;
10440 path_end += len;
10441 }
10442 else
10443#endif
10444 *p++ = *path_end++;
10445 }
10446 e = p;
10447 *e = NUL;
10448
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010449 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010450 /* Remove backslashes between "wildoff" and the start of the wildcard
10451 * component. */
10452 for (p = buf + wildoff; p < s; ++p)
10453 if (rem_backslash(p))
10454 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010455 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010456 --e;
10457 --s;
10458 }
10459
10460 /* Check for "**" between "s" and "e". */
10461 for (p = s; p < e; ++p)
10462 if (p[0] == '*' && p[1] == '*')
10463 starstar = TRUE;
10464
10465 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010466 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010467 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10468 if (pat == NULL)
10469 {
10470 vim_free(buf);
10471 return 0;
10472 }
10473
10474 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010475 if (flags & EW_ICASE)
10476 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10477 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010478 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010479 if (flags & (EW_NOERROR | EW_NOTWILD))
10480 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010481 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010482 if (flags & (EW_NOERROR | EW_NOTWILD))
10483 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010484 vim_free(pat);
10485
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010486 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010487 {
10488 vim_free(buf);
10489 return 0;
10490 }
10491
10492 /* If "**" is by itself, this is the first time we encounter it and more
10493 * is following then find matches without any directory. */
10494 if (!didstar && stardepth < 100 && starstar && e - s == 2
10495 && *path_end == '/')
10496 {
10497 STRCPY(s, path_end + 1);
10498 ++stardepth;
10499 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10500 --stardepth;
10501 }
10502
10503 /* open the directory for scanning */
10504 *s = NUL;
10505 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10506
10507 /* Find all matching entries */
10508 if (dirp != NULL)
10509 {
10510 for (;;)
10511 {
10512 dp = readdir(dirp);
10513 if (dp == NULL)
10514 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010515 if ((dp->d_name[0] != '.' || starts_with_dot
10516 || ((flags & EW_DODOT)
10517 && dp->d_name[1] != NUL
10518 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010519 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10520 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010521 || ((flags & EW_NOTWILD)
10522 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010523 {
10524 STRCPY(s, dp->d_name);
10525 len = STRLEN(buf);
10526
10527 if (starstar && stardepth < 100)
10528 {
10529 /* For "**" in the pattern first go deeper in the tree to
10530 * find matches. */
10531 STRCPY(buf + len, "/**");
10532 STRCPY(buf + len + 3, path_end);
10533 ++stardepth;
10534 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10535 --stardepth;
10536 }
10537
10538 STRCPY(buf + len, path_end);
10539 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10540 {
10541 /* need to expand another component of the path */
10542 /* remove backslashes for the remaining components only */
10543 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10544 }
10545 else
10546 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010547 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010548
Bram Moolenaar231334e2005-07-25 20:46:57 +000010549 /* no more wildcards, check if there is a match */
10550 /* remove backslashes for the remaining components only */
10551 if (*path_end != NUL)
10552 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010553 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010554 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010555 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010556 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010557#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010558 size_t precomp_len = STRLEN(buf)+1;
10559 char_u *precomp_buf =
10560 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010561
Bram Moolenaar231334e2005-07-25 20:46:57 +000010562 if (precomp_buf)
10563 {
10564 mch_memmove(buf, precomp_buf, precomp_len);
10565 vim_free(precomp_buf);
10566 }
10567#endif
10568 addfile(gap, buf, flags);
10569 }
10570 }
10571 }
10572 }
10573
10574 closedir(dirp);
10575 }
10576
10577 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010578 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010579
10580 matches = gap->ga_len - start_len;
10581 if (matches > 0)
10582 qsort(((char_u **)gap->ga_data) + start_len, matches,
10583 sizeof(char_u *), pstrcmp);
10584 return matches;
10585}
10586#endif
10587
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010588#if defined(FEAT_SEARCHPATH)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010589/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010590 * Moves "*psep" back to the previous path separator in "path".
10591 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010592 */
10593 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010594find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010595{
10596 /* skip the current separator */
10597 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010598 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010599
10600 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010601 while (*psep > path)
10602 {
10603 if (vim_ispathsep(**psep))
10604 return OK;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010605 MB_PTR_BACK(path, *psep);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010606 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010607
10608 return FAIL;
10609}
10610
10611/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010612 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10613 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010614 */
10615 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010616is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010617{
10618 int j;
10619 int candidate_len;
10620 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010621 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010622 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010623
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010624 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010625 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010626 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010627 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010628
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010629 candidate_len = (int)STRLEN(maybe_unique);
10630 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010631 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010632 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010633
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010634 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010635 if (fnamecmp(maybe_unique, rival) == 0
10636 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010637 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010638 }
10639
Bram Moolenaar162bd912010-07-28 22:29:10 +020010640 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010641}
10642
10643/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010644 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010645 * paths are expanded to their equivalent fullpath. This includes the "."
10646 * (relative to current buffer directory) and empty path (relative to current
10647 * directory) notations.
10648 *
10649 * TODO: handle upward search (;) and path limiter (**N) notations by
10650 * expanding each into their equivalent path(s).
10651 */
10652 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010653expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010654{
10655 char_u *path_option = *curbuf->b_p_path == NUL
10656 ? p_path : curbuf->b_p_path;
10657 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010658 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010659 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010660
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010661 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010662 return;
10663
10664 while (*path_option != NUL)
10665 {
10666 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10667
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010668 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010669 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010670 /* Relative to current buffer:
10671 * "/path/file" + "." -> "/path/"
10672 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010673 if (curbuf->b_ffname == NULL)
10674 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010675 p = gettail(curbuf->b_ffname);
10676 len = (int)(p - curbuf->b_ffname);
10677 if (len + (int)STRLEN(buf) >= MAXPATHL)
10678 continue;
10679 if (buf[1] == NUL)
10680 buf[len] = NUL;
10681 else
10682 STRMOVE(buf + len, buf + 2);
10683 mch_memmove(buf, curbuf->b_ffname, len);
10684 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010685 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010686 else if (buf[0] == NUL)
10687 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010688 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010689 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010690 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010691 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010692 else if (!mch_isFullName(buf))
10693 {
10694 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010695 len = (int)STRLEN(curdir);
10696 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010697 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010698 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010699 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010700 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010701 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010702 }
10703
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010704 if (ga_grow(gap, 1) == FAIL)
10705 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010706
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010707# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010708 /* Avoid the path ending in a backslash, it fails when a comma is
10709 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010710 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010711 if (buf[len - 1] == '\\')
10712 buf[len - 1] = '/';
10713# endif
10714
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010715 p = vim_strsave(buf);
10716 if (p == NULL)
10717 break;
10718 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010719 }
10720
10721 vim_free(buf);
10722}
10723
10724/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010725 * Returns a pointer to the file or directory name in "fname" that matches the
10726 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010727 *
10728 * path: /foo/bar/baz
10729 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010730 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010731 */
10732 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010733get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010734{
10735 int i;
10736 int maxlen = 0;
10737 char_u **path_part = (char_u **)gap->ga_data;
10738 char_u *cutoff = NULL;
10739
10740 for (i = 0; i < gap->ga_len; i++)
10741 {
10742 int j = 0;
10743
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010744 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010745# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010746 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10747#endif
10748 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010749 j++;
10750 if (j > maxlen)
10751 {
10752 maxlen = j;
10753 cutoff = &fname[j];
10754 }
10755 }
10756
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010757 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010758 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010759 while (vim_ispathsep(*cutoff))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010760 MB_PTR_ADV(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010761
10762 return cutoff;
10763}
10764
10765/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010766 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10767 * that they are unique with respect to each other while conserving the part
10768 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010769 */
10770 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010771uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010772{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010773 int i;
10774 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010775 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010776 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010777 char_u *pat;
10778 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010779 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010780 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010781 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010782 char_u **in_curdir = NULL;
10783 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010784
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010785 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010786 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010787
10788 /*
10789 * We need to prepend a '*' at the beginning of file_pattern so that the
10790 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010791 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010792 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010793 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010794 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010795 if (file_pattern == NULL)
10796 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010797 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010798 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010799 STRCAT(file_pattern, pattern);
10800 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10801 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010802 if (pat == NULL)
10803 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010804
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010805 regmatch.rm_ic = TRUE; /* always ignore case */
10806 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10807 vim_free(pat);
10808 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010809 return;
10810
Bram Moolenaar162bd912010-07-28 22:29:10 +020010811 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010812 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010813 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010814 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010815
10816 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010817 if (in_curdir == NULL)
10818 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010819
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010820 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010821 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010822 char_u *path = fnames[i];
10823 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010824 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010825 char_u *pathsep_p;
10826 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010827
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010828 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010829 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010830 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010831 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010832 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010833
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010834 /* Shorten the filename while maintaining its uniqueness */
10835 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010836
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010837 /* Don't assume all files can be reached without path when search
10838 * pattern starts with star star slash, so only remove path_cutoff
10839 * when possible. */
10840 if (pattern[0] == '*' && pattern[1] == '*'
10841 && vim_ispathsep_nocolon(pattern[2])
10842 && path_cutoff != NULL
10843 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10844 && is_unique(path_cutoff, gap, i))
10845 {
10846 sort_again = TRUE;
10847 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10848 }
10849 else
10850 {
10851 /* Here all files can be reached without path, so get shortest
10852 * unique path. We start at the end of the path. */
10853 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010854
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010855 while (find_previous_pathsep(path, &pathsep_p))
10856 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10857 && is_unique(pathsep_p + 1, gap, i)
10858 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10859 {
10860 sort_again = TRUE;
10861 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10862 break;
10863 }
10864 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010865
10866 if (mch_isFullName(path))
10867 {
10868 /*
10869 * Last resort: shorten relative to curdir if possible.
10870 * 'possible' means:
10871 * 1. It is under the current directory.
10872 * 2. The result is actually shorter than the original.
10873 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010874 * Before curdir After
10875 * /foo/bar/file.txt /foo/bar ./file.txt
10876 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10877 * /file.txt / /file.txt
10878 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010879 */
10880 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010881 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010882#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010883 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010884 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010885 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010886 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010887 && !vim_ispathsep(*short_name)
10888#endif
10889 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010890 {
10891 STRCPY(path, ".");
10892 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010893 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010894 }
10895 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010896 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010897 }
10898
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010899 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010900 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010901 {
10902 char_u *rel_path;
10903 char_u *path = in_curdir[i];
10904
10905 if (path == NULL)
10906 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010907
10908 /* If the {filename} is not unique, change it to ./{filename}.
10909 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010910 short_name = shorten_fname(path, curdir);
10911 if (short_name == NULL)
10912 short_name = path;
10913 if (is_unique(short_name, gap, i))
10914 {
10915 STRCPY(fnames[i], short_name);
10916 continue;
10917 }
10918
10919 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10920 if (rel_path == NULL)
10921 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010922 STRCPY(rel_path, ".");
10923 add_pathsep(rel_path);
10924 STRCAT(rel_path, short_name);
10925
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010926 vim_free(fnames[i]);
10927 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010928 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010929 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010930 }
10931
Bram Moolenaar162bd912010-07-28 22:29:10 +020010932theend:
10933 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010934 if (in_curdir != NULL)
10935 {
10936 for (i = 0; i < gap->ga_len; i++)
10937 vim_free(in_curdir[i]);
10938 vim_free(in_curdir);
10939 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010940 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010941 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010942
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010943 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010944 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010945}
10946
10947/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010948 * Calls globpath() with 'path' values for the given pattern and stores the
10949 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010950 * Returns the total number of matches.
10951 */
10952 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010953expand_in_path(
10954 garray_T *gap,
10955 char_u *pattern,
10956 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010957{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010958 char_u *curdir;
10959 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010960 char_u *paths = NULL;
Bram Moolenaar8a37b032018-02-03 20:43:08 +010010961 int glob_flags = 0;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010962
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010963 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010964 return 0;
10965 mch_dirname(curdir, MAXPATHL);
10966
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010967 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010968 expand_path_option(curdir, &path_ga);
10969 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010970 if (path_ga.ga_len == 0)
10971 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010972
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010973 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010974 ga_clear_strings(&path_ga);
10975 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010976 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010977
Bram Moolenaar8a37b032018-02-03 20:43:08 +010010978 if (flags & EW_ICASE)
10979 glob_flags |= WILD_ICASE;
10980 if (flags & EW_ADDSLASH)
10981 glob_flags |= WILD_ADD_SLASH;
10982 globpath(paths, pattern, gap, glob_flags);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010983 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010984
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010985 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010986}
10987#endif
10988
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010989#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10990/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010991 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10992 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010993 */
10994 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010995remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010996{
10997 int i;
10998 int j;
10999 char_u **fnames = (char_u **)gap->ga_data;
11000
Bram Moolenaardc685ab2010-08-13 21:16:49 +020011001 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011002 for (i = gap->ga_len - 1; i > 0; --i)
11003 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
11004 {
11005 vim_free(fnames[i]);
11006 for (j = i + 1; j < gap->ga_len; ++j)
11007 fnames[j - 1] = fnames[j];
11008 --gap->ga_len;
11009 }
11010}
11011#endif
11012
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011013/*
11014 * Return TRUE if "p" contains what looks like an environment variable.
11015 * Allowing for escaping.
11016 */
11017 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011018has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011019{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011020 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011021 {
11022 if (*p == '\\' && p[1] != NUL)
11023 ++p;
11024 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010011025#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011026 "$%"
11027#else
11028 "$"
11029#endif
11030 , *p) != NULL)
11031 return TRUE;
11032 }
11033 return FALSE;
11034}
11035
11036#ifdef SPECIAL_WILDCHAR
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011037/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +010011038 * Return TRUE if "p" contains a special wildcard character, one that Vim
11039 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011040 */
11041 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011042has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011043{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011044 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011045 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010011046 /* Allow for escaping. */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011047 if (*p == '\\' && p[1] != NUL)
11048 ++p;
11049 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
11050 return TRUE;
11051 }
11052 return FALSE;
11053}
11054#endif
11055
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056/*
11057 * Generic wildcard expansion code.
11058 *
11059 * Characters in "pat" that should not be expanded must be preceded with a
11060 * backslash. E.g., "/path\ with\ spaces/my\*star*"
11061 *
11062 * Return FAIL when no single file was found. In this case "num_file" is not
11063 * set, and "file" may contain an error message.
11064 * Return OK when some files found. "num_file" is set to the number of
11065 * matches, "file" to the array of matches. Call FreeWild() later.
11066 */
11067 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011068gen_expand_wildcards(
11069 int num_pat, /* number of input patterns */
11070 char_u **pat, /* array of input patterns */
11071 int *num_file, /* resulting number of files */
11072 char_u ***file, /* array of resulting files */
11073 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074{
11075 int i;
11076 garray_T ga;
11077 char_u *p;
11078 static int recursive = FALSE;
11079 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020011080 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011081#if defined(FEAT_SEARCHPATH)
11082 int did_expand_in_path = FALSE;
11083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011084
11085 /*
11086 * expand_env() is called to expand things like "~user". If this fails,
11087 * it calls ExpandOne(), which brings us back here. In this case, always
11088 * call the machine specific expansion function, if possible. Otherwise,
11089 * return FAIL.
11090 */
11091 if (recursive)
11092#ifdef SPECIAL_WILDCHAR
11093 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11094#else
11095 return FAIL;
11096#endif
11097
11098#ifdef SPECIAL_WILDCHAR
11099 /*
11100 * If there are any special wildcard characters which we cannot handle
11101 * here, call machine specific function for all the expansion. This
11102 * avoids starting the shell for each argument separately.
11103 * For `=expr` do use the internal function.
11104 */
11105 for (i = 0; i < num_pat; i++)
11106 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011107 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108# ifdef VIM_BACKTICK
11109 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
11110# endif
11111 )
11112 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11113 }
11114#endif
11115
11116 recursive = TRUE;
11117
11118 /*
11119 * The matching file names are stored in a growarray. Init it empty.
11120 */
11121 ga_init2(&ga, (int)sizeof(char_u *), 30);
11122
11123 for (i = 0; i < num_pat; ++i)
11124 {
11125 add_pat = -1;
11126 p = pat[i];
11127
11128#ifdef VIM_BACKTICK
11129 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020011130 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020011132 if (add_pat == -1)
11133 retval = FAIL;
11134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011135 else
11136#endif
11137 {
11138 /*
11139 * First expand environment variables, "~/" and "~user/".
11140 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011141 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000011143 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144 if (p == NULL)
11145 p = pat[i];
11146#ifdef UNIX
11147 /*
11148 * On Unix, if expand_env() can't expand an environment
11149 * variable, use the shell to do that. Discard previously
11150 * found file names and start all over again.
11151 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011152 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153 {
11154 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000011155 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020011157 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158 recursive = FALSE;
11159 return i;
11160 }
11161#endif
11162 }
11163
11164 /*
11165 * If there are wildcards: Expand file names and add each match to
11166 * the list. If there is no match, and EW_NOTFOUND is given, add
11167 * the pattern.
11168 * If there are no wildcards: Add the file name if it exists or
11169 * when EW_NOTFOUND is given.
11170 */
11171 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011172 {
11173#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011174 if ((flags & EW_PATH)
11175 && !mch_isFullName(p)
11176 && !(p[0] == '.'
11177 && (vim_ispathsep(p[1])
11178 || (p[1] == '.' && vim_ispathsep(p[2]))))
11179 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011180 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011181 /* :find completion where 'path' is used.
11182 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011183 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011184 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011185 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011186 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011187 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011188 else
11189#endif
11190 add_pat = mch_expandpath(&ga, p, flags);
11191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192 }
11193
11194 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
11195 {
11196 char_u *t = backslash_halve_save(p);
11197
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198 /* When EW_NOTFOUND is used, always add files and dirs. Makes
11199 * "vim c:/" work. */
11200 if (flags & EW_NOTFOUND)
11201 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020011202 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203 addfile(&ga, t, flags);
11204 vim_free(t);
11205 }
11206
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011207#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011208 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011209 uniquefy_paths(&ga, p);
11210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211 if (p != pat[i])
11212 vim_free(p);
11213 }
11214
11215 *num_file = ga.ga_len;
11216 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
11217
11218 recursive = FALSE;
11219
Bram Moolenaar336bd622016-01-17 18:23:58 +010011220 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221}
11222
11223# ifdef VIM_BACKTICK
11224
11225/*
11226 * Return TRUE if we can expand this backtick thing here.
11227 */
11228 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011229vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230{
11231 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
11232}
11233
11234/*
11235 * Expand an item in `backticks` by executing it as a command.
11236 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020011237 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238 */
11239 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011240expand_backtick(
11241 garray_T *gap,
11242 char_u *pat,
11243 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244{
11245 char_u *p;
11246 char_u *cmd;
11247 char_u *buffer;
11248 int cnt = 0;
11249 int i;
11250
11251 /* Create the command: lop off the backticks. */
11252 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
11253 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011254 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255
11256#ifdef FEAT_EVAL
11257 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011258 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259 else
11260#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011261 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011262 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263 vim_free(cmd);
11264 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011265 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266
11267 cmd = buffer;
11268 while (*cmd != NUL)
11269 {
11270 cmd = skipwhite(cmd); /* skip over white space */
11271 p = cmd;
11272 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
11273 ++p;
11274 /* add an entry if it is not empty */
11275 if (p > cmd)
11276 {
11277 i = *p;
11278 *p = NUL;
11279 addfile(gap, cmd, flags);
11280 *p = i;
11281 ++cnt;
11282 }
11283 cmd = p;
11284 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
11285 ++cmd;
11286 }
11287
11288 vim_free(buffer);
11289 return cnt;
11290}
11291# endif /* VIM_BACKTICK */
11292
11293/*
11294 * Add a file to a file list. Accepted flags:
11295 * EW_DIR add directories
11296 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011297 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298 * EW_NOTFOUND add even when it doesn't exist
11299 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011300 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301 */
11302 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011303addfile(
11304 garray_T *gap,
11305 char_u *f, /* filename */
11306 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011307{
11308 char_u *p;
11309 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011310 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011312 /* if the file/dir/link doesn't exist, may not add it */
11313 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011314 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315 return;
11316
11317#ifdef FNAME_ILLEGAL
11318 /* if the file/dir contains illegal characters, don't add it */
11319 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11320 return;
11321#endif
11322
11323 isdir = mch_isdir(f);
11324 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11325 return;
11326
Bram Moolenaarb5971142015-03-21 17:32:19 +010011327 /* If the file isn't executable, may not add it. Do accept directories.
11328 * When invoked from expand_shellcmd() do not use $PATH. */
11329 if (!isdir && (flags & EW_EXEC)
11330 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011331 return;
11332
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333 /* Make room for another item in the file list. */
11334 if (ga_grow(gap, 1) == FAIL)
11335 return;
11336
11337 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11338 if (p == NULL)
11339 return;
11340
11341 STRCPY(p, f);
11342#ifdef BACKSLASH_IN_FILENAME
11343 slash_adjust(p);
11344#endif
11345 /*
11346 * Append a slash or backslash after directory names if none is present.
11347 */
11348#ifndef DONT_ADD_PATHSEP_TO_DIR
11349 if (isdir && (flags & EW_ADDSLASH))
11350 add_pathsep(p);
11351#endif
11352 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353}
11354#endif /* !NO_EXPANDPATH */
11355
11356#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11357
11358#ifndef SEEK_SET
11359# define SEEK_SET 0
11360#endif
11361#ifndef SEEK_END
11362# define SEEK_END 2
11363#endif
11364
11365/*
11366 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011367 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11368 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369 * Returns an allocated string, or NULL for error.
11370 */
11371 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011372get_cmd_output(
11373 char_u *cmd,
11374 char_u *infile, /* optional input file name */
11375 int flags, /* can be SHELL_SILENT */
11376 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377{
11378 char_u *tempname;
11379 char_u *command;
11380 char_u *buffer = NULL;
11381 int len;
11382 int i = 0;
11383 FILE *fd;
11384
11385 if (check_restricted() || check_secure())
11386 return NULL;
11387
11388 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011389 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390 {
11391 EMSG(_(e_notmp));
11392 return NULL;
11393 }
11394
11395 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011396 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011397 if (command == NULL)
11398 goto done;
11399
11400 /*
11401 * Call the shell to execute the command (errors are ignored).
11402 * Don't check timestamps here.
11403 */
11404 ++no_check_timestamps;
11405 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11406 --no_check_timestamps;
11407
11408 vim_free(command);
11409
11410 /*
11411 * read the names from the file into memory
11412 */
11413# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011414 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415 fd = mch_fopen((char *)tempname, "r");
11416# else
11417 fd = mch_fopen((char *)tempname, READBIN);
11418# endif
11419
11420 if (fd == NULL)
11421 {
11422 EMSG2(_(e_notopen), tempname);
11423 goto done;
11424 }
11425
11426 fseek(fd, 0L, SEEK_END);
11427 len = ftell(fd); /* get size of temp file */
11428 fseek(fd, 0L, SEEK_SET);
11429
11430 buffer = alloc(len + 1);
11431 if (buffer != NULL)
11432 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11433 fclose(fd);
11434 mch_remove(tempname);
11435 if (buffer == NULL)
11436 goto done;
11437#ifdef VMS
11438 len = i; /* VMS doesn't give us what we asked for... */
11439#endif
11440 if (i != len)
11441 {
11442 EMSG2(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +010011443 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011445 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011446 {
11447 /* Change NUL into SOH, otherwise the string is truncated. */
11448 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011449 if (buffer[i] == NUL)
11450 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011451
Bram Moolenaar162bd912010-07-28 22:29:10 +020011452 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011453 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011454 else
11455 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011456
11457done:
11458 vim_free(tempname);
11459 return buffer;
11460}
11461#endif
11462
11463/*
11464 * Free the list of files returned by expand_wildcards() or other expansion
11465 * functions.
11466 */
11467 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011468FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011470 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011471 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472 while (count--)
11473 vim_free(files[count]);
11474 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475}
11476
11477/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011478 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479 * Don't do this when still processing a command or a mapping.
11480 * Don't do this when inside a ":normal" command.
11481 */
11482 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011483goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011484{
11485 return (p_im && stuff_empty() && typebuf_typed());
11486}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011487
11488/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011489 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011490 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11491 * - Remove any argument. E.g., "csh -f" -> "csh".
11492 * But don't allow a space in the path, so that this works:
11493 * "/usr/bin/csh --rcfile ~/.cshrc"
11494 * But don't do that for Windows, it's common to have a space in the path.
11495 */
11496 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011497get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011498{
11499 char_u *p;
11500
11501#ifdef WIN3264
11502 p = gettail(p_sh);
11503 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11504#else
11505 p = skiptowhite(p_sh);
11506 if (*p == NUL)
11507 {
11508 /* No white space, use the tail. */
11509 p = vim_strsave(gettail(p_sh));
11510 }
11511 else
11512 {
11513 char_u *p1, *p2;
11514
11515 /* Find the last path separator before the space. */
11516 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011517 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011518 if (vim_ispathsep(*p2))
11519 p1 = p2 + 1;
11520 p = vim_strnsave(p1, (int)(p - p1));
11521 }
11522#endif
11523 return p;
11524}