blob: 4c9bd2fed85166cf2f9824bf2ce97aa94b78df1c [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * misc1.c: functions that didn't seem to fit elsewhere
12 */
13
14#include "vim.h"
15#include "version.h"
16
Bram Moolenaar828c3d72018-06-19 18:58:07 +020017#if defined(FEAT_CMDL_COMPL) && defined(WIN3264)
18# include <lm.h>
19#endif
20
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010021static char_u *vim_version_dir(char_u *vimdir);
22static char_u *remove_tail(char_u *p, char_u *pend, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023
Bram Moolenaar24305862012-08-15 14:05:05 +020024/* All user names (for ~user completion as done by shell). */
25#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
26static garray_T ga_users;
27#endif
28
Bram Moolenaar071d4272004-06-13 20:20:40 +000029/*
30 * Count the size (in window cells) of the indent in the current line.
31 */
32 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010033get_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000034{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020035#ifdef FEAT_VARTABS
36 return get_indent_str_vtab(ml_get_curline(), (int)curbuf->b_p_ts,
37 curbuf->b_p_vts_array, FALSE);
38#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020039 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000041}
42
43/*
44 * Count the size (in window cells) of the indent in line "lnum".
45 */
46 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010047get_indent_lnum(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000048{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020049#ifdef FEAT_VARTABS
50 return get_indent_str_vtab(ml_get(lnum), (int)curbuf->b_p_ts,
51 curbuf->b_p_vts_array, FALSE);
52#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020053 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000055}
56
57#if defined(FEAT_FOLDING) || defined(PROTO)
58/*
59 * Count the size (in window cells) of the indent in line "lnum" of buffer
60 * "buf".
61 */
62 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010063get_indent_buf(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000064{
Bram Moolenaar04958cb2018-06-23 19:23:02 +020065#ifdef FEAT_VARTABS
66 return get_indent_str_vtab(ml_get_buf(buf, lnum, FALSE),
67 (int)curbuf->b_p_ts, buf->b_p_vts_array, FALSE);
68#else
Bram Moolenaar597a4222014-06-25 14:39:50 +020069 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +020070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000071}
72#endif
73
74/*
75 * count the size (in window cells) of the indent in line "ptr", with
76 * 'tabstop' at "ts"
77 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +000078 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010079get_indent_str(
80 char_u *ptr,
81 int ts,
82 int list) /* if TRUE, count only screen size for tabs */
Bram Moolenaar071d4272004-06-13 20:20:40 +000083{
84 int count = 0;
85
86 for ( ; *ptr; ++ptr)
87 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020088 if (*ptr == TAB)
89 {
90 if (!list || lcs_tab1) /* count a tab for what it is worth */
91 count += ts - (count % ts);
92 else
Bram Moolenaare4df1642014-08-29 12:58:44 +020093 /* In list mode, when tab is not set, count screen char width
94 * for Tab, displays: ^I */
Bram Moolenaar597a4222014-06-25 14:39:50 +020095 count += ptr2cells(ptr);
96 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 else if (*ptr == ' ')
98 ++count; /* count a space for one */
99 else
100 break;
101 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000102 return count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103}
104
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200105#ifdef FEAT_VARTABS
106/*
107 * Count the size (in window cells) of the indent in line "ptr", using
108 * variable tabstops.
109 * if "list" is TRUE, count only screen size for tabs.
110 */
111 int
112get_indent_str_vtab(char_u *ptr, int ts, int *vts, int list)
113{
114 int count = 0;
115
116 for ( ; *ptr; ++ptr)
117 {
118 if (*ptr == TAB) /* count a tab for what it is worth */
119 {
120 if (!list || lcs_tab1)
121 count += tabstop_padding(count, ts, vts);
122 else
123 /* In list mode, when tab is not set, count screen char width
124 * for Tab, displays: ^I */
125 count += ptr2cells(ptr);
126 }
127 else if (*ptr == ' ')
128 ++count; /* count a space for one */
129 else
130 break;
131 }
132 return count;
133}
134#endif
135
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136/*
137 * Set the indent of the current line.
138 * Leaves the cursor on the first non-blank in the line.
139 * Caller must take care of undo.
140 * "flags":
141 * SIN_CHANGED: call changed_bytes() if the line was changed.
142 * SIN_INSERT: insert the indent in front of the line.
143 * SIN_UNDO: save line for undo before changing it.
144 * Returns TRUE if the line was changed.
145 */
146 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100147set_indent(
148 int size, /* measured in spaces */
149 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150{
151 char_u *p;
152 char_u *newline;
153 char_u *oldline;
154 char_u *s;
155 int todo;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000156 int ind_len; /* measured in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 int line_len;
158 int doit = FALSE;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000159 int ind_done = 0; /* measured in spaces */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200160#ifdef FEAT_VARTABS
161 int ind_col = 0;
162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 int tab_pad;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000164 int retval = FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000165 int orig_char_len = -1; /* number of initial whitespace chars when
Bram Moolenaar5002c292007-07-24 13:26:15 +0000166 'et' and 'pi' are both set */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167
168 /*
169 * First check if there is anything to do and compute the number of
170 * characters needed for the indent.
171 */
172 todo = size;
173 ind_len = 0;
174 p = oldline = ml_get_curline();
175
176 /* Calculate the buffer size for the new indent, and check to see if it
177 * isn't already set */
178
Bram Moolenaar5002c292007-07-24 13:26:15 +0000179 /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and
180 * 'preserveindent' are set count the number of characters at the
181 * beginning of the line to be copied */
182 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 {
184 /* If 'preserveindent' is set then reuse as much as possible of
185 * the existing indent structure for the new indent */
186 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
187 {
188 ind_done = 0;
189
190 /* count as many characters as we can use */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100191 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 {
193 if (*p == TAB)
194 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200195#ifdef FEAT_VARTABS
196 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
197 curbuf->b_p_vts_array);
198#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 tab_pad = (int)curbuf->b_p_ts
200 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 /* stop if this tab will overshoot the target */
203 if (todo < tab_pad)
204 break;
205 todo -= tab_pad;
206 ++ind_len;
207 ind_done += tab_pad;
208 }
209 else
210 {
211 --todo;
212 ++ind_len;
213 ++ind_done;
214 }
215 ++p;
216 }
217
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200218#ifdef FEAT_VARTABS
219 /* These diverge from this point. */
220 ind_col = ind_done;
221#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +0000222 /* Set initial number of whitespace chars to copy if we are
223 * preserving indent but expandtab is set */
224 if (curbuf->b_p_et)
225 orig_char_len = ind_len;
226
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200228#ifdef FEAT_VARTABS
229 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
230 curbuf->b_p_vts_array);
231#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200233#endif
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000234 if (todo >= tab_pad && orig_char_len == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 {
236 doit = TRUE;
237 todo -= tab_pad;
238 ++ind_len;
239 /* ind_done += tab_pad; */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200240#ifdef FEAT_VARTABS
241 ind_col += tab_pad;
242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 }
244 }
245
246 /* count tabs required for indent */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200247#ifdef FEAT_VARTABS
248 for (;;)
249 {
250 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
251 curbuf->b_p_vts_array);
252 if (todo < tab_pad)
253 break;
254 if (*p != TAB)
255 doit = TRUE;
256 else
257 ++p;
258 todo -= tab_pad;
259 ++ind_len;
260 ind_col += tab_pad;
261 }
262#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 while (todo >= (int)curbuf->b_p_ts)
264 {
265 if (*p != TAB)
266 doit = TRUE;
267 else
268 ++p;
269 todo -= (int)curbuf->b_p_ts;
270 ++ind_len;
271 /* ind_done += (int)curbuf->b_p_ts; */
272 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 }
275 /* count spaces required for indent */
276 while (todo > 0)
277 {
278 if (*p != ' ')
279 doit = TRUE;
280 else
281 ++p;
282 --todo;
283 ++ind_len;
284 /* ++ind_done; */
285 }
286
287 /* Return if the indent is OK already. */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100288 if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 return FALSE;
290
291 /* Allocate memory for the new line. */
292 if (flags & SIN_INSERT)
293 p = oldline;
294 else
295 p = skipwhite(p);
296 line_len = (int)STRLEN(p) + 1;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000297
298 /* If 'preserveindent' and 'expandtab' are both set keep the original
299 * characters and allocate accordingly. We will fill the rest with spaces
300 * after the if (!curbuf->b_p_et) below. */
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000301 if (orig_char_len != -1)
Bram Moolenaar5002c292007-07-24 13:26:15 +0000302 {
303 newline = alloc(orig_char_len + size - ind_done + line_len);
304 if (newline == NULL)
305 return FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000306 todo = size - ind_done;
307 ind_len = orig_char_len + todo; /* Set total length of indent in
308 * characters, which may have been
309 * undercounted until now */
Bram Moolenaar5002c292007-07-24 13:26:15 +0000310 p = oldline;
311 s = newline;
312 while (orig_char_len > 0)
313 {
314 *s++ = *p++;
315 orig_char_len--;
316 }
Bram Moolenaar913626c2008-01-03 11:43:42 +0000317
Bram Moolenaar5002c292007-07-24 13:26:15 +0000318 /* Skip over any additional white space (useful when newindent is less
319 * than old) */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100320 while (VIM_ISWHITE(*p))
Bram Moolenaar913626c2008-01-03 11:43:42 +0000321 ++p;
Bram Moolenaarcc00b952007-08-11 12:32:57 +0000322
Bram Moolenaar5002c292007-07-24 13:26:15 +0000323 }
324 else
325 {
326 todo = size;
327 newline = alloc(ind_len + line_len);
328 if (newline == NULL)
329 return FALSE;
330 s = newline;
331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332
333 /* Put the characters in the new line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 /* if 'expandtab' isn't set: use TABs */
335 if (!curbuf->b_p_et)
336 {
337 /* If 'preserveindent' is set then reuse as much as possible of
338 * the existing indent structure for the new indent */
339 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
340 {
341 p = oldline;
342 ind_done = 0;
343
Bram Moolenaar1c465442017-03-12 20:10:05 +0100344 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 {
346 if (*p == TAB)
347 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200348#ifdef FEAT_VARTABS
349 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
350 curbuf->b_p_vts_array);
351#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 tab_pad = (int)curbuf->b_p_ts
353 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 /* stop if this tab will overshoot the target */
356 if (todo < tab_pad)
357 break;
358 todo -= tab_pad;
359 ind_done += tab_pad;
360 }
361 else
362 {
363 --todo;
364 ++ind_done;
365 }
366 *s++ = *p++;
367 }
368
369 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200370#ifdef FEAT_VARTABS
371 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
372 curbuf->b_p_vts_array);
373#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 if (todo >= tab_pad)
377 {
378 *s++ = TAB;
379 todo -= tab_pad;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200380#ifdef FEAT_VARTABS
381 ind_done += tab_pad;
382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 }
384
385 p = skipwhite(p);
386 }
387
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200388#ifdef FEAT_VARTABS
389 for (;;)
390 {
391 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
392 curbuf->b_p_vts_array);
393 if (todo < tab_pad)
394 break;
395 *s++ = TAB;
396 todo -= tab_pad;
397 ind_done += tab_pad;
398 }
399#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 while (todo >= (int)curbuf->b_p_ts)
401 {
402 *s++ = TAB;
403 todo -= (int)curbuf->b_p_ts;
404 }
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 }
407 while (todo > 0)
408 {
409 *s++ = ' ';
410 --todo;
411 }
412 mch_memmove(s, p, (size_t)line_len);
413
Bram Moolenaar663bc892019-01-08 23:07:24 +0100414 // Replace the line (unless undo fails).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
416 {
417 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
418 if (flags & SIN_CHANGED)
419 changed_bytes(curwin->w_cursor.lnum, 0);
Bram Moolenaar663bc892019-01-08 23:07:24 +0100420
421 // Correct saved cursor position if it is in this line.
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200422 if (saved_cursor.lnum == curwin->w_cursor.lnum)
423 {
424 if (saved_cursor.col >= (colnr_T)(p - oldline))
Bram Moolenaar663bc892019-01-08 23:07:24 +0100425 // cursor was after the indent, adjust for the number of
426 // bytes added/removed
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200427 saved_cursor.col += ind_len - (colnr_T)(p - oldline);
428 else if (saved_cursor.col >= (colnr_T)(s - newline))
Bram Moolenaar663bc892019-01-08 23:07:24 +0100429 // cursor was in the indent, and is now after it, put it back
430 // at the start of the indent (replacing spaces with TAB)
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200431 saved_cursor.col = (colnr_T)(s - newline);
432 }
Bram Moolenaar663bc892019-01-08 23:07:24 +0100433#ifdef FEAT_TEXT_PROP
434 adjust_prop_columns(curwin->w_cursor.lnum, (colnr_T)(p - oldline),
435 ind_len - (colnr_T)(p - oldline));
436#endif
Bram Moolenaar5409c052005-03-18 20:27:04 +0000437 retval = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 }
439 else
440 vim_free(newline);
441
442 curwin->w_cursor.col = ind_len;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000443 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444}
445
446/*
447 * Copy the indent from ptr to the current line (and fill to size)
448 * Leaves the cursor on the first non-blank in the line.
449 * Returns TRUE if the line was changed.
450 */
451 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100452copy_indent(int size, char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453{
454 char_u *p = NULL;
455 char_u *line = NULL;
456 char_u *s;
457 int todo;
458 int ind_len;
459 int line_len = 0;
460 int tab_pad;
461 int ind_done;
462 int round;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200463#ifdef FEAT_VARTABS
464 int ind_col;
465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466
467 /* Round 1: compute the number of characters needed for the indent
468 * Round 2: copy the characters. */
469 for (round = 1; round <= 2; ++round)
470 {
471 todo = size;
472 ind_len = 0;
473 ind_done = 0;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200474#ifdef FEAT_VARTABS
475 ind_col = 0;
476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 s = src;
478
479 /* Count/copy the usable portion of the source line */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100480 while (todo > 0 && VIM_ISWHITE(*s))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 {
482 if (*s == TAB)
483 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200484#ifdef FEAT_VARTABS
485 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
486 curbuf->b_p_vts_array);
487#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 tab_pad = (int)curbuf->b_p_ts
489 - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 /* Stop if this tab will overshoot the target */
492 if (todo < tab_pad)
493 break;
494 todo -= tab_pad;
495 ind_done += tab_pad;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200496#ifdef FEAT_VARTABS
497 ind_col += tab_pad;
498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 }
500 else
501 {
502 --todo;
503 ++ind_done;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200504#ifdef FEAT_VARTABS
505 ++ind_col;
506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 }
508 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000509 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 *p++ = *s;
511 ++s;
512 }
513
514 /* Fill to next tabstop with a tab, if possible */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200515#ifdef FEAT_VARTABS
516 tab_pad = tabstop_padding(ind_done, curbuf->b_p_ts,
517 curbuf->b_p_vts_array);
518#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200520#endif
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200521 if (todo >= tab_pad && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 {
523 todo -= tab_pad;
524 ++ind_len;
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200525#ifdef FEAT_VARTABS
526 ind_col += tab_pad;
527#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000528 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 *p++ = TAB;
530 }
531
532 /* Add tabs required for indent */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200533 if (!curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 {
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200535#ifdef FEAT_VARTABS
536 for (;;)
537 {
538 tab_pad = tabstop_padding(ind_col, curbuf->b_p_ts,
539 curbuf->b_p_vts_array);
540 if (todo < tab_pad)
541 break;
542 todo -= tab_pad;
543 ++ind_len;
544 ind_col += tab_pad;
545 if (p != NULL)
546 *p++ = TAB;
547 }
548#else
549 while (todo >= (int)curbuf->b_p_ts)
550 {
551 todo -= (int)curbuf->b_p_ts;
552 ++ind_len;
553 if (p != NULL)
554 *p++ = TAB;
555 }
556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 }
558
559 /* Count/add spaces required for indent */
560 while (todo > 0)
561 {
562 --todo;
563 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000564 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 *p++ = ' ';
566 }
567
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000568 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 {
570 /* Allocate memory for the result: the copied indent, new indent
571 * and the rest of the line. */
572 line_len = (int)STRLEN(ml_get_curline()) + 1;
573 line = alloc(ind_len + line_len);
574 if (line == NULL)
575 return FALSE;
576 p = line;
577 }
578 }
579
580 /* Append the original line */
581 mch_memmove(p, ml_get_curline(), (size_t)line_len);
582
583 /* Replace the line */
584 ml_replace(curwin->w_cursor.lnum, line, FALSE);
585
586 /* Put the cursor after the indent. */
587 curwin->w_cursor.col = ind_len;
588 return TRUE;
589}
590
591/*
592 * Return the indent of the current line after a number. Return -1 if no
593 * number was found. Used for 'n' in 'formatoptions': numbered list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000594 * Since a pattern is used it can actually handle more than numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 */
596 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100597get_number_indent(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 colnr_T col;
600 pos_T pos;
601
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200602 regmatch_T regmatch;
603 int lead_len = 0; /* length of comment leader */
604
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 if (lnum > curbuf->b_ml.ml_line_count)
606 return -1;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000607 pos.lnum = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200608
609#ifdef FEAT_COMMENTS
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200610 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
611 if ((State & INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200612 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000613#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200614 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
615 if (regmatch.regprog != NULL)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200616 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200617 regmatch.rm_ic = FALSE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200618
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200619 /* vim_regexec() expects a pointer to a line. This lets us
620 * start matching for the flp beyond any comment leader... */
621 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200622 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200623 pos.lnum = lnum;
624 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200625#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200626 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200627#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200628 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200629 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200630 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000631
632 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 getvcol(curwin, &pos, &col, NULL, NULL);
635 return (int)col;
636}
637
Bram Moolenaar597a4222014-06-25 14:39:50 +0200638#if defined(FEAT_LINEBREAK) || defined(PROTO)
639/*
640 * Return appropriate space number for breakindent, taking influencing
641 * parameters into account. Window must be specified, since it is not
642 * necessarily always the current one.
643 */
644 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100645get_breakindent_win(
646 win_T *wp,
647 char_u *line) /* start of the line */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200648{
649 static int prev_indent = 0; /* cached indent value */
650 static long prev_ts = 0L; /* cached tabstop value */
651 static char_u *prev_line = NULL; /* cached pointer to line */
Bram Moolenaar79518e22017-02-17 16:31:35 +0100652 static varnumber_T prev_tick = 0; /* changedtick of cached value */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200653#ifdef FEAT_VARTABS
654 static int *prev_vts = NULL; /* cached vartabs values */
655#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200656 int bri = 0;
657 /* window width minus window margin space, i.e. what rests for text */
Bram Moolenaar02631462017-09-22 15:20:32 +0200658 const int eff_wwidth = wp->w_width
Bram Moolenaar597a4222014-06-25 14:39:50 +0200659 - ((wp->w_p_nu || wp->w_p_rnu)
660 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
661 ? number_width(wp) + 1 : 0);
662
663 /* used cached indent, unless pointer or 'tabstop' changed */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200664 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200665 || prev_tick != CHANGEDTICK(wp->w_buffer)
666#ifdef FEAT_VARTABS
667 || prev_vts != wp->w_buffer->b_p_vts_array
668#endif
669 )
Bram Moolenaar597a4222014-06-25 14:39:50 +0200670 {
671 prev_line = line;
672 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100673 prev_tick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200674#ifdef FEAT_VARTABS
675 prev_vts = wp->w_buffer->b_p_vts_array;
676 prev_indent = get_indent_str_vtab(line,
677 (int)wp->w_buffer->b_p_ts,
678 wp->w_buffer->b_p_vts_array, wp->w_p_list);
679#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200680 prev_indent = get_indent_str(line,
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200681 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200682#endif
Bram Moolenaar597a4222014-06-25 14:39:50 +0200683 }
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200684 bri = prev_indent + wp->w_p_brishift;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200685
686 /* indent minus the length of the showbreak string */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200687 if (wp->w_p_brisbr)
688 bri -= vim_strsize(p_sbr);
689
690 /* Add offset for number column, if 'n' is in 'cpoptions' */
691 bri += win_col_off2(wp);
692
693 /* never indent past left window margin */
694 if (bri < 0)
695 bri = 0;
696 /* always leave at least bri_min characters on the left,
697 * if text width is sufficient */
698 else if (bri > eff_wwidth - wp->w_p_brimin)
699 bri = (eff_wwidth - wp->w_p_brimin < 0)
700 ? 0 : eff_wwidth - wp->w_p_brimin;
701
702 return bri;
703}
704#endif
705
706
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
708
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709/*
710 * Return TRUE if the string "line" starts with a word from 'cinwords'.
711 */
712 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100713cin_is_cinword(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714{
715 char_u *cinw;
716 char_u *cinw_buf;
717 int cinw_len;
718 int retval = FALSE;
719 int len;
720
721 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1;
722 cinw_buf = alloc((unsigned)cinw_len);
723 if (cinw_buf != NULL)
724 {
725 line = skipwhite(line);
726 for (cinw = curbuf->b_p_cinw; *cinw; )
727 {
728 len = copy_option_part(&cinw, cinw_buf, cinw_len, ",");
729 if (STRNCMP(line, cinw_buf, len) == 0
730 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1])))
731 {
732 retval = TRUE;
733 break;
734 }
735 }
736 vim_free(cinw_buf);
737 }
738 return retval;
739}
740#endif
741
742/*
743 * open_line: Add a new line below or above the current line.
744 *
745 * For VREPLACE mode, we only add a new line when we get to the end of the
746 * file, otherwise we just start replacing the next line.
747 *
748 * Caller must take care of undo. Since VREPLACE may affect any number of
749 * lines however, it may call u_save_cursor() again when starting to change a
750 * new line.
751 * "flags": OPENLINE_DELSPACES delete spaces after cursor
752 * OPENLINE_DO_COM format comments
753 * OPENLINE_KEEPTRAIL keep trailing spaces
754 * OPENLINE_MARKFIX adjust mark positions after the line break
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200755 * OPENLINE_COM_LIST format comments with list or 2nd line indent
756 *
757 * "second_line_indent": indent for after ^^D in Insert mode or if flag
758 * OPENLINE_COM_LIST
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 *
Bram Moolenaar24a2d722018-04-24 19:36:43 +0200760 * Return OK for success, FAIL for failure
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 */
762 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100763open_line(
764 int dir, /* FORWARD or BACKWARD */
765 int flags,
766 int second_line_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767{
768 char_u *saved_line; /* copy of the original line */
769 char_u *next_line = NULL; /* copy of the next line */
770 char_u *p_extra = NULL; /* what goes to next line */
771 int less_cols = 0; /* less columns for mark in new line */
772 int less_cols_off = 0; /* columns to skip for mark adjust */
773 pos_T old_cursor; /* old cursor position */
774 int newcol = 0; /* new cursor column */
775 int newindent = 0; /* auto-indent of the new line */
776 int n;
777 int trunc_line = FALSE; /* truncate current line afterwards */
Bram Moolenaar24a2d722018-04-24 19:36:43 +0200778 int retval = FAIL; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779#ifdef FEAT_COMMENTS
780 int extra_len = 0; /* length of p_extra string */
781 int lead_len; /* length of comment leader */
782 char_u *lead_flags; /* position in 'comments' for comment leader */
783 char_u *leader = NULL; /* copy of comment leader */
784#endif
785 char_u *allocated = NULL; /* allocated memory */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 int saved_char = NUL; /* init for GCC */
788#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
789 pos_T *pos;
790#endif
791#ifdef FEAT_SMARTINDENT
792 int do_si = (!p_paste && curbuf->b_p_si
793# ifdef FEAT_CINDENT
794 && !curbuf->b_p_cin
795# endif
Bram Moolenaar69a76fe2017-08-03 17:54:03 +0200796# ifdef FEAT_EVAL
797 && *curbuf->b_p_inde == NUL
798# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 );
800 int no_si = FALSE; /* reset did_si afterwards */
801 int first_char = NUL; /* init for GCC */
802#endif
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +0200803#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 int vreplace_mode;
805#endif
806 int did_append; /* appended a new line */
807 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */
808
809 /*
810 * make a copy of the current line so we can mess with it
811 */
812 saved_line = vim_strsave(ml_get_curline());
813 if (saved_line == NULL) /* out of memory! */
814 return FALSE;
815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 if (State & VREPLACE_FLAG)
817 {
818 /*
819 * With VREPLACE we make a copy of the next line, which we will be
820 * starting to replace. First make the new line empty and let vim play
821 * with the indenting and comment leader to its heart's content. Then
822 * we grab what it ended up putting on the new line, put back the
823 * original line, and call ins_char() to put each new character onto
824 * the line, replacing what was there before and pushing the right
825 * stuff onto the replace stack. -- webb.
826 */
827 if (curwin->w_cursor.lnum < orig_line_count)
828 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
829 else
830 next_line = vim_strsave((char_u *)"");
831 if (next_line == NULL) /* out of memory! */
832 goto theend;
833
834 /*
835 * In VREPLACE mode, a NL replaces the rest of the line, and starts
836 * replacing the next line, so push all of the characters left on the
837 * line onto the replace stack. We'll push any other characters that
838 * might be replaced at the start of the next line (due to autoindent
839 * etc) a bit later.
840 */
841 replace_push(NUL); /* Call twice because BS over NL expects it */
842 replace_push(NUL);
843 p = saved_line + curwin->w_cursor.col;
844 while (*p != NUL)
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000845 {
846#ifdef FEAT_MBYTE
847 if (has_mbyte)
848 p += replace_push_mb(p);
849 else
850#endif
851 replace_push(*p++);
852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 saved_line[curwin->w_cursor.col] = NUL;
854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +0200856 if ((State & INSERT) && !(State & VREPLACE_FLAG))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 {
858 p_extra = saved_line + curwin->w_cursor.col;
859#ifdef FEAT_SMARTINDENT
860 if (do_si) /* need first char after new line break */
861 {
862 p = skipwhite(p_extra);
863 first_char = *p;
864 }
865#endif
866#ifdef FEAT_COMMENTS
867 extra_len = (int)STRLEN(p_extra);
868#endif
869 saved_char = *p_extra;
870 *p_extra = NUL;
871 }
872
873 u_clearline(); /* cannot do "U" command when adding lines */
874#ifdef FEAT_SMARTINDENT
875 did_si = FALSE;
876#endif
877 ai_col = 0;
878
879 /*
880 * If we just did an auto-indent, then we didn't type anything on
881 * the prior line, and it should be truncated. Do this even if 'ai' is not
882 * set because automatically inserting a comment leader also sets did_ai.
883 */
884 if (dir == FORWARD && did_ai)
885 trunc_line = TRUE;
886
887 /*
888 * If 'autoindent' and/or 'smartindent' is set, try to figure out what
889 * indent to use for the new line.
890 */
891 if (curbuf->b_p_ai
892#ifdef FEAT_SMARTINDENT
893 || do_si
894#endif
895 )
896 {
897 /*
898 * count white space on current line
899 */
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200900#ifdef FEAT_VARTABS
901 newindent = get_indent_str_vtab(saved_line, curbuf->b_p_ts,
902 curbuf->b_p_vts_array, FALSE);
903#else
Bram Moolenaar597a4222014-06-25 14:39:50 +0200904 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200905#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200906 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
907 newindent = second_line_indent; /* for ^^D command in insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908
909#ifdef FEAT_SMARTINDENT
910 /*
911 * Do smart indenting.
912 * In insert/replace mode (only when dir == FORWARD)
913 * we may move some text to the next line. If it starts with '{'
914 * don't add an indent. Fixes inserting a NL before '{' in line
915 * "if (condition) {"
916 */
917 if (!trunc_line && do_si && *saved_line != NUL
918 && (p_extra == NULL || first_char != '{'))
919 {
920 char_u *ptr;
921 char_u last_char;
922
923 old_cursor = curwin->w_cursor;
924 ptr = saved_line;
925# ifdef FEAT_COMMENTS
926 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200927 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 else
929 lead_len = 0;
930# endif
931 if (dir == FORWARD)
932 {
933 /*
934 * Skip preprocessor directives, unless they are
935 * recognised as comments.
936 */
937 if (
938# ifdef FEAT_COMMENTS
939 lead_len == 0 &&
940# endif
941 ptr[0] == '#')
942 {
943 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
944 ptr = ml_get(--curwin->w_cursor.lnum);
945 newindent = get_indent();
946 }
947# ifdef FEAT_COMMENTS
948 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200949 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 else
951 lead_len = 0;
952 if (lead_len > 0)
953 {
954 /*
955 * This case gets the following right:
956 * \*
957 * * A comment (read '\' as '/').
958 * *\
959 * #define IN_THE_WAY
960 * This should line up here;
961 */
962 p = skipwhite(ptr);
963 if (p[0] == '/' && p[1] == '*')
964 p++;
965 if (p[0] == '*')
966 {
967 for (p++; *p; p++)
968 {
969 if (p[0] == '/' && p[-1] == '*')
970 {
971 /*
972 * End of C comment, indent should line up
973 * with the line containing the start of
974 * the comment
975 */
976 curwin->w_cursor.col = (colnr_T)(p - ptr);
977 if ((pos = findmatch(NULL, NUL)) != NULL)
978 {
979 curwin->w_cursor.lnum = pos->lnum;
980 newindent = get_indent();
981 }
982 }
983 }
984 }
985 }
986 else /* Not a comment line */
987# endif
988 {
989 /* Find last non-blank in line */
990 p = ptr + STRLEN(ptr) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100991 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 --p;
993 last_char = *p;
994
995 /*
996 * find the character just before the '{' or ';'
997 */
998 if (last_char == '{' || last_char == ';')
999 {
1000 if (p > ptr)
1001 --p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001002 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 --p;
1004 }
1005 /*
1006 * Try to catch lines that are split over multiple
1007 * lines. eg:
1008 * if (condition &&
1009 * condition) {
1010 * Should line up here!
1011 * }
1012 */
1013 if (*p == ')')
1014 {
1015 curwin->w_cursor.col = (colnr_T)(p - ptr);
1016 if ((pos = findmatch(NULL, '(')) != NULL)
1017 {
1018 curwin->w_cursor.lnum = pos->lnum;
1019 newindent = get_indent();
1020 ptr = ml_get_curline();
1021 }
1022 }
1023 /*
1024 * If last character is '{' do indent, without
1025 * checking for "if" and the like.
1026 */
1027 if (last_char == '{')
1028 {
1029 did_si = TRUE; /* do indent */
1030 no_si = TRUE; /* don't delete it when '{' typed */
1031 }
1032 /*
1033 * Look for "if" and the like, use 'cinwords'.
1034 * Don't do this if the previous line ended in ';' or
1035 * '}'.
1036 */
1037 else if (last_char != ';' && last_char != '}'
1038 && cin_is_cinword(ptr))
1039 did_si = TRUE;
1040 }
1041 }
1042 else /* dir == BACKWARD */
1043 {
1044 /*
1045 * Skip preprocessor directives, unless they are
1046 * recognised as comments.
1047 */
1048 if (
1049# ifdef FEAT_COMMENTS
1050 lead_len == 0 &&
1051# endif
1052 ptr[0] == '#')
1053 {
1054 int was_backslashed = FALSE;
1055
1056 while ((ptr[0] == '#' || was_backslashed) &&
1057 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
1058 {
1059 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
1060 was_backslashed = TRUE;
1061 else
1062 was_backslashed = FALSE;
1063 ptr = ml_get(++curwin->w_cursor.lnum);
1064 }
1065 if (was_backslashed)
1066 newindent = 0; /* Got to end of file */
1067 else
1068 newindent = get_indent();
1069 }
1070 p = skipwhite(ptr);
1071 if (*p == '}') /* if line starts with '}': do indent */
1072 did_si = TRUE;
1073 else /* can delete indent when '{' typed */
1074 can_si_back = TRUE;
1075 }
1076 curwin->w_cursor = old_cursor;
1077 }
1078 if (do_si)
1079 can_si = TRUE;
1080#endif /* FEAT_SMARTINDENT */
1081
1082 did_ai = TRUE;
1083 }
1084
1085#ifdef FEAT_COMMENTS
1086 /*
1087 * Find out if the current line starts with a comment leader.
1088 * This may then be inserted in front of the new line.
1089 */
1090 end_comment_pending = NUL;
1091 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +02001092 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 else
1094 lead_len = 0;
1095 if (lead_len > 0)
1096 {
1097 char_u *lead_repl = NULL; /* replaces comment leader */
1098 int lead_repl_len = 0; /* length of *lead_repl */
1099 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
1100 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
1101 char_u *comment_end = NULL; /* where lead_end has been found */
1102 int extra_space = FALSE; /* append extra space */
1103 int current_flag;
1104 int require_blank = FALSE; /* requires blank after middle */
1105 char_u *p2;
1106
1107 /*
1108 * If the comment leader has the start, middle or end flag, it may not
1109 * be used or may be replaced with the middle leader.
1110 */
1111 for (p = lead_flags; *p && *p != ':'; ++p)
1112 {
1113 if (*p == COM_BLANK)
1114 {
1115 require_blank = TRUE;
1116 continue;
1117 }
1118 if (*p == COM_START || *p == COM_MIDDLE)
1119 {
1120 current_flag = *p;
1121 if (*p == COM_START)
1122 {
1123 /*
1124 * Doing "O" on a start of comment does not insert leader.
1125 */
1126 if (dir == BACKWARD)
1127 {
1128 lead_len = 0;
1129 break;
1130 }
1131
1132 /* find start of middle part */
1133 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1134 require_blank = FALSE;
1135 }
1136
1137 /*
1138 * Isolate the strings of the middle and end leader.
1139 */
1140 while (*p && p[-1] != ':') /* find end of middle flags */
1141 {
1142 if (*p == COM_BLANK)
1143 require_blank = TRUE;
1144 ++p;
1145 }
1146 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
1147
1148 while (*p && p[-1] != ':') /* find end of end flags */
1149 {
1150 /* Check whether we allow automatic ending of comments */
1151 if (*p == COM_AUTO_END)
1152 end_comment_pending = -1; /* means we want to set it */
1153 ++p;
1154 }
1155 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
1156
1157 if (end_comment_pending == -1) /* we can set it now */
1158 end_comment_pending = lead_end[n - 1];
1159
1160 /*
1161 * If the end of the comment is in the same line, don't use
1162 * the comment leader.
1163 */
1164 if (dir == FORWARD)
1165 {
1166 for (p = saved_line + lead_len; *p; ++p)
1167 if (STRNCMP(p, lead_end, n) == 0)
1168 {
1169 comment_end = p;
1170 lead_len = 0;
1171 break;
1172 }
1173 }
1174
1175 /*
1176 * Doing "o" on a start of comment inserts the middle leader.
1177 */
1178 if (lead_len > 0)
1179 {
1180 if (current_flag == COM_START)
1181 {
1182 lead_repl = lead_middle;
1183 lead_repl_len = (int)STRLEN(lead_middle);
1184 }
1185
1186 /*
1187 * If we have hit RETURN immediately after the start
1188 * comment leader, then put a space after the middle
1189 * comment leader on the next line.
1190 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001191 if (!VIM_ISWHITE(saved_line[lead_len - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 && ((p_extra != NULL
1193 && (int)curwin->w_cursor.col == lead_len)
1194 || (p_extra == NULL
1195 && saved_line[lead_len] == NUL)
1196 || require_blank))
1197 extra_space = TRUE;
1198 }
1199 break;
1200 }
1201 if (*p == COM_END)
1202 {
1203 /*
1204 * Doing "o" on the end of a comment does not insert leader.
1205 * Remember where the end is, might want to use it to find the
1206 * start (for C-comments).
1207 */
1208 if (dir == FORWARD)
1209 {
1210 comment_end = skipwhite(saved_line);
1211 lead_len = 0;
1212 break;
1213 }
1214
1215 /*
1216 * Doing "O" on the end of a comment inserts the middle leader.
1217 * Find the string for the middle leader, searching backwards.
1218 */
1219 while (p > curbuf->b_p_com && *p != ',')
1220 --p;
1221 for (lead_repl = p; lead_repl > curbuf->b_p_com
1222 && lead_repl[-1] != ':'; --lead_repl)
1223 ;
1224 lead_repl_len = (int)(p - lead_repl);
1225
1226 /* We can probably always add an extra space when doing "O" on
1227 * the comment-end */
1228 extra_space = TRUE;
1229
1230 /* Check whether we allow automatic ending of comments */
1231 for (p2 = p; *p2 && *p2 != ':'; p2++)
1232 {
1233 if (*p2 == COM_AUTO_END)
1234 end_comment_pending = -1; /* means we want to set it */
1235 }
1236 if (end_comment_pending == -1)
1237 {
1238 /* Find last character in end-comment string */
1239 while (*p2 && *p2 != ',')
1240 p2++;
1241 end_comment_pending = p2[-1];
1242 }
1243 break;
1244 }
1245 if (*p == COM_FIRST)
1246 {
1247 /*
1248 * Comment leader for first line only: Don't repeat leader
1249 * when using "O", blank out leader when using "o".
1250 */
1251 if (dir == BACKWARD)
1252 lead_len = 0;
1253 else
1254 {
1255 lead_repl = (char_u *)"";
1256 lead_repl_len = 0;
1257 }
1258 break;
1259 }
1260 }
1261 if (lead_len)
1262 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001263 /* allocate buffer (may concatenate p_extra later) */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001264 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001265 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 allocated = leader; /* remember to free it later */
1267
1268 if (leader == NULL)
1269 lead_len = 0;
1270 else
1271 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001272 vim_strncpy(leader, saved_line, lead_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273
1274 /*
1275 * Replace leader with lead_repl, right or left adjusted
1276 */
1277 if (lead_repl != NULL)
1278 {
1279 int c = 0;
1280 int off = 0;
1281
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001282 for (p = lead_flags; *p != NUL && *p != ':'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 {
1284 if (*p == COM_RIGHT || *p == COM_LEFT)
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001285 c = *p++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 else if (VIM_ISDIGIT(*p) || *p == '-')
1287 off = getdigits(&p);
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001288 else
1289 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 }
1291 if (c == COM_RIGHT) /* right adjusted leader */
1292 {
1293 /* find last non-white in the leader to line up with */
1294 for (p = leader + lead_len - 1; p > leader
Bram Moolenaar1c465442017-03-12 20:10:05 +01001295 && VIM_ISWHITE(*p); --p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 ++p;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001298
1299#ifdef FEAT_MBYTE
1300 /* Compute the length of the replaced characters in
1301 * screen characters, not bytes. */
1302 {
1303 int repl_size = vim_strnsize(lead_repl,
1304 lead_repl_len);
1305 int old_size = 0;
1306 char_u *endp = p;
1307 int l;
1308
1309 while (old_size < repl_size && p > leader)
1310 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001311 MB_PTR_BACK(leader, p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001312 old_size += ptr2cells(p);
1313 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001314 l = lead_repl_len - (int)(endp - p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001315 if (l != 0)
1316 mch_memmove(endp + l, endp,
1317 (size_t)((leader + lead_len) - endp));
1318 lead_len += l;
1319 }
1320#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 if (p < leader + lead_repl_len)
1322 p = leader;
1323 else
1324 p -= lead_repl_len;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1327 if (p + lead_repl_len > leader + lead_len)
1328 p[lead_repl_len] = NUL;
1329
1330 /* blank-out any other chars from the old leader. */
1331 while (--p >= leader)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001332 {
1333#ifdef FEAT_MBYTE
1334 int l = mb_head_off(leader, p);
1335
1336 if (l > 1)
1337 {
1338 p -= l;
1339 if (ptr2cells(p) > 1)
1340 {
1341 p[1] = ' ';
1342 --l;
1343 }
1344 mch_memmove(p + 1, p + l + 1,
1345 (size_t)((leader + lead_len) - (p + l + 1)));
1346 lead_len -= l;
1347 *p = ' ';
1348 }
1349 else
1350#endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01001351 if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 }
1355 else /* left adjusted leader */
1356 {
1357 p = skipwhite(leader);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001358#ifdef FEAT_MBYTE
1359 /* Compute the length of the replaced characters in
1360 * screen characters, not bytes. Move the part that is
1361 * not to be overwritten. */
1362 {
1363 int repl_size = vim_strnsize(lead_repl,
1364 lead_repl_len);
1365 int i;
1366 int l;
1367
Bram Moolenaardc633cf2016-04-23 14:33:19 +02001368 for (i = 0; i < lead_len && p[i] != NUL; i += l)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001369 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001370 l = (*mb_ptr2len)(p + i);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001371 if (vim_strnsize(p, i + l) > repl_size)
1372 break;
1373 }
1374 if (i != lead_repl_len)
1375 {
1376 mch_memmove(p + lead_repl_len, p + i,
Bram Moolenaar2d7ff052009-11-17 15:08:26 +00001377 (size_t)(lead_len - i - (p - leader)));
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001378 lead_len += lead_repl_len - i;
1379 }
1380 }
1381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1383
1384 /* Replace any remaining non-white chars in the old
1385 * leader by spaces. Keep Tabs, the indent must
1386 * remain the same. */
1387 for (p += lead_repl_len; p < leader + lead_len; ++p)
Bram Moolenaar1c465442017-03-12 20:10:05 +01001388 if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 {
1390 /* Don't put a space before a TAB. */
1391 if (p + 1 < leader + lead_len && p[1] == TAB)
1392 {
1393 --lead_len;
1394 mch_memmove(p, p + 1,
1395 (leader + lead_len) - p);
1396 }
1397 else
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001398 {
1399#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001400 int l = (*mb_ptr2len)(p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001401
1402 if (l > 1)
1403 {
1404 if (ptr2cells(p) > 1)
1405 {
1406 /* Replace a double-wide char with
1407 * two spaces */
1408 --l;
1409 *p++ = ' ';
1410 }
1411 mch_memmove(p + 1, p + l,
1412 (leader + lead_len) - p);
1413 lead_len -= l - 1;
1414 }
1415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 }
1419 *p = NUL;
1420 }
1421
1422 /* Recompute the indent, it may have changed. */
1423 if (curbuf->b_p_ai
1424#ifdef FEAT_SMARTINDENT
1425 || do_si
1426#endif
1427 )
Bram Moolenaar04958cb2018-06-23 19:23:02 +02001428#ifdef FEAT_VARTABS
1429 newindent = get_indent_str_vtab(leader, curbuf->b_p_ts,
1430 curbuf->b_p_vts_array, FALSE);
1431#else
1432 newindent = get_indent_str(leader,
1433 (int)curbuf->b_p_ts, FALSE);
1434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435
1436 /* Add the indent offset */
1437 if (newindent + off < 0)
1438 {
1439 off = -newindent;
1440 newindent = 0;
1441 }
1442 else
1443 newindent += off;
1444
1445 /* Correct trailing spaces for the shift, so that
1446 * alignment remains equal. */
1447 while (off > 0 && lead_len > 0
1448 && leader[lead_len - 1] == ' ')
1449 {
1450 /* Don't do it when there is a tab before the space */
1451 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1452 break;
1453 --lead_len;
1454 --off;
1455 }
1456
1457 /* If the leader ends in white space, don't add an
1458 * extra space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001459 if (lead_len > 0 && VIM_ISWHITE(leader[lead_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 extra_space = FALSE;
1461 leader[lead_len] = NUL;
1462 }
1463
1464 if (extra_space)
1465 {
1466 leader[lead_len++] = ' ';
1467 leader[lead_len] = NUL;
1468 }
1469
1470 newcol = lead_len;
1471
1472 /*
1473 * if a new indent will be set below, remove the indent that
1474 * is in the comment leader
1475 */
1476 if (newindent
1477#ifdef FEAT_SMARTINDENT
1478 || did_si
1479#endif
1480 )
1481 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001482 while (lead_len && VIM_ISWHITE(*leader))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 {
1484 --lead_len;
1485 --newcol;
1486 ++leader;
1487 }
1488 }
1489
1490 }
1491#ifdef FEAT_SMARTINDENT
1492 did_si = can_si = FALSE;
1493#endif
1494 }
1495 else if (comment_end != NULL)
1496 {
1497 /*
1498 * We have finished a comment, so we don't use the leader.
1499 * If this was a C-comment and 'ai' or 'si' is set do a normal
1500 * indent to align with the line containing the start of the
1501 * comment.
1502 */
1503 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1504 (curbuf->b_p_ai
1505#ifdef FEAT_SMARTINDENT
1506 || do_si
1507#endif
1508 ))
1509 {
1510 old_cursor = curwin->w_cursor;
1511 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1512 if ((pos = findmatch(NULL, NUL)) != NULL)
1513 {
1514 curwin->w_cursor.lnum = pos->lnum;
1515 newindent = get_indent();
1516 }
1517 curwin->w_cursor = old_cursor;
1518 }
1519 }
1520 }
1521#endif
1522
1523 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */
1524 if (p_extra != NULL)
1525 {
1526 *p_extra = saved_char; /* restore char that NUL replaced */
1527
1528 /*
1529 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
1530 * non-blank.
1531 *
1532 * When in REPLACE mode, put the deleted blanks on the replace stack,
1533 * preceded by a NUL, so they can be put back when a BS is entered.
1534 */
1535 if (REPLACE_NORMAL(State))
1536 replace_push(NUL); /* end of extra blanks */
1537 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
1538 {
1539 while ((*p_extra == ' ' || *p_extra == '\t')
1540#ifdef FEAT_MBYTE
1541 && (!enc_utf8
1542 || !utf_iscomposing(utf_ptr2char(p_extra + 1)))
1543#endif
1544 )
1545 {
1546 if (REPLACE_NORMAL(State))
1547 replace_push(*p_extra);
1548 ++p_extra;
1549 ++less_cols_off;
1550 }
1551 }
1552 if (*p_extra != NUL)
1553 did_ai = FALSE; /* append some text, don't truncate now */
1554
1555 /* columns for marks adjusted for removed columns */
1556 less_cols = (int)(p_extra - saved_line);
1557 }
1558
1559 if (p_extra == NULL)
1560 p_extra = (char_u *)""; /* append empty line */
1561
1562#ifdef FEAT_COMMENTS
1563 /* concatenate leader and p_extra, if there is a leader */
1564 if (lead_len)
1565 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001566 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
1567 {
1568 int i;
Bram Moolenaar36105782012-06-14 20:59:25 +02001569 int padding = second_line_indent
1570 - (newindent + (int)STRLEN(leader));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001571
1572 /* Here whitespace is inserted after the comment char.
1573 * Below, set_indent(newindent, SIN_INSERT) will insert the
1574 * whitespace needed before the comment char. */
1575 for (i = 0; i < padding; i++)
1576 {
1577 STRCAT(leader, " ");
Bram Moolenaar5fb9ec52012-07-25 16:10:03 +02001578 less_cols--;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001579 newcol++;
1580 }
1581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 STRCAT(leader, p_extra);
1583 p_extra = leader;
1584 did_ai = TRUE; /* So truncating blanks works with comments */
1585 less_cols -= lead_len;
1586 }
1587 else
1588 end_comment_pending = NUL; /* turns out there was no leader */
1589#endif
1590
1591 old_cursor = curwin->w_cursor;
1592 if (dir == BACKWARD)
1593 --curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 {
1596 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
1597 == FAIL)
1598 goto theend;
1599 /* Postpone calling changed_lines(), because it would mess up folding
Bram Moolenaar82faa252016-06-04 20:14:07 +02001600 * with markers.
1601 * Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01001602 * be marks there. But still needed in diff mode. */
1603 if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count
1604#ifdef FEAT_DIFF
1605 || curwin->w_p_diff
1606#endif
1607 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02001608 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 did_append = TRUE;
1610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 else
1612 {
1613 /*
1614 * In VREPLACE mode we are starting to replace the next line.
1615 */
1616 curwin->w_cursor.lnum++;
1617 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1618 {
1619 /* In case we NL to a new line, BS to the previous one, and NL
1620 * again, we don't want to save the new line for undo twice.
1621 */
1622 (void)u_save_cursor(); /* errors are ignored! */
1623 vr_lines_changed++;
1624 }
1625 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1626 changed_bytes(curwin->w_cursor.lnum, 0);
1627 curwin->w_cursor.lnum--;
1628 did_append = FALSE;
1629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630
1631 if (newindent
1632#ifdef FEAT_SMARTINDENT
1633 || did_si
1634#endif
1635 )
1636 {
1637 ++curwin->w_cursor.lnum;
1638#ifdef FEAT_SMARTINDENT
1639 if (did_si)
1640 {
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001641 int sw = (int)get_sw_value(curbuf);
Bram Moolenaar14f24742012-08-08 18:01:05 +02001642
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001644 newindent -= newindent % sw;
1645 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 }
1647#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001648 /* Copy the indent */
1649 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 {
1651 (void)copy_indent(newindent, saved_line);
1652
1653 /*
1654 * Set the 'preserveindent' option so that any further screwing
1655 * with the line doesn't entirely destroy our efforts to preserve
1656 * it. It gets restored at the function end.
1657 */
1658 curbuf->b_p_pi = TRUE;
1659 }
1660 else
1661 (void)set_indent(newindent, SIN_INSERT);
1662 less_cols -= curwin->w_cursor.col;
1663
1664 ai_col = curwin->w_cursor.col;
1665
1666 /*
1667 * In REPLACE mode, for each character in the new indent, there must
1668 * be a NUL on the replace stack, for when it is deleted with BS
1669 */
1670 if (REPLACE_NORMAL(State))
1671 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1672 replace_push(NUL);
1673 newcol += curwin->w_cursor.col;
1674#ifdef FEAT_SMARTINDENT
1675 if (no_si)
1676 did_si = FALSE;
1677#endif
1678 }
1679
1680#ifdef FEAT_COMMENTS
1681 /*
1682 * In REPLACE mode, for each character in the extra leader, there must be
1683 * a NUL on the replace stack, for when it is deleted with BS.
1684 */
1685 if (REPLACE_NORMAL(State))
1686 while (lead_len-- > 0)
1687 replace_push(NUL);
1688#endif
1689
1690 curwin->w_cursor = old_cursor;
1691
1692 if (dir == FORWARD)
1693 {
1694 if (trunc_line || (State & INSERT))
1695 {
1696 /* truncate current line at cursor */
1697 saved_line[curwin->w_cursor.col] = NUL;
1698 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1699 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1700 truncate_spaces(saved_line);
1701 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1702 saved_line = NULL;
1703 if (did_append)
1704 {
1705 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1706 curwin->w_cursor.lnum + 1, 1L);
1707 did_append = FALSE;
1708
1709 /* Move marks after the line break to the new line. */
1710 if (flags & OPENLINE_MARKFIX)
1711 mark_col_adjust(curwin->w_cursor.lnum,
1712 curwin->w_cursor.col + less_cols_off,
Bram Moolenaare1e714e2018-12-31 23:58:24 +01001713 1L, (long)-less_cols, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 }
1715 else
1716 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1717 }
1718
1719 /*
1720 * Put the cursor on the new line. Careful: the scrollup() above may
1721 * have moved w_cursor, we must use old_cursor.
1722 */
1723 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1724 }
1725 if (did_append)
1726 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1727
1728 curwin->w_cursor.col = newcol;
1729#ifdef FEAT_VIRTUALEDIT
1730 curwin->w_cursor.coladd = 0;
1731#endif
1732
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02001733#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 /*
1735 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1736 * fixthisline() from doing it (via change_indent()) by telling it we're in
1737 * normal INSERT mode.
1738 */
1739 if (State & VREPLACE_FLAG)
1740 {
1741 vreplace_mode = State; /* So we know to put things right later */
1742 State = INSERT;
1743 }
1744 else
1745 vreplace_mode = 0;
1746#endif
1747#ifdef FEAT_LISP
1748 /*
1749 * May do lisp indenting.
1750 */
1751 if (!p_paste
1752# ifdef FEAT_COMMENTS
1753 && leader == NULL
1754# endif
1755 && curbuf->b_p_lisp
1756 && curbuf->b_p_ai)
1757 {
1758 fixthisline(get_lisp_indent);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001759 ai_col = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 }
1761#endif
1762#ifdef FEAT_CINDENT
1763 /*
1764 * May do indenting after opening a new line.
1765 */
1766 if (!p_paste
1767 && (curbuf->b_p_cin
1768# ifdef FEAT_EVAL
1769 || *curbuf->b_p_inde != NUL
1770# endif
1771 )
1772 && in_cinkeys(dir == FORWARD
1773 ? KEY_OPEN_FORW
1774 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1775 {
1776 do_c_expr_indent();
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001777 ai_col = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 }
1779#endif
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02001780#if defined(FEAT_LISP) || defined(FEAT_CINDENT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 if (vreplace_mode != 0)
1782 State = vreplace_mode;
1783#endif
1784
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 /*
1786 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1787 * original line, and inserts the new stuff char by char, pushing old stuff
1788 * onto the replace stack (via ins_char()).
1789 */
1790 if (State & VREPLACE_FLAG)
1791 {
1792 /* Put new line in p_extra */
1793 p_extra = vim_strsave(ml_get_curline());
1794 if (p_extra == NULL)
1795 goto theend;
1796
1797 /* Put back original line */
1798 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1799
1800 /* Insert new stuff into line again */
1801 curwin->w_cursor.col = 0;
1802#ifdef FEAT_VIRTUALEDIT
1803 curwin->w_cursor.coladd = 0;
1804#endif
1805 ins_bytes(p_extra); /* will call changed_bytes() */
1806 vim_free(p_extra);
1807 next_line = NULL;
1808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001810 retval = OK; /* success! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811theend:
1812 curbuf->b_p_pi = saved_pi;
1813 vim_free(saved_line);
1814 vim_free(next_line);
1815 vim_free(allocated);
1816 return retval;
1817}
1818
1819#if defined(FEAT_COMMENTS) || defined(PROTO)
1820/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +02001821 * get_leader_len() returns the length in bytes of the prefix of the given
1822 * string which introduces a comment. If this string is not a comment then
1823 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 * When "flags" is not NULL, it is set to point to the flags of the recognized
1825 * comment leader.
1826 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001827 * If "include_space" is set, include trailing whitespace while calculating the
1828 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 */
1830 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001831get_leader_len(
1832 char_u *line,
1833 char_u **flags,
1834 int backward,
1835 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836{
1837 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001838 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 int got_com = FALSE;
1840 int found_one;
1841 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1842 char_u *string; /* pointer to comment string */
1843 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001844 int middle_match_len = 0;
1845 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001846 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847
Bram Moolenaar81340392012-06-06 16:12:59 +02001848 result = i = 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001849 while (VIM_ISWHITE(line[i])) /* leading white space is ignored */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 ++i;
1851
1852 /*
1853 * Repeat to match several nested comment strings.
1854 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001855 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 {
1857 /*
1858 * scan through the 'comments' option for a match
1859 */
1860 found_one = FALSE;
1861 for (list = curbuf->b_p_com; *list; )
1862 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001863 /* Get one option part into part_buf[]. Advance "list" to next
1864 * one. Put "string" at start of string. */
1865 if (!got_com && flags != NULL)
1866 *flags = list; /* remember where flags started */
1867 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1869 string = vim_strchr(part_buf, ':');
1870 if (string == NULL) /* missing ':', ignore this part */
1871 continue;
1872 *string++ = NUL; /* isolate flags from string */
1873
Bram Moolenaara4271d52011-05-10 13:38:27 +02001874 /* If we found a middle match previously, use that match when this
1875 * is not a middle or end. */
1876 if (middle_match_len != 0
1877 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1878 && vim_strchr(part_buf, COM_END) == NULL)
1879 break;
1880
1881 /* When we already found a nested comment, only accept further
1882 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1884 continue;
1885
Bram Moolenaara4271d52011-05-10 13:38:27 +02001886 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1888 continue;
1889
Bram Moolenaara4271d52011-05-10 13:38:27 +02001890 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 * When string starts with white space, must have some white space
1892 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001893 * TABs and spaces). */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001894 if (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001896 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001897 continue; /* missing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001898 while (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 ++string;
1900 }
1901 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1902 ;
1903 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001904 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905
Bram Moolenaara4271d52011-05-10 13:38:27 +02001906 /* When 'b' flag used, there must be white space or an
1907 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001909 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 continue;
1911
Bram Moolenaara4271d52011-05-10 13:38:27 +02001912 /* We have found a match, stop searching unless this is a middle
1913 * comment. The middle comment can be a substring of the end
1914 * comment in which case it's better to return the length of the
1915 * end comment and its flags. Thus we keep searching with middle
1916 * and end matches and use an end match if it matches better. */
1917 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1918 {
1919 if (middle_match_len == 0)
1920 {
1921 middle_match_len = j;
1922 saved_flags = prev_list;
1923 }
1924 continue;
1925 }
1926 if (middle_match_len != 0 && j > middle_match_len)
1927 /* Use this match instead of the middle match, since it's a
1928 * longer thus better match. */
1929 middle_match_len = 0;
1930
1931 if (middle_match_len == 0)
1932 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 found_one = TRUE;
1934 break;
1935 }
1936
Bram Moolenaara4271d52011-05-10 13:38:27 +02001937 if (middle_match_len != 0)
1938 {
1939 /* Use the previously found middle match after failing to find a
1940 * match with an end. */
1941 if (!got_com && flags != NULL)
1942 *flags = saved_flags;
1943 i += middle_match_len;
1944 found_one = TRUE;
1945 }
1946
1947 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 if (!found_one)
1949 break;
1950
Bram Moolenaar81340392012-06-06 16:12:59 +02001951 result = i;
1952
Bram Moolenaara4271d52011-05-10 13:38:27 +02001953 /* Include any trailing white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001954 while (VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 ++i;
1956
Bram Moolenaar81340392012-06-06 16:12:59 +02001957 if (include_space)
1958 result = i;
1959
Bram Moolenaara4271d52011-05-10 13:38:27 +02001960 /* If this comment doesn't nest, stop here. */
1961 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 if (vim_strchr(part_buf, COM_NEST) == NULL)
1963 break;
1964 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001965 return result;
1966}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001967
Bram Moolenaar81340392012-06-06 16:12:59 +02001968/*
1969 * Return the offset at which the last comment in line starts. If there is no
1970 * comment in the whole line, -1 is returned.
1971 *
1972 * When "flags" is not null, it is set to point to the flags describing the
1973 * recognized comment leader.
1974 */
1975 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001976get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +02001977{
1978 int result = -1;
1979 int i, j;
1980 int lower_check_bound = 0;
1981 char_u *string;
1982 char_u *com_leader;
1983 char_u *com_flags;
1984 char_u *list;
1985 int found_one;
1986 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1987
1988 /*
1989 * Repeat to match several nested comment strings.
1990 */
1991 i = (int)STRLEN(line);
1992 while (--i >= lower_check_bound)
1993 {
1994 /*
1995 * scan through the 'comments' option for a match
1996 */
1997 found_one = FALSE;
1998 for (list = curbuf->b_p_com; *list; )
1999 {
2000 char_u *flags_save = list;
2001
2002 /*
2003 * Get one option part into part_buf[]. Advance list to next one.
2004 * put string at start of string.
2005 */
2006 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
2007 string = vim_strchr(part_buf, ':');
2008 if (string == NULL) /* If everything is fine, this cannot actually
2009 * happen. */
2010 {
2011 continue;
2012 }
2013 *string++ = NUL; /* Isolate flags from string. */
2014 com_leader = string;
2015
2016 /*
2017 * Line contents and string must match.
2018 * When string starts with white space, must have some white space
2019 * (but the amount does not need to match, there might be a mix of
2020 * TABs and spaces).
2021 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002022 if (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +02002023 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01002024 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar81340392012-06-06 16:12:59 +02002025 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +01002026 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +02002027 ++string;
2028 }
2029 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
2030 /* do nothing */;
2031 if (string[j] != NUL)
2032 continue;
2033
2034 /*
2035 * When 'b' flag used, there must be white space or an
2036 * end-of-line after the string in the line.
2037 */
2038 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002039 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar81340392012-06-06 16:12:59 +02002040 continue;
Bram Moolenaar53932812018-12-07 21:08:49 +01002041
Bram Moolenaar4af72592018-12-09 15:00:52 +01002042 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
Bram Moolenaar53932812018-12-07 21:08:49 +01002043 {
Bram Moolenaar4af72592018-12-09 15:00:52 +01002044 // For a middlepart comment, only consider it to match if
2045 // everything before the current position in the line is
2046 // whitespace. Otherwise we would think we are inside a
2047 // comment if the middle part appears somewhere in the middle
2048 // of the line. E.g. for C the "*" appears often.
Bram Moolenaar53932812018-12-07 21:08:49 +01002049 for (j = 0; VIM_ISWHITE(line[j]) && j <= i; j++)
2050 ;
2051 if (j < i)
2052 continue;
Bram Moolenaar81340392012-06-06 16:12:59 +02002053 }
2054
2055 /*
2056 * We have found a match, stop searching.
2057 */
2058 found_one = TRUE;
2059
2060 if (flags)
2061 *flags = flags_save;
2062 com_flags = flags_save;
2063
2064 break;
2065 }
2066
2067 if (found_one)
2068 {
2069 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
2070 int len1, len2, off;
2071
2072 result = i;
2073 /*
2074 * If this comment nests, continue searching.
2075 */
2076 if (vim_strchr(part_buf, COM_NEST) != NULL)
2077 continue;
2078
2079 lower_check_bound = i;
2080
2081 /* Let's verify whether the comment leader found is a substring
2082 * of other comment leaders. If it is, let's adjust the
2083 * lower_check_bound so that we make sure that we have determined
2084 * the comment leader correctly.
2085 */
2086
Bram Moolenaar1c465442017-03-12 20:10:05 +01002087 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +02002088 ++com_leader;
2089 len1 = (int)STRLEN(com_leader);
2090
2091 for (list = curbuf->b_p_com; *list; )
2092 {
2093 char_u *flags_save = list;
2094
2095 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
2096 if (flags_save == com_flags)
2097 continue;
2098 string = vim_strchr(part_buf2, ':');
2099 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +01002100 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +02002101 ++string;
2102 len2 = (int)STRLEN(string);
2103 if (len2 == 0)
2104 continue;
2105
2106 /* Now we have to verify whether string ends with a substring
2107 * beginning the com_leader. */
2108 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
2109 {
2110 --off;
2111 if (!STRNCMP(string + off, com_leader, len2 - off))
2112 {
2113 if (i - off < lower_check_bound)
2114 lower_check_bound = i - off;
2115 }
2116 }
2117 }
2118 }
2119 }
2120 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121}
2122#endif
2123
2124/*
2125 * Return the number of window lines occupied by buffer line "lnum".
2126 */
2127 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002128plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129{
2130 return plines_win(curwin, lnum, TRUE);
2131}
2132
2133 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002134plines_win(
2135 win_T *wp,
2136 linenr_T lnum,
2137 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138{
2139#if defined(FEAT_DIFF) || defined(PROTO)
2140 /* Check for filler lines above this buffer line. When folded the result
2141 * is one line anyway. */
2142 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
2143}
2144
2145 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002146plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147{
2148 return plines_win_nofill(curwin, lnum, TRUE);
2149}
2150
2151 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002152plines_win_nofill(
2153 win_T *wp,
2154 linenr_T lnum,
2155 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156{
2157#endif
2158 int lines;
2159
2160 if (!wp->w_p_wrap)
2161 return 1;
2162
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 if (wp->w_width == 0)
2164 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165
2166#ifdef FEAT_FOLDING
2167 /* A folded lines is handled just like an empty line. */
2168 /* NOTE: Caller must handle lines that are MAYBE folded. */
2169 if (lineFolded(wp, lnum) == TRUE)
2170 return 1;
2171#endif
2172
2173 lines = plines_win_nofold(wp, lnum);
2174 if (winheight > 0 && lines > wp->w_height)
2175 return (int)wp->w_height;
2176 return lines;
2177}
2178
2179/*
2180 * Return number of window lines physical line "lnum" will occupy in window
2181 * "wp". Does not care about folding, 'wrap' or 'diff'.
2182 */
2183 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002184plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185{
2186 char_u *s;
2187 long col;
2188 int width;
2189
2190 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2191 if (*s == NUL) /* empty line */
2192 return 1;
2193 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
2194
2195 /*
2196 * If list mode is on, then the '$' at the end of the line may take up one
2197 * extra column.
2198 */
2199 if (wp->w_p_list && lcs_eol != NUL)
2200 col += 1;
2201
2202 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002203 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002205 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 if (width <= 0)
2207 return 32000;
2208 if (col <= width)
2209 return 1;
2210 col -= width;
2211 width += win_col_off2(wp);
2212 return (col + (width - 1)) / width + 1;
2213}
2214
2215/*
2216 * Like plines_win(), but only reports the number of physical screen lines
2217 * used from the start of the line to the given column number.
2218 */
2219 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002220plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221{
2222 long col;
2223 char_u *s;
2224 int lines = 0;
2225 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002226 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227
2228#ifdef FEAT_DIFF
2229 /* Check for filler lines above this buffer line. When folded the result
2230 * is one line anyway. */
2231 lines = diff_check_fill(wp, lnum);
2232#endif
2233
2234 if (!wp->w_p_wrap)
2235 return lines + 1;
2236
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 if (wp->w_width == 0)
2238 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239
Bram Moolenaar597a4222014-06-25 14:39:50 +02002240 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241
2242 col = 0;
2243 while (*s != NUL && --column >= 0)
2244 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02002245 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002246 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 }
2248
2249 /*
2250 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2251 * INSERT mode, then col must be adjusted so that it represents the last
2252 * screen position of the TAB. This only fixes an error when the TAB wraps
2253 * from one screen line to the next (when 'columns' is not a multiple of
2254 * 'ts') -- webb.
2255 */
2256 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02002257 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258
2259 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002260 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002262 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002263 if (width <= 0)
2264 return 9999;
2265
2266 lines += 1;
2267 if (col > width)
2268 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2269 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270}
2271
2272 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002273plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274{
2275 int count = 0;
2276
2277 while (first <= last)
2278 {
2279#ifdef FEAT_FOLDING
2280 int x;
2281
2282 /* Check if there are any really folded lines, but also included lines
2283 * that are maybe folded. */
2284 x = foldedCount(wp, first, NULL);
2285 if (x > 0)
2286 {
2287 ++count; /* count 1 for "+-- folded" line */
2288 first += x;
2289 }
2290 else
2291#endif
2292 {
2293#ifdef FEAT_DIFF
2294 if (first == wp->w_topline)
2295 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2296 else
2297#endif
2298 count += plines_win(wp, first, TRUE);
2299 ++first;
2300 }
2301 }
2302 return (count);
2303}
2304
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305/*
2306 * Insert string "p" at the cursor position. Stops at a NUL byte.
2307 * Handles Replace mode and multi-byte characters.
2308 */
2309 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002310ins_bytes(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311{
2312 ins_bytes_len(p, (int)STRLEN(p));
2313}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315/*
2316 * Insert string "p" with length "len" at the cursor position.
2317 * Handles Replace mode and multi-byte characters.
2318 */
2319 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002320ins_bytes_len(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321{
2322 int i;
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02002323#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 int n;
2325
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002326 if (has_mbyte)
2327 for (i = 0; i < len; i += n)
2328 {
2329 if (enc_utf8)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002330 // avoid reading past p[len]
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002331 n = utfc_ptr2len_len(p + i, len - i);
2332 else
2333 n = (*mb_ptr2len)(p + i);
2334 ins_char_bytes(p + i, n);
2335 }
2336 else
Bram Moolenaar1f0bfe52018-07-29 16:09:22 +02002337#endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002338 for (i = 0; i < len; ++i)
2339 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341
2342/*
2343 * Insert or replace a single character at the cursor position.
2344 * When in REPLACE or VREPLACE mode, replace any existing character.
2345 * Caller must have prepared for undo.
2346 * For multi-byte characters we get the whole character, the caller must
2347 * convert bytes to a character.
2348 */
2349 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002350ins_char(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002352 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar560379d2017-01-21 22:50:00 +01002353 int n = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354
Bram Moolenaar6a8ede92017-01-22 19:49:12 +01002355#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 n = (*mb_char2bytes)(c, buf);
2357
2358 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2359 * Happens for CTRL-Vu9900. */
2360 if (buf[0] == 0)
2361 buf[0] = '\n';
Bram Moolenaar560379d2017-01-21 22:50:00 +01002362#else
2363 buf[0] = c;
2364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365
2366 ins_char_bytes(buf, n);
2367}
2368
2369 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002370ins_char_bytes(char_u *buf, int charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371{
2372 int c = buf[0];
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002373 int newlen; // nr of bytes inserted
2374 int oldlen; // nr of bytes deleted (0 when not replacing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 char_u *p;
2376 char_u *newp;
2377 char_u *oldp;
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002378 int linelen; // length of old line including NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 colnr_T col;
2380 linenr_T lnum = curwin->w_cursor.lnum;
2381 int i;
2382
2383#ifdef FEAT_VIRTUALEDIT
2384 /* Break tabs if needed. */
2385 if (virtual_active() && curwin->w_cursor.coladd > 0)
2386 coladvance_force(getviscol());
2387#endif
2388
2389 col = curwin->w_cursor.col;
2390 oldp = ml_get(lnum);
2391 linelen = (int)STRLEN(oldp) + 1;
2392
2393 /* The lengths default to the values for when not replacing. */
2394 oldlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 newlen = charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396
2397 if (State & REPLACE_FLAG)
2398 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 if (State & VREPLACE_FLAG)
2400 {
2401 colnr_T new_vcol = 0; /* init for GCC */
2402 colnr_T vcol;
2403 int old_list;
2404#ifndef FEAT_MBYTE
Bram Moolenaar402385a2019-01-11 14:10:03 +01002405 char_u cbuf[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406#endif
2407
2408 /*
2409 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2410 * Returns the old value of list, so when finished,
2411 * curwin->w_p_list should be set back to this.
2412 */
2413 old_list = curwin->w_p_list;
2414 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2415 curwin->w_p_list = FALSE;
2416
2417 /*
2418 * In virtual replace mode each character may replace one or more
2419 * characters (zero if it's a TAB). Count the number of bytes to
2420 * be deleted to make room for the new character, counting screen
2421 * cells. May result in adding spaces to fill a gap.
2422 */
2423 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2424#ifndef FEAT_MBYTE
Bram Moolenaar402385a2019-01-11 14:10:03 +01002425 cbuf[0] = c;
2426 cbuf[1] = NUL;
2427 new_vcol = vcol + chartabsize(cbuf, vcol);
2428#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 new_vcol = vcol + chartabsize(buf, vcol);
Bram Moolenaar402385a2019-01-11 14:10:03 +01002430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2432 {
2433 vcol += chartabsize(oldp + col + oldlen, vcol);
2434 /* Don't need to remove a TAB that takes us to the right
2435 * position. */
2436 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2437 break;
2438#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002439 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440#else
2441 ++oldlen;
2442#endif
2443 /* Deleted a bit too much, insert spaces. */
2444 if (vcol > new_vcol)
2445 newlen += vcol - new_vcol;
2446 }
2447 curwin->w_p_list = old_list;
2448 }
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002449 else if (oldp[col] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 {
2451 /* normal replace */
2452#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002453 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454#else
2455 oldlen = 1;
2456#endif
2457 }
2458
2459
2460 /* Push the replaced bytes onto the replace stack, so that they can be
2461 * put back when BS is used. The bytes of a multi-byte character are
2462 * done the other way around, so that the first byte is popped off
2463 * first (it tells the byte length of the character). */
2464 replace_push(NUL);
2465 for (i = 0; i < oldlen; ++i)
2466 {
2467#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002468 if (has_mbyte)
2469 i += replace_push_mb(oldp + col + i) - 1;
2470 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002472 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 }
2474 }
2475
2476 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2477 if (newp == NULL)
2478 return;
2479
2480 /* Copy bytes before the cursor. */
2481 if (col > 0)
2482 mch_memmove(newp, oldp, (size_t)col);
2483
2484 /* Copy bytes after the changed character(s). */
2485 p = newp + col;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02002486 if (linelen > col + oldlen)
2487 mch_memmove(p + newlen, oldp + col + oldlen,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 (size_t)(linelen - col - oldlen));
2489
2490 /* Insert or overwrite the new character. */
2491#ifdef FEAT_MBYTE
2492 mch_memmove(p, buf, charlen);
2493 i = charlen;
2494#else
2495 *p = c;
2496 i = 1;
2497#endif
2498
2499 /* Fill with spaces when necessary. */
2500 while (i < newlen)
2501 p[i++] = ' ';
2502
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002503 // Replace the line in the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 ml_replace(lnum, newp, FALSE);
2505
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002506 // mark the buffer as changed and prepare for displaying
2507 inserted_bytes(lnum, col, newlen - oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508
2509 /*
2510 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2511 * show the match for right parens and braces.
2512 */
2513 if (p_sm && (State & INSERT)
2514 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002515#ifdef FEAT_INS_EXPAND
2516 && !ins_compl_active()
2517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002519 {
2520#ifdef FEAT_MBYTE
2521 if (has_mbyte)
2522 showmatch(mb_ptr2char(buf));
2523 else
2524#endif
2525 showmatch(c);
2526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527
2528#ifdef FEAT_RIGHTLEFT
2529 if (!p_ri || (State & REPLACE_FLAG))
2530#endif
2531 {
2532 /* Normal insert: move cursor right */
2533#ifdef FEAT_MBYTE
2534 curwin->w_cursor.col += charlen;
2535#else
2536 ++curwin->w_cursor.col;
2537#endif
2538 }
2539 /*
2540 * TODO: should try to update w_row here, to avoid recomputing it later.
2541 */
2542}
2543
2544/*
2545 * Insert a string at the cursor position.
2546 * Note: Does NOT handle Replace mode.
2547 * Caller must have prepared for undo.
2548 */
2549 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002550ins_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551{
2552 char_u *oldp, *newp;
2553 int newlen = (int)STRLEN(s);
2554 int oldlen;
2555 colnr_T col;
2556 linenr_T lnum = curwin->w_cursor.lnum;
2557
2558#ifdef FEAT_VIRTUALEDIT
2559 if (virtual_active() && curwin->w_cursor.coladd > 0)
2560 coladvance_force(getviscol());
2561#endif
2562
2563 col = curwin->w_cursor.col;
2564 oldp = ml_get(lnum);
2565 oldlen = (int)STRLEN(oldp);
2566
2567 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2568 if (newp == NULL)
2569 return;
2570 if (col > 0)
2571 mch_memmove(newp, oldp, (size_t)col);
2572 mch_memmove(newp + col, s, (size_t)newlen);
2573 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2574 ml_replace(lnum, newp, FALSE);
Bram Moolenaar44746aa2019-01-02 00:02:11 +01002575 inserted_bytes(lnum, col, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 curwin->w_cursor.col += newlen;
2577}
2578
2579/*
2580 * Delete one character under the cursor.
2581 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2582 * Caller must have prepared for undo.
2583 *
2584 * return FAIL for failure, OK otherwise
2585 */
2586 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002587del_char(int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588{
2589#ifdef FEAT_MBYTE
2590 if (has_mbyte)
2591 {
2592 /* Make sure the cursor is at the start of a character. */
2593 mb_adjust_cursor();
2594 if (*ml_get_cursor() == NUL)
2595 return FAIL;
2596 return del_chars(1L, fixpos);
2597 }
2598#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002599 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600}
2601
2602#if defined(FEAT_MBYTE) || defined(PROTO)
2603/*
2604 * Like del_bytes(), but delete characters instead of bytes.
2605 */
2606 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002607del_chars(long count, int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608{
2609 long bytes = 0;
2610 long i;
2611 char_u *p;
2612 int l;
2613
2614 p = ml_get_cursor();
2615 for (i = 0; i < count && *p != NUL; ++i)
2616 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002617 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 bytes += l;
2619 p += l;
2620 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002621 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622}
2623#endif
2624
2625/*
2626 * Delete "count" bytes under the cursor.
2627 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2628 * Caller must have prepared for undo.
2629 *
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002630 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 */
2632 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002633del_bytes(
2634 long count,
2635 int fixpos_arg,
2636 int use_delcombine UNUSED) /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637{
2638 char_u *oldp, *newp;
2639 colnr_T oldlen;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002640 colnr_T newlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 linenr_T lnum = curwin->w_cursor.lnum;
2642 colnr_T col = curwin->w_cursor.col;
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002643 int alloc_newp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002645 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646
2647 oldp = ml_get(lnum);
2648 oldlen = (int)STRLEN(oldp);
2649
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002650 /* Can't do anything when the cursor is on the NUL after the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 if (col >= oldlen)
2652 return FAIL;
2653
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002654 /* If "count" is zero there is nothing to do. */
2655 if (count == 0)
2656 return OK;
2657
2658 /* If "count" is negative the caller must be doing something wrong. */
2659 if (count < 1)
2660 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002661 siemsg("E950: Invalid count for del_bytes(): %ld", count);
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002662 return FAIL;
2663 }
2664
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665#ifdef FEAT_MBYTE
2666 /* If 'delcombine' is set and deleting (less than) one character, only
2667 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002668 if (p_deco && use_delcombine && enc_utf8
2669 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002671 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 int n;
2673
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002674 (void)utfc_ptr2char(oldp + col, cc);
2675 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 {
2677 /* Find the last composing char, there can be several. */
2678 n = col;
2679 do
2680 {
2681 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002682 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 n += count;
2684 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2685 fixpos = 0;
2686 }
2687 }
2688#endif
2689
2690 /*
2691 * When count is too big, reduce it.
2692 */
2693 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2694 if (movelen <= 1)
2695 {
2696 /*
2697 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002698 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2699 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002701 if (col > 0 && fixpos && restart_edit == 0
2702#ifdef FEAT_VIRTUALEDIT
2703 && (ve_flags & VE_ONEMORE) == 0
2704#endif
2705 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 {
2707 --curwin->w_cursor.col;
2708#ifdef FEAT_VIRTUALEDIT
2709 curwin->w_cursor.coladd = 0;
2710#endif
2711#ifdef FEAT_MBYTE
2712 if (has_mbyte)
2713 curwin->w_cursor.col -=
2714 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2715#endif
2716 }
2717 count = oldlen - col;
2718 movelen = 1;
2719 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002720 newlen = oldlen - count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721
2722 /*
2723 * If the old line has been allocated the deletion can be done in the
2724 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002725 * Can't do this when using Netbeans, because we would need to invoke
2726 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002727 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002730 if (netbeans_active())
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002731 alloc_newp = TRUE;
Bram Moolenaare21877a2008-02-13 09:58:14 +00002732 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002734 alloc_newp = !ml_line_alloced(); // check if oldp was allocated
2735 if (!alloc_newp)
2736 newp = oldp; // use same allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 else
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002738 { // need to allocate a new line
2739 newp = alloc((unsigned)(newlen + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 if (newp == NULL)
2741 return FAIL;
2742 mch_memmove(newp, oldp, (size_t)col);
2743 }
2744 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002745 if (alloc_newp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 ml_replace(lnum, newp, FALSE);
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002747#ifdef FEAT_TEXT_PROP
2748 else
2749 {
2750 // Also move any following text properties.
2751 if (oldlen + 1 < curbuf->b_ml.ml_line_len)
2752 mch_memmove(newp + newlen + 1, oldp + oldlen + 1,
2753 (size_t)curbuf->b_ml.ml_line_len - oldlen - 1);
2754 curbuf->b_ml.ml_line_len -= count;
2755 }
2756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757
Bram Moolenaar98aefe72018-12-13 22:20:09 +01002758 // mark the buffer as changed and prepare for displaying
Bram Moolenaar33c8ca92019-01-02 18:00:27 +01002759 inserted_bytes(lnum, curwin->w_cursor.col, -count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760
2761 return OK;
2762}
2763
2764/*
2765 * Delete from cursor to end of line.
2766 * Caller must have prepared for undo.
2767 *
2768 * return FAIL for failure, OK otherwise
2769 */
2770 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002771truncate_line(
2772 int fixpos) /* if TRUE fix the cursor position when done */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773{
2774 char_u *newp;
2775 linenr_T lnum = curwin->w_cursor.lnum;
2776 colnr_T col = curwin->w_cursor.col;
2777
2778 if (col == 0)
2779 newp = vim_strsave((char_u *)"");
2780 else
2781 newp = vim_strnsave(ml_get(lnum), col);
2782
2783 if (newp == NULL)
2784 return FAIL;
2785
2786 ml_replace(lnum, newp, FALSE);
2787
2788 /* mark the buffer as changed and prepare for displaying */
2789 changed_bytes(lnum, curwin->w_cursor.col);
2790
2791 /*
2792 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2793 */
2794 if (fixpos && curwin->w_cursor.col > 0)
2795 --curwin->w_cursor.col;
2796
2797 return OK;
2798}
2799
2800/*
2801 * Delete "nlines" lines at the cursor.
2802 * Saves the lines for undo first if "undo" is TRUE.
2803 */
2804 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002805del_lines(
2806 long nlines, /* number of lines to delete */
2807 int undo) /* if TRUE, prepare for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808{
2809 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002810 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811
2812 if (nlines <= 0)
2813 return;
2814
2815 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002816 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 return;
2818
2819 for (n = 0; n < nlines; )
2820 {
2821 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2822 break;
2823
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002824 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 ++n;
2826
2827 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002828 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 break;
2830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002832 /* Correct the cursor position before calling deleted_lines_mark(), it may
2833 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 curwin->w_cursor.col = 0;
2835 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002836
2837 /* adjust marks, mark the buffer as changed and prepare for displaying */
2838 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839}
2840
2841 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002842gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002844 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002846 /* When searching columns is sometimes put at the end of a line. */
2847 if (pos->col == MAXCOL)
2848 return NUL;
2849 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850#ifdef FEAT_MBYTE
2851 if (has_mbyte)
2852 return (*mb_ptr2char)(ptr);
2853#endif
2854 return (int)*ptr;
2855}
2856
2857 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002858gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859{
2860#ifdef FEAT_MBYTE
2861 if (has_mbyte)
2862 return (*mb_ptr2char)(ml_get_cursor());
2863#endif
2864 return (int)*ml_get_cursor();
2865}
2866
2867/*
2868 * Write a character at the current cursor position.
2869 * It is directly written into the block.
2870 */
2871 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002872pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873{
2874 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2875 + curwin->w_cursor.col) = c;
2876}
2877
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878/*
2879 * When extra == 0: Return TRUE if the cursor is before or on the first
2880 * non-blank in the line.
2881 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2882 * the line.
2883 */
2884 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002885inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886{
2887 char_u *ptr;
2888 colnr_T col;
2889
Bram Moolenaar1c465442017-03-12 20:10:05 +01002890 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 ++ptr;
2892 if (col >= curwin->w_cursor.col + extra)
2893 return TRUE;
2894 else
2895 return FALSE;
2896}
2897
2898/*
2899 * Skip to next part of an option argument: Skip space and comma.
2900 */
2901 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002902skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903{
2904 if (*p == ',')
2905 ++p;
2906 while (*p == ' ')
2907 ++p;
2908 return p;
2909}
2910
2911/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002912 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 *
2914 * Most often called through changed_bytes() and changed_lines(), which also
2915 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002916 *
2917 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 */
2919 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002920changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921{
2922#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02002923 if (p_imst == IM_ON_THE_SPOT)
2924 {
2925 /* The text of the preediting area is inserted, but this doesn't
2926 * mean a change of the buffer yet. That is delayed until the
2927 * text is committed. (this means preedit becomes empty) */
2928 if (im_is_preediting() && !xim_changed_while_preediting)
2929 return;
2930 xim_changed_while_preediting = FALSE;
2931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932#endif
2933
2934 if (!curbuf->b_changed)
2935 {
2936 int save_msg_scroll = msg_scroll;
2937
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002938 /* Give a warning about changing a read-only file. This may also
2939 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002941
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 /* Create a swap file if that is wanted.
2943 * Don't do this for "nofile" and "nowrite" buffer types. */
2944 if (curbuf->b_may_swap
2945#ifdef FEAT_QUICKFIX
2946 && !bt_dontwrite(curbuf)
2947#endif
2948 )
2949 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002950 int save_need_wait_return = need_wait_return;
2951
2952 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 ml_open_file(curbuf);
2954
2955 /* The ml_open_file() can cause an ATTENTION message.
2956 * Wait two seconds, to make sure the user reads this unexpected
2957 * message. Since we could be anywhere, call wait_return() now,
2958 * and don't let the emsg() set msg_scroll. */
2959 if (need_wait_return && emsg_silent == 0)
2960 {
2961 out_flush();
2962 ui_delay(2000L, TRUE);
2963 wait_return(TRUE);
2964 msg_scroll = save_msg_scroll;
2965 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002966 else
2967 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002969 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002971 ++CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972}
2973
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002974/*
2975 * Internal part of changed(), no user interaction.
2976 */
2977 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002978changed_int(void)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002979{
2980 curbuf->b_changed = TRUE;
2981 ml_setflags(curbuf);
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002982 check_status(curbuf);
2983 redraw_tabline = TRUE;
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002984#ifdef FEAT_TITLE
2985 need_maketitle = TRUE; /* set window title later */
2986#endif
2987}
2988
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002989static void changedOneline(buf_T *buf, linenr_T lnum);
2990static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
2991static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992
2993/*
2994 * Changed bytes within a single line for the current buffer.
2995 * - marks the windows on this buffer to be redisplayed
2996 * - marks the buffer changed by calling changed()
2997 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002998 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 */
3000 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003001changed_bytes(linenr_T lnum, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002{
Bram Moolenaardba8a912005-04-24 22:08:39 +00003003 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00003005
3006#ifdef FEAT_DIFF
3007 /* Diff highlighting in other diff windows may need to be updated too. */
3008 if (curwin->w_p_diff)
3009 {
3010 win_T *wp;
3011 linenr_T wlnum;
3012
Bram Moolenaar29323592016-07-24 22:04:11 +02003013 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00003014 if (wp->w_p_diff && wp != curwin)
3015 {
3016 redraw_win_later(wp, VALID);
3017 wlnum = diff_lnum_win(lnum, wp);
3018 if (wlnum > 0)
3019 changedOneline(wp->w_buffer, wlnum);
3020 }
3021 }
3022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023}
3024
Bram Moolenaar44746aa2019-01-02 00:02:11 +01003025/*
3026 * Like changed_bytes() but also adjust text properties for "added" bytes.
3027 * When "added" is negative text was deleted.
3028 */
3029 void
Bram Moolenaar402385a2019-01-11 14:10:03 +01003030inserted_bytes(linenr_T lnum, colnr_T col, int added UNUSED)
Bram Moolenaar44746aa2019-01-02 00:02:11 +01003031{
3032 changed_bytes(lnum, col);
3033
3034#ifdef FEAT_TEXT_PROP
3035 if (curbuf->b_has_textprop && added != 0)
3036 adjust_prop_columns(lnum, col, added);
3037#endif
3038}
3039
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003041changedOneline(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042{
Bram Moolenaardba8a912005-04-24 22:08:39 +00003043 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 {
3045 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003046 if (lnum < buf->b_mod_top)
3047 buf->b_mod_top = lnum;
3048 else if (lnum >= buf->b_mod_bot)
3049 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 }
3051 else
3052 {
3053 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003054 buf->b_mod_set = TRUE;
3055 buf->b_mod_top = lnum;
3056 buf->b_mod_bot = lnum + 1;
3057 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 }
3059}
3060
3061/*
3062 * Appended "count" lines below line "lnum" in the current buffer.
3063 * Must be called AFTER the change and after mark_adjust().
3064 * Takes care of marking the buffer to be redrawn and sets the changed flag.
3065 */
3066 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003067appended_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068{
3069 changed_lines(lnum + 1, 0, lnum + 1, count);
3070}
3071
3072/*
3073 * Like appended_lines(), but adjust marks first.
3074 */
3075 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003076appended_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077{
Bram Moolenaar82faa252016-06-04 20:14:07 +02003078 /* Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01003079 * be marks there. But it's still needed in diff mode. */
3080 if (lnum + count < curbuf->b_ml.ml_line_count
3081#ifdef FEAT_DIFF
3082 || curwin->w_p_diff
3083#endif
3084 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02003085 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 changed_lines(lnum + 1, 0, lnum + 1, count);
3087}
3088
3089/*
3090 * Deleted "count" lines at line "lnum" in the current buffer.
3091 * Must be called AFTER the change and after mark_adjust().
3092 * Takes care of marking the buffer to be redrawn and sets the changed flag.
3093 */
3094 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003095deleted_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096{
3097 changed_lines(lnum, 0, lnum + count, -count);
3098}
3099
3100/*
3101 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00003102 * Make sure the cursor is on a valid line before calling, a GUI callback may
3103 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 */
3105 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003106deleted_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107{
3108 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
3109 changed_lines(lnum, 0, lnum + count, -count);
3110}
3111
3112/*
3113 * Changed lines for the current buffer.
3114 * Must be called AFTER the change and after mark_adjust().
3115 * - mark the buffer changed by calling changed()
3116 * - mark the windows on this buffer to be redisplayed
3117 * - invalidate cached values
3118 * "lnum" is the first line that needs displaying, "lnume" the first line
3119 * below the changed lines (BEFORE the change).
3120 * When only inserting lines, "lnum" and "lnume" are equal.
3121 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003122 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 */
3124 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003125changed_lines(
3126 linenr_T lnum, /* first line with change */
3127 colnr_T col, /* column in first line with change */
3128 linenr_T lnume, /* line below last changed line */
3129 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130{
Bram Moolenaardba8a912005-04-24 22:08:39 +00003131 changed_lines_buf(curbuf, lnum, lnume, xtra);
3132
3133#ifdef FEAT_DIFF
Bram Moolenaare3521d92018-09-16 14:10:31 +02003134 if (xtra == 0 && curwin->w_p_diff && !diff_internal())
Bram Moolenaardba8a912005-04-24 22:08:39 +00003135 {
3136 /* When the number of lines doesn't change then mark_adjust() isn't
3137 * called and other diff buffers still need to be marked for
3138 * displaying. */
3139 win_T *wp;
3140 linenr_T wlnum;
3141
Bram Moolenaar29323592016-07-24 22:04:11 +02003142 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00003143 if (wp->w_p_diff && wp != curwin)
3144 {
3145 redraw_win_later(wp, VALID);
3146 wlnum = diff_lnum_win(lnum, wp);
3147 if (wlnum > 0)
3148 changed_lines_buf(wp->w_buffer, wlnum,
3149 lnume - lnum + wlnum, 0L);
3150 }
3151 }
3152#endif
3153
3154 changed_common(lnum, col, lnume, xtra);
3155}
3156
3157 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003158changed_lines_buf(
3159 buf_T *buf,
3160 linenr_T lnum, /* first line with change */
3161 linenr_T lnume, /* line below last changed line */
3162 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003163{
3164 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 {
3166 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003167 if (lnum < buf->b_mod_top)
3168 buf->b_mod_top = lnum;
3169 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 {
3171 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003172 buf->b_mod_bot += xtra;
3173 if (buf->b_mod_bot < lnum)
3174 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00003176 if (lnume + xtra > buf->b_mod_bot)
3177 buf->b_mod_bot = lnume + xtra;
3178 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 }
3180 else
3181 {
3182 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003183 buf->b_mod_set = TRUE;
3184 buf->b_mod_top = lnum;
3185 buf->b_mod_bot = lnume + xtra;
3186 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188}
3189
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003190/*
3191 * Common code for when a change is was made.
3192 * See changed_lines() for the arguments.
3193 * Careful: may trigger autocommands that reload the buffer.
3194 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003196changed_common(
3197 linenr_T lnum,
3198 colnr_T col,
3199 linenr_T lnume,
3200 long xtra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201{
3202 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003203 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 int i;
3205#ifdef FEAT_JUMPLIST
3206 int cols;
3207 pos_T *p;
3208 int add;
3209#endif
3210
3211 /* mark the buffer as modified */
3212 changed();
3213
Bram Moolenaare3521d92018-09-16 14:10:31 +02003214#ifdef FEAT_DIFF
3215 if (curwin->w_p_diff && diff_internal())
3216 curtab->tp_diff_update = TRUE;
3217#endif
3218
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 /* set the '. mark */
3220 if (!cmdmod.keepjumps)
3221 {
3222 curbuf->b_last_change.lnum = lnum;
3223 curbuf->b_last_change.col = col;
3224
3225#ifdef FEAT_JUMPLIST
3226 /* Create a new entry if a new undo-able change was started or we
3227 * don't have an entry yet. */
3228 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3229 {
3230 if (curbuf->b_changelistlen == 0)
3231 add = TRUE;
3232 else
3233 {
3234 /* Don't create a new entry when the line number is the same
3235 * as the last one and the column is not too far away. Avoids
3236 * creating many entries for typing "xxxxx". */
3237 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3238 if (p->lnum != lnum)
3239 add = TRUE;
3240 else
3241 {
3242 cols = comp_textwidth(FALSE);
3243 if (cols == 0)
3244 cols = 79;
3245 add = (p->col + cols < col || col + cols < p->col);
3246 }
3247 }
3248 if (add)
3249 {
3250 /* This is the first of a new sequence of undo-able changes
3251 * and it's at some distance of the last change. Use a new
3252 * position in the changelist. */
3253 curbuf->b_new_change = FALSE;
3254
3255 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3256 {
3257 /* changelist is full: remove oldest entry */
3258 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3259 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3260 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003261 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 {
3263 /* Correct position in changelist for other windows on
3264 * this buffer. */
3265 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3266 --wp->w_changelistidx;
3267 }
3268 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003269 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 {
3271 /* For other windows, if the position in the changelist is
3272 * at the end it stays at the end. */
3273 if (wp->w_buffer == curbuf
3274 && wp->w_changelistidx == curbuf->b_changelistlen)
3275 ++wp->w_changelistidx;
3276 }
3277 ++curbuf->b_changelistlen;
3278 }
3279 }
3280 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3281 curbuf->b_last_change;
3282 /* The current window is always after the last change, so that "g,"
3283 * takes you back to it. */
3284 curwin->w_changelistidx = curbuf->b_changelistlen;
3285#endif
3286 }
3287
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003288 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 {
3290 if (wp->w_buffer == curbuf)
3291 {
3292 /* Mark this window to be redrawn later. */
3293 if (wp->w_redr_type < VALID)
3294 wp->w_redr_type = VALID;
3295
3296 /* Check if a change in the buffer has invalidated the cached
3297 * values for the cursor. */
3298#ifdef FEAT_FOLDING
3299 /*
3300 * Update the folds for this window. Can't postpone this, because
3301 * a following operator might work on the whole fold: ">>dd".
3302 */
3303 foldUpdate(wp, lnum, lnume + xtra - 1);
3304
3305 /* The change may cause lines above or below the change to become
3306 * included in a fold. Set lnum/lnume to the first/last line that
3307 * might be displayed differently.
3308 * Set w_cline_folded here as an efficient way to update it when
3309 * inserting lines just above a closed fold. */
3310 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3311 if (wp->w_cursor.lnum == lnum)
3312 wp->w_cline_folded = i;
3313 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3314 if (wp->w_cursor.lnum == lnume)
3315 wp->w_cline_folded = i;
3316
3317 /* If the changed line is in a range of previously folded lines,
3318 * compare with the first line in that range. */
3319 if (wp->w_cursor.lnum <= lnum)
3320 {
3321 i = find_wl_entry(wp, lnum);
3322 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3323 changed_line_abv_curs_win(wp);
3324 }
3325#endif
3326
3327 if (wp->w_cursor.lnum > lnum)
3328 changed_line_abv_curs_win(wp);
3329 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3330 changed_cline_bef_curs_win(wp);
3331 if (wp->w_botline >= lnum)
3332 {
3333 /* Assume that botline doesn't change (inserted lines make
3334 * other lines scroll down below botline). */
3335 approximate_botline_win(wp);
3336 }
3337
3338 /* Check if any w_lines[] entries have become invalid.
3339 * For entries below the change: Correct the lnums for
3340 * inserted/deleted lines. Makes it possible to stop displaying
3341 * after the change. */
3342 for (i = 0; i < wp->w_lines_valid; ++i)
3343 if (wp->w_lines[i].wl_valid)
3344 {
3345 if (wp->w_lines[i].wl_lnum >= lnum)
3346 {
3347 if (wp->w_lines[i].wl_lnum < lnume)
3348 {
3349 /* line included in change */
3350 wp->w_lines[i].wl_valid = FALSE;
3351 }
3352 else if (xtra != 0)
3353 {
3354 /* line below change */
3355 wp->w_lines[i].wl_lnum += xtra;
3356#ifdef FEAT_FOLDING
3357 wp->w_lines[i].wl_lastlnum += xtra;
3358#endif
3359 }
3360 }
3361#ifdef FEAT_FOLDING
3362 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3363 {
3364 /* change somewhere inside this range of folded lines,
3365 * may need to be redrawn */
3366 wp->w_lines[i].wl_valid = FALSE;
3367 }
3368#endif
3369 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003370
3371#ifdef FEAT_FOLDING
3372 /* Take care of side effects for setting w_topline when folds have
3373 * changed. Esp. when the buffer was changed in another window. */
3374 if (hasAnyFolding(wp))
3375 set_topline(wp, wp->w_topline);
3376#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003377 /* relative numbering may require updating more */
3378 if (wp->w_p_rnu)
3379 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 }
3381 }
3382
3383 /* Call update_screen() later, which checks out what needs to be redrawn,
3384 * since it notices b_mod_set and then uses b_mod_*. */
3385 if (must_redraw < VALID)
3386 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003387
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003388 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003389 if (lnum <= curwin->w_cursor.lnum
3390 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003391 last_cursormoved.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392}
3393
3394/*
3395 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3396 */
3397 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003398unchanged(
3399 buf_T *buf,
3400 int ff) /* also reset 'fileformat' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003402 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 {
3404 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003405 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 if (ff)
3407 save_file_ff(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003409 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410#ifdef FEAT_TITLE
3411 need_maketitle = TRUE; /* set window title later */
3412#endif
3413 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003414 ++CHANGEDTICK(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415#ifdef FEAT_NETBEANS_INTG
3416 netbeans_unmodified(buf);
3417#endif
3418}
3419
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420/*
3421 * check_status: called when the status bars for the buffer 'buf'
3422 * need to be updated
3423 */
3424 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003425check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426{
3427 win_T *wp;
3428
Bram Moolenaar29323592016-07-24 22:04:11 +02003429 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 if (wp->w_buffer == buf && wp->w_status_height)
3431 {
3432 wp->w_redr_status = TRUE;
3433 if (must_redraw < VALID)
3434 must_redraw = VALID;
3435 }
3436}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437
3438/*
3439 * If the file is readonly, give a warning message with the first change.
3440 * Don't do this for autocommands.
3441 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003442 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003444 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 */
3446 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003447change_warning(
3448 int col) /* column for message; non-zero when in insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 mode and 'showmode' is on */
3450{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003451 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3452
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 if (curbuf->b_did_warn == FALSE
3454 && curbufIsChanged() == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 && !autocmd_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 && curbuf->b_p_ro)
3457 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003458 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003460 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 if (!curbuf->b_p_ro)
3462 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 /*
3464 * Do what msg() does, but with a column offset if the warning should
3465 * be after the mode message.
3466 */
3467 msg_start();
3468 if (msg_row == Rows - 1)
3469 msg_col = col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003470 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003471 msg_puts_attr(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST);
Bram Moolenaar496c5262009-03-18 14:42:00 +00003472#ifdef FEAT_EVAL
3473 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 msg_clr_eos();
3476 (void)msg_end();
Bram Moolenaare5f2a072017-02-01 22:31:49 +01003477 if (msg_silent == 0 && !silent_mode
3478#ifdef FEAT_EVAL
3479 && time_for_testing != 1
3480#endif
3481 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 {
3483 out_flush();
3484 ui_delay(1000L, TRUE); /* give the user time to think about it */
3485 }
3486 curbuf->b_did_warn = TRUE;
3487 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3488 if (msg_row < Rows - 1)
3489 showmode();
3490 }
3491}
3492
3493/*
3494 * Ask for a reply from the user, a 'y' or a 'n'.
3495 * No other characters are accepted, the message is repeated until a valid
3496 * reply is entered or CTRL-C is hit.
3497 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3498 * from any buffers but directly from the user.
3499 *
3500 * return the 'y' or 'n'
3501 */
3502 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003503ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504{
3505 int r = ' ';
3506 int save_State = State;
3507
3508 if (exiting) /* put terminal in raw mode for this question */
3509 settmode(TMODE_RAW);
3510 ++no_wait_return;
3511#ifdef USE_ON_FLY_SCROLL
3512 dont_scroll = TRUE; /* disallow scrolling here */
3513#endif
3514 State = CONFIRM; /* mouse behaves like with :confirm */
3515#ifdef FEAT_MOUSE
3516 setmouse(); /* disables mouse for xterm */
3517#endif
3518 ++no_mapping;
3519 ++allow_keys; /* no mapping here, but recognize keys */
3520
3521 while (r != 'y' && r != 'n')
3522 {
3523 /* same highlighting as for wait_return */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003524 smsg_attr(HL_ATTR(HLF_R), "%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 if (direct)
3526 r = get_keystroke();
3527 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003528 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 if (r == Ctrl_C || r == ESC)
3530 r = 'n';
3531 msg_putchar(r); /* show what you typed */
3532 out_flush();
3533 }
3534 --no_wait_return;
3535 State = save_State;
3536#ifdef FEAT_MOUSE
3537 setmouse();
3538#endif
3539 --no_mapping;
3540 --allow_keys;
3541
3542 return r;
3543}
3544
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003545#if defined(FEAT_MOUSE) || defined(PROTO)
3546/*
3547 * Return TRUE if "c" is a mouse key.
3548 */
3549 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003550is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003551{
3552 return c == K_LEFTMOUSE
3553 || c == K_LEFTMOUSE_NM
3554 || c == K_LEFTDRAG
3555 || c == K_LEFTRELEASE
3556 || c == K_LEFTRELEASE_NM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003557 || c == K_MOUSEMOVE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003558 || c == K_MIDDLEMOUSE
3559 || c == K_MIDDLEDRAG
3560 || c == K_MIDDLERELEASE
3561 || c == K_RIGHTMOUSE
3562 || c == K_RIGHTDRAG
3563 || c == K_RIGHTRELEASE
3564 || c == K_MOUSEDOWN
3565 || c == K_MOUSEUP
3566 || c == K_MOUSELEFT
3567 || c == K_MOUSERIGHT
3568 || c == K_X1MOUSE
3569 || c == K_X1DRAG
3570 || c == K_X1RELEASE
3571 || c == K_X2MOUSE
3572 || c == K_X2DRAG
3573 || c == K_X2RELEASE;
3574}
3575#endif
3576
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577/*
3578 * Get a key stroke directly from the user.
3579 * Ignores mouse clicks and scrollbar events, except a click for the left
3580 * button (used at the more prompt).
3581 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3582 * Disadvantage: typeahead is ignored.
3583 * Translates the interrupt character for unix to ESC.
3584 */
3585 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003586get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003588 char_u *buf = NULL;
3589 int buflen = 150;
3590 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 int len = 0;
3592 int n;
3593 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003594 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595
3596 mapped_ctrl_c = FALSE; /* mappings are not used here */
3597 for (;;)
3598 {
3599 cursor_on();
3600 out_flush();
3601
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003602 /* Leave some room for check_termcode() to insert a key code into (max
3603 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3604 * bytes. */
3605 maxlen = (buflen - 6 - len) / 3;
3606 if (buf == NULL)
3607 buf = alloc(buflen);
3608 else if (maxlen < 10)
3609 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003610 char_u *t_buf = buf;
3611
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003612 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003613 * escape sequence. */
3614 buflen += 100;
3615 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003616 if (buf == NULL)
3617 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003618 maxlen = (buflen - 6 - len) / 3;
3619 }
3620 if (buf == NULL)
3621 {
3622 do_outofmem_msg((long_u)buflen);
3623 return ESC; /* panic! */
3624 }
3625
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003627 * terminal code to complete. */
3628 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 if (n > 0)
3630 {
3631 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003632 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003634 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003636 else if (len > 0)
3637 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638
Bram Moolenaar4395a712006-09-05 18:57:57 +00003639 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003640 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003641 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003643
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003644 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003645 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003646 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003647 {
3648 /* Redrawing was postponed, do it now. */
3649 update_screen(0);
3650 setcursor(); /* put cursor back where it belongs */
3651 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003652 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003653 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003654 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003656 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 continue;
3658
3659 /* Handle modifier and/or special key code. */
3660 n = buf[0];
3661 if (n == K_SPECIAL)
3662 {
3663 n = TO_SPECIAL(buf[1], buf[2]);
3664 if (buf[1] == KS_MODIFIER
3665 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003666#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003667 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003668#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003669#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 || n == K_VER_SCROLLBAR
3671 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672#endif
3673 )
3674 {
3675 if (buf[1] == KS_MODIFIER)
3676 mod_mask = buf[2];
3677 len -= 3;
3678 if (len > 0)
3679 mch_memmove(buf, buf + 3, (size_t)len);
3680 continue;
3681 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003682 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 }
3684#ifdef FEAT_MBYTE
3685 if (has_mbyte)
3686 {
3687 if (MB_BYTE2LEN(n) > len)
3688 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003689 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 n = (*mb_ptr2char)(buf);
3691 }
3692#endif
3693#ifdef UNIX
3694 if (n == intr_char)
3695 n = ESC;
3696#endif
3697 break;
3698 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003699 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700
3701 mapped_ctrl_c = save_mapped_ctrl_c;
3702 return n;
3703}
3704
3705/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003706 * Get a number from the user.
3707 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 */
3709 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003710get_number(
3711 int colon, /* allow colon to abort */
3712 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713{
3714 int n = 0;
3715 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003716 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003718 if (mouse_used != NULL)
3719 *mouse_used = FALSE;
3720
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 /* When not printing messages, the user won't know what to type, return a
3722 * zero (as if CR was hit). */
3723 if (msg_silent != 0)
3724 return 0;
3725
3726#ifdef USE_ON_FLY_SCROLL
3727 dont_scroll = TRUE; /* disallow scrolling here */
3728#endif
3729 ++no_mapping;
3730 ++allow_keys; /* no mapping here, but recognize keys */
3731 for (;;)
3732 {
3733 windgoto(msg_row, msg_col);
3734 c = safe_vgetc();
3735 if (VIM_ISDIGIT(c))
3736 {
3737 n = n * 10 + c - '0';
3738 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003739 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 }
3741 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3742 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003743 if (typed > 0)
3744 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003745 msg_puts("\b \b");
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003746 --typed;
3747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003750#ifdef FEAT_MOUSE
3751 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3752 {
3753 *mouse_used = TRUE;
3754 n = mouse_row + 1;
3755 break;
3756 }
3757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 else if (n == 0 && c == ':' && colon)
3759 {
3760 stuffcharReadbuff(':');
3761 if (!exmode_active)
3762 cmdline_row = msg_row;
3763 skip_redraw = TRUE; /* skip redraw once */
3764 do_redraw = FALSE;
3765 break;
3766 }
3767 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3768 break;
3769 }
3770 --no_mapping;
3771 --allow_keys;
3772 return n;
3773}
3774
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003775/*
3776 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003777 * When "mouse_used" is not NULL allow using the mouse and in that case return
3778 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003779 */
3780 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003781prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003782{
3783 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003784 int save_cmdline_row;
3785 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003786
3787 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003788 if (mouse_used != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003789 msg_puts(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003790 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003791 msg_puts(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003792
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003793 // Set the state such that text can be selected/copied/pasted and we still
3794 // get mouse events. redraw_after_callback() will not redraw if cmdline_row
3795 // is zero.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003796 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003797 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003798 save_State = State;
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003799 State = CMDLINE;
Bram Moolenaar73658312018-04-24 17:41:57 +02003800#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003801 // May show different mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02003802 setmouse();
3803#endif
3804
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003805 i = get_number(TRUE, mouse_used);
3806 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003807 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003808 /* don't call wait_return() now */
3809 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003810 cmdline_row = msg_row - 1;
3811 need_wait_return = FALSE;
3812 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003813 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003814 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003815 else
3816 cmdline_row = save_cmdline_row;
3817 State = save_State;
Bram Moolenaar73658312018-04-24 17:41:57 +02003818#ifdef FEAT_MOUSE
Bram Moolenaar4cbdf152018-08-26 21:23:07 +02003819 // May need to restore mouse shape.
Bram Moolenaar73658312018-04-24 17:41:57 +02003820 setmouse();
3821#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003822
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003823 return i;
3824}
3825
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003827msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828{
3829 long pn;
3830
3831 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3833 return;
3834
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003835 /* We don't want to overwrite another important message, but do overwrite
3836 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3837 * then "put" reports the last action. */
3838 if (keep_msg != NULL && !keep_msg_more)
3839 return;
3840
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 if (n > 0)
3842 pn = n;
3843 else
3844 pn = -n;
3845
3846 if (pn > p_report)
3847 {
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003848 if (n > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003849 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003850 NGETTEXT("%ld more line", "%ld more lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003852 vim_snprintf(msg_buf, MSG_BUF_LEN,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003853 NGETTEXT("%ld line less", "%ld fewer lines", pn), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 if (got_int)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003855 vim_strcat((char_u *)msg_buf, (char_u *)_(" (Interrupted)"),
3856 MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 if (msg(msg_buf))
3858 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003859 set_keep_msg((char_u *)msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003860 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 }
3862 }
3863}
3864
3865/*
3866 * flush map and typeahead buffers and give a warning for an error
3867 */
3868 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003869beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870{
3871 if (emsg_silent == 0)
3872 {
Bram Moolenaar6a2633b2018-10-07 23:16:36 +02003873 flush_buffers(FLUSH_MINIMAL);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003874 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 }
3876}
3877
3878/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003879 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 */
3881 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003882vim_beep(
3883 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003885#ifdef FEAT_EVAL
3886 called_vim_beep = TRUE;
3887#endif
3888
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 if (emsg_silent == 0)
3890 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003891 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3892 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003893#ifdef ELAPSED_FUNC
3894 static int did_init = FALSE;
Bram Moolenaar1ac56c22019-01-17 22:28:22 +01003895 static elapsed_T start_tv;
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003896
3897 /* Only beep once per half a second, otherwise a sequence of beeps
3898 * would freeze Vim. */
3899 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
3900 {
3901 did_init = TRUE;
3902 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003904 if (p_vb
3905#ifdef FEAT_GUI
3906 /* While the GUI is starting up the termcap is set for
3907 * the GUI but the output still goes to a terminal. */
3908 && !(gui.in_use && gui.starting)
3909#endif
3910 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003911 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003912 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003913#ifdef FEAT_VTP
3914 /* No restore color information, refresh the screen. */
3915 if (has_vtp_working() != 0
3916# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003917 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003918# endif
3919 )
3920 {
3921 redraw_later(CLEAR);
3922 update_screen(0);
3923 redrawcmd();
3924 }
3925#endif
3926 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003927 else
3928 out_char(BELL);
3929#ifdef ELAPSED_FUNC
3930 }
3931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003933
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003934 /* When 'debug' contains "beep" produce a message. If we are sourcing
3935 * a script or executing a function give the user a hint where the beep
3936 * comes from. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003937 if (vim_strchr(p_debug, 'e') != NULL)
3938 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003939 msg_source(HL_ATTR(HLF_W));
Bram Moolenaar32526b32019-01-19 17:43:09 +01003940 msg_attr(_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 }
3943}
3944
3945/*
3946 * To get the "real" home directory:
3947 * - get value of $HOME
3948 * For Unix:
3949 * - go to that directory
3950 * - do mch_dirname() to get the real name of that directory.
3951 * This also works with mounts and links.
3952 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
Bram Moolenaar25a494c2018-11-16 19:39:50 +01003953 * For Windows:
3954 * This code is duplicated in init_homedir() in dosinst.c. Keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 */
3956static char_u *homedir = NULL;
3957
3958 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003959init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960{
3961 char_u *var;
3962
Bram Moolenaar05159a02005-02-26 23:04:13 +00003963 /* In case we are called a second time (when 'encoding' changes). */
Bram Moolenaard23a8232018-02-10 18:45:26 +01003964 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003965
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966#ifdef VMS
3967 var = mch_getenv((char_u *)"SYS$LOGIN");
3968#else
3969 var = mch_getenv((char_u *)"HOME");
3970#endif
3971
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972#ifdef WIN3264
3973 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02003974 * Typically, $HOME is not defined on Windows, unless the user has
3975 * specifically defined it for Vim's sake. However, on Windows NT
3976 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3977 * each user. Try constructing $HOME from these.
3978 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02003979 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02003980 {
3981 char_u *homedrive, *homepath;
3982
3983 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3984 homepath = mch_getenv((char_u *)"HOMEPATH");
3985 if (homepath == NULL || *homepath == NUL)
3986 homepath = (char_u *)"\\";
3987 if (homedrive != NULL
3988 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3989 {
3990 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3991 if (NameBuff[0] != NUL)
3992 var = NameBuff;
3993 }
3994 }
3995
3996 if (var == NULL)
3997 var = mch_getenv((char_u *)"USERPROFILE");
3998
3999 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 * Weird but true: $HOME may contain an indirect reference to another
4001 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
4002 * when $HOME is being set.
4003 */
4004 if (var != NULL && *var == '%')
4005 {
4006 char_u *p;
4007 char_u *exp;
4008
4009 p = vim_strchr(var + 1, '%');
4010 if (p != NULL)
4011 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004012 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 exp = mch_getenv(NameBuff);
4014 if (exp != NULL && *exp != NUL
4015 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
4016 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00004017 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 }
4020 }
4021 }
4022
Bram Moolenaar48340b62017-08-29 22:08:53 +02004023 if (var != NULL && *var == NUL) /* empty is same as not set */
4024 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025
Bram Moolenaar48340b62017-08-29 22:08:53 +02004026# ifdef FEAT_MBYTE
Bram Moolenaar05159a02005-02-26 23:04:13 +00004027 if (enc_utf8 && var != NULL)
4028 {
4029 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004030 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004031
4032 /* Convert from active codepage to UTF-8. Other conversions are
4033 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004034 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004035 if (pp != NULL)
4036 {
4037 homedir = pp;
4038 return;
4039 }
4040 }
4041# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 /*
4044 * Default home dir is C:/
4045 * Best assumption we can make in such a situation.
4046 */
4047 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004048 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004050
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 if (var != NULL)
4052 {
4053#ifdef UNIX
4054 /*
4055 * Change to the directory and get the actual path. This resolves
4056 * links. Don't do it when we can't return.
4057 */
4058 if (mch_dirname(NameBuff, MAXPATHL) == OK
4059 && mch_chdir((char *)NameBuff) == 0)
4060 {
4061 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
4062 var = IObuff;
4063 if (mch_chdir((char *)NameBuff) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004064 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 }
4066#endif
4067 homedir = vim_strsave(var);
4068 }
4069}
4070
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004071#if defined(EXITFREE) || defined(PROTO)
4072 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004073free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004074{
4075 vim_free(homedir);
4076}
Bram Moolenaar24305862012-08-15 14:05:05 +02004077
4078# ifdef FEAT_CMDL_COMPL
4079 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004080free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02004081{
4082 ga_clear_strings(&ga_users);
4083}
4084# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00004085#endif
4086
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004088 * Call expand_env() and store the result in an allocated string.
4089 * This is not very memory efficient, this expects the result to be freed
4090 * again soon.
4091 */
4092 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004093expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004094{
4095 return expand_env_save_opt(src, FALSE);
4096}
4097
4098/*
4099 * Idem, but when "one" is TRUE handle the string as one file name, only
4100 * expand "~" at the start.
4101 */
4102 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004103expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004104{
4105 char_u *p;
4106
4107 p = alloc(MAXPATHL);
4108 if (p != NULL)
4109 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
4110 return p;
4111}
4112
4113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 * Expand environment variable with path name.
4115 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004116 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 * If anything fails no expansion is done and dst equals src.
4118 */
4119 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004120expand_env(
4121 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
4122 char_u *dst, /* where to put the result */
4123 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004125 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126}
4127
4128 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004129expand_env_esc(
4130 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
4131 char_u *dst, /* where to put the result */
4132 int dstlen, /* maximum length of the result */
4133 int esc, /* escape spaces in expanded variables */
4134 int one, /* "srcp" is one file name */
4135 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004137 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 char_u *tail;
4139 int c;
4140 char_u *var;
4141 int copy_char;
4142 int mustfree; /* var was allocated, need to free it later */
4143 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004144 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004146 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004147 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004148
4149 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 --dstlen; /* leave one char space for "\," */
4151 while (*src && dstlen > 0)
4152 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004153#ifdef FEAT_EVAL
4154 /* Skip over `=expr`. */
4155 if (src[0] == '`' && src[1] == '=')
4156 {
4157 size_t len;
4158
4159 var = src;
4160 src += 2;
4161 (void)skip_expr(&src);
4162 if (*src == '`')
4163 ++src;
4164 len = src - var;
4165 if (len > (size_t)dstlen)
4166 len = dstlen;
4167 vim_strncpy(dst, var, len);
4168 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02004169 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02004170 continue;
4171 }
4172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004174 if ((*src == '$'
4175#ifdef VMS
4176 && at_start
4177#endif
4178 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004179#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 || *src == '%'
4181#endif
4182 || (*src == '~' && at_start))
4183 {
4184 mustfree = FALSE;
4185
4186 /*
4187 * The variable name is copied into dst temporarily, because it may
4188 * be a string in read-only memory and a NUL needs to be appended.
4189 */
4190 if (*src != '~') /* environment var */
4191 {
4192 tail = src + 1;
4193 var = dst;
4194 c = dstlen - 1;
4195
4196#ifdef UNIX
4197 /* Unix has ${var-name} type environment vars */
4198 if (*tail == '{' && !vim_isIDc('{'))
4199 {
4200 tail++; /* ignore '{' */
4201 while (c-- > 0 && *tail && *tail != '}')
4202 *var++ = *tail++;
4203 }
4204 else
4205#endif
4206 {
4207 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004208#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 || (*src == '%' && *tail != '%')
4210#endif
4211 ))
4212 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 }
4215 }
4216
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004217#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218# ifdef UNIX
4219 if (src[1] == '{' && *tail != '}')
4220# else
4221 if (*src == '%' && *tail != '%')
4222# endif
4223 var = NULL;
4224 else
4225 {
4226# ifdef UNIX
4227 if (src[1] == '{')
4228# else
4229 if (*src == '%')
4230#endif
4231 ++tail;
4232#endif
4233 *var = NUL;
4234 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004235#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 }
4237#endif
4238 }
4239 /* home directory */
4240 else if ( src[1] == NUL
4241 || vim_ispathsep(src[1])
4242 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4243 {
4244 var = homedir;
4245 tail = src + 1;
4246 }
4247 else /* user directory */
4248 {
4249#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4250 /*
4251 * Copy ~user to dst[], so we can put a NUL after it.
4252 */
4253 tail = src;
4254 var = dst;
4255 c = dstlen - 1;
4256 while ( c-- > 0
4257 && *tail
4258 && vim_isfilec(*tail)
4259 && !vim_ispathsep(*tail))
4260 *var++ = *tail++;
4261 *var = NUL;
4262# ifdef UNIX
4263 /*
4264 * If the system supports getpwnam(), use it.
4265 * Otherwise, or if getpwnam() fails, the shell is used to
4266 * expand ~user. This is slower and may fail if the shell
4267 * does not support ~user (old versions of /bin/sh).
4268 */
4269# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4270 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004271 /* Note: memory allocated by getpwnam() is never freed.
4272 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01004273 struct passwd *pw = (*dst == NUL)
4274 ? NULL : getpwnam((char *)dst + 1);
4275
4276 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 }
4278 if (var == NULL)
4279# endif
4280 {
4281 expand_T xpc;
4282
4283 ExpandInit(&xpc);
4284 xpc.xp_context = EXPAND_FILES;
4285 var = ExpandOne(&xpc, dst, NULL,
4286 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 mustfree = TRUE;
4288 }
4289
4290# else /* !UNIX, thus VMS */
4291 /*
4292 * USER_HOME is a comma-separated list of
4293 * directories to search for the user account in.
4294 */
4295 {
4296 char_u test[MAXPATHL], paths[MAXPATHL];
4297 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004298 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299
4300 STRCPY(paths, USER_HOME);
4301 next_path = paths;
4302 while (*next_path)
4303 {
4304 for (path = next_path; *next_path && *next_path != ',';
4305 next_path++);
4306 if (*next_path)
4307 *next_path++ = NUL;
4308 STRCPY(test, path);
4309 STRCAT(test, "/");
4310 STRCAT(test, dst + 1);
4311 if (mch_stat(test, &st) == 0)
4312 {
4313 var = alloc(STRLEN(test) + 1);
4314 STRCPY(var, test);
4315 mustfree = TRUE;
4316 break;
4317 }
4318 }
4319 }
4320# endif /* UNIX */
4321#else
4322 /* cannot expand user's home directory, so don't try */
4323 var = NULL;
4324 tail = (char_u *)""; /* for gcc */
4325#endif /* UNIX || VMS */
4326 }
4327
4328#ifdef BACKSLASH_IN_FILENAME
4329 /* If 'shellslash' is set change backslashes to forward slashes.
4330 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4331 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4332 {
4333 char_u *p = vim_strsave(var);
4334
4335 if (p != NULL)
4336 {
4337 if (mustfree)
4338 vim_free(var);
4339 var = p;
4340 mustfree = TRUE;
4341 forward_slash(var);
4342 }
4343 }
4344#endif
4345
4346 /* If "var" contains white space, escape it with a backslash.
4347 * Required for ":e ~/tt" when $HOME includes a space. */
4348 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4349 {
4350 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4351
4352 if (p != NULL)
4353 {
4354 if (mustfree)
4355 vim_free(var);
4356 var = p;
4357 mustfree = TRUE;
4358 }
4359 }
4360
4361 if (var != NULL && *var != NUL
4362 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4363 {
4364 STRCPY(dst, var);
4365 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004366 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 /* if var[] ends in a path separator and tail[] starts
4368 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004369 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4371 && dst[-1] != ':'
4372#endif
4373 && vim_ispathsep(*tail))
4374 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004375 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 src = tail;
4377 copy_char = FALSE;
4378 }
4379 if (mustfree)
4380 vim_free(var);
4381 }
4382
4383 if (copy_char) /* copy at least one char */
4384 {
4385 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004386 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004387 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4388 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 */
4390 at_start = FALSE;
4391 if (src[0] == '\\' && src[1] != NUL)
4392 {
4393 *dst++ = *src++;
4394 --dstlen;
4395 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004396 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02004398 if (dstlen > 0)
4399 {
4400 *dst++ = *src++;
4401 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004402
Bram Moolenaar1c864092017-08-06 18:15:45 +02004403 if (startstr != NULL && src - startstr_len >= srcp
4404 && STRNCMP(src - startstr_len, startstr,
4405 startstr_len) == 0)
4406 at_start = TRUE;
4407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02004409
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 }
4411 *dst = NUL;
4412}
4413
4414/*
4415 * Vim's version of getenv().
4416 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004417 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004418 * "mustfree" is set to TRUE when returned is allocated, it must be
4419 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 */
4421 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004422vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423{
4424 char_u *p;
4425 char_u *pend;
4426 int vimruntime;
4427
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004428#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 /* use "C:/" when $HOME is not set */
4430 if (STRCMP(name, "HOME") == 0)
4431 return homedir;
4432#endif
4433
4434 p = mch_getenv(name);
4435 if (p != NULL && *p == NUL) /* empty is the same as not set */
4436 p = NULL;
4437
4438 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004439 {
4440#if defined(FEAT_MBYTE) && defined(WIN3264)
4441 if (enc_utf8)
4442 {
4443 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004444 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004445
4446 /* Convert from active codepage to UTF-8. Other conversions are
4447 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004448 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004449 if (pp != NULL)
4450 {
4451 p = pp;
4452 *mustfree = TRUE;
4453 }
4454 }
4455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458
4459 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4460 if (!vimruntime && STRCMP(name, "VIM") != 0)
4461 return NULL;
4462
4463 /*
4464 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4465 * Don't do this when default_vimruntime_dir is non-empty.
4466 */
4467 if (vimruntime
4468#ifdef HAVE_PATHDEF
4469 && *default_vimruntime_dir == NUL
4470#endif
4471 )
4472 {
4473 p = mch_getenv((char_u *)"VIM");
4474 if (p != NULL && *p == NUL) /* empty is the same as not set */
4475 p = NULL;
4476 if (p != NULL)
4477 {
4478 p = vim_version_dir(p);
4479 if (p != NULL)
4480 *mustfree = TRUE;
4481 else
4482 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004483
4484#if defined(FEAT_MBYTE) && defined(WIN3264)
4485 if (enc_utf8)
4486 {
4487 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004488 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004489
4490 /* Convert from active codepage to UTF-8. Other conversions
4491 * are not done, because they would fail for non-ASCII
4492 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004493 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004494 if (pp != NULL)
4495 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004496 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004497 vim_free(p);
4498 p = pp;
4499 *mustfree = TRUE;
4500 }
4501 }
4502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 }
4504 }
4505
4506 /*
4507 * When expanding $VIM or $VIMRUNTIME fails, try using:
4508 * - the directory name from 'helpfile' (unless it contains '$')
4509 * - the executable name from argv[0]
4510 */
4511 if (p == NULL)
4512 {
4513 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4514 p = p_hf;
4515#ifdef USE_EXE_NAME
4516 /*
4517 * Use the name of the executable, obtained from argv[0].
4518 */
4519 else
4520 p = exe_name;
4521#endif
4522 if (p != NULL)
4523 {
4524 /* remove the file name */
4525 pend = gettail(p);
4526
4527 /* remove "doc/" from 'helpfile', if present */
4528 if (p == p_hf)
4529 pend = remove_tail(p, pend, (char_u *)"doc");
4530
4531#ifdef USE_EXE_NAME
4532# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004533 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 if (p == exe_name)
4535 {
4536 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004537 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004539 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4540 if (pend1 != pend)
4541 {
4542 pnew = alloc((unsigned)(pend1 - p) + 15);
4543 if (pnew != NULL)
4544 {
4545 STRNCPY(pnew, p, (pend1 - p));
4546 STRCPY(pnew + (pend1 - p), "Resources/vim");
4547 p = pnew;
4548 pend = p + STRLEN(p);
4549 }
4550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 }
4552# endif
4553 /* remove "src/" from exe_name, if present */
4554 if (p == exe_name)
4555 pend = remove_tail(p, pend, (char_u *)"src");
4556#endif
4557
4558 /* for $VIM, remove "runtime/" or "vim54/", if present */
4559 if (!vimruntime)
4560 {
4561 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4562 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4563 }
4564
4565 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004566 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004569#ifdef MACOS_X
4570 if (p == exe_name || p == p_hf)
4571#endif
4572 /* check that the result is a directory name */
4573 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574
4575 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01004576 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 else
4578 {
4579#ifdef USE_EXE_NAME
4580 /* may add "/vim54" or "/runtime" if it exists */
4581 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4582 {
4583 vim_free(p);
4584 p = pend;
4585 }
4586#endif
4587 *mustfree = TRUE;
4588 }
4589 }
4590 }
4591
4592#ifdef HAVE_PATHDEF
4593 /* When there is a pathdef.c file we can use default_vim_dir and
4594 * default_vimruntime_dir */
4595 if (p == NULL)
4596 {
4597 /* Only use default_vimruntime_dir when it is not empty */
4598 if (vimruntime && *default_vimruntime_dir != NUL)
4599 {
4600 p = default_vimruntime_dir;
4601 *mustfree = FALSE;
4602 }
4603 else if (*default_vim_dir != NUL)
4604 {
4605 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4606 *mustfree = TRUE;
4607 else
4608 {
4609 p = default_vim_dir;
4610 *mustfree = FALSE;
4611 }
4612 }
4613 }
4614#endif
4615
4616 /*
4617 * Set the environment variable, so that the new value can be found fast
4618 * next time, and others can also use it (e.g. Perl).
4619 */
4620 if (p != NULL)
4621 {
4622 if (vimruntime)
4623 {
4624 vim_setenv((char_u *)"VIMRUNTIME", p);
4625 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 }
4627 else
4628 {
4629 vim_setenv((char_u *)"VIM", p);
4630 didset_vim = TRUE;
4631 }
4632 }
4633 return p;
4634}
4635
4636/*
4637 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4638 * Return NULL if not, return its name in allocated memory otherwise.
4639 */
4640 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004641vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642{
4643 char_u *p;
4644
4645 if (vimdir == NULL || *vimdir == NUL)
4646 return NULL;
4647 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4648 if (p != NULL && mch_isdir(p))
4649 return p;
4650 vim_free(p);
4651 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4652 if (p != NULL && mch_isdir(p))
4653 return p;
4654 vim_free(p);
4655 return NULL;
4656}
4657
4658/*
4659 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4660 * the length of "name/". Otherwise return "pend".
4661 */
4662 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004663remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664{
4665 int len = (int)STRLEN(name) + 1;
4666 char_u *newend = pend - len;
4667
4668 if (newend >= p
4669 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004670 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 return newend;
4672 return pend;
4673}
4674
Bram Moolenaar137374f2018-05-13 15:59:50 +02004675 void
4676vim_unsetenv(char_u *var)
4677{
4678#ifdef HAVE_UNSETENV
4679 unsetenv((char *)var);
4680#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02004681 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02004682#endif
4683}
4684
4685
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 * Our portable version of setenv.
4688 */
4689 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004690vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691{
4692#ifdef HAVE_SETENV
4693 mch_setenv((char *)name, (char *)val, 1);
4694#else
4695 char_u *envbuf;
4696
4697 /*
4698 * Putenv does not copy the string, it has to remain
4699 * valid. The allocated memory will never be freed.
4700 */
4701 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4702 if (envbuf != NULL)
4703 {
4704 sprintf((char *)envbuf, "%s=%s", name, val);
4705 putenv((char *)envbuf);
4706 }
4707#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004708#ifdef FEAT_GETTEXT
4709 /*
4710 * When setting $VIMRUNTIME adjust the directory to find message
4711 * translations to $VIMRUNTIME/lang.
4712 */
4713 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4714 {
4715 char_u *buf = concat_str(val, (char_u *)"/lang");
4716
4717 if (buf != NULL)
4718 {
4719 bindtextdomain(VIMPACKAGE, (char *)buf);
4720 vim_free(buf);
4721 }
4722 }
4723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724}
4725
4726#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4727/*
4728 * Function given to ExpandGeneric() to obtain an environment variable name.
4729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004731get_env_name(
4732 expand_T *xp UNUSED,
4733 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734{
Bram Moolenaard0573012017-10-28 21:11:06 +02004735# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02004737 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 */
4739 return NULL;
4740# else
4741# ifndef __WIN32__
4742 /* Borland C++ 5.2 has this in a header file. */
4743 extern char **environ;
4744# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004745# define ENVNAMELEN 100
4746 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 char_u *str;
4748 int n;
4749
4750 str = (char_u *)environ[idx];
4751 if (str == NULL)
4752 return NULL;
4753
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004754 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 {
4756 if (str[n] == '=' || str[n] == NUL)
4757 break;
4758 name[n] = str[n];
4759 }
4760 name[n] = NUL;
4761 return name;
4762# endif
4763}
Bram Moolenaar24305862012-08-15 14:05:05 +02004764
4765/*
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004766 * Add a user name to the list of users in ga_users.
4767 * Do nothing if user name is NULL or empty.
4768 */
4769 static void
4770add_user(char_u *user, int need_copy)
4771{
4772 char_u *user_copy = (user != NULL && need_copy)
4773 ? vim_strsave(user) : user;
4774
4775 if (user_copy == NULL || *user_copy == NUL || ga_grow(&ga_users, 1) == FAIL)
4776 {
4777 if (need_copy)
4778 vim_free(user);
4779 return;
4780 }
4781 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user_copy;
4782}
4783
4784/*
Bram Moolenaar24305862012-08-15 14:05:05 +02004785 * Find all user names for user completion.
4786 * Done only once and then cached.
4787 */
4788 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004789init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004790{
Bram Moolenaar24305862012-08-15 14:05:05 +02004791 static int lazy_init_done = FALSE;
4792
4793 if (lazy_init_done)
4794 return;
4795
4796 lazy_init_done = TRUE;
4797 ga_init2(&ga_users, sizeof(char_u *), 20);
4798
4799# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4800 {
Bram Moolenaar24305862012-08-15 14:05:05 +02004801 struct passwd* pw;
4802
4803 setpwent();
4804 while ((pw = getpwent()) != NULL)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004805 add_user((char_u *)pw->pw_name, TRUE);
Bram Moolenaar24305862012-08-15 14:05:05 +02004806 endpwent();
4807 }
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004808# elif defined(WIN3264)
4809 {
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004810 DWORD nusers = 0, ntotal = 0, i;
4811 PUSER_INFO_0 uinfo;
4812
4813 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
4814 &nusers, &ntotal, NULL) == NERR_Success)
4815 {
4816 for (i = 0; i < nusers; i++)
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004817 add_user(utf16_to_enc(uinfo[i].usri0_name, NULL), FALSE);
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004818
4819 NetApiBufferFree(uinfo);
4820 }
4821 }
Bram Moolenaar24305862012-08-15 14:05:05 +02004822# endif
Bram Moolenaar6b0b83f2018-09-10 19:03:05 +02004823# if defined(HAVE_GETPWNAM)
4824 {
4825 char_u *user_env = mch_getenv((char_u *)"USER");
4826
4827 // The $USER environment variable may be a valid remote user name (NIS,
4828 // LDAP) not already listed by getpwent(), as getpwent() only lists
4829 // local user names. If $USER is not already listed, check whether it
4830 // is a valid remote user name using getpwnam() and if it is, add it to
4831 // the list of user names.
4832
4833 if (user_env != NULL && *user_env != NUL)
4834 {
4835 int i;
4836
4837 for (i = 0; i < ga_users.ga_len; i++)
4838 {
4839 char_u *local_user = ((char_u **)ga_users.ga_data)[i];
4840
4841 if (STRCMP(local_user, user_env) == 0)
4842 break;
4843 }
4844
4845 if (i == ga_users.ga_len)
4846 {
4847 struct passwd *pw = getpwnam((char *)user_env);
4848
4849 if (pw != NULL)
4850 add_user((char_u *)pw->pw_name, TRUE);
4851 }
4852 }
4853 }
4854# endif
Bram Moolenaar24305862012-08-15 14:05:05 +02004855}
4856
4857/*
4858 * Function given to ExpandGeneric() to obtain an user names.
4859 */
4860 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004861get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004862{
4863 init_users();
4864 if (idx < ga_users.ga_len)
4865 return ((char_u **)ga_users.ga_data)[idx];
4866 return NULL;
4867}
4868
4869/*
4870 * Check whether name matches a user name. Return:
4871 * 0 if name does not match any user name.
4872 * 1 if name partially matches the beginning of a user name.
4873 * 2 is name fully matches a user name.
4874 */
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02004875 int
4876match_user(char_u *name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004877{
4878 int i;
4879 int n = (int)STRLEN(name);
4880 int result = 0;
4881
4882 init_users();
4883 for (i = 0; i < ga_users.ga_len; i++)
4884 {
4885 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4886 return 2; /* full match */
4887 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4888 result = 1; /* partial match */
4889 }
4890 return result;
4891}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892#endif
4893
4894/*
4895 * Replace home directory by "~" in each space or comma separated file name in
4896 * 'src'.
4897 * If anything fails (except when out of space) dst equals src.
4898 */
4899 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004900home_replace(
4901 buf_T *buf, /* when not NULL, check for help files */
4902 char_u *src, /* input file name */
4903 char_u *dst, /* where to put the result */
4904 int dstlen, /* maximum length of the result */
4905 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 spaces and commas in the file name. */
4907{
4908 size_t dirlen = 0, envlen = 0;
4909 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004910 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 char_u *p;
4912
4913 if (src == NULL)
4914 {
4915 *dst = NUL;
4916 return;
4917 }
4918
4919 /*
4920 * If the file is a help file, remove the path completely.
4921 */
4922 if (buf != NULL && buf->b_help)
4923 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02004924 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 return;
4926 }
4927
4928 /*
4929 * We check both the value of the $HOME environment variable and the
4930 * "real" home directory.
4931 */
4932 if (homedir != NULL)
4933 dirlen = STRLEN(homedir);
4934
4935#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004936 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004938 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4939#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004940#ifdef WIN3264
4941 if (homedir_env == NULL)
4942 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
4943#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004944 /* Empty is the same as not set. */
4945 if (homedir_env != NULL && *homedir_env == NUL)
4946 homedir_env = NULL;
4947
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004948#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaaref0a1d52018-12-30 11:38:57 +01004949 if (homedir_env != NULL && *homedir_env == '~')
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004950 {
4951 int usedlen = 0;
4952 int flen;
4953 char_u *fbuf = NULL;
4954
4955 flen = (int)STRLEN(homedir_env);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004956 (void)modify_fname((char_u *)":p", FALSE, &usedlen,
Bram Moolenaard12f8112012-06-20 17:56:09 +02004957 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004958 flen = (int)STRLEN(homedir_env);
4959 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4960 /* Remove the trailing / that is added to a directory. */
4961 homedir_env[flen - 1] = NUL;
4962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963#endif
4964
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 if (homedir_env != NULL)
4966 envlen = STRLEN(homedir_env);
4967
4968 if (!one)
4969 src = skipwhite(src);
4970 while (*src && dstlen > 0)
4971 {
4972 /*
4973 * Here we are at the beginning of a file name.
4974 * First, check to see if the beginning of the file name matches
4975 * $HOME or the "real" home directory. Check that there is a '/'
4976 * after the match (so that if e.g. the file is "/home/pieter/bla",
4977 * and the home directory is "/home/piet", the file does not end up
4978 * as "~er/bla" (which would seem to indicate the file "bla" in user
4979 * er's home directory)).
4980 */
4981 p = homedir;
4982 len = dirlen;
4983 for (;;)
4984 {
4985 if ( len
4986 && fnamencmp(src, p, len) == 0
4987 && (vim_ispathsep(src[len])
4988 || (!one && (src[len] == ',' || src[len] == ' '))
4989 || src[len] == NUL))
4990 {
4991 src += len;
4992 if (--dstlen > 0)
4993 *dst++ = '~';
4994
4995 /*
4996 * If it's just the home directory, add "/".
4997 */
4998 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4999 *dst++ = '/';
5000 break;
5001 }
5002 if (p == homedir_env)
5003 break;
5004 p = homedir_env;
5005 len = envlen;
5006 }
5007
5008 /* if (!one) skip to separator: space or comma */
5009 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
5010 *dst++ = *src++;
5011 /* skip separator */
5012 while ((*src == ' ' || *src == ',') && --dstlen > 0)
5013 *dst++ = *src++;
5014 }
5015 /* if (dstlen == 0) out of space, what to do??? */
5016
5017 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02005018
5019 if (homedir_env != homedir_env_orig)
5020 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021}
5022
5023/*
5024 * Like home_replace, store the replaced string in allocated memory.
5025 * When something fails, NULL is returned.
5026 */
5027 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005028home_replace_save(
5029 buf_T *buf, /* when not NULL, check for help files */
5030 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031{
5032 char_u *dst;
5033 unsigned len;
5034
5035 len = 3; /* space for "~/" and trailing NUL */
5036 if (src != NULL) /* just in case */
5037 len += (unsigned)STRLEN(src);
5038 dst = alloc(len);
5039 if (dst != NULL)
5040 home_replace(buf, src, dst, len, TRUE);
5041 return dst;
5042}
5043
5044/*
5045 * Compare two file names and return:
5046 * FPC_SAME if they both exist and are the same file.
5047 * FPC_SAMEX if they both don't exist and have the same file name.
5048 * FPC_DIFF if they both exist and are different files.
5049 * FPC_NOTX if they both don't exist.
5050 * FPC_DIFFX if one of them doesn't exist.
5051 * For the first name environment variables are expanded
5052 */
5053 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005054fullpathcmp(
5055 char_u *s1,
5056 char_u *s2,
5057 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058{
5059#ifdef UNIX
5060 char_u exp1[MAXPATHL];
5061 char_u full1[MAXPATHL];
5062 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02005063 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 int r1, r2;
5065
5066 expand_env(s1, exp1, MAXPATHL);
5067 r1 = mch_stat((char *)exp1, &st1);
5068 r2 = mch_stat((char *)s2, &st2);
5069 if (r1 != 0 && r2 != 0)
5070 {
5071 /* if mch_stat() doesn't work, may compare the names */
5072 if (checkname)
5073 {
5074 if (fnamecmp(exp1, s2) == 0)
5075 return FPC_SAMEX;
5076 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
5077 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
5078 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
5079 return FPC_SAMEX;
5080 }
5081 return FPC_NOTX;
5082 }
5083 if (r1 != 0 || r2 != 0)
5084 return FPC_DIFFX;
5085 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
5086 return FPC_SAME;
5087 return FPC_DIFF;
5088#else
5089 char_u *exp1; /* expanded s1 */
5090 char_u *full1; /* full path of s1 */
5091 char_u *full2; /* full path of s2 */
5092 int retval = FPC_DIFF;
5093 int r1, r2;
5094
5095 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
5096 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
5097 {
5098 full1 = exp1 + MAXPATHL;
5099 full2 = full1 + MAXPATHL;
5100
5101 expand_env(s1, exp1, MAXPATHL);
5102 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
5103 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
5104
5105 /* If vim_FullName() fails, the file probably doesn't exist. */
5106 if (r1 != OK && r2 != OK)
5107 {
5108 if (checkname && fnamecmp(exp1, s2) == 0)
5109 retval = FPC_SAMEX;
5110 else
5111 retval = FPC_NOTX;
5112 }
5113 else if (r1 != OK || r2 != OK)
5114 retval = FPC_DIFFX;
5115 else if (fnamecmp(full1, full2))
5116 retval = FPC_DIFF;
5117 else
5118 retval = FPC_SAME;
5119 vim_free(exp1);
5120 }
5121 return retval;
5122#endif
5123}
5124
5125/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005126 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02005127 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005128 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 */
5130 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005131gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132{
5133 char_u *p1, *p2;
5134
5135 if (fname == NULL)
5136 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01005137 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01005139 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005141 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 }
5143 return p1;
5144}
5145
Bram Moolenaar31710262010-08-13 13:36:15 +02005146#if defined(FEAT_SEARCHPATH)
Bram Moolenaar31710262010-08-13 13:36:15 +02005147/*
5148 * Return the end of the directory name, on the first path
5149 * separator:
5150 * "/path/file", "/path/dir/", "/path//dir", "/file"
5151 * ^ ^ ^ ^
5152 */
5153 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005154gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02005155{
5156 char_u *dir_end = fname;
5157 char_u *next_dir_end = fname;
5158 int look_for_sep = TRUE;
5159 char_u *p;
5160
5161 for (p = fname; *p != NUL; )
5162 {
5163 if (vim_ispathsep(*p))
5164 {
5165 if (look_for_sep)
5166 {
5167 next_dir_end = p;
5168 look_for_sep = FALSE;
5169 }
5170 }
5171 else
5172 {
5173 if (!look_for_sep)
5174 dir_end = next_dir_end;
5175 look_for_sep = TRUE;
5176 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005177 MB_PTR_ADV(p);
Bram Moolenaar31710262010-08-13 13:36:15 +02005178 }
5179 return dir_end;
5180}
5181#endif
5182
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005184 * Get pointer to tail of "fname", including path separators. Putting a NUL
5185 * here leaves the directory name. Takes care of "c:/" and "//".
5186 * Always returns a valid pointer.
5187 */
5188 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005189gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005190{
5191 char_u *p;
5192 char_u *t;
5193
5194 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
5195 t = gettail(fname);
5196 while (t > p && after_pathsep(fname, t))
5197 --t;
5198#ifdef VMS
5199 /* path separator is part of the path */
5200 ++t;
5201#endif
5202 return t;
5203}
5204
5205/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 * get the next path component (just after the next path separator).
5207 */
5208 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005209getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210{
5211 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005212 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 if (*fname)
5214 ++fname;
5215 return fname;
5216}
5217
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218/*
5219 * Get a pointer to one character past the head of a path name.
5220 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
5221 * If there is no head, path is returned.
5222 */
5223 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005224get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225{
5226 char_u *retval;
5227
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005228#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 /* may skip "c:" */
5230 if (isalpha(path[0]) && path[1] == ':')
5231 retval = path + 2;
5232 else
5233 retval = path;
5234#else
5235# if defined(AMIGA)
5236 /* may skip "label:" */
5237 retval = vim_strchr(path, ':');
5238 if (retval == NULL)
5239 retval = path;
5240# else /* Unix */
5241 retval = path;
5242# endif
5243#endif
5244
5245 while (vim_ispathsep(*retval))
5246 ++retval;
5247
5248 return retval;
5249}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250
5251/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01005252 * Return TRUE if 'c' is a path separator.
5253 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 */
5255 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005256vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257{
Bram Moolenaare60acc12011-05-10 16:41:25 +02005258#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02005260#else
5261# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005263# else
5264# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
5266 return (c == ':' || c == '[' || c == ']' || c == '/'
5267 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02005268# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005270# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02005272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273}
5274
Bram Moolenaar69c35002013-11-04 02:54:12 +01005275/*
5276 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
5277 */
5278 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005279vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01005280{
5281 return vim_ispathsep(c)
5282#ifdef BACKSLASH_IN_FILENAME
5283 && c != ':'
5284#endif
5285 ;
5286}
5287
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5289/*
5290 * return TRUE if 'c' is a path list separator.
5291 */
5292 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005293vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294{
5295#ifdef UNIX
5296 return (c == ':');
5297#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005298 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299#endif
5300}
5301#endif
5302
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005303/*
5304 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5305 * It's done in-place.
5306 */
5307 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005308shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005309{
5310 char_u *tail, *s, *d;
5311 int skip = FALSE;
5312
5313 tail = gettail(str);
5314 d = str;
5315 for (s = str; ; ++s)
5316 {
5317 if (s >= tail) /* copy the whole tail */
5318 {
5319 *d++ = *s;
5320 if (*s == NUL)
5321 break;
5322 }
5323 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5324 {
5325 *d++ = *s;
5326 skip = FALSE;
5327 }
5328 else if (!skip)
5329 {
5330 *d++ = *s; /* copy next char */
5331 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5332 skip = TRUE;
5333# ifdef FEAT_MBYTE
5334 if (has_mbyte)
5335 {
5336 int l = mb_ptr2len(s);
5337
5338 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005339 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005340 }
5341# endif
5342 }
5343 }
5344}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005345
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005346/*
5347 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5348 * Also returns TRUE if there is no directory name.
5349 * "fname" must be writable!.
5350 */
5351 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005352dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005353{
5354 char_u *p;
5355 int c;
5356 int retval;
5357
5358 p = gettail_sep(fname);
5359 if (p == fname)
5360 return TRUE;
5361 c = *p;
5362 *p = NUL;
5363 retval = mch_isdir(fname);
5364 *p = c;
5365 return retval;
5366}
5367
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005369 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5370 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 */
5372 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005373vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005375#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005377#else
5378 if (p_fic)
5379 return MB_STRICMP(x, y);
5380 return STRCMP(x, y);
5381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382}
5383
5384 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005385vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005387#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005388 char_u *px = x;
5389 char_u *py = y;
5390 int cx = NUL;
5391 int cy = NUL;
5392
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005393 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005395 cx = PTR2CHAR(px);
5396 cy = PTR2CHAR(py);
5397 if (cx == NUL || cy == NUL
5398 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5399 && !(cx == '/' && cy == '\\')
5400 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005402 len -= MB_PTR2LEN(px);
5403 px += MB_PTR2LEN(px);
5404 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 }
5406 if (len == 0)
5407 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005408 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005409#else
5410 if (p_fic)
5411 return MB_STRNICMP(x, y, len);
5412 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005414}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415
5416/*
5417 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005418 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 */
5420 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005421concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422{
5423 char_u *dest;
5424
5425 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5426 if (dest != NULL)
5427 {
5428 STRCPY(dest, fname1);
5429 if (sep)
5430 add_pathsep(dest);
5431 STRCAT(dest, fname2);
5432 }
5433 return dest;
5434}
5435
Bram Moolenaard6754642005-01-17 22:18:45 +00005436/*
5437 * Concatenate two strings and return the result in allocated memory.
5438 * Returns NULL when out of memory.
5439 */
5440 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005441concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005442{
5443 char_u *dest;
5444 size_t l = STRLEN(str1);
5445
5446 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5447 if (dest != NULL)
5448 {
5449 STRCPY(dest, str1);
5450 STRCPY(dest + l, str2);
5451 }
5452 return dest;
5453}
Bram Moolenaard6754642005-01-17 22:18:45 +00005454
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455/*
5456 * Add a path separator to a file name, unless it already ends in a path
5457 * separator.
5458 */
5459 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005460add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005462 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 STRCAT(p, PATHSEPSTR);
5464}
5465
5466/*
5467 * FullName_save - Make an allocated copy of a full file name.
5468 * Returns NULL when out of memory.
5469 */
5470 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005471FullName_save(
5472 char_u *fname,
5473 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005474 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475{
5476 char_u *buf;
5477 char_u *new_fname = NULL;
5478
5479 if (fname == NULL)
5480 return NULL;
5481
5482 buf = alloc((unsigned)MAXPATHL);
5483 if (buf != NULL)
5484 {
5485 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5486 new_fname = vim_strsave(buf);
5487 else
5488 new_fname = vim_strsave(fname);
5489 vim_free(buf);
5490 }
5491 return new_fname;
5492}
5493
5494#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5495
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005496static char_u *skip_string(char_u *p);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005497static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498
5499/*
5500 * Find the start of a comment, not knowing if we are in a comment right now.
5501 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005502 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005504 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005505ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005506{
5507 return find_start_comment(curbuf->b_ind_maxcomment);
5508}
5509
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005511find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512{
5513 pos_T *pos;
5514 char_u *line;
5515 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005516 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005518 for (;;)
5519 {
5520 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5521 if (pos == NULL)
5522 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005524 /*
5525 * Check if the comment start we found is inside a string.
5526 * If it is then restrict the search to below this line and try again.
5527 */
5528 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005529 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005530 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005531 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005532 break;
5533 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5534 if (cur_maxcomment <= 0)
5535 {
5536 pos = NULL;
5537 break;
5538 }
5539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 return pos;
5541}
5542
5543/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005544 * Find the start of a comment or raw string, not knowing if we are in a
5545 * comment or raw string right now.
5546 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaardde81312017-08-26 17:49:01 +02005547 * If is_raw is given and returns start of raw_string, sets it to true.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005548 * Return NULL when not inside a comment or raw string.
5549 * "CORS" -> Comment Or Raw String
5550 */
5551 static pos_T *
Bram Moolenaardde81312017-08-26 17:49:01 +02005552ind_find_start_CORS(linenr_T *is_raw) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005553{
Bram Moolenaar089af182015-10-07 11:41:49 +02005554 static pos_T comment_pos_copy;
5555 pos_T *comment_pos;
5556 pos_T *rs_pos;
5557
5558 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5559 if (comment_pos != NULL)
5560 {
5561 /* Need to make a copy of the static pos in findmatchlimit(),
5562 * calling find_start_rawstring() may change it. */
5563 comment_pos_copy = *comment_pos;
5564 comment_pos = &comment_pos_copy;
5565 }
5566 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005567
5568 /* If comment_pos is before rs_pos the raw string is inside the comment.
5569 * If rs_pos is before comment_pos the comment is inside the raw string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005570 if (comment_pos == NULL || (rs_pos != NULL
5571 && LT_POS(*rs_pos, *comment_pos)))
Bram Moolenaardde81312017-08-26 17:49:01 +02005572 {
5573 if (is_raw != NULL && rs_pos != NULL)
5574 *is_raw = rs_pos->lnum;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005575 return rs_pos;
Bram Moolenaardde81312017-08-26 17:49:01 +02005576 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005577 return comment_pos;
5578}
5579
5580/*
5581 * Find the start of a raw string, not knowing if we are in one right now.
5582 * Search starts at w_cursor.lnum and goes backwards.
5583 * Return NULL when not inside a raw string.
5584 */
5585 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005586find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005587{
5588 pos_T *pos;
5589 char_u *line;
5590 char_u *p;
5591 int cur_maxcomment = ind_maxcomment;
5592
5593 for (;;)
5594 {
5595 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5596 if (pos == NULL)
5597 break;
5598
5599 /*
5600 * Check if the raw string start we found is inside a string.
5601 * If it is then restrict the search to below this line and try again.
5602 */
5603 line = ml_get(pos->lnum);
5604 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5605 p = skip_string(p);
5606 if ((colnr_T)(p - line) <= pos->col)
5607 break;
5608 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5609 if (cur_maxcomment <= 0)
5610 {
5611 pos = NULL;
5612 break;
5613 }
5614 }
5615 return pos;
5616}
5617
5618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 * Skip to the end of a "string" and a 'c' character.
5620 * If there is no string or character, return argument unmodified.
5621 */
5622 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005623skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624{
5625 int i;
5626
5627 /*
5628 * We loop, because strings may be concatenated: "date""time".
5629 */
5630 for ( ; ; ++p)
5631 {
5632 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5633 {
5634 if (!p[1]) /* ' at end of line */
5635 break;
5636 i = 2;
5637 if (p[1] == '\\') /* '\n' or '\000' */
5638 {
5639 ++i;
5640 while (vim_isdigit(p[i - 1])) /* '\000' */
5641 ++i;
5642 }
5643 if (p[i] == '\'') /* check for trailing ' */
5644 {
5645 p += i;
5646 continue;
5647 }
5648 }
5649 else if (p[0] == '"') /* start of string */
5650 {
5651 for (++p; p[0]; ++p)
5652 {
5653 if (p[0] == '\\' && p[1] != NUL)
5654 ++p;
5655 else if (p[0] == '"') /* end of string */
5656 break;
5657 }
5658 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005659 continue; /* continue for another string */
5660 }
5661 else if (p[0] == 'R' && p[1] == '"')
5662 {
5663 /* Raw string: R"[delim](...)[delim]" */
5664 char_u *delim = p + 2;
5665 char_u *paren = vim_strchr(delim, '(');
5666
5667 if (paren != NULL)
5668 {
5669 size_t delim_len = paren - delim;
5670
5671 for (p += 3; *p; ++p)
5672 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5673 && p[delim_len + 1] == '"')
5674 {
5675 p += delim_len + 1;
5676 break;
5677 }
5678 if (p[0] == '"')
5679 continue; /* continue for another string */
5680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 }
5682 break; /* no string found */
5683 }
5684 if (!*p)
5685 --p; /* backup from NUL */
5686 return p;
5687}
5688#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5689
5690#if defined(FEAT_CINDENT) || defined(PROTO)
5691
5692/*
5693 * Do C or expression indenting on the current line.
5694 */
5695 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005696do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697{
5698# ifdef FEAT_EVAL
5699 if (*curbuf->b_p_inde != NUL)
5700 fixthisline(get_expr_indent);
5701 else
5702# endif
5703 fixthisline(get_c_indent);
5704}
5705
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005706/* Find result cache for cpp_baseclass */
5707typedef struct {
5708 int found;
5709 lpos_T lpos;
5710} cpp_baseclass_cache_T;
5711
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712/*
5713 * Functions for C-indenting.
5714 * Most of this originally comes from Eric Fischer.
5715 */
5716/*
5717 * Below "XXX" means that this function may unlock the current line.
5718 */
5719
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005720static int cin_isdefault(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005721static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005722static int cin_iscomment(char_u *);
5723static int cin_islinecomment(char_u *);
5724static int cin_isterminated(char_u *, int, int);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005725static int cin_iselse(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005726static int cin_ends_in(char_u *, char_u *, char_u *);
5727static int cin_starts_with(char_u *s, char *word);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005728static pos_T *find_match_paren(int);
5729static pos_T *find_match_char(int c, int ind_maxparen);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005730static int find_last_paren(char_u *l, int start, int end);
5731static int find_match(int lookfor, linenr_T ourscope);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732
5733/*
5734 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005735 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 */
5737 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005738cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739{
5740 while (*s)
5741 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005742 char_u *prev_s = s;
5743
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005745
5746 /* Perl/shell # comment comment continues until eol. Require a space
5747 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005748 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005749 {
5750 s += STRLEN(s);
5751 break;
5752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 if (*s != '/')
5754 break;
5755 ++s;
5756 if (*s == '/') /* slash-slash comment continues till eol */
5757 {
5758 s += STRLEN(s);
5759 break;
5760 }
5761 if (*s != '*')
5762 break;
5763 for (++s; *s; ++s) /* skip slash-star comment */
5764 if (s[0] == '*' && s[1] == '/')
5765 {
5766 s += 2;
5767 break;
5768 }
5769 }
5770 return s;
5771}
5772
5773/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005774 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 * not considered code.
5776 */
5777 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005778cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779{
5780 return *cin_skipcomment(s) == NUL;
5781}
5782
5783/*
5784 * Check previous lines for a "//" line comment, skipping over blank lines.
5785 */
5786 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005787find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788{
5789 static pos_T pos;
5790 char_u *line;
5791 char_u *p;
5792
5793 pos = curwin->w_cursor;
5794 while (--pos.lnum > 0)
5795 {
5796 line = ml_get(pos.lnum);
5797 p = skipwhite(line);
5798 if (cin_islinecomment(p))
5799 {
5800 pos.col = (int)(p - line);
5801 return &pos;
5802 }
5803 if (*p != NUL)
5804 break;
5805 }
5806 return NULL;
5807}
5808
5809/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005810 * Return TRUE if "text" starts with "key:".
5811 */
5812 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005813cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005814{
5815 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005816 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005817
5818 if (*s == '\'' || *s == '"')
5819 {
5820 /* can be 'key': or "key": */
5821 quote = *s;
5822 ++s;
5823 }
5824 if (!vim_isIDc(*s)) /* need at least one ID character */
5825 return FALSE;
5826
5827 while (vim_isIDc(*s))
5828 ++s;
5829 if (*s == quote)
5830 ++s;
5831
5832 s = cin_skipcomment(s);
5833
5834 /* "::" is not a label, it's C++ */
5835 return (*s == ':' && s[1] != ':');
5836}
5837
5838/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005840 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 */
5842 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005843cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844{
5845 if (!vim_isIDc(**s)) /* need at least one ID character */
5846 return FALSE;
5847
5848 while (vim_isIDc(**s))
5849 (*s)++;
5850
5851 *s = cin_skipcomment(*s);
5852
5853 /* "::" is not a label, it's C++ */
5854 return (**s == ':' && *++*s != ':');
5855}
5856
5857/*
5858 * Recognize a label: "label:".
5859 * Note: curwin->w_cursor must be where we are looking for the label.
5860 */
5861 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005862cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863{
5864 char_u *s;
5865
5866 s = cin_skipcomment(ml_get_curline());
5867
5868 /*
5869 * Exclude "default" from labels, since it should be indented
5870 * like a switch label. Same for C++ scope declarations.
5871 */
5872 if (cin_isdefault(s))
5873 return FALSE;
5874 if (cin_isscopedecl(s))
5875 return FALSE;
5876
5877 if (cin_islabel_skip(&s))
5878 {
5879 /*
5880 * Only accept a label if the previous line is terminated or is a case
5881 * label.
5882 */
5883 pos_T cursor_save;
5884 pos_T *trypos;
5885 char_u *line;
5886
5887 cursor_save = curwin->w_cursor;
5888 while (curwin->w_cursor.lnum > 1)
5889 {
5890 --curwin->w_cursor.lnum;
5891
5892 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005893 * If we're in a comment or raw string now, skip to the start of
5894 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 */
5896 curwin->w_cursor.col = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02005897 if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 curwin->w_cursor = *trypos;
5899
5900 line = ml_get_curline();
5901 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5902 continue;
5903 if (*(line = cin_skipcomment(line)) == NUL)
5904 continue;
5905
5906 curwin->w_cursor = cursor_save;
5907 if (cin_isterminated(line, TRUE, FALSE)
5908 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005909 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 || (cin_islabel_skip(&line) && cin_nocode(line)))
5911 return TRUE;
5912 return FALSE;
5913 }
5914 curwin->w_cursor = cursor_save;
5915 return TRUE; /* label at start of file??? */
5916 }
5917 return FALSE;
5918}
5919
5920/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005921 * Recognize structure initialization and enumerations:
5922 * "[typedef] [static|public|protected|private] enum"
5923 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 */
5925 static int
5926cin_isinit(void)
5927{
5928 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005929 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930
5931 s = cin_skipcomment(ml_get_curline());
5932
Bram Moolenaar75342212013-03-07 13:13:52 +01005933 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 s = cin_skipcomment(s + 7);
5935
Bram Moolenaar75342212013-03-07 13:13:52 +01005936 for (;;)
5937 {
5938 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005939
Bram Moolenaar75342212013-03-07 13:13:52 +01005940 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5941 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005942 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005943 if (cin_starts_with(s, skip[i]))
5944 {
5945 s = cin_skipcomment(s + l);
5946 l = 0;
5947 break;
5948 }
5949 }
5950 if (l != 0)
5951 break;
5952 }
5953
5954 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 return TRUE;
5956
5957 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5958 return TRUE;
5959
5960 return FALSE;
5961}
5962
5963/*
5964 * Recognize a switch label: "case .*:" or "default:".
5965 */
5966 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005967cin_iscase(
5968 char_u *s,
5969 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970{
5971 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005972 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 {
5974 for (s += 4; *s; ++s)
5975 {
5976 s = cin_skipcomment(s);
5977 if (*s == ':')
5978 {
5979 if (s[1] == ':') /* skip over "::" for C++ */
5980 ++s;
5981 else
5982 return TRUE;
5983 }
5984 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005985 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5987 return FALSE; /* stop at comment */
5988 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005989 {
5990 /* JS etc. */
5991 if (strict)
5992 return FALSE; /* stop at string */
5993 else
5994 return TRUE;
5995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 }
5997 return FALSE;
5998 }
5999
6000 if (cin_isdefault(s))
6001 return TRUE;
6002 return FALSE;
6003}
6004
6005/*
6006 * Recognize a "default" switch label.
6007 */
6008 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006009cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010{
6011 return (STRNCMP(s, "default", 7) == 0
6012 && *(s = cin_skipcomment(s + 7)) == ':'
6013 && s[1] != ':');
6014}
6015
6016/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02006017 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 */
6019 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006020cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021{
6022 int i;
6023
6024 s = cin_skipcomment(s);
6025 if (STRNCMP(s, "public", 6) == 0)
6026 i = 6;
6027 else if (STRNCMP(s, "protected", 9) == 0)
6028 i = 9;
6029 else if (STRNCMP(s, "private", 7) == 0)
6030 i = 7;
6031 else
6032 return FALSE;
6033 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
6034}
6035
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006036/* Maximum number of lines to search back for a "namespace" line. */
6037#define FIND_NAMESPACE_LIM 20
6038
6039/*
6040 * Recognize a "namespace" scope declaration.
6041 */
6042 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006043cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006044{
6045 char_u *p;
6046 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006047 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006048
6049 s = cin_skipcomment(s);
6050 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
6051 {
6052 p = cin_skipcomment(skipwhite(s + 9));
6053 while (*p != NUL)
6054 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006055 if (VIM_ISWHITE(*p))
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006056 {
6057 has_name = TRUE; /* found end of a name */
6058 p = cin_skipcomment(skipwhite(p));
6059 }
6060 else if (*p == '{')
6061 {
6062 break;
6063 }
6064 else if (vim_iswordc(*p))
6065 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006066 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006067 if (has_name)
6068 return FALSE; /* word character after skipping past name */
6069 ++p;
6070 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01006071 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
6072 {
6073 if (!has_name_start || has_name)
6074 return FALSE;
6075 /* C++ 17 nested namespace */
6076 p += 3;
6077 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006078 else
6079 {
6080 return FALSE;
6081 }
6082 }
6083 return TRUE;
6084 }
6085 return FALSE;
6086}
6087
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088/*
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006089 * Recognize a `extern "C"` or `extern "C++"` linkage specifications.
6090 */
6091 static int
6092cin_is_cpp_extern_c(char_u *s)
6093{
6094 char_u *p;
6095 int has_string_literal = FALSE;
6096
6097 s = cin_skipcomment(s);
6098 if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
6099 {
6100 p = cin_skipcomment(skipwhite(s + 6));
6101 while (*p != NUL)
6102 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01006103 if (VIM_ISWHITE(*p))
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006104 {
6105 p = cin_skipcomment(skipwhite(p));
6106 }
6107 else if (*p == '{')
6108 {
6109 break;
6110 }
6111 else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
6112 {
6113 if (has_string_literal)
6114 return FALSE;
6115 has_string_literal = TRUE;
6116 p += 3;
6117 }
6118 else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
6119 && p[4] == '"')
6120 {
6121 if (has_string_literal)
6122 return FALSE;
6123 has_string_literal = TRUE;
6124 p += 5;
6125 }
6126 else
6127 {
6128 return FALSE;
6129 }
6130 }
6131 return has_string_literal ? TRUE : FALSE;
6132 }
6133 return FALSE;
6134}
6135
6136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 * Return a pointer to the first non-empty non-comment character after a ':'.
6138 * Return NULL if not found.
6139 * case 234: a = b;
6140 * ^
6141 */
6142 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006143after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144{
6145 for ( ; *l; ++l)
6146 {
6147 if (*l == ':')
6148 {
6149 if (l[1] == ':') /* skip over "::" for C++ */
6150 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006151 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 break;
6153 }
6154 else if (*l == '\'' && l[1] && l[2] == '\'')
6155 l += 2; /* skip over 'x' */
6156 }
6157 if (*l == NUL)
6158 return NULL;
6159 l = cin_skipcomment(l + 1);
6160 if (*l == NUL)
6161 return NULL;
6162 return l;
6163}
6164
6165/*
6166 * Get indent of line "lnum", skipping a label.
6167 * Return 0 if there is nothing after the label.
6168 */
6169 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006170get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171{
6172 char_u *l;
6173 pos_T fp;
6174 colnr_T col;
6175 char_u *p;
6176
6177 l = ml_get(lnum);
6178 p = after_label(l);
6179 if (p == NULL)
6180 return 0;
6181
6182 fp.col = (colnr_T)(p - l);
6183 fp.lnum = lnum;
6184 getvcol(curwin, &fp, &col, NULL, NULL);
6185 return (int)col;
6186}
6187
6188/*
6189 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006190 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 * label: if (asdf && asdfasdf)
6192 * ^
6193 */
6194 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006195skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196{
6197 char_u *l;
6198 int amount;
6199 pos_T cursor_save;
6200
6201 cursor_save = curwin->w_cursor;
6202 curwin->w_cursor.lnum = lnum;
6203 l = ml_get_curline();
6204 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006205 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 {
6207 amount = get_indent_nolabel(lnum);
6208 l = after_label(ml_get_curline());
6209 if (l == NULL) /* just in case */
6210 l = ml_get_curline();
6211 }
6212 else
6213 {
6214 amount = get_indent();
6215 l = ml_get_curline();
6216 }
6217 *pp = l;
6218
6219 curwin->w_cursor = cursor_save;
6220 return amount;
6221}
6222
6223/*
6224 * Return the indent of the first variable name after a type in a declaration.
6225 * int a, indent of "a"
6226 * static struct foo b, indent of "b"
6227 * enum bla c, indent of "c"
6228 * Returns zero when it doesn't look like a declaration.
6229 */
6230 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006231cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232{
6233 char_u *line, *p, *s;
6234 int len;
6235 pos_T fp;
6236 colnr_T col;
6237
6238 line = ml_get_curline();
6239 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006240 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 if (len == 6 && STRNCMP(p, "static", 6) == 0)
6242 {
6243 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006244 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 }
6246 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
6247 p = skipwhite(p + 6);
6248 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
6249 p = skipwhite(p + 4);
6250 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
6251 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
6252 {
6253 s = skipwhite(p + len);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006254 if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3]))
6255 || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4]))
6256 || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5]))
6257 || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 p = s;
6259 }
6260 for (len = 0; vim_isIDc(p[len]); ++len)
6261 ;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006262 if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 return 0;
6264
6265 p = skipwhite(p + len);
6266 fp.lnum = curwin->w_cursor.lnum;
6267 fp.col = (colnr_T)(p - line);
6268 getvcol(curwin, &fp, &col, NULL, NULL);
6269 return (int)col;
6270}
6271
6272/*
6273 * Return the indent of the first non-blank after an equal sign.
6274 * char *foo = "here";
6275 * Return zero if no (useful) equal sign found.
6276 * Return -1 if the line above "lnum" ends in a backslash.
6277 * foo = "asdf\
6278 * asdf\
6279 * here";
6280 */
6281 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006282cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283{
6284 char_u *line;
6285 char_u *s;
6286 colnr_T col;
6287 pos_T fp;
6288
6289 if (lnum > 1)
6290 {
6291 line = ml_get(lnum - 1);
6292 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6293 return -1;
6294 }
6295
6296 line = s = ml_get(lnum);
6297 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
6298 {
6299 if (cin_iscomment(s)) /* ignore comments */
6300 s = cin_skipcomment(s);
6301 else
6302 ++s;
6303 }
6304 if (*s != '=')
6305 return 0;
6306
6307 s = skipwhite(s + 1);
6308 if (cin_nocode(s))
6309 return 0;
6310
6311 if (*s == '"') /* nice alignment for continued strings */
6312 ++s;
6313
6314 fp.lnum = lnum;
6315 fp.col = (colnr_T)(s - line);
6316 getvcol(curwin, &fp, &col, NULL, NULL);
6317 return (int)col;
6318}
6319
6320/*
6321 * Recognize a preprocessor statement: Any line that starts with '#'.
6322 */
6323 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006324cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006326 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 return TRUE;
6328 return FALSE;
6329}
6330
6331/*
6332 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6333 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6334 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006335 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 */
6337 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006338cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339{
6340 char_u *line = *pp;
6341 linenr_T lnum = *lnump;
6342 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006343 int candidate_amount = *amount;
6344
6345 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6346 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006348 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 {
6350 if (cin_ispreproc(line))
6351 {
6352 retval = TRUE;
6353 *lnump = lnum;
6354 break;
6355 }
6356 if (lnum == 1)
6357 break;
6358 line = ml_get(--lnum);
6359 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6360 break;
6361 }
6362
6363 if (lnum != *lnump)
6364 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006365 if (retval)
6366 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 return retval;
6368}
6369
6370/*
6371 * Recognize the start of a C or C++ comment.
6372 */
6373 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006374cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375{
6376 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6377}
6378
6379/*
6380 * Recognize the start of a "//" comment.
6381 */
6382 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006383cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384{
6385 return (p[0] == '/' && p[1] == '/');
6386}
6387
6388/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006389 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6390 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006392 * If a line begins with an "else", only consider it terminated if no unmatched
6393 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 * Return the character terminating the line (ending char's have precedence if
6395 * both apply in order to determine initializations).
6396 */
6397 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006398cin_isterminated(
6399 char_u *s,
6400 int incl_open, /* include '{' at the end as terminator */
6401 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006403 char_u found_start = 0;
6404 unsigned n_open = 0;
6405 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406
6407 s = cin_skipcomment(s);
6408
6409 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6410 found_start = *s;
6411
Bram Moolenaar496f9512011-05-19 16:35:09 +02006412 if (!found_start)
6413 is_else = cin_iselse(s);
6414
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 while (*s)
6416 {
6417 /* skip over comments, "" strings and 'c'haracters */
6418 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006419 if (*s == '}' && n_open > 0)
6420 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006421 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006422 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 && cin_nocode(s + 1))
6424 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006425 else if (*s == '{')
6426 {
6427 if (incl_open && cin_nocode(s + 1))
6428 return *s;
6429 else
6430 ++n_open;
6431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432
6433 if (*s)
6434 s++;
6435 }
6436 return found_start;
6437}
6438
6439/*
6440 * Recognize the basic picture of a function declaration -- it needs to
6441 * have an open paren somewhere and a close paren at the end of the line and
6442 * no semicolons anywhere.
6443 * When a line ends in a comma we continue looking in the next line.
6444 * "sp" points to a string with the line. When looking at other lines it must
6445 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006446 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006447 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 */
6449 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006450cin_isfuncdecl(
6451 char_u **sp,
6452 linenr_T first_lnum,
6453 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454{
6455 char_u *s;
6456 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006457 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006459 pos_T *trypos;
6460 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461
6462 if (sp == NULL)
6463 s = ml_get(lnum);
6464 else
6465 s = *sp;
6466
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006467 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006468 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006469 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006470 {
6471 lnum = trypos->lnum;
6472 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006473 {
6474 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006475 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006476 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006477
6478 s = ml_get(lnum);
6479 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006480 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006481
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006482 /* Ignore line starting with #. */
6483 if (cin_ispreproc(s))
6484 return FALSE;
6485
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6487 {
6488 if (cin_iscomment(s)) /* ignore comments */
6489 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006490 else if (*s == ':')
6491 {
6492 if (*(s + 1) == ':')
6493 s += 2;
6494 else
6495 /* To avoid a mistake in the following situation:
6496 * A::A(int a, int b)
6497 * : a(0) // <--not a function decl
6498 * , b(0)
6499 * {...
6500 */
6501 return FALSE;
6502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 else
6504 ++s;
6505 }
6506 if (*s != '(')
6507 return FALSE; /* ';', ' or " before any () or no '(' */
6508
6509 while (*s && *s != ';' && *s != '\'' && *s != '"')
6510 {
6511 if (*s == ')' && cin_nocode(s + 1))
6512 {
6513 /* ')' at the end: may have found a match
6514 * Check for he previous line not to end in a backslash:
6515 * #if defined(x) && \
6516 * defined(y)
6517 */
6518 lnum = first_lnum - 1;
6519 s = ml_get(lnum);
6520 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6521 retval = TRUE;
6522 goto done;
6523 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006524 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006526 int comma = (*s == ',');
6527
6528 /* ',' at the end: continue looking in the next line.
6529 * At the end: check for ',' in the next line, for this style:
6530 * func(arg1
6531 * , arg2) */
6532 for (;;)
6533 {
6534 if (lnum >= curbuf->b_ml.ml_line_count)
6535 break;
6536 s = ml_get(++lnum);
6537 if (!cin_ispreproc(s))
6538 break;
6539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 if (lnum >= curbuf->b_ml.ml_line_count)
6541 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006542 /* Require a comma at end of the line or a comma or ')' at the
6543 * start of next line. */
6544 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006545 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006546 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006547 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 }
6549 else if (cin_iscomment(s)) /* ignore comments */
6550 s = cin_skipcomment(s);
6551 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006552 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006554 just_started = FALSE;
6555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 }
6557
6558done:
6559 if (lnum != first_lnum && sp != NULL)
6560 *sp = ml_get(first_lnum);
6561
6562 return retval;
6563}
6564
6565 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006566cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006568 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569}
6570
6571 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006572cin_iselse(
6573 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574{
6575 if (*p == '}') /* accept "} else" */
6576 p = cin_skipcomment(p + 1);
6577 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6578}
6579
6580 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006581cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582{
6583 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6584}
6585
6586/*
6587 * Check if this is a "while" that should have a matching "do".
6588 * We only accept a "while (condition) ;", with only white space between the
6589 * ')' and ';'. The condition may be spread over several lines.
6590 */
6591 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006592cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593{
6594 pos_T cursor_save;
6595 pos_T *trypos;
6596 int retval = FALSE;
6597
6598 p = cin_skipcomment(p);
6599 if (*p == '}') /* accept "} while (cond);" */
6600 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006601 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 {
6603 cursor_save = curwin->w_cursor;
6604 curwin->w_cursor.lnum = lnum;
6605 curwin->w_cursor.col = 0;
6606 p = ml_get_curline();
6607 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6608 {
6609 ++p;
6610 ++curwin->w_cursor.col;
6611 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006612 if ((trypos = findmatchlimit(NULL, 0, 0,
6613 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6615 retval = TRUE;
6616 curwin->w_cursor = cursor_save;
6617 }
6618 return retval;
6619}
6620
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006621/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006622 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006623 * Return 0 if there is none.
6624 * Otherwise return !0 and update "*poffset" to point to the place where the
6625 * string was found.
6626 */
6627 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006628cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006629{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006630 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006631
6632 if (offset-- < 2)
6633 return 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006634 while (offset > 2 && VIM_ISWHITE(line[offset]))
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006635 --offset;
6636
6637 offset -= 1;
6638 if (!STRNCMP(line + offset, "if", 2))
6639 goto probablyFound;
6640
6641 if (offset >= 1)
6642 {
6643 offset -= 1;
6644 if (!STRNCMP(line + offset, "for", 3))
6645 goto probablyFound;
6646
6647 if (offset >= 2)
6648 {
6649 offset -= 2;
6650 if (!STRNCMP(line + offset, "while", 5))
6651 goto probablyFound;
6652 }
6653 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006654 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006655
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006656probablyFound:
6657 if (!offset || !vim_isIDc(line[offset - 1]))
6658 {
6659 *poffset = offset;
6660 return 1;
6661 }
6662 return 0;
6663}
6664
6665/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006666 * Return TRUE if we are at the end of a do-while.
6667 * do
6668 * nothing;
6669 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006670 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006671 * Adjust the cursor to the line with "while".
6672 */
6673 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006674cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006675{
6676 char_u *line;
6677 char_u *p;
6678 char_u *s;
6679 pos_T *trypos;
6680 int i;
6681
6682 if (terminated != ';') /* there must be a ';' at the end */
6683 return FALSE;
6684
6685 p = line = ml_get_curline();
6686 while (*p != NUL)
6687 {
6688 p = cin_skipcomment(p);
6689 if (*p == ')')
6690 {
6691 s = skipwhite(p + 1);
6692 if (*s == ';' && cin_nocode(s + 1))
6693 {
6694 /* Found ");" at end of the line, now check there is "while"
6695 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006696 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006697 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006698 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006699 if (trypos != NULL)
6700 {
6701 s = cin_skipcomment(ml_get(trypos->lnum));
6702 if (*s == '}') /* accept "} while (cond);" */
6703 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006704 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006705 {
6706 curwin->w_cursor.lnum = trypos->lnum;
6707 return TRUE;
6708 }
6709 }
6710
6711 /* Searching may have made "line" invalid, get it again. */
6712 line = ml_get_curline();
6713 p = line + i;
6714 }
6715 }
6716 if (*p != NUL)
6717 ++p;
6718 }
6719 return FALSE;
6720}
6721
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006723cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724{
6725 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6726}
6727
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006728/*
6729 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 * constructor-initialization. eg:
6731 *
6732 * class MyClass :
6733 * baseClass <-- here
6734 * class MyClass : public baseClass,
6735 * anotherBaseClass <-- here (should probably lineup ??)
6736 * MyClass::MyClass(...) :
6737 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006738 *
6739 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 */
6741 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006742cin_is_cpp_baseclass(
6743 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006745 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 char_u *s;
6747 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006748 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006749 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006751 if (pos->lnum <= lnum)
6752 return cached->found; /* Use the cached result */
6753
6754 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006756 s = skipwhite(line);
6757 if (*s == '#') /* skip #define FOO x ? (x) : x */
6758 return FALSE;
6759 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 if (*s == NUL)
6761 return FALSE;
6762
6763 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6764
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006765 /* Search for a line starting with '#', empty, ending in ';' or containing
6766 * '{' or '}' and start below it. This handles the following situations:
6767 * a = cond ?
6768 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006769 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006770 * func::foo()
6771 * : something
6772 * {}
6773 * Foo::Foo (int one, int two)
6774 * : something(4),
6775 * somethingelse(3)
6776 * {}
6777 */
6778 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006780 line = ml_get(lnum - 1);
6781 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006782 if (*s == '#' || *s == NUL)
6783 break;
6784 while (*s != NUL)
6785 {
6786 s = cin_skipcomment(s);
6787 if (*s == '{' || *s == '}'
6788 || (*s == ';' && cin_nocode(s + 1)))
6789 break;
6790 if (*s != NUL)
6791 ++s;
6792 }
6793 if (*s != NUL)
6794 break;
6795 --lnum;
6796 }
6797
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006798 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006799 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006800 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006801 for (;;)
6802 {
6803 if (*s == NUL)
6804 {
6805 if (lnum == curwin->w_cursor.lnum)
6806 break;
6807 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006808 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006809 s = line;
6810 }
6811 if (s == line)
6812 {
6813 /* don't recognize "case (foo):" as a baseclass */
6814 if (cin_iscase(s, FALSE))
6815 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006816 s = cin_skipcomment(line);
6817 if (*s == NUL)
6818 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006819 }
6820
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006821 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006822 s = skip_string(s) + 1;
6823 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 {
6825 if (s[1] == ':')
6826 {
6827 /* skip double colon. It can't be a constructor
6828 * initialization any more */
6829 lookfor_ctor_init = FALSE;
6830 s = cin_skipcomment(s + 2);
6831 }
6832 else if (lookfor_ctor_init || class_or_struct)
6833 {
6834 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006835 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 cpp_base_class = TRUE;
6837 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006838 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 s = cin_skipcomment(s + 1);
6840 }
6841 else
6842 s = cin_skipcomment(s + 1);
6843 }
6844 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6845 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6846 {
6847 class_or_struct = TRUE;
6848 lookfor_ctor_init = FALSE;
6849
6850 if (*s == 'c')
6851 s = cin_skipcomment(s + 5);
6852 else
6853 s = cin_skipcomment(s + 6);
6854 }
6855 else
6856 {
6857 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6858 {
6859 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6860 }
6861 else if (s[0] == ')')
6862 {
6863 /* Constructor-initialization is assumed if we come across
6864 * something like "):" */
6865 class_or_struct = FALSE;
6866 lookfor_ctor_init = TRUE;
6867 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006868 else if (s[0] == '?')
6869 {
6870 /* Avoid seeing '() :' after '?' as constructor init. */
6871 return FALSE;
6872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 else if (!vim_isIDc(s[0]))
6874 {
6875 /* if it is not an identifier, we are wrong */
6876 class_or_struct = FALSE;
6877 lookfor_ctor_init = FALSE;
6878 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006879 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 {
6881 /* it can't be a constructor-initialization any more */
6882 lookfor_ctor_init = FALSE;
6883
6884 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006885 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006886 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 }
6888
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006889 /* When the line ends in a comma don't align with it. */
6890 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006891 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006892
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 s = cin_skipcomment(s + 1);
6894 }
6895 }
6896
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006897 cached->found = cpp_base_class;
6898 if (cpp_base_class)
6899 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 return cpp_base_class;
6901}
6902
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006903 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006904get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006905{
6906 int amount;
6907 colnr_T vcol;
6908 pos_T *trypos;
6909
6910 if (col == 0)
6911 {
6912 amount = get_indent();
6913 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006914 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006915 amount = get_indent_lnum(trypos->lnum); /* XXX */
6916 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006917 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006918 }
6919 else
6920 {
6921 curwin->w_cursor.col = col;
6922 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6923 amount = (int)vcol;
6924 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006925 if (amount < curbuf->b_ind_cpp_baseclass)
6926 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006927 return amount;
6928}
6929
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930/*
6931 * Return TRUE if string "s" ends with the string "find", possibly followed by
6932 * white space and comments. Skip strings and comments.
6933 * Ignore "ignore" after "find" if it's not NULL.
6934 */
6935 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006936cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937{
6938 char_u *p = s;
6939 char_u *r;
6940 int len = (int)STRLEN(find);
6941
6942 while (*p != NUL)
6943 {
6944 p = cin_skipcomment(p);
6945 if (STRNCMP(p, find, len) == 0)
6946 {
6947 r = skipwhite(p + len);
6948 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6949 r = skipwhite(r + STRLEN(ignore));
6950 if (cin_nocode(r))
6951 return TRUE;
6952 }
6953 if (*p != NUL)
6954 ++p;
6955 }
6956 return FALSE;
6957}
6958
6959/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006960 * Return TRUE when "s" starts with "word" and then a non-ID character.
6961 */
6962 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006963cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006964{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006965 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006966
6967 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6968}
6969
6970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 * Skip strings, chars and comments until at or past "trypos".
6972 * Return the column found.
6973 */
6974 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006975cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976{
6977 char_u *line;
6978 char_u *p;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006979 char_u *new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980
6981 p = line = ml_get(trypos->lnum);
6982 while (*p && (colnr_T)(p - line) < trypos->col)
6983 {
6984 if (cin_iscomment(p))
6985 p = cin_skipcomment(p);
6986 else
6987 {
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006988 new_p = skip_string(p);
6989 if (new_p == p)
6990 ++p;
6991 else
6992 p = new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 }
6994 }
6995 return (int)(p - line);
6996}
6997
6998/*
6999 * Find the '{' at the start of the block we are in.
7000 * Return NULL if no match found.
7001 * Ignore a '{' that is in a comment, makes indenting the next three lines
7002 * work. */
7003/* foo() */
7004/* { */
7005/* } */
7006
7007 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007008find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009{
7010 pos_T cursor_save;
7011 pos_T *trypos;
7012 pos_T *pos;
7013 static pos_T pos_copy;
7014
7015 cursor_save = curwin->w_cursor;
7016 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
7017 {
7018 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
7019 trypos = &pos_copy;
7020 curwin->w_cursor = *trypos;
7021 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007022 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaardde81312017-08-26 17:49:01 +02007024 && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 break;
7026 if (pos != NULL)
7027 curwin->w_cursor.lnum = pos->lnum;
7028 }
7029 curwin->w_cursor = cursor_save;
7030 return trypos;
7031}
7032
7033/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02007034 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007035 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 */
7037 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007038find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02007040 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007041}
7042
7043 static pos_T *
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02007044find_match_char(int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007045{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 pos_T cursor_save;
7047 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007048 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007049 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050
7051 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007052 ind_maxp_wk = ind_maxparen;
7053retry:
7054 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 {
7056 /* check if the ( is in a // comment */
7057 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01007058 {
7059 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
7060 if (ind_maxp_wk > 0)
7061 {
7062 curwin->w_cursor = *trypos;
7063 curwin->w_cursor.col = 0; /* XXX */
7064 goto retry;
7065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 else
7069 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01007070 pos_T *trypos_wk;
7071
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 pos_copy = *trypos; /* copy trypos, findmatch will change it */
7073 trypos = &pos_copy;
7074 curwin->w_cursor = *trypos;
Bram Moolenaardde81312017-08-26 17:49:01 +02007075 if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01007076 {
7077 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
7078 - trypos_wk->lnum);
7079 if (ind_maxp_wk > 0)
7080 {
7081 curwin->w_cursor = *trypos_wk;
7082 goto retry;
7083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01007085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 }
7087 }
7088 curwin->w_cursor = cursor_save;
7089 return trypos;
7090}
7091
7092/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02007093 * Find the matching '(', ignoring it if it is in a comment or before an
7094 * unmatched {.
7095 * Return NULL if no match found.
7096 */
7097 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01007098find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02007099{
7100 pos_T *trypos = find_match_paren(ind_maxparen);
7101
7102 if (trypos != NULL)
7103 {
7104 pos_T *tryposBrace = find_start_brace();
7105
7106 /* If both an unmatched '(' and '{' is found. Ignore the '('
7107 * position if the '{' is further down. */
7108 if (tryposBrace != NULL
7109 && (trypos->lnum != tryposBrace->lnum
7110 ? trypos->lnum < tryposBrace->lnum
7111 : trypos->col < tryposBrace->col))
7112 trypos = NULL;
7113 }
7114 return trypos;
7115}
7116
7117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 * Return ind_maxparen corrected for the difference in line number between the
7119 * cursor position and "startpos". This makes sure that searching for a
7120 * matching paren above the cursor line doesn't find a match because of
7121 * looking a few lines further.
7122 */
7123 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007124corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125{
7126 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
7127
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007128 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
7129 return curbuf->b_ind_maxparen - (int)n;
7130 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131}
7132
7133/*
7134 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007135 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 */
7137 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007138find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139{
7140 int i;
7141 int retval = FALSE;
7142 int open_count = 0;
7143
7144 curwin->w_cursor.col = 0; /* default is start of line */
7145
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01007146 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 {
7148 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
7149 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
7150 if (l[i] == start)
7151 ++open_count;
7152 else if (l[i] == end)
7153 {
7154 if (open_count > 0)
7155 --open_count;
7156 else
7157 {
7158 curwin->w_cursor.col = i;
7159 retval = TRUE;
7160 }
7161 }
7162 }
7163 return retval;
7164}
7165
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007166/*
7167 * Parse 'cinoptions' and set the values in "curbuf".
7168 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
7169 */
7170 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01007171parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007172{
7173 char_u *p;
7174 char_u *l;
7175 char_u *digits;
7176 int n;
7177 int divider;
7178 int fraction = 0;
7179 int sw = (int)get_sw_value(buf);
7180
7181 /*
7182 * Set the default values.
7183 */
7184 /* Spaces from a block's opening brace the prevailing indent for that
7185 * block should be. */
7186 buf->b_ind_level = sw;
7187
7188 /* Spaces from the edge of the line an open brace that's at the end of a
7189 * line is imagined to be. */
7190 buf->b_ind_open_imag = 0;
7191
7192 /* Spaces from the prevailing indent for a line that is not preceded by
7193 * an opening brace. */
7194 buf->b_ind_no_brace = 0;
7195
7196 /* Column where the first { of a function should be located }. */
7197 buf->b_ind_first_open = 0;
7198
7199 /* Spaces from the prevailing indent a leftmost open brace should be
7200 * located. */
7201 buf->b_ind_open_extra = 0;
7202
7203 /* Spaces from the matching open brace (real location for one at the left
7204 * edge; imaginary location from one that ends a line) the matching close
7205 * brace should be located. */
7206 buf->b_ind_close_extra = 0;
7207
7208 /* Spaces from the edge of the line an open brace sitting in the leftmost
7209 * column is imagined to be. */
7210 buf->b_ind_open_left_imag = 0;
7211
7212 /* Spaces jump labels should be shifted to the left if N is non-negative,
7213 * otherwise the jump label will be put to column 1. */
7214 buf->b_ind_jump_label = -1;
7215
7216 /* Spaces from the switch() indent a "case xx" label should be located. */
7217 buf->b_ind_case = sw;
7218
7219 /* Spaces from the "case xx:" code after a switch() should be located. */
7220 buf->b_ind_case_code = sw;
7221
7222 /* Lineup break at end of case in switch() with case label. */
7223 buf->b_ind_case_break = 0;
7224
7225 /* Spaces from the class declaration indent a scope declaration label
7226 * should be located. */
7227 buf->b_ind_scopedecl = sw;
7228
7229 /* Spaces from the scope declaration label code should be located. */
7230 buf->b_ind_scopedecl_code = sw;
7231
7232 /* Amount K&R-style parameters should be indented. */
7233 buf->b_ind_param = sw;
7234
7235 /* Amount a function type spec should be indented. */
7236 buf->b_ind_func_type = sw;
7237
7238 /* Amount a cpp base class declaration or constructor initialization
7239 * should be indented. */
7240 buf->b_ind_cpp_baseclass = sw;
7241
7242 /* additional spaces beyond the prevailing indent a continuation line
7243 * should be located. */
7244 buf->b_ind_continuation = sw;
7245
7246 /* Spaces from the indent of the line with an unclosed parentheses. */
7247 buf->b_ind_unclosed = sw * 2;
7248
7249 /* Spaces from the indent of the line with an unclosed parentheses, which
7250 * itself is also unclosed. */
7251 buf->b_ind_unclosed2 = sw;
7252
7253 /* Suppress ignoring spaces from the indent of a line starting with an
7254 * unclosed parentheses. */
7255 buf->b_ind_unclosed_noignore = 0;
7256
7257 /* If the opening paren is the last nonwhite character on the line, and
7258 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
7259 * context (for very long lines). */
7260 buf->b_ind_unclosed_wrapped = 0;
7261
7262 /* Suppress ignoring white space when lining up with the character after
7263 * an unclosed parentheses. */
7264 buf->b_ind_unclosed_whiteok = 0;
7265
7266 /* Indent a closing parentheses under the line start of the matching
7267 * opening parentheses. */
7268 buf->b_ind_matching_paren = 0;
7269
7270 /* Indent a closing parentheses under the previous line. */
7271 buf->b_ind_paren_prev = 0;
7272
7273 /* Extra indent for comments. */
7274 buf->b_ind_comment = 0;
7275
7276 /* Spaces from the comment opener when there is nothing after it. */
7277 buf->b_ind_in_comment = 3;
7278
7279 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
7280 * after the comment opener. */
7281 buf->b_ind_in_comment2 = 0;
7282
7283 /* Max lines to search for an open paren. */
7284 buf->b_ind_maxparen = 20;
7285
7286 /* Max lines to search for an open comment. */
7287 buf->b_ind_maxcomment = 70;
7288
7289 /* Handle braces for java code. */
7290 buf->b_ind_java = 0;
7291
7292 /* Not to confuse JS object properties with labels. */
7293 buf->b_ind_js = 0;
7294
7295 /* Handle blocked cases correctly. */
7296 buf->b_ind_keep_case_label = 0;
7297
7298 /* Handle C++ namespace. */
7299 buf->b_ind_cpp_namespace = 0;
7300
7301 /* Handle continuation lines containing conditions of if(), for() and
7302 * while(). */
7303 buf->b_ind_if_for_while = 0;
7304
Bram Moolenaar6b643942017-03-05 19:44:06 +01007305 /* indentation for # comments */
7306 buf->b_ind_hash_comment = 0;
7307
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007308 /* Handle C++ extern "C" or "C++" */
7309 buf->b_ind_cpp_extern_c = 0;
7310
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007311 for (p = buf->b_p_cino; *p; )
7312 {
7313 l = p++;
7314 if (*p == '-')
7315 ++p;
7316 digits = p; /* remember where the digits start */
7317 n = getdigits(&p);
7318 divider = 0;
7319 if (*p == '.') /* ".5s" means a fraction */
7320 {
7321 fraction = atol((char *)++p);
7322 while (VIM_ISDIGIT(*p))
7323 {
7324 ++p;
7325 if (divider)
7326 divider *= 10;
7327 else
7328 divider = 10;
7329 }
7330 }
7331 if (*p == 's') /* "2s" means two times 'shiftwidth' */
7332 {
7333 if (p == digits)
7334 n = sw; /* just "s" is one 'shiftwidth' */
7335 else
7336 {
7337 n *= sw;
7338 if (divider)
7339 n += (sw * fraction + divider / 2) / divider;
7340 }
7341 ++p;
7342 }
7343 if (l[1] == '-')
7344 n = -n;
7345
7346 /* When adding an entry here, also update the default 'cinoptions' in
7347 * doc/indent.txt, and add explanation for it! */
7348 switch (*l)
7349 {
7350 case '>': buf->b_ind_level = n; break;
7351 case 'e': buf->b_ind_open_imag = n; break;
7352 case 'n': buf->b_ind_no_brace = n; break;
7353 case 'f': buf->b_ind_first_open = n; break;
7354 case '{': buf->b_ind_open_extra = n; break;
7355 case '}': buf->b_ind_close_extra = n; break;
7356 case '^': buf->b_ind_open_left_imag = n; break;
7357 case 'L': buf->b_ind_jump_label = n; break;
7358 case ':': buf->b_ind_case = n; break;
7359 case '=': buf->b_ind_case_code = n; break;
7360 case 'b': buf->b_ind_case_break = n; break;
7361 case 'p': buf->b_ind_param = n; break;
7362 case 't': buf->b_ind_func_type = n; break;
7363 case '/': buf->b_ind_comment = n; break;
7364 case 'c': buf->b_ind_in_comment = n; break;
7365 case 'C': buf->b_ind_in_comment2 = n; break;
7366 case 'i': buf->b_ind_cpp_baseclass = n; break;
7367 case '+': buf->b_ind_continuation = n; break;
7368 case '(': buf->b_ind_unclosed = n; break;
7369 case 'u': buf->b_ind_unclosed2 = n; break;
7370 case 'U': buf->b_ind_unclosed_noignore = n; break;
7371 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7372 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7373 case 'm': buf->b_ind_matching_paren = n; break;
7374 case 'M': buf->b_ind_paren_prev = n; break;
7375 case ')': buf->b_ind_maxparen = n; break;
7376 case '*': buf->b_ind_maxcomment = n; break;
7377 case 'g': buf->b_ind_scopedecl = n; break;
7378 case 'h': buf->b_ind_scopedecl_code = n; break;
7379 case 'j': buf->b_ind_java = n; break;
7380 case 'J': buf->b_ind_js = n; break;
7381 case 'l': buf->b_ind_keep_case_label = n; break;
7382 case '#': buf->b_ind_hash_comment = n; break;
7383 case 'N': buf->b_ind_cpp_namespace = n; break;
7384 case 'k': buf->b_ind_if_for_while = n; break;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007385 case 'E': buf->b_ind_cpp_extern_c = n; break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007386 }
7387 if (*p == ',')
7388 ++p;
7389 }
7390}
7391
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007392/*
7393 * Return the desired indent for C code.
7394 * Return -1 if the indent should be left alone (inside a raw string).
7395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007397get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 pos_T cur_curpos;
7400 int amount;
7401 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007402 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 colnr_T col;
7404 char_u *theline;
7405 char_u *linecopy;
7406 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007407 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007409 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 pos_T our_paren_pos;
7411 char_u *start;
7412 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007413#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414#define BRACE_AT_START 2 /* '{' is at start of line */
7415#define BRACE_AT_END 3 /* '{' is at end of line */
7416 linenr_T ourscope;
7417 char_u *l;
7418 char_u *look;
7419 char_u terminated;
7420 int lookfor;
7421#define LOOKFOR_INITIAL 0
7422#define LOOKFOR_IF 1
7423#define LOOKFOR_DO 2
7424#define LOOKFOR_CASE 3
7425#define LOOKFOR_ANY 4
7426#define LOOKFOR_TERM 5
7427#define LOOKFOR_UNTERM 6
7428#define LOOKFOR_SCOPEDECL 7
7429#define LOOKFOR_NOBREAK 8
7430#define LOOKFOR_CPP_BASECLASS 9
7431#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007432#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007433#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434
7435 int whilelevel;
7436 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 int n;
7438 int iscase;
7439 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007440 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007442 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007443 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007444 int js_cur_has_key = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02007445 linenr_T raw_string_start = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007446 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007448 /* make a copy, value is changed below */
7449 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450
7451 /* remember where the cursor was when we started */
7452 cur_curpos = curwin->w_cursor;
7453
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007454 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007455 if (cur_curpos.lnum == 1)
7456 return 0;
7457
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 /* Get a copy of the current contents of the line.
7459 * This is required, because only the most recent line obtained with
7460 * ml_get is valid! */
7461 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7462 if (linecopy == NULL)
7463 return 0;
7464
7465 /*
7466 * In insert mode and the cursor is on a ')' truncate the line at the
7467 * cursor position. We don't want to line up with the matching '(' when
7468 * inserting new stuff.
7469 * For unknown reasons the cursor might be past the end of the line, thus
7470 * check for that.
7471 */
7472 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007473 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 && linecopy[curwin->w_cursor.col] == ')')
7475 linecopy[curwin->w_cursor.col] = NUL;
7476
7477 theline = skipwhite(linecopy);
7478
7479 /* move the cursor to the start of the line */
7480
7481 curwin->w_cursor.col = 0;
7482
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007483 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007484
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007486 * If we are inside a raw string don't change the indent.
7487 * Ignore a raw string inside a comment.
7488 */
7489 comment_pos = ind_find_start_comment();
7490 if (comment_pos != NULL)
7491 {
7492 /* findmatchlimit() static pos is overwritten, make a copy */
7493 tryposCopy = *comment_pos;
7494 comment_pos = &tryposCopy;
7495 }
7496 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007497 if (trypos != NULL && (comment_pos == NULL
7498 || LT_POS(*trypos, *comment_pos)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007499 {
7500 amount = -1;
7501 goto laterend;
7502 }
7503
7504 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 * #defines and so on always go at the left when included in 'cinkeys'.
7506 */
7507 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007508 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007509 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007510 goto theend;
7511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512
7513 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007514 * Is it a non-case label? Then that goes at the left margin too unless:
7515 * - JS flag is set.
7516 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007518 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007519 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 {
7521 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007522 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 }
7524
7525 /*
7526 * If we're inside a "//" comment and there is a "//" comment in a
7527 * previous line, lineup with that one.
7528 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007529 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 && (trypos = find_line_comment()) != NULL) /* XXX */
7531 {
7532 /* find how indented the line beginning the comment is */
7533 getvcol(curwin, trypos, &col, NULL, NULL);
7534 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007535 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 }
7537
7538 /*
7539 * If we're inside a comment and not looking at the start of the
7540 * comment, try using the 'comments' option.
7541 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007542 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 {
7544 int lead_start_len = 2;
7545 int lead_middle_len = 1;
7546 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7547 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7548 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7549 char_u *p;
7550 int start_align = 0;
7551 int start_off = 0;
7552 int done = FALSE;
7553
7554 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007555 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007557 *lead_start = NUL;
7558 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559
7560 p = curbuf->b_p_com;
7561 while (*p != NUL)
7562 {
7563 int align = 0;
7564 int off = 0;
7565 int what = 0;
7566
7567 while (*p != NUL && *p != ':')
7568 {
7569 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7570 what = *p++;
7571 else if (*p == COM_LEFT || *p == COM_RIGHT)
7572 align = *p++;
7573 else if (VIM_ISDIGIT(*p) || *p == '-')
7574 off = getdigits(&p);
7575 else
7576 ++p;
7577 }
7578
7579 if (*p == ':')
7580 ++p;
7581 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7582 if (what == COM_START)
7583 {
7584 STRCPY(lead_start, lead_end);
7585 lead_start_len = (int)STRLEN(lead_start);
7586 start_off = off;
7587 start_align = align;
7588 }
7589 else if (what == COM_MIDDLE)
7590 {
7591 STRCPY(lead_middle, lead_end);
7592 lead_middle_len = (int)STRLEN(lead_middle);
7593 }
7594 else if (what == COM_END)
7595 {
7596 /* If our line starts with the middle comment string, line it
7597 * up with the comment opener per the 'comments' option. */
7598 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7599 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7600 {
7601 done = TRUE;
7602 if (curwin->w_cursor.lnum > 1)
7603 {
7604 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007605 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 * the middle comment string matches in the previous
7607 * line, use the indent of that line. XXX */
7608 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7609 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7610 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7611 else if (STRNCMP(look, lead_middle,
7612 lead_middle_len) == 0)
7613 {
7614 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7615 break;
7616 }
7617 /* If the start comment string doesn't match with the
7618 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007619 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 lead_start, lead_start_len) != 0)
7621 continue;
7622 }
7623 if (start_off != 0)
7624 amount += start_off;
7625 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007626 amount += vim_strsize(lead_start)
7627 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 break;
7629 }
7630
7631 /* If our line starts with the end comment string, line it up
7632 * with the middle comment */
7633 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7634 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7635 {
7636 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7637 /* XXX */
7638 if (off != 0)
7639 amount += off;
7640 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007641 amount += vim_strsize(lead_start)
7642 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 done = TRUE;
7644 break;
7645 }
7646 }
7647 }
7648
7649 /* If our line starts with an asterisk, line up with the
7650 * asterisk in the comment opener; otherwise, line up
7651 * with the first character of the comment text.
7652 */
7653 if (done)
7654 ;
7655 else if (theline[0] == '*')
7656 amount += 1;
7657 else
7658 {
7659 /*
7660 * If we are more than one line away from the comment opener, take
7661 * the indent of the previous non-empty line. If 'cino' has "CO"
7662 * and we are just below the comment opener and there are any
7663 * white characters after it line up with the text after it;
7664 * otherwise, add the amount specified by "c" in 'cino'
7665 */
7666 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007667 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 {
7669 if (linewhite(lnum)) /* skip blank lines */
7670 continue;
7671 amount = get_indent_lnum(lnum); /* XXX */
7672 break;
7673 }
7674 if (amount == -1) /* use the comment opener */
7675 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007676 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007678 start = ml_get(comment_pos->lnum);
7679 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007681 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007683 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007685 if (curbuf->b_ind_in_comment2 || *look == NUL)
7686 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 }
7688 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007689 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 }
7691
7692 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007693 * Are we looking at a ']' that has a match?
7694 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007695 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007696 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7697 {
7698 /* align with the line containing the '['. */
7699 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007700 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007701 }
7702
7703 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 * Are we inside parentheses or braces?
7705 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007706 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007707 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007708 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 || trypos != NULL)
7710 {
7711 if (trypos != NULL && tryposBrace != NULL)
7712 {
7713 /* Both an unmatched '(' and '{' is found. Use the one which is
7714 * closer to the current cursor position, set the other to NULL. */
7715 if (trypos->lnum != tryposBrace->lnum
7716 ? trypos->lnum < tryposBrace->lnum
7717 : trypos->col < tryposBrace->col)
7718 trypos = NULL;
7719 else
7720 tryposBrace = NULL;
7721 }
7722
7723 if (trypos != NULL)
7724 {
7725 /*
7726 * If the matching paren is more than one line away, use the indent of
7727 * a previous non-empty line that matches the same paren.
7728 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007729 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007731 /* Line up with the start of the matching paren line. */
7732 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7733 }
7734 else
7735 {
7736 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007737 our_paren_pos = *trypos;
7738 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007740 l = skipwhite(ml_get(lnum));
7741 if (cin_nocode(l)) /* skip comment lines */
7742 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007743 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007744 continue; /* ignore #define, #if, etc. */
7745 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007747 /* Skip a comment or raw string. XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02007748 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007749 {
7750 lnum = trypos->lnum + 1;
7751 continue;
7752 }
7753
7754 /* XXX */
7755 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007756 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007757 && trypos->lnum == our_paren_pos.lnum
7758 && trypos->col == our_paren_pos.col)
7759 {
7760 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007762 if (theline[0] == ')')
7763 {
7764 if (our_paren_pos.lnum != lnum
7765 && cur_amount > amount)
7766 cur_amount = amount;
7767 amount = -1;
7768 }
7769 break;
7770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 }
7772 }
7773
7774 /*
7775 * Line up with line where the matching paren is. XXX
7776 * If the line starts with a '(' or the indent for unclosed
7777 * parentheses is zero, line up with the unclosed parentheses.
7778 */
7779 if (amount == -1)
7780 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007781 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007782 int is_if_for_while = 0;
7783
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007784 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007785 {
7786 /* Look for the outermost opening parenthesis on this line
7787 * and check whether it belongs to an "if", "for" or "while". */
7788
7789 pos_T cursor_save = curwin->w_cursor;
7790 pos_T outermost;
7791 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007792
7793 trypos = &our_paren_pos;
7794 do {
7795 outermost = *trypos;
7796 curwin->w_cursor.lnum = outermost.lnum;
7797 curwin->w_cursor.col = outermost.col;
7798
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007799 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007800 } while (trypos && trypos->lnum == outermost.lnum);
7801
7802 curwin->w_cursor = cursor_save;
7803
7804 line = ml_get(outermost.lnum);
7805
7806 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007807 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007808 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007809
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007810 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007811 look = skipwhite(look);
7812 if (*look == '(')
7813 {
7814 linenr_T save_lnum = curwin->w_cursor.lnum;
7815 char_u *line;
7816 int look_col;
7817
7818 /* Ignore a '(' in front of the line that has a match before
7819 * our matching '('. */
7820 curwin->w_cursor.lnum = our_paren_pos.lnum;
7821 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007822 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007823 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007824 if ((trypos = findmatchlimit(NULL, ')', 0,
7825 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007826 != NULL
7827 && trypos->lnum == our_paren_pos.lnum
7828 && trypos->col < our_paren_pos.col)
7829 ignore_paren_col = trypos->col + 1;
7830
7831 curwin->w_cursor.lnum = save_lnum;
7832 look = ml_get(our_paren_pos.lnum) + look_col;
7833 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007834 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7835 && is_if_for_while == 0)
7836 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007837 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 {
7839 /*
7840 * If we're looking at a close paren, line up right there;
7841 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007842 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 * the last nonwhite character of the line, use either the
7844 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007845 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 * lines).
7847 */
7848 if (theline[0] != ')')
7849 {
7850 cur_amount = MAXCOL;
7851 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007852 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 && cin_ends_in(l, (char_u *)"(", NULL))
7854 {
7855 /* look for opening unmatched paren, indent one level
7856 * for each additional level */
7857 n = 1;
7858 for (col = 0; col < our_paren_pos.col; ++col)
7859 {
7860 switch (l[col])
7861 {
7862 case '(':
7863 case '{': ++n;
7864 break;
7865
7866 case ')':
7867 case '}': if (n > 1)
7868 --n;
7869 break;
7870 }
7871 }
7872
7873 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007874 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007876 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 our_paren_pos.col++;
7878 else
7879 {
7880 col = our_paren_pos.col + 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01007881 while (VIM_ISWHITE(l[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 col++;
7883 if (l[col] != NUL) /* In case of trailing space */
7884 our_paren_pos.col = col;
7885 else
7886 our_paren_pos.col++;
7887 }
7888 }
7889
7890 /*
7891 * Find how indented the paren is, or the character after it
7892 * if we did the above "if".
7893 */
7894 if (our_paren_pos.col > 0)
7895 {
7896 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7897 if (cur_amount > (int)col)
7898 cur_amount = col;
7899 }
7900 }
7901
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007902 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 {
7904 /* Line up with the start of the matching paren line. */
7905 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007906 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7907 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007908 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 {
7910 if (cur_amount != MAXCOL)
7911 amount = cur_amount;
7912 }
7913 else
7914 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007915 /* Add b_ind_unclosed2 for each '(' before our matching one,
7916 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007918 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 {
7920 --our_paren_pos.col;
7921 switch (*ml_get_pos(&our_paren_pos))
7922 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007923 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 col = our_paren_pos.col;
7925 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007926 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 col = MAXCOL;
7928 break;
7929 }
7930 }
7931
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007932 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 * braces */
7934 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007935 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 else
7937 {
7938 curwin->w_cursor.lnum = our_paren_pos.lnum;
7939 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007940 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7941 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007942 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007944 {
7945 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007946 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007947 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007948 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 }
7951 /*
7952 * For a line starting with ')' use the minimum of the two
7953 * positions, to avoid giving it more indent than the previous
7954 * lines:
7955 * func_long_name( if (x
7956 * arg && yy
7957 * ) ^ not here ) ^ not here
7958 */
7959 if (cur_amount < amount)
7960 amount = cur_amount;
7961 }
7962 }
7963
7964 /* add extra indent for a comment */
7965 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007966 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 else
7969 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007970 /*
7971 * We are inside braces, there is a { before this line at the position
7972 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007973 * Make a copy of tryposBrace, it may point to pos_copy inside
7974 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007975 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007976 tryposCopy = *tryposBrace;
7977 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 ourscope = trypos->lnum;
7980 start = ml_get(ourscope);
7981
7982 /*
7983 * Now figure out how indented the line is in general.
7984 * If the brace was at the start of the line, we use that;
7985 * otherwise, check out the indentation of the line as
7986 * a whole and then add the "imaginary indent" to that.
7987 */
7988 look = skipwhite(start);
7989 if (*look == '{')
7990 {
7991 getvcol(curwin, trypos, &col, NULL, NULL);
7992 amount = col;
7993 if (*start == '{')
7994 start_brace = BRACE_IN_COL0;
7995 else
7996 start_brace = BRACE_AT_START;
7997 }
7998 else
7999 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008000 /* That opening brace might have been on a continuation
8001 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 curwin->w_cursor.lnum = ourscope;
8003
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008004 /* Position the cursor over the rightmost paren, so that
8005 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 lnum = ourscope;
8007 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008008 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
8009 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 lnum = trypos->lnum;
8011
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008012 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 * case 1: if (asdf &&
8014 * ldfd) {
8015 * }
8016 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02008017 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
8018 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02008020 else if (curbuf->b_ind_js)
8021 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008023 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024
8025 start_brace = BRACE_AT_END;
8026 }
8027
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008028 /* For Javascript check if the line starts with "key:". */
8029 if (curbuf->b_ind_js)
8030 js_cur_has_key = cin_has_js_key(theline);
8031
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008033 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 * we want to be. otherwise, add the amount of room
8035 * that an indent is supposed to be.
8036 */
8037 if (theline[0] == '}')
8038 {
8039 /*
8040 * they may want closing braces to line up with something
8041 * other than the open brace. indulge them, if so.
8042 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008043 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 }
8045 else
8046 {
8047 /*
8048 * If we're looking at an "else", try to find an "if"
8049 * to match it with.
8050 * If we're looking at a "while", try to find a "do"
8051 * to match it with.
8052 */
8053 lookfor = LOOKFOR_INITIAL;
8054 if (cin_iselse(theline))
8055 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008056 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 lookfor = LOOKFOR_DO;
8058 if (lookfor != LOOKFOR_INITIAL)
8059 {
8060 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008061 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 {
8063 amount = get_indent(); /* XXX */
8064 goto theend;
8065 }
8066 }
8067
8068 /*
8069 * We get here if we are not on an "while-of-do" or "else" (or
8070 * failed to find a matching "if").
8071 * Search backwards for something to line up with.
8072 * First set amount for when we don't find anything.
8073 */
8074
8075 /*
8076 * if the '{' is _really_ at the left margin, use the imaginary
8077 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008078 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 */
8080
8081 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
8082 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008083 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008084 lookfor_cpp_namespace = TRUE;
8085 }
8086 else if (start_brace == BRACE_AT_START &&
8087 lookfor_cpp_namespace) /* '{' is at start */
8088 {
8089
8090 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 }
8092 else
8093 {
8094 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008095 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008096 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008097
8098 l = skipwhite(ml_get_curline());
8099 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008100 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008101 else if (cin_is_cpp_extern_c(l))
8102 amount += curbuf->b_ind_cpp_extern_c;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 else
8105 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008106 /* Compensate for adding b_ind_open_extra later. */
8107 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 if (amount < 0)
8109 amount = 0;
8110 }
8111 }
8112
8113 lookfor_break = FALSE;
8114
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008115 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 {
8117 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008118 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 }
8120 else if (cin_isscopedecl(theline)) /* private:, ... */
8121 {
8122 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008123 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 }
8125 else
8126 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008127 if (curbuf->b_ind_case_break && cin_isbreak(theline))
8128 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 lookfor_break = TRUE;
8130
8131 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008132 /* b_ind_level from start of block */
8133 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 }
8135 scope_amount = amount;
8136 whilelevel = 0;
8137
8138 /*
8139 * Search backwards. If we find something we recognize, line up
8140 * with that.
8141 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008142 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 * the usual amount relative to the conditional
8144 * that opens the block.
8145 */
8146 curwin->w_cursor = cur_curpos;
8147 for (;;)
8148 {
8149 curwin->w_cursor.lnum--;
8150 curwin->w_cursor.col = 0;
8151
8152 /*
8153 * If we went all the way back to the start of our scope, line
8154 * up with it.
8155 */
8156 if (curwin->w_cursor.lnum <= ourscope)
8157 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008158 /* We reached end of scope:
8159 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008161 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 * don't add ind_continuation, otherwise it is a variable
8163 * declaration:
8164 * int x,
8165 * here; <-- add ind_continuation
8166 */
8167 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8168 {
8169 if (curwin->w_cursor.lnum == 0
8170 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008171 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008173 /* nothing found (abuse curbuf->b_ind_maxparen as
8174 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 * initialization) */
8176 if (cont_amount > 0)
8177 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008178 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 amount += ind_continuation;
8180 break;
8181 }
8182
8183 l = ml_get_curline();
8184
8185 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008186 * If we're in a comment or raw string now, skip to
8187 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 */
Bram Moolenaardde81312017-08-26 17:49:01 +02008189 trypos = ind_find_start_CORS(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 if (trypos != NULL)
8191 {
8192 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008193 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 continue;
8195 }
8196
8197 /*
8198 * Skip preprocessor directives and blank lines.
8199 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008200 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8201 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 continue;
8203
8204 if (cin_nocode(l))
8205 continue;
8206
8207 terminated = cin_isterminated(l, FALSE, TRUE);
8208
8209 /*
8210 * If we are at top level and the line looks like a
8211 * function declaration, we are done
8212 * (it's a variable declaration).
8213 */
8214 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008215 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 {
8217 /* if the line is terminated with another ','
8218 * it is a continued variable initialization.
8219 * don't add extra indent.
8220 * TODO: does not work, if a function
8221 * declaration is split over multiple lines:
8222 * cin_isfuncdecl returns FALSE then.
8223 */
8224 if (terminated == ',')
8225 break;
8226
8227 /* if it es a enum declaration or an assignment,
8228 * we are done.
8229 */
8230 if (terminated != ';' && cin_isinit())
8231 break;
8232
8233 /* nothing useful found */
8234 if (terminated == 0 || terminated == '{')
8235 continue;
8236 }
8237
8238 if (terminated != ';')
8239 {
8240 /* Skip parens and braces. Position the cursor
8241 * over the rightmost paren, so that matching it
8242 * will take us back to the start of the line.
8243 */ /* XXX */
8244 trypos = NULL;
8245 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008246 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008247 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248
8249 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008250 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251
8252 if (trypos != NULL)
8253 {
8254 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008255 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 continue;
8257 }
8258 }
8259
8260 /* it's a variable declaration, add indentation
8261 * like in
8262 * int a,
8263 * b;
8264 */
8265 if (cont_amount > 0)
8266 amount = cont_amount;
8267 else
8268 amount += ind_continuation;
8269 }
8270 else if (lookfor == LOOKFOR_UNTERM)
8271 {
8272 if (cont_amount > 0)
8273 amount = cont_amount;
8274 else
8275 amount += ind_continuation;
8276 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008277 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008278 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02008279 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008280 && lookfor != LOOKFOR_CPP_BASECLASS
8281 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02008282 {
8283 amount = scope_amount;
8284 if (theline[0] == '{')
8285 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008286 amount += curbuf->b_ind_open_extra;
8287 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008288 }
8289 }
8290
8291 if (lookfor_cpp_namespace)
8292 {
8293 /*
8294 * Looking for C++ namespace, need to look further
8295 * back.
8296 */
8297 if (curwin->w_cursor.lnum == ourscope)
8298 continue;
8299
8300 if (curwin->w_cursor.lnum == 0
8301 || curwin->w_cursor.lnum
8302 < ourscope - FIND_NAMESPACE_LIM)
8303 break;
8304
8305 l = ml_get_curline();
8306
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008307 /* If we're in a comment or raw string now, skip
8308 * to the start of it. */
Bram Moolenaardde81312017-08-26 17:49:01 +02008309 trypos = ind_find_start_CORS(NULL);
Bram Moolenaare79d1532011-10-04 18:03:47 +02008310 if (trypos != NULL)
8311 {
8312 curwin->w_cursor.lnum = trypos->lnum + 1;
8313 curwin->w_cursor.col = 0;
8314 continue;
8315 }
8316
8317 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008318 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8319 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02008320 continue;
8321
8322 /* Finally the actual check for "namespace". */
8323 if (cin_is_cpp_namespace(l))
8324 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008325 amount += curbuf->b_ind_cpp_namespace
8326 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008327 break;
8328 }
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008329 else if (cin_is_cpp_extern_c(l))
8330 {
8331 amount += curbuf->b_ind_cpp_extern_c
8332 - added_to_amount;
8333 break;
8334 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008335
8336 if (cin_nocode(l))
8337 continue;
8338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 }
8340 break;
8341 }
8342
8343 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008344 * If we're in a comment or raw string now, skip to the start
8345 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008347 if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 {
8349 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008350 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 continue;
8352 }
8353
8354 l = ml_get_curline();
8355
8356 /*
8357 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008358 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008360 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 if (iscase || cin_isscopedecl(l))
8362 {
8363 /* we are only looking for cpp base class
8364 * declaration/initialization any longer */
8365 if (lookfor == LOOKFOR_CPP_BASECLASS)
8366 break;
8367
8368 /* When looking for a "do" we are not interested in
8369 * labels. */
8370 if (whilelevel > 0)
8371 continue;
8372
8373 /*
8374 * case xx:
8375 * c = 99 + <- this indent plus continuation
8376 *-> here;
8377 */
8378 if (lookfor == LOOKFOR_UNTERM
8379 || lookfor == LOOKFOR_ENUM_OR_INIT)
8380 {
8381 if (cont_amount > 0)
8382 amount = cont_amount;
8383 else
8384 amount += ind_continuation;
8385 break;
8386 }
8387
8388 /*
8389 * case xx: <- line up with this case
8390 * x = 333;
8391 * case yy:
8392 */
8393 if ( (iscase && lookfor == LOOKFOR_CASE)
8394 || (iscase && lookfor_break)
8395 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8396 {
8397 /*
8398 * Check that this case label is not for another
8399 * switch()
8400 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008401 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008402 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 {
8404 amount = get_indent(); /* XXX */
8405 break;
8406 }
8407 continue;
8408 }
8409
8410 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8411
8412 /*
8413 * case xx: if (cond) <- line up with this if
8414 * y = y + 1;
8415 * -> s = 99;
8416 *
8417 * case xx:
8418 * if (cond) <- line up with this line
8419 * y = y + 1;
8420 * -> s = 99;
8421 */
8422 if (lookfor == LOOKFOR_TERM)
8423 {
8424 if (n)
8425 amount = n;
8426
8427 if (!lookfor_break)
8428 break;
8429 }
8430
8431 /*
8432 * case xx: x = x + 1; <- line up with this x
8433 * -> y = y + 1;
8434 *
8435 * case xx: if (cond) <- line up with this if
8436 * -> y = y + 1;
8437 */
8438 if (n)
8439 {
8440 amount = n;
8441 l = after_label(ml_get_curline());
8442 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008443 {
8444 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008445 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008446 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008447 amount += curbuf->b_ind_level
8448 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 break;
8451 }
8452
8453 /*
8454 * Try to get the indent of a statement before the switch
8455 * label. If nothing is found, line up relative to the
8456 * switch label.
8457 * break; <- may line up with this line
8458 * case xx:
8459 * -> y = 1;
8460 */
8461 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008462 ? curbuf->b_ind_case_code
8463 : curbuf->b_ind_scopedecl_code);
8464 lookfor = curbuf->b_ind_case_break
8465 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 continue;
8467 }
8468
8469 /*
8470 * Looking for a switch() label or C++ scope declaration,
8471 * ignore other lines, skip {}-blocks.
8472 */
8473 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8474 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008475 if (find_last_paren(l, '{', '}')
8476 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008477 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008479 curwin->w_cursor.col = 0;
8480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 continue;
8482 }
8483
8484 /*
8485 * Ignore jump labels with nothing after them.
8486 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008487 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 {
8489 l = after_label(ml_get_curline());
8490 if (l == NULL || cin_nocode(l))
8491 continue;
8492 }
8493
8494 /*
8495 * Ignore #defines, #if, etc.
8496 * Ignore comment and empty lines.
8497 * (need to get the line again, cin_islabel() may have
8498 * unlocked it)
8499 */
8500 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008501 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 || cin_nocode(l))
8503 continue;
8504
8505 /*
8506 * Are we at the start of a cpp base class declaration or
8507 * constructor initialization?
8508 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008509 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008510 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008511 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008512 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008513 l = ml_get_curline();
8514 }
8515 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 {
8517 if (lookfor == LOOKFOR_UNTERM)
8518 {
8519 if (cont_amount > 0)
8520 amount = cont_amount;
8521 else
8522 amount += ind_continuation;
8523 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008524 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008526 /* Need to find start of the declaration. */
8527 lookfor = LOOKFOR_UNTERM;
8528 ind_continuation = 0;
8529 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 }
8531 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008532 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008533 amount = get_baseclass_amount(
8534 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 break;
8536 }
8537 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8538 {
8539 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008540 * declaration or initialization before the opening brace.
8541 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 if (cin_isterminated(l, TRUE, FALSE))
8543 break;
8544 else
8545 continue;
8546 }
8547
8548 /*
8549 * What happens next depends on the line being terminated.
8550 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008551 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 * 123,
8553 * sizeof
8554 * here
8555 * Otherwise check whether it is a enumeration or structure
8556 * initialisation (not indented) or a variable declaration
8557 * (indented).
8558 */
8559 terminated = cin_isterminated(l, FALSE, TRUE);
8560
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008561 if (js_cur_has_key)
8562 {
8563 js_cur_has_key = 0; /* only check the first line */
8564 if (curbuf->b_ind_js && terminated == ',')
8565 {
8566 /* For Javascript we might be inside an object:
8567 * key: something, <- align with this
8568 * key: something
8569 * or:
8570 * key: something + <- align with this
8571 * something,
8572 * key: something
8573 */
8574 lookfor = LOOKFOR_JS_KEY;
8575 }
8576 }
8577 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8578 {
8579 amount = get_indent();
8580 break;
8581 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008582 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008583 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008584 if (tryposBrace != NULL && tryposBrace->lnum
8585 >= curwin->w_cursor.lnum)
8586 break;
8587 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008588 /* line below current line is the one that starts a
8589 * (possibly broken) line ending in a comma. */
8590 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008591 else
8592 {
8593 amount = get_indent();
8594 if (curwin->w_cursor.lnum - 1 == ourscope)
8595 /* line above is start of the scope, thus current
8596 * line is the one that stars a (possibly broken)
8597 * line ending in a comma. */
8598 break;
8599 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008600 }
8601
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8603 && terminated == ','))
8604 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008605 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8606 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008607 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 /*
8609 * if we're in the middle of a paren thing,
8610 * go back to the line that starts it so
8611 * we can get the right prevailing indent
8612 * if ( foo &&
8613 * bar )
8614 */
8615 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008616 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008618 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 */
8620 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008621 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008622 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8623 || (trypos->lnum == tryposBrace->lnum
8624 && trypos->col < tryposBrace->col)))
8625 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626
8627 /*
8628 * If we are looking for ',', we also look for matching
8629 * braces.
8630 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008631 if (trypos == NULL && terminated == ','
8632 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008633 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634
8635 if (trypos != NULL)
8636 {
8637 /*
8638 * Check if we are on a case label now. This is
8639 * handled above.
8640 * case xx: if ( asdf &&
8641 * asdf)
8642 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008643 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008645 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 {
8647 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008648 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 continue;
8650 }
8651 }
8652
8653 /*
8654 * Skip over continuation lines to find the one to get the
8655 * indent from
8656 * char *usethis = "bla\
8657 * bla",
8658 * here;
8659 */
8660 if (terminated == ',')
8661 {
8662 while (curwin->w_cursor.lnum > 1)
8663 {
8664 l = ml_get(curwin->w_cursor.lnum - 1);
8665 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8666 break;
8667 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008668 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 }
8670 }
8671
8672 /*
8673 * Get indent and pointer to text for current line,
8674 * ignoring any jump label. XXX
8675 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008676 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008677 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008678 else
8679 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 /*
8681 * If this is just above the line we are indenting, and it
8682 * starts with a '{', line it up with this line.
8683 * while (not)
8684 * -> {
8685 * }
8686 */
8687 if (terminated != ',' && lookfor != LOOKFOR_TERM
8688 && theline[0] == '{')
8689 {
8690 amount = cur_amount;
8691 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008692 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 * doesn't start with a '{', which must have a match
8694 * in the same line (scope is the same). Probably:
8695 * { 1, 2 },
8696 * -> { 3, 4 }
8697 */
8698 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008699 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008701 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 {
8703 /* have to look back, whether it is a cpp base
8704 * class declaration or initialization */
8705 lookfor = LOOKFOR_CPP_BASECLASS;
8706 continue;
8707 }
8708 break;
8709 }
8710
8711 /*
8712 * Check if we are after an "if", "while", etc.
8713 * Also allow " } else".
8714 */
8715 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8716 {
8717 /*
8718 * Found an unterminated line after an if (), line up
8719 * with the last one.
8720 * if (cond)
8721 * 100 +
8722 * -> here;
8723 */
8724 if (lookfor == LOOKFOR_UNTERM
8725 || lookfor == LOOKFOR_ENUM_OR_INIT)
8726 {
8727 if (cont_amount > 0)
8728 amount = cont_amount;
8729 else
8730 amount += ind_continuation;
8731 break;
8732 }
8733
8734 /*
8735 * If this is just above the line we are indenting, we
8736 * are finished.
8737 * while (not)
8738 * -> here;
8739 * Otherwise this indent can be used when the line
8740 * before this is terminated.
8741 * yyy;
8742 * if (stat)
8743 * while (not)
8744 * xxx;
8745 * -> here;
8746 */
8747 amount = cur_amount;
8748 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008749 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 if (lookfor != LOOKFOR_TERM)
8751 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008752 amount += curbuf->b_ind_level
8753 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 break;
8755 }
8756
8757 /*
8758 * Special trick: when expecting the while () after a
8759 * do, line up with the while()
8760 * do
8761 * x = 1;
8762 * -> here
8763 */
8764 l = skipwhite(ml_get_curline());
8765 if (cin_isdo(l))
8766 {
8767 if (whilelevel == 0)
8768 break;
8769 --whilelevel;
8770 }
8771
8772 /*
8773 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008774 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 * Need to use the scope of this "else". XXX
8776 * If whilelevel != 0 continue looking for a "do {".
8777 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008778 if (cin_iselse(l) && whilelevel == 0)
8779 {
8780 /* If we're looking at "} else", let's make sure we
8781 * find the opening brace of the enclosing scope,
8782 * not the one from "if () {". */
8783 if (*l == '}')
8784 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008785 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008786
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008787 if ((trypos = find_start_brace()) == NULL
8788 || find_match(LOOKFOR_IF, trypos->lnum)
8789 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008790 break;
8791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 }
8793
8794 /*
8795 * If we're below an unterminated line that is not an
8796 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008797 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 * the line before this one.
8799 */
8800 else
8801 {
8802 /*
8803 * Found two unterminated lines on a row, line up with
8804 * the last one.
8805 * c = 99 +
8806 * 100 +
8807 * -> here;
8808 */
8809 if (lookfor == LOOKFOR_UNTERM)
8810 {
8811 /* When line ends in a comma add extra indent */
8812 if (terminated == ',')
8813 amount += ind_continuation;
8814 break;
8815 }
8816
8817 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8818 {
8819 /* Found two lines ending in ',', lineup with the
8820 * lowest one, but check for cpp base class
8821 * declaration/initialization, if it is an
8822 * opening brace or we are looking just for
8823 * enumerations/initializations. */
8824 if (terminated == ',')
8825 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008826 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 break;
8828
8829 lookfor = LOOKFOR_CPP_BASECLASS;
8830 continue;
8831 }
8832
8833 /* Ignore unterminated lines in between, but
8834 * reduce indent. */
8835 if (amount > cur_amount)
8836 amount = cur_amount;
8837 }
8838 else
8839 {
8840 /*
8841 * Found first unterminated line on a row, may
8842 * line up with this line, remember its indent
8843 * 100 +
8844 * -> here;
8845 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008846 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008848
8849 n = (int)STRLEN(l);
8850 if (terminated == ',' && (*skipwhite(l) == ']'
8851 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008852 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853
8854 /*
8855 * If previous line ends in ',', check whether we
8856 * are in an initialization or enum
8857 * struct xxx =
8858 * {
8859 * sizeof a,
8860 * 124 };
8861 * or a normal possible continuation line.
8862 * but only, of no other statement has been found
8863 * yet.
8864 */
8865 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8866 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008867 if (curbuf->b_ind_js)
8868 {
8869 /* Search for a line ending in a comma
8870 * and line up with the line below it
8871 * (could be the current line).
8872 * some = [
8873 * 1, <- line up here
8874 * 2,
8875 * some = [
8876 * 3 + <- line up here
8877 * 4 *
8878 * 5,
8879 * 6,
8880 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008881 if (cin_iscomment(skipwhite(l)))
8882 break;
8883 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008884 trypos = find_match_char('[',
8885 curbuf->b_ind_maxparen);
8886 if (trypos != NULL)
8887 {
8888 if (trypos->lnum
8889 == curwin->w_cursor.lnum - 1)
8890 {
8891 /* Current line is first inside
8892 * [], line up with it. */
8893 break;
8894 }
8895 ourscope = trypos->lnum;
8896 }
8897 }
8898 else
8899 {
8900 lookfor = LOOKFOR_ENUM_OR_INIT;
8901 cont_amount = cin_first_id_amount();
8902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 }
8904 else
8905 {
8906 if (lookfor == LOOKFOR_INITIAL
8907 && *l != NUL
8908 && l[STRLEN(l) - 1] == '\\')
8909 /* XXX */
8910 cont_amount = cin_get_equal_amount(
8911 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008912 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008913 && lookfor != LOOKFOR_JS_KEY
Bram Moolenaardde81312017-08-26 17:49:01 +02008914 && lookfor != LOOKFOR_COMMA
8915 && raw_string_start != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 lookfor = LOOKFOR_UNTERM;
8917 }
8918 }
8919 }
8920 }
8921
8922 /*
8923 * Check if we are after a while (cond);
8924 * If so: Ignore until the matching "do".
8925 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008926 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 {
8928 /*
8929 * Found an unterminated line after a while ();, line up
8930 * with the last one.
8931 * while (cond);
8932 * 100 + <- line up with this one
8933 * -> here;
8934 */
8935 if (lookfor == LOOKFOR_UNTERM
8936 || lookfor == LOOKFOR_ENUM_OR_INIT)
8937 {
8938 if (cont_amount > 0)
8939 amount = cont_amount;
8940 else
8941 amount += ind_continuation;
8942 break;
8943 }
8944
8945 if (whilelevel == 0)
8946 {
8947 lookfor = LOOKFOR_TERM;
8948 amount = get_indent(); /* XXX */
8949 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008950 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 }
8952 ++whilelevel;
8953 }
8954
8955 /*
8956 * We are after a "normal" statement.
8957 * If we had another statement we can stop now and use the
8958 * indent of that other statement.
8959 * Otherwise the indent of the current statement may be used,
8960 * search backwards for the next "normal" statement.
8961 */
8962 else
8963 {
8964 /*
8965 * Skip single break line, if before a switch label. It
8966 * may be lined up with the case label.
8967 */
8968 if (lookfor == LOOKFOR_NOBREAK
8969 && cin_isbreak(skipwhite(ml_get_curline())))
8970 {
8971 lookfor = LOOKFOR_ANY;
8972 continue;
8973 }
8974
8975 /*
8976 * Handle "do {" line.
8977 */
8978 if (whilelevel > 0)
8979 {
8980 l = cin_skipcomment(ml_get_curline());
8981 if (cin_isdo(l))
8982 {
8983 amount = get_indent(); /* XXX */
8984 --whilelevel;
8985 continue;
8986 }
8987 }
8988
8989 /*
8990 * Found a terminated line above an unterminated line. Add
8991 * the amount for a continuation line.
8992 * x = 1;
8993 * y = foo +
8994 * -> here;
8995 * or
8996 * int x = 1;
8997 * int foo,
8998 * -> here;
8999 */
9000 if (lookfor == LOOKFOR_UNTERM
9001 || lookfor == LOOKFOR_ENUM_OR_INIT)
9002 {
9003 if (cont_amount > 0)
9004 amount = cont_amount;
9005 else
9006 amount += ind_continuation;
9007 break;
9008 }
9009
9010 /*
9011 * Found a terminated line above a terminated line or "if"
9012 * etc. line. Use the amount of the line below us.
9013 * x = 1; x = 1;
9014 * if (asdf) y = 2;
9015 * while (asdf) ->here;
9016 * here;
9017 * ->foo;
9018 */
9019 if (lookfor == LOOKFOR_TERM)
9020 {
9021 if (!lookfor_break && whilelevel == 0)
9022 break;
9023 }
9024
9025 /*
9026 * First line above the one we're indenting is terminated.
9027 * To know what needs to be done look further backward for
9028 * a terminated line.
9029 */
9030 else
9031 {
9032 /*
9033 * position the cursor over the rightmost paren, so
9034 * that matching it will take us back to the start of
9035 * the line. Helps for:
9036 * func(asdr,
9037 * asdfasdf);
9038 * here;
9039 */
9040term_again:
9041 l = ml_get_curline();
9042 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009043 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009044 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 {
9046 /*
9047 * Check if we are on a case label now. This is
9048 * handled above.
9049 * case xx: if ( asdf &&
9050 * asdf)
9051 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009052 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02009054 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 {
9056 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009057 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 continue;
9059 }
9060 }
9061
9062 /* When aligning with the case statement, don't align
9063 * with a statement after it.
9064 * case 1: { <-- don't use this { position
9065 * stat;
9066 * }
9067 * case 2:
9068 * stat;
9069 * }
9070 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009071 iscase = (curbuf->b_ind_keep_case_label
9072 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073
9074 /*
9075 * Get indent and pointer to text for current line,
9076 * ignoring any jump label.
9077 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009078 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079
9080 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009081 amount += curbuf->b_ind_open_extra;
9082 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00009083 l = skipwhite(l);
9084 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009085 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
9087
9088 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009089 * When a terminated line starts with "else" skip to
9090 * the matching "if":
9091 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009092 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009093 * Need to use the scope of this "else". XXX
9094 * If whilelevel != 0 continue looking for a "do {".
9095 */
9096 if (lookfor == LOOKFOR_TERM
9097 && *l != '}'
9098 && cin_iselse(l)
9099 && whilelevel == 0)
9100 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009101 if ((trypos = find_start_brace()) == NULL
9102 || find_match(LOOKFOR_IF, trypos->lnum)
9103 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009104 break;
9105 continue;
9106 }
9107
9108 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 * If we're at the end of a block, skip to the start of
9110 * that block.
9111 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01009112 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009113 if (find_last_paren(l, '{', '}') /* XXX */
9114 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009116 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 /* if not "else {" check for terminated again */
9118 /* but skip block for "} else {" */
9119 l = cin_skipcomment(ml_get_curline());
9120 if (*l == '}' || !cin_iselse(l))
9121 goto term_again;
9122 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00009123 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 }
9125 }
9126 }
9127 }
9128 }
9129 }
9130
9131 /* add extra indent for a comment */
9132 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009133 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02009134
9135 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009136 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
9137 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009138
9139 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009141
9142 /*
9143 * ok -- we're not inside any sort of structure at all!
9144 *
9145 * This means we're at the top level, and everything should
9146 * basically just match where the previous line is, except
9147 * for the lines immediately following a function declaration,
9148 * which are K&R-style parameters and need to be indented.
9149 *
9150 * if our line starts with an open brace, forget about any
9151 * prevailing indent and make sure it looks like the start
9152 * of a function
9153 */
9154
9155 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009157 amount = curbuf->b_ind_first_open;
9158 goto theend;
9159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009161 /*
9162 * If the NEXT line is a function declaration, the current
9163 * line needs to be indented as a function type spec.
9164 * Don't do this if the current line looks like a comment or if the
9165 * current line is terminated, ie. ends in ';', or if the current line
9166 * contains { or }: "void f() {\n if (1)"
9167 */
9168 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
9169 && !cin_nocode(theline)
9170 && vim_strchr(theline, '{') == NULL
9171 && vim_strchr(theline, '}') == NULL
9172 && !cin_ends_in(theline, (char_u *)":", NULL)
9173 && !cin_ends_in(theline, (char_u *)",", NULL)
9174 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
9175 cur_curpos.lnum + 1)
9176 && !cin_isterminated(theline, FALSE, TRUE))
9177 {
9178 amount = curbuf->b_ind_func_type;
9179 goto theend;
9180 }
9181
9182 /* search backwards until we find something we recognize */
9183 amount = 0;
9184 curwin->w_cursor = cur_curpos;
9185 while (curwin->w_cursor.lnum > 1)
9186 {
9187 curwin->w_cursor.lnum--;
9188 curwin->w_cursor.col = 0;
9189
9190 l = ml_get_curline();
9191
9192 /*
9193 * If we're in a comment or raw string now, skip to the start
9194 * of it.
9195 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02009196 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009198 curwin->w_cursor.lnum = trypos->lnum + 1;
9199 curwin->w_cursor.col = 0;
9200 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 }
9202
9203 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009204 * Are we at the start of a cpp base class declaration or
9205 * constructor initialization?
9206 */ /* XXX */
9207 n = FALSE;
9208 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009210 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
9211 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009213 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009215 /* XXX */
9216 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
9217 break;
9218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009220 /*
9221 * Skip preprocessor directives and blank lines.
9222 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009223 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009224 continue;
9225
9226 if (cin_nocode(l))
9227 continue;
9228
9229 /*
9230 * If the previous line ends in ',', use one level of
9231 * indentation:
9232 * int foo,
9233 * bar;
9234 * do this before checking for '}' in case of eg.
9235 * enum foobar
9236 * {
9237 * ...
9238 * } foo,
9239 * bar;
9240 */
9241 n = 0;
9242 if (cin_ends_in(l, (char_u *)",", NULL)
9243 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
9244 {
9245 /* take us back to opening paren */
9246 if (find_last_paren(l, '(', ')')
9247 && (trypos = find_match_paren(
9248 curbuf->b_ind_maxparen)) != NULL)
9249 curwin->w_cursor = *trypos;
9250
9251 /* For a line ending in ',' that is a continuation line go
9252 * back to the first line with a backslash:
9253 * char *foo = "bla\
9254 * bla",
9255 * here;
9256 */
9257 while (n == 0 && curwin->w_cursor.lnum > 1)
9258 {
9259 l = ml_get(curwin->w_cursor.lnum - 1);
9260 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
9261 break;
9262 --curwin->w_cursor.lnum;
9263 curwin->w_cursor.col = 0;
9264 }
9265
9266 amount = get_indent(); /* XXX */
9267
9268 if (amount == 0)
9269 amount = cin_first_id_amount();
9270 if (amount == 0)
9271 amount = ind_continuation;
9272 break;
9273 }
9274
9275 /*
9276 * If the line looks like a function declaration, and we're
9277 * not in a comment, put it the left margin.
9278 */
9279 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
9280 break;
9281 l = ml_get_curline();
9282
9283 /*
9284 * Finding the closing '}' of a previous function. Put
9285 * current line at the left margin. For when 'cino' has "fs".
9286 */
9287 if (*skipwhite(l) == '}')
9288 break;
9289
9290 /* (matching {)
9291 * If the previous line ends on '};' (maybe followed by
9292 * comments) align at column 0. For example:
9293 * char *string_array[] = { "foo",
9294 * / * x * / "b};ar" }; / * foobar * /
9295 */
9296 if (cin_ends_in(l, (char_u *)"};", NULL))
9297 break;
9298
9299 /*
9300 * If the previous line ends on '[' we are probably in an
9301 * array constant:
9302 * something = [
9303 * 234, <- extra indent
9304 */
9305 if (cin_ends_in(l, (char_u *)"[", NULL))
9306 {
9307 amount = get_indent() + ind_continuation;
9308 break;
9309 }
9310
9311 /*
9312 * Find a line only has a semicolon that belongs to a previous
9313 * line ending in '}', e.g. before an #endif. Don't increase
9314 * indent then.
9315 */
9316 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
9317 {
9318 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319
9320 while (curwin->w_cursor.lnum > 1)
9321 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009322 look = ml_get(--curwin->w_cursor.lnum);
9323 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009324 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009326 }
9327 if (curwin->w_cursor.lnum > 0
9328 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009331 curwin->w_cursor = curpos_save;
9332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009334 /*
9335 * If the PREVIOUS line is a function declaration, the current
9336 * line (and the ones that follow) needs to be indented as
9337 * parameters.
9338 */
9339 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
9340 {
9341 amount = curbuf->b_ind_param;
9342 break;
9343 }
9344
9345 /*
9346 * If the previous line ends in ';' and the line before the
9347 * previous line ends in ',' or '\', ident to column zero:
9348 * int foo,
9349 * bar;
9350 * indent_to_0 here;
9351 */
9352 if (cin_ends_in(l, (char_u *)";", NULL))
9353 {
9354 l = ml_get(curwin->w_cursor.lnum - 1);
9355 if (cin_ends_in(l, (char_u *)",", NULL)
9356 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9357 break;
9358 l = ml_get_curline();
9359 }
9360
9361 /*
9362 * Doesn't look like anything interesting -- so just
9363 * use the indent of this line.
9364 *
9365 * Position the cursor over the rightmost paren, so that
9366 * matching it will take us back to the start of the line.
9367 */
9368 find_last_paren(l, '(', ')');
9369
9370 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9371 curwin->w_cursor = *trypos;
9372 amount = get_indent(); /* XXX */
9373 break;
9374 }
9375
9376 /* add extra indent for a comment */
9377 if (cin_iscomment(theline))
9378 amount += curbuf->b_ind_comment;
9379
9380 /* add extra indent if the previous line ended in a backslash:
9381 * "asdfasdf\
9382 * here";
9383 * char *foo = "asdf\
9384 * here";
9385 */
9386 if (cur_curpos.lnum > 1)
9387 {
9388 l = ml_get(cur_curpos.lnum - 1);
9389 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9390 {
9391 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9392 if (cur_amount > 0)
9393 amount = cur_amount;
9394 else if (cur_amount == 0)
9395 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 }
9397 }
9398
9399theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009400 if (amount < 0)
9401 amount = 0;
9402
9403laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 /* put the cursor back where it belongs */
9405 curwin->w_cursor = cur_curpos;
9406
9407 vim_free(linecopy);
9408
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 return amount;
9410}
9411
9412 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009413find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414{
9415 char_u *look;
9416 pos_T *theirscope;
9417 char_u *mightbeif;
9418 int elselevel;
9419 int whilelevel;
9420
9421 if (lookfor == LOOKFOR_IF)
9422 {
9423 elselevel = 1;
9424 whilelevel = 0;
9425 }
9426 else
9427 {
9428 elselevel = 0;
9429 whilelevel = 1;
9430 }
9431
9432 curwin->w_cursor.col = 0;
9433
9434 while (curwin->w_cursor.lnum > ourscope + 1)
9435 {
9436 curwin->w_cursor.lnum--;
9437 curwin->w_cursor.col = 0;
9438
9439 look = cin_skipcomment(ml_get_curline());
9440 if (cin_iselse(look)
9441 || cin_isif(look)
9442 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009443 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444 {
9445 /*
9446 * if we've gone outside the braces entirely,
9447 * we must be out of scope...
9448 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009449 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 if (theirscope == NULL)
9451 break;
9452
9453 /*
9454 * and if the brace enclosing this is further
9455 * back than the one enclosing the else, we're
9456 * out of luck too.
9457 */
9458 if (theirscope->lnum < ourscope)
9459 break;
9460
9461 /*
9462 * and if they're enclosed in a *deeper* brace,
9463 * then we can ignore it because it's in a
9464 * different scope...
9465 */
9466 if (theirscope->lnum > ourscope)
9467 continue;
9468
9469 /*
9470 * if it was an "else" (that's not an "else if")
9471 * then we need to go back to another if, so
9472 * increment elselevel
9473 */
9474 look = cin_skipcomment(ml_get_curline());
9475 if (cin_iselse(look))
9476 {
9477 mightbeif = cin_skipcomment(look + 4);
9478 if (!cin_isif(mightbeif))
9479 ++elselevel;
9480 continue;
9481 }
9482
9483 /*
9484 * if it was a "while" then we need to go back to
9485 * another "do", so increment whilelevel. XXX
9486 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009487 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 {
9489 ++whilelevel;
9490 continue;
9491 }
9492
9493 /* If it's an "if" decrement elselevel */
9494 look = cin_skipcomment(ml_get_curline());
9495 if (cin_isif(look))
9496 {
9497 elselevel--;
9498 /*
9499 * When looking for an "if" ignore "while"s that
9500 * get in the way.
9501 */
9502 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9503 whilelevel = 0;
9504 }
9505
9506 /* If it's a "do" decrement whilelevel */
9507 if (cin_isdo(look))
9508 whilelevel--;
9509
9510 /*
9511 * if we've used up all the elses, then
9512 * this must be the if that we want!
9513 * match the indent level of that if.
9514 */
9515 if (elselevel <= 0 && whilelevel <= 0)
9516 {
9517 return OK;
9518 }
9519 }
9520 }
9521 return FAIL;
9522}
9523
9524# if defined(FEAT_EVAL) || defined(PROTO)
9525/*
9526 * Get indent level from 'indentexpr'.
9527 */
9528 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009529get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530{
Bram Moolenaar97db5542017-04-21 23:18:26 +02009531 int indent = -1;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009532 char_u *inde_copy;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009533 pos_T save_pos;
9534 colnr_T save_curswant;
9535 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009537 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9538 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009540 /* Save and restore cursor position and curswant, in case it was changed
9541 * via :normal commands */
9542 save_pos = curwin->w_cursor;
9543 save_curswant = curwin->w_curswant;
9544 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009546 if (use_sandbox)
9547 ++sandbox;
9548 ++textlock;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009549
9550 /* Need to make a copy, the 'indentexpr' option could be changed while
9551 * evaluating it. */
9552 inde_copy = vim_strsave(curbuf->b_p_inde);
9553 if (inde_copy != NULL)
9554 {
9555 indent = (int)eval_to_number(inde_copy);
9556 vim_free(inde_copy);
9557 }
9558
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009559 if (use_sandbox)
9560 --sandbox;
9561 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562
9563 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9564 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9565 * command. */
9566 save_State = State;
9567 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009568 curwin->w_cursor = save_pos;
9569 curwin->w_curswant = save_curswant;
9570 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 check_cursor();
9572 State = save_State;
9573
9574 /* If there is an error, just keep the current indent. */
9575 if (indent < 0)
9576 indent = get_indent();
9577
9578 return indent;
9579}
9580# endif
9581
9582#endif /* FEAT_CINDENT */
9583
9584#if defined(FEAT_LISP) || defined(PROTO)
9585
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009587lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588{
9589 char_u buf[LSIZE];
9590 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009591 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592
9593 while (*word != NUL)
9594 {
9595 (void)copy_option_part(&word, buf, LSIZE, ",");
9596 len = (int)STRLEN(buf);
9597 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9598 return TRUE;
9599 }
9600 return FALSE;
9601}
9602
9603/*
9604 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9605 * The incompatible newer method is quite a bit better at indenting
9606 * code in lisp-like languages than the traditional one; it's still
9607 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9608 *
9609 * TODO:
9610 * Findmatch() should be adapted for lisp, also to make showmatch
9611 * work correctly: now (v5.3) it seems all C/C++ oriented:
9612 * - it does not recognize the #\( and #\) notations as character literals
9613 * - it doesn't know about comments starting with a semicolon
9614 * - it incorrectly interprets '(' as a character literal
9615 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009616 * Update from Sergey Khorev:
9617 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618 */
9619 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009620get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009622 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 int amount;
9624 char_u *that;
9625 colnr_T col;
9626 colnr_T firsttry;
9627 int parencount, quotecount;
9628 int vi_lisp;
9629
9630 /* Set vi_lisp to use the vi-compatible method */
9631 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9632
9633 realpos = curwin->w_cursor;
9634 curwin->w_cursor.col = 0;
9635
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009636 if ((pos = findmatch(NULL, '(')) == NULL)
9637 pos = findmatch(NULL, '[');
9638 else
9639 {
9640 paren = *pos;
9641 pos = findmatch(NULL, '[');
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009642 if (pos == NULL || LT_POSP(pos, &paren))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009643 pos = &paren;
9644 }
9645 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 {
9647 /* Extra trick: Take the indent of the first previous non-white
9648 * line that is at the same () level. */
9649 amount = -1;
9650 parencount = 0;
9651
9652 while (--curwin->w_cursor.lnum >= pos->lnum)
9653 {
9654 if (linewhite(curwin->w_cursor.lnum))
9655 continue;
9656 for (that = ml_get_curline(); *that != NUL; ++that)
9657 {
9658 if (*that == ';')
9659 {
9660 while (*(that + 1) != NUL)
9661 ++that;
9662 continue;
9663 }
9664 if (*that == '\\')
9665 {
9666 if (*(that + 1) != NUL)
9667 ++that;
9668 continue;
9669 }
9670 if (*that == '"' && *(that + 1) != NUL)
9671 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009672 while (*++that && *that != '"')
9673 {
9674 /* skipping escaped characters in the string */
9675 if (*that == '\\')
9676 {
9677 if (*++that == NUL)
9678 break;
9679 if (that[1] == NUL)
9680 {
9681 ++that;
9682 break;
9683 }
9684 }
9685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009687 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009689 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 --parencount;
9691 }
9692 if (parencount == 0)
9693 {
9694 amount = get_indent();
9695 break;
9696 }
9697 }
9698
9699 if (amount == -1)
9700 {
9701 curwin->w_cursor.lnum = pos->lnum;
9702 curwin->w_cursor.col = pos->col;
9703 col = pos->col;
9704
9705 that = ml_get_curline();
9706
9707 if (vi_lisp && get_indent() == 0)
9708 amount = 2;
9709 else
9710 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009711 char_u *line = that;
9712
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 amount = 0;
9714 while (*that && col)
9715 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009716 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 col--;
9718 }
9719
9720 /*
9721 * Some keywords require "body" indenting rules (the
9722 * non-standard-lisp ones are Scheme special forms):
9723 *
9724 * (let ((a 1)) instead (let ((a 1))
9725 * (...)) of (...))
9726 */
9727
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009728 if (!vi_lisp && (*that == '(' || *that == '[')
9729 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 amount += 2;
9731 else
9732 {
9733 that++;
9734 amount++;
9735 firsttry = amount;
9736
Bram Moolenaar1c465442017-03-12 20:10:05 +01009737 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009739 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 ++that;
9741 }
9742
9743 if (*that && *that != ';') /* not a comment line */
9744 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009745 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009747 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 firsttry++;
9749
9750 parencount = 0;
9751 quotecount = 0;
9752
9753 if (vi_lisp
9754 || (*that != '"'
9755 && *that != '\''
9756 && *that != '#'
9757 && (*that < '0' || *that > '9')))
9758 {
9759 while (*that
Bram Moolenaar1c465442017-03-12 20:10:05 +01009760 && (!VIM_ISWHITE(*that)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 || quotecount
9762 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009763 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 && !quotecount
9765 && !parencount
9766 && vi_lisp)))
9767 {
9768 if (*that == '"')
9769 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009770 if ((*that == '(' || *that == '[')
9771 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009773 if ((*that == ')' || *that == ']')
9774 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 --parencount;
9776 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009777 amount += lbr_chartabsize_adv(
9778 line, &that, (colnr_T)amount);
9779 amount += lbr_chartabsize_adv(
9780 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 }
9782 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01009783 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009785 amount += lbr_chartabsize(
9786 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 that++;
9788 }
9789 if (!*that || *that == ';')
9790 amount = firsttry;
9791 }
9792 }
9793 }
9794 }
9795 }
9796 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009797 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798
9799 curwin->w_cursor = realpos;
9800
9801 return amount;
9802}
9803#endif /* FEAT_LISP */
9804
9805 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009806prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009808#if defined(SIGHUP) && defined(SIG_IGN)
9809 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9810 * makes Vim exit and then handling SIGHUP causes various reentrance
9811 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009812 signal(SIGHUP, SIG_IGN);
9813#endif
9814
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815#ifdef FEAT_GUI
9816 if (gui.in_use)
9817 {
9818 gui.dying = TRUE;
9819 out_trash(); /* trash any pending output */
9820 }
9821 else
9822#endif
9823 {
9824 windgoto((int)Rows - 1, 0);
9825
9826 /*
9827 * Switch terminal mode back now, so messages end up on the "normal"
9828 * screen (if there are two screens).
9829 */
9830 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009831 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832 out_flush();
9833 }
9834}
9835
9836/*
9837 * Preserve files and exit.
9838 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009839 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9840 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 */
9842 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009843preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844{
9845 buf_T *buf;
9846
9847 prepare_to_exit();
9848
Bram Moolenaar4770d092006-01-12 23:22:24 +00009849 /* Setting this will prevent free() calls. That avoids calling free()
9850 * recursively when free() was invoked with a bad pointer. */
9851 really_exiting = TRUE;
9852
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853 out_str(IObuff);
9854 screen_start(); /* don't know where cursor is now */
9855 out_flush();
9856
9857 ml_close_notmod(); /* close all not-modified buffers */
9858
Bram Moolenaar29323592016-07-24 22:04:11 +02009859 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860 {
9861 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9862 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009863 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 screen_start(); /* don't know where cursor is now */
9865 out_flush();
9866 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9867 break;
9868 }
9869 }
9870
9871 ml_close_all(FALSE); /* close all memfiles, without deleting */
9872
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009873 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874
9875 getout(1);
9876}
9877
9878/*
9879 * return TRUE if "fname" exists.
9880 */
9881 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009882vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009884 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885
9886 if (mch_stat((char *)fname, &st))
9887 return FALSE;
9888 return TRUE;
9889}
9890
9891/*
9892 * Check for CTRL-C pressed, but only once in a while.
9893 * Should be used instead of ui_breakcheck() for functions that check for
9894 * each line in the file. Calling ui_breakcheck() each time takes too much
9895 * time, because it can be a system call.
9896 */
9897
9898#ifndef BREAKCHECK_SKIP
9899# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9900# define BREAKCHECK_SKIP 200
9901# else
9902# define BREAKCHECK_SKIP 32
9903# endif
9904#endif
9905
9906static int breakcheck_count = 0;
9907
9908 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009909line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910{
9911 if (++breakcheck_count >= BREAKCHECK_SKIP)
9912 {
9913 breakcheck_count = 0;
9914 ui_breakcheck();
9915 }
9916}
9917
9918/*
9919 * Like line_breakcheck() but check 10 times less often.
9920 */
9921 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009922fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923{
9924 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9925 {
9926 breakcheck_count = 0;
9927 ui_breakcheck();
9928 }
9929}
9930
9931/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009932 * Invoke expand_wildcards() for one pattern.
9933 * Expand items like "%:h" before the expansion.
9934 * Returns OK or FAIL.
9935 */
9936 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009937expand_wildcards_eval(
9938 char_u **pat, /* pointer to input pattern */
9939 int *num_file, /* resulting number of files */
9940 char_u ***file, /* array of resulting files */
9941 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009942{
9943 int ret = FAIL;
9944 char_u *eval_pat = NULL;
9945 char_u *exp_pat = *pat;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009946 char *ignored_msg;
Bram Moolenaard7834d32009-12-02 16:14:36 +00009947 int usedlen;
9948
9949 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9950 {
9951 ++emsg_off;
9952 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9953 NULL, &ignored_msg, NULL);
9954 --emsg_off;
9955 if (eval_pat != NULL)
9956 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9957 }
9958
9959 if (exp_pat != NULL)
9960 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9961
9962 if (eval_pat != NULL)
9963 {
9964 vim_free(exp_pat);
9965 vim_free(eval_pat);
9966 }
9967
9968 return ret;
9969}
9970
9971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9973 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009974 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 */
9976 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009977expand_wildcards(
9978 int num_pat, /* number of input patterns */
9979 char_u **pat, /* array of input patterns */
9980 int *num_files, /* resulting number of files */
9981 char_u ***files, /* array of resulting files */
9982 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983{
9984 int retval;
9985 int i, j;
9986 char_u *p;
9987 int non_suf_match; /* number without matching suffix */
9988
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009989 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990
9991 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009992 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 return retval;
9994
9995#ifdef FEAT_WILDIGN
9996 /*
9997 * Remove names that match 'wildignore'.
9998 */
9999 if (*p_wig)
10000 {
10001 char_u *ffname;
10002
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010003 /* check all files in (*files)[] */
10004 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010006 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 if (ffname == NULL) /* out of memory */
10008 break;
10009# ifdef VMS
10010 vms_remove_version(ffname);
10011# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010012 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010014 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010015 vim_free((*files)[i]);
10016 for (j = i; j + 1 < *num_files; ++j)
10017 (*files)[j] = (*files)[j + 1];
10018 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 --i;
10020 }
10021 vim_free(ffname);
10022 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010023
10024 /* If the number of matches is now zero, we fail. */
10025 if (*num_files == 0)
10026 {
Bram Moolenaard23a8232018-02-10 18:45:26 +010010027 VIM_CLEAR(*files);
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010028 return FAIL;
10029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 }
10031#endif
10032
10033 /*
10034 * Move the names where 'suffixes' match to the end.
10035 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010036 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 {
10038 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010039 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010041 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 {
10043 /*
10044 * Move the name without matching suffix to the front
10045 * of the list.
10046 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010047 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010048 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +020010049 (*files)[j] = (*files)[j - 1];
10050 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 }
10052 }
10053 }
10054
10055 return retval;
10056}
10057
10058/*
10059 * Return TRUE if "fname" matches with an entry in 'suffixes'.
10060 */
10061 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010062match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063{
10064 int fnamelen, setsuflen;
10065 char_u *setsuf;
10066#define MAXSUFLEN 30 /* maximum length of a file suffix */
10067 char_u suf_buf[MAXSUFLEN];
10068
10069 fnamelen = (int)STRLEN(fname);
10070 setsuflen = 0;
10071 for (setsuf = p_su; *setsuf; )
10072 {
10073 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +000010074 if (setsuflen == 0)
10075 {
10076 char_u *tail = gettail(fname);
10077
10078 /* empty entry: match name without a '.' */
10079 if (vim_strchr(tail, '.') == NULL)
10080 {
10081 setsuflen = 1;
10082 break;
10083 }
10084 }
10085 else
10086 {
10087 if (fnamelen >= setsuflen
10088 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
10089 (size_t)setsuflen) == 0)
10090 break;
10091 setsuflen = 0;
10092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 }
10094 return (setsuflen != 0);
10095}
10096
10097#if !defined(NO_EXPANDPATH) || defined(PROTO)
10098
10099# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010100static int vim_backtick(char_u *p);
10101static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102# endif
10103
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010104# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105/*
10106 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
10107 * it's shared between these systems.
10108 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010109# if defined(PROTO)
10110# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111# else
10112# ifdef __BORLANDC__
10113# define _cdecl _RTLENTRYF
10114# endif
10115# endif
10116
10117/*
10118 * comparison function for qsort in dos_expandpath()
10119 */
10120 static int _cdecl
10121pstrcmp(const void *a, const void *b)
10122{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010123 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124}
10125
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126/*
Bram Moolenaar231334e2005-07-25 20:46:57 +000010127 * Recursively expand one path component into all matching files and/or
10128 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 * Return the number of matches found.
10130 * "path" has backslashes before chars that are not to be expanded, starting
10131 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +000010132 * Return the number of matches found.
10133 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 */
10135 static int
10136dos_expandpath(
10137 garray_T *gap,
10138 char_u *path,
10139 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +000010140 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +000010141 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010143 char_u *buf;
10144 char_u *path_end;
10145 char_u *p, *s, *e;
10146 int start_len = gap->ga_len;
10147 char_u *pat;
10148 regmatch_T regmatch;
10149 int starts_with_dot;
10150 int matches;
10151 int len;
10152 int starstar = FALSE;
10153 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154 WIN32_FIND_DATA fb;
10155 HANDLE hFind = (HANDLE)0;
10156# ifdef FEAT_MBYTE
10157 WIN32_FIND_DATAW wfb;
10158 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
10159# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010161 int ok;
10162
10163 /* Expanding "**" may take a long time, check for CTRL-C. */
10164 if (stardepth > 0)
10165 {
10166 ui_breakcheck();
10167 if (got_int)
10168 return 0;
10169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170
Bram Moolenaar7314efd2015-10-31 15:32:52 +010010171 /* Make room for file name. When doing encoding conversion the actual
10172 * length may be quite a bit longer, thus use the maximum possible length. */
10173 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174 if (buf == NULL)
10175 return 0;
10176
10177 /*
10178 * Find the first part in the path name that contains a wildcard or a ~1.
10179 * Copy it into buf, including the preceding characters.
10180 */
10181 p = buf;
10182 s = buf;
10183 e = NULL;
10184 path_end = path;
10185 while (*path_end != NUL)
10186 {
10187 /* May ignore a wildcard that has a backslash before it; it will
10188 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10189 if (path_end >= path + wildoff && rem_backslash(path_end))
10190 *p++ = *path_end++;
10191 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
10192 {
10193 if (e != NULL)
10194 break;
10195 s = p + 1;
10196 }
10197 else if (path_end >= path + wildoff
10198 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
10199 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +010010200# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201 if (has_mbyte)
10202 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010203 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 STRNCPY(p, path_end, len);
10205 p += len;
10206 path_end += len;
10207 }
10208 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +010010209# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 *p++ = *path_end++;
10211 }
10212 e = p;
10213 *e = NUL;
10214
10215 /* now we have one wildcard component between s and e */
10216 /* Remove backslashes between "wildoff" and the start of the wildcard
10217 * component. */
10218 for (p = buf + wildoff; p < s; ++p)
10219 if (rem_backslash(p))
10220 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010221 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 --e;
10223 --s;
10224 }
10225
Bram Moolenaar231334e2005-07-25 20:46:57 +000010226 /* Check for "**" between "s" and "e". */
10227 for (p = s; p < e; ++p)
10228 if (p[0] == '*' && p[1] == '*')
10229 starstar = TRUE;
10230
Bram Moolenaard82103e2016-01-17 17:04:05 +010010231 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10233 if (pat == NULL)
10234 {
10235 vim_free(buf);
10236 return 0;
10237 }
10238
10239 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010240 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010241 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242 regmatch.rm_ic = TRUE; /* Always ignore case */
10243 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010244 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010245 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 vim_free(pat);
10247
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010248 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249 {
10250 vim_free(buf);
10251 return 0;
10252 }
10253
10254 /* remember the pattern or file name being looked for */
10255 matchname = vim_strsave(s);
10256
Bram Moolenaar231334e2005-07-25 20:46:57 +000010257 /* If "**" is by itself, this is the first time we encounter it and more
10258 * is following then find matches without any directory. */
10259 if (!didstar && stardepth < 100 && starstar && e - s == 2
10260 && *path_end == '/')
10261 {
10262 STRCPY(s, path_end + 1);
10263 ++stardepth;
10264 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10265 --stardepth;
10266 }
10267
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268 /* Scan all files in the directory with "dir/ *.*" */
10269 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270# ifdef FEAT_MBYTE
10271 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
10272 {
10273 /* The active codepage differs from 'encoding'. Attempt using the
10274 * wide function. If it fails because it is not implemented fall back
10275 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010276 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277 if (wn != NULL)
10278 {
10279 hFind = FindFirstFileW(wn, &wfb);
10280 if (hFind == INVALID_HANDLE_VALUE
10281 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
Bram Moolenaard23a8232018-02-10 18:45:26 +010010282 VIM_CLEAR(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283 }
10284 }
10285
10286 if (wn == NULL)
10287# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010288 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290
10291 while (ok)
10292 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293# ifdef FEAT_MBYTE
10294 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010295 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296 else
10297# endif
10298 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299 /* Ignore entries starting with a dot, unless when asked for. Accept
10300 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010301 if ((p[0] != '.' || starts_with_dot
10302 || ((flags & EW_DODOT)
10303 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010305 || (regmatch.regprog != NULL
10306 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010307 || ((flags & EW_NOTWILD)
10308 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010312
10313 if (starstar && stardepth < 100)
10314 {
10315 /* For "**" in the pattern first go deeper in the tree to
10316 * find matches. */
10317 STRCPY(buf + len, "/**");
10318 STRCPY(buf + len + 3, path_end);
10319 ++stardepth;
10320 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
10321 --stardepth;
10322 }
10323
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324 STRCPY(buf + len, path_end);
10325 if (mch_has_exp_wildcard(path_end))
10326 {
10327 /* need to expand another component of the path */
10328 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010329 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 }
10331 else
10332 {
10333 /* no more wildcards, check if there is a match */
10334 /* remove backslashes for the remaining components only */
10335 if (*path_end != 0)
10336 backslash_halve(buf + len + 1);
10337 if (mch_getperm(buf) >= 0) /* add existing file */
10338 addfile(gap, buf, flags);
10339 }
10340 }
10341
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342# ifdef FEAT_MBYTE
10343 if (wn != NULL)
10344 {
10345 vim_free(p);
10346 ok = FindNextFileW(hFind, &wfb);
10347 }
10348 else
10349# endif
10350 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351
10352 /* If no more matches and no match was used, try expanding the name
10353 * itself. Finds the long name of a short filename. */
10354 if (!ok && matchname != NULL && gap->ga_len == start_len)
10355 {
10356 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357 FindClose(hFind);
10358# ifdef FEAT_MBYTE
10359 if (wn != NULL)
10360 {
10361 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010362 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 if (wn != NULL)
10364 hFind = FindFirstFileW(wn, &wfb);
10365 }
10366 if (wn == NULL)
10367# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010368 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaard23a8232018-02-10 18:45:26 +010010370 VIM_CLEAR(matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371 }
10372 }
10373
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 FindClose(hFind);
10375# ifdef FEAT_MBYTE
10376 vim_free(wn);
10377# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010379 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 vim_free(matchname);
10381
10382 matches = gap->ga_len - start_len;
10383 if (matches > 0)
10384 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10385 sizeof(char_u *), pstrcmp);
10386 return matches;
10387}
10388
10389 int
10390mch_expandpath(
10391 garray_T *gap,
10392 char_u *path,
10393 int flags) /* EW_* flags */
10394{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010395 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010397# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398
Bram Moolenaar231334e2005-07-25 20:46:57 +000010399#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10400 || defined(PROTO)
10401/*
10402 * Unix style wildcard expansion code.
10403 * It's here because it's used both for Unix and Mac.
10404 */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010405 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010406pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010407{
10408 return (pathcmp(*(char **)a, *(char **)b, -1));
10409}
10410
10411/*
10412 * Recursively expand one path component into all matching files and/or
10413 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10414 * "path" has backslashes before chars that are not to be expanded, starting
10415 * at "path + wildoff".
10416 * Return the number of matches found.
10417 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10418 */
10419 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010420unix_expandpath(
10421 garray_T *gap,
10422 char_u *path,
10423 int wildoff,
10424 int flags, /* EW_* flags */
10425 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010426{
10427 char_u *buf;
10428 char_u *path_end;
10429 char_u *p, *s, *e;
10430 int start_len = gap->ga_len;
10431 char_u *pat;
10432 regmatch_T regmatch;
10433 int starts_with_dot;
10434 int matches;
10435 int len;
10436 int starstar = FALSE;
10437 static int stardepth = 0; /* depth for "**" expansion */
10438
10439 DIR *dirp;
10440 struct dirent *dp;
10441
10442 /* Expanding "**" may take a long time, check for CTRL-C. */
10443 if (stardepth > 0)
10444 {
10445 ui_breakcheck();
10446 if (got_int)
10447 return 0;
10448 }
10449
10450 /* make room for file name */
10451 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10452 if (buf == NULL)
10453 return 0;
10454
10455 /*
10456 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010457 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010458 * Copy it into "buf", including the preceding characters.
10459 */
10460 p = buf;
10461 s = buf;
10462 e = NULL;
10463 path_end = path;
10464 while (*path_end != NUL)
10465 {
10466 /* May ignore a wildcard that has a backslash before it; it will
10467 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10468 if (path_end >= path + wildoff && rem_backslash(path_end))
10469 *p++ = *path_end++;
10470 else if (*path_end == '/')
10471 {
10472 if (e != NULL)
10473 break;
10474 s = p + 1;
10475 }
10476 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010477 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010478 || (!p_fic && (flags & EW_ICASE)
10479 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010480 e = p;
10481#ifdef FEAT_MBYTE
10482 if (has_mbyte)
10483 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010484 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010485 STRNCPY(p, path_end, len);
10486 p += len;
10487 path_end += len;
10488 }
10489 else
10490#endif
10491 *p++ = *path_end++;
10492 }
10493 e = p;
10494 *e = NUL;
10495
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010496 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010497 /* Remove backslashes between "wildoff" and the start of the wildcard
10498 * component. */
10499 for (p = buf + wildoff; p < s; ++p)
10500 if (rem_backslash(p))
10501 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010502 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010503 --e;
10504 --s;
10505 }
10506
10507 /* Check for "**" between "s" and "e". */
10508 for (p = s; p < e; ++p)
10509 if (p[0] == '*' && p[1] == '*')
10510 starstar = TRUE;
10511
10512 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010513 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010514 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10515 if (pat == NULL)
10516 {
10517 vim_free(buf);
10518 return 0;
10519 }
10520
10521 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010522 if (flags & EW_ICASE)
10523 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10524 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010525 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010526 if (flags & (EW_NOERROR | EW_NOTWILD))
10527 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010528 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010529 if (flags & (EW_NOERROR | EW_NOTWILD))
10530 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010531 vim_free(pat);
10532
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010533 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010534 {
10535 vim_free(buf);
10536 return 0;
10537 }
10538
10539 /* If "**" is by itself, this is the first time we encounter it and more
10540 * is following then find matches without any directory. */
10541 if (!didstar && stardepth < 100 && starstar && e - s == 2
10542 && *path_end == '/')
10543 {
10544 STRCPY(s, path_end + 1);
10545 ++stardepth;
10546 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10547 --stardepth;
10548 }
10549
10550 /* open the directory for scanning */
10551 *s = NUL;
10552 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10553
10554 /* Find all matching entries */
10555 if (dirp != NULL)
10556 {
10557 for (;;)
10558 {
10559 dp = readdir(dirp);
10560 if (dp == NULL)
10561 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010562 if ((dp->d_name[0] != '.' || starts_with_dot
10563 || ((flags & EW_DODOT)
10564 && dp->d_name[1] != NUL
10565 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010566 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10567 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010568 || ((flags & EW_NOTWILD)
10569 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010570 {
10571 STRCPY(s, dp->d_name);
10572 len = STRLEN(buf);
10573
10574 if (starstar && stardepth < 100)
10575 {
10576 /* For "**" in the pattern first go deeper in the tree to
10577 * find matches. */
10578 STRCPY(buf + len, "/**");
10579 STRCPY(buf + len + 3, path_end);
10580 ++stardepth;
10581 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10582 --stardepth;
10583 }
10584
10585 STRCPY(buf + len, path_end);
10586 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10587 {
10588 /* need to expand another component of the path */
10589 /* remove backslashes for the remaining components only */
10590 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10591 }
10592 else
10593 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010594 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010595
Bram Moolenaar231334e2005-07-25 20:46:57 +000010596 /* no more wildcards, check if there is a match */
10597 /* remove backslashes for the remaining components only */
10598 if (*path_end != NUL)
10599 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010600 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010601 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010602 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010603 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010604#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010605 size_t precomp_len = STRLEN(buf)+1;
10606 char_u *precomp_buf =
10607 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010608
Bram Moolenaar231334e2005-07-25 20:46:57 +000010609 if (precomp_buf)
10610 {
10611 mch_memmove(buf, precomp_buf, precomp_len);
10612 vim_free(precomp_buf);
10613 }
10614#endif
10615 addfile(gap, buf, flags);
10616 }
10617 }
10618 }
10619 }
10620
10621 closedir(dirp);
10622 }
10623
10624 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010625 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010626
10627 matches = gap->ga_len - start_len;
10628 if (matches > 0)
10629 qsort(((char_u **)gap->ga_data) + start_len, matches,
10630 sizeof(char_u *), pstrcmp);
10631 return matches;
10632}
10633#endif
10634
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010635#if defined(FEAT_SEARCHPATH)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010636/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010637 * Moves "*psep" back to the previous path separator in "path".
10638 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010639 */
10640 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010641find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010642{
10643 /* skip the current separator */
10644 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010645 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010646
10647 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010648 while (*psep > path)
10649 {
10650 if (vim_ispathsep(**psep))
10651 return OK;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010652 MB_PTR_BACK(path, *psep);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010653 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010654
10655 return FAIL;
10656}
10657
10658/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010659 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10660 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010661 */
10662 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010663is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010664{
10665 int j;
10666 int candidate_len;
10667 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010668 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010669 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010670
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010671 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010672 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010673 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010674 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010675
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010676 candidate_len = (int)STRLEN(maybe_unique);
10677 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010678 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010679 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010680
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010681 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010682 if (fnamecmp(maybe_unique, rival) == 0
10683 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010684 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010685 }
10686
Bram Moolenaar162bd912010-07-28 22:29:10 +020010687 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010688}
10689
10690/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010691 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010692 * paths are expanded to their equivalent fullpath. This includes the "."
10693 * (relative to current buffer directory) and empty path (relative to current
10694 * directory) notations.
10695 *
10696 * TODO: handle upward search (;) and path limiter (**N) notations by
10697 * expanding each into their equivalent path(s).
10698 */
10699 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010700expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010701{
10702 char_u *path_option = *curbuf->b_p_path == NUL
10703 ? p_path : curbuf->b_p_path;
10704 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010705 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010706 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010707
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010708 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010709 return;
10710
10711 while (*path_option != NUL)
10712 {
10713 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10714
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010715 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010716 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010717 /* Relative to current buffer:
10718 * "/path/file" + "." -> "/path/"
10719 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010720 if (curbuf->b_ffname == NULL)
10721 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010722 p = gettail(curbuf->b_ffname);
10723 len = (int)(p - curbuf->b_ffname);
10724 if (len + (int)STRLEN(buf) >= MAXPATHL)
10725 continue;
10726 if (buf[1] == NUL)
10727 buf[len] = NUL;
10728 else
10729 STRMOVE(buf + len, buf + 2);
10730 mch_memmove(buf, curbuf->b_ffname, len);
10731 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010732 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010733 else if (buf[0] == NUL)
10734 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010735 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010736 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010737 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010738 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010739 else if (!mch_isFullName(buf))
10740 {
10741 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010742 len = (int)STRLEN(curdir);
10743 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010744 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010745 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010746 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010747 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010748 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010749 }
10750
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010751 if (ga_grow(gap, 1) == FAIL)
10752 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010753
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010754# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010755 /* Avoid the path ending in a backslash, it fails when a comma is
10756 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010757 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010758 if (buf[len - 1] == '\\')
10759 buf[len - 1] = '/';
10760# endif
10761
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010762 p = vim_strsave(buf);
10763 if (p == NULL)
10764 break;
10765 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010766 }
10767
10768 vim_free(buf);
10769}
10770
10771/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010772 * Returns a pointer to the file or directory name in "fname" that matches the
10773 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010774 *
10775 * path: /foo/bar/baz
10776 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010777 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010778 */
10779 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010780get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010781{
10782 int i;
10783 int maxlen = 0;
10784 char_u **path_part = (char_u **)gap->ga_data;
10785 char_u *cutoff = NULL;
10786
10787 for (i = 0; i < gap->ga_len; i++)
10788 {
10789 int j = 0;
10790
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010791 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010792# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010793 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10794#endif
10795 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010796 j++;
10797 if (j > maxlen)
10798 {
10799 maxlen = j;
10800 cutoff = &fname[j];
10801 }
10802 }
10803
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010804 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010805 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010806 while (vim_ispathsep(*cutoff))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010807 MB_PTR_ADV(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010808
10809 return cutoff;
10810}
10811
10812/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010813 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10814 * that they are unique with respect to each other while conserving the part
10815 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010816 */
10817 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010818uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010819{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010820 int i;
10821 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010822 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010823 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010824 char_u *pat;
10825 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010826 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010827 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010828 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010829 char_u **in_curdir = NULL;
10830 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010831
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010832 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010833 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010834
10835 /*
10836 * We need to prepend a '*' at the beginning of file_pattern so that the
10837 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010838 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010839 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010840 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010841 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010842 if (file_pattern == NULL)
10843 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010844 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010845 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010846 STRCAT(file_pattern, pattern);
10847 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10848 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010849 if (pat == NULL)
10850 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010851
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010852 regmatch.rm_ic = TRUE; /* always ignore case */
10853 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10854 vim_free(pat);
10855 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010856 return;
10857
Bram Moolenaar162bd912010-07-28 22:29:10 +020010858 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010859 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010860 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010861 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010862
10863 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010864 if (in_curdir == NULL)
10865 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010866
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010867 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010868 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010869 char_u *path = fnames[i];
10870 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010871 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010872 char_u *pathsep_p;
10873 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010874
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010875 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010876 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010877 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010878 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010879 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010880
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010881 /* Shorten the filename while maintaining its uniqueness */
10882 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010883
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010884 /* Don't assume all files can be reached without path when search
10885 * pattern starts with star star slash, so only remove path_cutoff
10886 * when possible. */
10887 if (pattern[0] == '*' && pattern[1] == '*'
10888 && vim_ispathsep_nocolon(pattern[2])
10889 && path_cutoff != NULL
10890 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10891 && is_unique(path_cutoff, gap, i))
10892 {
10893 sort_again = TRUE;
10894 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10895 }
10896 else
10897 {
10898 /* Here all files can be reached without path, so get shortest
10899 * unique path. We start at the end of the path. */
10900 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010901
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010902 while (find_previous_pathsep(path, &pathsep_p))
10903 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10904 && is_unique(pathsep_p + 1, gap, i)
10905 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10906 {
10907 sort_again = TRUE;
10908 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10909 break;
10910 }
10911 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010912
10913 if (mch_isFullName(path))
10914 {
10915 /*
10916 * Last resort: shorten relative to curdir if possible.
10917 * 'possible' means:
10918 * 1. It is under the current directory.
10919 * 2. The result is actually shorter than the original.
10920 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010921 * Before curdir After
10922 * /foo/bar/file.txt /foo/bar ./file.txt
10923 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10924 * /file.txt / /file.txt
10925 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010926 */
10927 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010928 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010929#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010930 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010931 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010932 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010933 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010934 && !vim_ispathsep(*short_name)
10935#endif
10936 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010937 {
10938 STRCPY(path, ".");
10939 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010940 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010941 }
10942 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010943 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010944 }
10945
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010946 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010947 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010948 {
10949 char_u *rel_path;
10950 char_u *path = in_curdir[i];
10951
10952 if (path == NULL)
10953 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010954
10955 /* If the {filename} is not unique, change it to ./{filename}.
10956 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010957 short_name = shorten_fname(path, curdir);
10958 if (short_name == NULL)
10959 short_name = path;
10960 if (is_unique(short_name, gap, i))
10961 {
10962 STRCPY(fnames[i], short_name);
10963 continue;
10964 }
10965
10966 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10967 if (rel_path == NULL)
10968 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010969 STRCPY(rel_path, ".");
10970 add_pathsep(rel_path);
10971 STRCAT(rel_path, short_name);
10972
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010973 vim_free(fnames[i]);
10974 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010975 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010976 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010977 }
10978
Bram Moolenaar162bd912010-07-28 22:29:10 +020010979theend:
10980 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010981 if (in_curdir != NULL)
10982 {
10983 for (i = 0; i < gap->ga_len; i++)
10984 vim_free(in_curdir[i]);
10985 vim_free(in_curdir);
10986 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010987 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010988 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010989
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010990 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010991 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010992}
10993
10994/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010995 * Calls globpath() with 'path' values for the given pattern and stores the
10996 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010997 * Returns the total number of matches.
10998 */
10999 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011000expand_in_path(
11001 garray_T *gap,
11002 char_u *pattern,
11003 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011004{
Bram Moolenaar162bd912010-07-28 22:29:10 +020011005 char_u *curdir;
11006 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020011007 char_u *paths = NULL;
Bram Moolenaar8a37b032018-02-03 20:43:08 +010011008 int glob_flags = 0;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011009
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020011010 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020011011 return 0;
11012 mch_dirname(curdir, MAXPATHL);
11013
Bram Moolenaar0be992e2010-08-12 21:50:51 +020011014 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020011015 expand_path_option(curdir, &path_ga);
11016 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020011017 if (path_ga.ga_len == 0)
11018 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011019
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020011020 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011021 ga_clear_strings(&path_ga);
11022 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020011023 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020011024
Bram Moolenaar8a37b032018-02-03 20:43:08 +010011025 if (flags & EW_ICASE)
11026 glob_flags |= WILD_ICASE;
11027 if (flags & EW_ADDSLASH)
11028 glob_flags |= WILD_ADD_SLASH;
11029 globpath(paths, pattern, gap, glob_flags);
Bram Moolenaar162bd912010-07-28 22:29:10 +020011030 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011031
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020011032 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011033}
11034#endif
11035
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011036#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
11037/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020011038 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
11039 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011040 */
11041 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011042remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011043{
11044 int i;
11045 int j;
11046 char_u **fnames = (char_u **)gap->ga_data;
11047
Bram Moolenaardc685ab2010-08-13 21:16:49 +020011048 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020011049 for (i = gap->ga_len - 1; i > 0; --i)
11050 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
11051 {
11052 vim_free(fnames[i]);
11053 for (j = i + 1; j < gap->ga_len; ++j)
11054 fnames[j - 1] = fnames[j];
11055 --gap->ga_len;
11056 }
11057}
11058#endif
11059
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011060/*
11061 * Return TRUE if "p" contains what looks like an environment variable.
11062 * Allowing for escaping.
11063 */
11064 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011065has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011066{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011067 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011068 {
11069 if (*p == '\\' && p[1] != NUL)
11070 ++p;
11071 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010011072#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011073 "$%"
11074#else
11075 "$"
11076#endif
11077 , *p) != NULL)
11078 return TRUE;
11079 }
11080 return FALSE;
11081}
11082
11083#ifdef SPECIAL_WILDCHAR
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011084/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +010011085 * Return TRUE if "p" contains a special wildcard character, one that Vim
11086 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011087 */
11088 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011089has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011090{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011091 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011092 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010011093 /* Allow for escaping. */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011094 if (*p == '\\' && p[1] != NUL)
11095 ++p;
11096 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
11097 return TRUE;
11098 }
11099 return FALSE;
11100}
11101#endif
11102
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103/*
11104 * Generic wildcard expansion code.
11105 *
11106 * Characters in "pat" that should not be expanded must be preceded with a
11107 * backslash. E.g., "/path\ with\ spaces/my\*star*"
11108 *
11109 * Return FAIL when no single file was found. In this case "num_file" is not
11110 * set, and "file" may contain an error message.
11111 * Return OK when some files found. "num_file" is set to the number of
11112 * matches, "file" to the array of matches. Call FreeWild() later.
11113 */
11114 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011115gen_expand_wildcards(
11116 int num_pat, /* number of input patterns */
11117 char_u **pat, /* array of input patterns */
11118 int *num_file, /* resulting number of files */
11119 char_u ***file, /* array of resulting files */
11120 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121{
11122 int i;
11123 garray_T ga;
11124 char_u *p;
11125 static int recursive = FALSE;
11126 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020011127 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011128#if defined(FEAT_SEARCHPATH)
11129 int did_expand_in_path = FALSE;
11130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131
11132 /*
11133 * expand_env() is called to expand things like "~user". If this fails,
11134 * it calls ExpandOne(), which brings us back here. In this case, always
11135 * call the machine specific expansion function, if possible. Otherwise,
11136 * return FAIL.
11137 */
11138 if (recursive)
11139#ifdef SPECIAL_WILDCHAR
11140 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11141#else
11142 return FAIL;
11143#endif
11144
11145#ifdef SPECIAL_WILDCHAR
11146 /*
11147 * If there are any special wildcard characters which we cannot handle
11148 * here, call machine specific function for all the expansion. This
11149 * avoids starting the shell for each argument separately.
11150 * For `=expr` do use the internal function.
11151 */
11152 for (i = 0; i < num_pat; i++)
11153 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011154 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155# ifdef VIM_BACKTICK
11156 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
11157# endif
11158 )
11159 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
11160 }
11161#endif
11162
11163 recursive = TRUE;
11164
11165 /*
11166 * The matching file names are stored in a growarray. Init it empty.
11167 */
11168 ga_init2(&ga, (int)sizeof(char_u *), 30);
11169
11170 for (i = 0; i < num_pat; ++i)
11171 {
11172 add_pat = -1;
11173 p = pat[i];
11174
11175#ifdef VIM_BACKTICK
11176 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020011177 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020011179 if (add_pat == -1)
11180 retval = FAIL;
11181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011182 else
11183#endif
11184 {
11185 /*
11186 * First expand environment variables, "~/" and "~user/".
11187 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011188 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011189 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000011190 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011191 if (p == NULL)
11192 p = pat[i];
11193#ifdef UNIX
11194 /*
11195 * On Unix, if expand_env() can't expand an environment
11196 * variable, use the shell to do that. Discard previously
11197 * found file names and start all over again.
11198 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011199 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200 {
11201 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000011202 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020011204 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011205 recursive = FALSE;
11206 return i;
11207 }
11208#endif
11209 }
11210
11211 /*
11212 * If there are wildcards: Expand file names and add each match to
11213 * the list. If there is no match, and EW_NOTFOUND is given, add
11214 * the pattern.
11215 * If there are no wildcards: Add the file name if it exists or
11216 * when EW_NOTFOUND is given.
11217 */
11218 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011219 {
11220#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011221 if ((flags & EW_PATH)
11222 && !mch_isFullName(p)
11223 && !(p[0] == '.'
11224 && (vim_ispathsep(p[1])
11225 || (p[1] == '.' && vim_ispathsep(p[2]))))
11226 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011227 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011228 /* :find completion where 'path' is used.
11229 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011230 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011231 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011232 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011233 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011234 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011235 else
11236#endif
11237 add_pat = mch_expandpath(&ga, p, flags);
11238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011239 }
11240
11241 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
11242 {
11243 char_u *t = backslash_halve_save(p);
11244
Bram Moolenaar071d4272004-06-13 20:20:40 +000011245 /* When EW_NOTFOUND is used, always add files and dirs. Makes
11246 * "vim c:/" work. */
11247 if (flags & EW_NOTFOUND)
11248 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020011249 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250 addfile(&ga, t, flags);
11251 vim_free(t);
11252 }
11253
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011254#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011255 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011256 uniquefy_paths(&ga, p);
11257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011258 if (p != pat[i])
11259 vim_free(p);
11260 }
11261
11262 *num_file = ga.ga_len;
11263 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
11264
11265 recursive = FALSE;
11266
Bram Moolenaar336bd622016-01-17 18:23:58 +010011267 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268}
11269
11270# ifdef VIM_BACKTICK
11271
11272/*
11273 * Return TRUE if we can expand this backtick thing here.
11274 */
11275 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011276vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277{
11278 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
11279}
11280
11281/*
11282 * Expand an item in `backticks` by executing it as a command.
11283 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020011284 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285 */
11286 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011287expand_backtick(
11288 garray_T *gap,
11289 char_u *pat,
11290 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011291{
11292 char_u *p;
11293 char_u *cmd;
11294 char_u *buffer;
11295 int cnt = 0;
11296 int i;
11297
11298 /* Create the command: lop off the backticks. */
11299 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
11300 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011301 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011302
11303#ifdef FEAT_EVAL
11304 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011305 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011306 else
11307#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011308 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011309 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310 vim_free(cmd);
11311 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011312 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313
11314 cmd = buffer;
11315 while (*cmd != NUL)
11316 {
11317 cmd = skipwhite(cmd); /* skip over white space */
11318 p = cmd;
11319 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
11320 ++p;
11321 /* add an entry if it is not empty */
11322 if (p > cmd)
11323 {
11324 i = *p;
11325 *p = NUL;
11326 addfile(gap, cmd, flags);
11327 *p = i;
11328 ++cnt;
11329 }
11330 cmd = p;
11331 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
11332 ++cmd;
11333 }
11334
11335 vim_free(buffer);
11336 return cnt;
11337}
11338# endif /* VIM_BACKTICK */
11339
11340/*
11341 * Add a file to a file list. Accepted flags:
11342 * EW_DIR add directories
11343 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011344 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345 * EW_NOTFOUND add even when it doesn't exist
11346 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011347 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011348 */
11349 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011350addfile(
11351 garray_T *gap,
11352 char_u *f, /* filename */
11353 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354{
11355 char_u *p;
11356 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011357 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011358
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011359 /* if the file/dir/link doesn't exist, may not add it */
11360 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011361 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011362 return;
11363
11364#ifdef FNAME_ILLEGAL
11365 /* if the file/dir contains illegal characters, don't add it */
11366 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11367 return;
11368#endif
11369
11370 isdir = mch_isdir(f);
11371 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11372 return;
11373
Bram Moolenaarb5971142015-03-21 17:32:19 +010011374 /* If the file isn't executable, may not add it. Do accept directories.
11375 * When invoked from expand_shellcmd() do not use $PATH. */
11376 if (!isdir && (flags & EW_EXEC)
11377 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011378 return;
11379
Bram Moolenaar071d4272004-06-13 20:20:40 +000011380 /* Make room for another item in the file list. */
11381 if (ga_grow(gap, 1) == FAIL)
11382 return;
11383
11384 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11385 if (p == NULL)
11386 return;
11387
11388 STRCPY(p, f);
11389#ifdef BACKSLASH_IN_FILENAME
11390 slash_adjust(p);
11391#endif
11392 /*
11393 * Append a slash or backslash after directory names if none is present.
11394 */
11395#ifndef DONT_ADD_PATHSEP_TO_DIR
11396 if (isdir && (flags & EW_ADDSLASH))
11397 add_pathsep(p);
11398#endif
11399 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011400}
11401#endif /* !NO_EXPANDPATH */
11402
11403#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11404
11405#ifndef SEEK_SET
11406# define SEEK_SET 0
11407#endif
11408#ifndef SEEK_END
11409# define SEEK_END 2
11410#endif
11411
11412/*
11413 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011414 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11415 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011416 * Returns an allocated string, or NULL for error.
11417 */
11418 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011419get_cmd_output(
11420 char_u *cmd,
11421 char_u *infile, /* optional input file name */
11422 int flags, /* can be SHELL_SILENT */
11423 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011424{
11425 char_u *tempname;
11426 char_u *command;
11427 char_u *buffer = NULL;
11428 int len;
11429 int i = 0;
11430 FILE *fd;
11431
11432 if (check_restricted() || check_secure())
11433 return NULL;
11434
11435 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011436 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011438 emsg(_(e_notmp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439 return NULL;
11440 }
11441
11442 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011443 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444 if (command == NULL)
11445 goto done;
11446
11447 /*
11448 * Call the shell to execute the command (errors are ignored).
11449 * Don't check timestamps here.
11450 */
11451 ++no_check_timestamps;
11452 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11453 --no_check_timestamps;
11454
11455 vim_free(command);
11456
11457 /*
11458 * read the names from the file into memory
11459 */
11460# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011461 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011462 fd = mch_fopen((char *)tempname, "r");
11463# else
11464 fd = mch_fopen((char *)tempname, READBIN);
11465# endif
11466
11467 if (fd == NULL)
11468 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011469 semsg(_(e_notopen), tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470 goto done;
11471 }
11472
11473 fseek(fd, 0L, SEEK_END);
11474 len = ftell(fd); /* get size of temp file */
11475 fseek(fd, 0L, SEEK_SET);
11476
11477 buffer = alloc(len + 1);
11478 if (buffer != NULL)
11479 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11480 fclose(fd);
11481 mch_remove(tempname);
11482 if (buffer == NULL)
11483 goto done;
11484#ifdef VMS
11485 len = i; /* VMS doesn't give us what we asked for... */
11486#endif
11487 if (i != len)
11488 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011489 semsg(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +010011490 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011491 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011492 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011493 {
11494 /* Change NUL into SOH, otherwise the string is truncated. */
11495 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011496 if (buffer[i] == NUL)
11497 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011498
Bram Moolenaar162bd912010-07-28 22:29:10 +020011499 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011500 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011501 else
11502 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503
11504done:
11505 vim_free(tempname);
11506 return buffer;
11507}
11508#endif
11509
11510/*
11511 * Free the list of files returned by expand_wildcards() or other expansion
11512 * functions.
11513 */
11514 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011515FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011516{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011517 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011519 while (count--)
11520 vim_free(files[count]);
11521 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011522}
11523
11524/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011525 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011526 * Don't do this when still processing a command or a mapping.
11527 * Don't do this when inside a ":normal" command.
11528 */
11529 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011530goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011531{
11532 return (p_im && stuff_empty() && typebuf_typed());
11533}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011534
11535/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011536 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011537 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11538 * - Remove any argument. E.g., "csh -f" -> "csh".
11539 * But don't allow a space in the path, so that this works:
11540 * "/usr/bin/csh --rcfile ~/.cshrc"
11541 * But don't do that for Windows, it's common to have a space in the path.
11542 */
11543 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011544get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011545{
11546 char_u *p;
11547
11548#ifdef WIN3264
11549 p = gettail(p_sh);
11550 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11551#else
11552 p = skiptowhite(p_sh);
11553 if (*p == NUL)
11554 {
11555 /* No white space, use the tail. */
11556 p = vim_strsave(gettail(p_sh));
11557 }
11558 else
11559 {
11560 char_u *p1, *p2;
11561
11562 /* Find the last path separator before the space. */
11563 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011564 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011565 if (vim_ispathsep(*p2))
11566 p1 = p2 + 1;
11567 p = vim_strnsave(p1, (int)(p - p1));
11568 }
11569#endif
11570 return p;
11571}