blob: d797a0bc65ad5714df8f16aa1767a21f22a1c294 [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 Moolenaar06ae70d2013-06-17 19:26:36 +020023#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010024static void init_users(void);
Bram Moolenaar06ae70d2013-06-17 19:26:36 +020025#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010026static int copy_indent(int size, char_u *src);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027
Bram Moolenaar24305862012-08-15 14:05:05 +020028/* All user names (for ~user completion as done by shell). */
29#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
30static garray_T ga_users;
31#endif
32
Bram Moolenaar071d4272004-06-13 20:20:40 +000033/*
34 * Count the size (in window cells) of the indent in the current line.
35 */
36 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010037get_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000038{
Bram Moolenaar597a4222014-06-25 14:39:50 +020039 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040}
41
42/*
43 * Count the size (in window cells) of the indent in line "lnum".
44 */
45 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010046get_indent_lnum(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000047{
Bram Moolenaar597a4222014-06-25 14:39:50 +020048 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000049}
50
51#if defined(FEAT_FOLDING) || defined(PROTO)
52/*
53 * Count the size (in window cells) of the indent in line "lnum" of buffer
54 * "buf".
55 */
56 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010057get_indent_buf(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000058{
Bram Moolenaar597a4222014-06-25 14:39:50 +020059 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000060}
61#endif
62
63/*
64 * count the size (in window cells) of the indent in line "ptr", with
65 * 'tabstop' at "ts"
66 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +000067 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010068get_indent_str(
69 char_u *ptr,
70 int ts,
71 int list) /* if TRUE, count only screen size for tabs */
Bram Moolenaar071d4272004-06-13 20:20:40 +000072{
73 int count = 0;
74
75 for ( ; *ptr; ++ptr)
76 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020077 if (*ptr == TAB)
78 {
79 if (!list || lcs_tab1) /* count a tab for what it is worth */
80 count += ts - (count % ts);
81 else
Bram Moolenaare4df1642014-08-29 12:58:44 +020082 /* In list mode, when tab is not set, count screen char width
83 * for Tab, displays: ^I */
Bram Moolenaar597a4222014-06-25 14:39:50 +020084 count += ptr2cells(ptr);
85 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000086 else if (*ptr == ' ')
87 ++count; /* count a space for one */
88 else
89 break;
90 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +000091 return count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000092}
93
94/*
95 * Set the indent of the current line.
96 * Leaves the cursor on the first non-blank in the line.
97 * Caller must take care of undo.
98 * "flags":
99 * SIN_CHANGED: call changed_bytes() if the line was changed.
100 * SIN_INSERT: insert the indent in front of the line.
101 * SIN_UNDO: save line for undo before changing it.
102 * Returns TRUE if the line was changed.
103 */
104 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100105set_indent(
106 int size, /* measured in spaces */
107 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108{
109 char_u *p;
110 char_u *newline;
111 char_u *oldline;
112 char_u *s;
113 int todo;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000114 int ind_len; /* measured in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 int line_len;
116 int doit = FALSE;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000117 int ind_done = 0; /* measured in spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118 int tab_pad;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000119 int retval = FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000120 int orig_char_len = -1; /* number of initial whitespace chars when
Bram Moolenaar5002c292007-07-24 13:26:15 +0000121 'et' and 'pi' are both set */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122
123 /*
124 * First check if there is anything to do and compute the number of
125 * characters needed for the indent.
126 */
127 todo = size;
128 ind_len = 0;
129 p = oldline = ml_get_curline();
130
131 /* Calculate the buffer size for the new indent, and check to see if it
132 * isn't already set */
133
Bram Moolenaar5002c292007-07-24 13:26:15 +0000134 /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and
135 * 'preserveindent' are set count the number of characters at the
136 * beginning of the line to be copied */
137 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 {
139 /* If 'preserveindent' is set then reuse as much as possible of
140 * the existing indent structure for the new indent */
141 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
142 {
143 ind_done = 0;
144
145 /* count as many characters as we can use */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100146 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 {
148 if (*p == TAB)
149 {
150 tab_pad = (int)curbuf->b_p_ts
151 - (ind_done % (int)curbuf->b_p_ts);
152 /* stop if this tab will overshoot the target */
153 if (todo < tab_pad)
154 break;
155 todo -= tab_pad;
156 ++ind_len;
157 ind_done += tab_pad;
158 }
159 else
160 {
161 --todo;
162 ++ind_len;
163 ++ind_done;
164 }
165 ++p;
166 }
167
Bram Moolenaar5002c292007-07-24 13:26:15 +0000168 /* Set initial number of whitespace chars to copy if we are
169 * preserving indent but expandtab is set */
170 if (curbuf->b_p_et)
171 orig_char_len = ind_len;
172
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 /* Fill to next tabstop with a tab, if possible */
174 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000175 if (todo >= tab_pad && orig_char_len == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 {
177 doit = TRUE;
178 todo -= tab_pad;
179 ++ind_len;
180 /* ind_done += tab_pad; */
181 }
182 }
183
184 /* count tabs required for indent */
185 while (todo >= (int)curbuf->b_p_ts)
186 {
187 if (*p != TAB)
188 doit = TRUE;
189 else
190 ++p;
191 todo -= (int)curbuf->b_p_ts;
192 ++ind_len;
193 /* ind_done += (int)curbuf->b_p_ts; */
194 }
195 }
196 /* count spaces required for indent */
197 while (todo > 0)
198 {
199 if (*p != ' ')
200 doit = TRUE;
201 else
202 ++p;
203 --todo;
204 ++ind_len;
205 /* ++ind_done; */
206 }
207
208 /* Return if the indent is OK already. */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100209 if (!doit && !VIM_ISWHITE(*p) && !(flags & SIN_INSERT))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 return FALSE;
211
212 /* Allocate memory for the new line. */
213 if (flags & SIN_INSERT)
214 p = oldline;
215 else
216 p = skipwhite(p);
217 line_len = (int)STRLEN(p) + 1;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000218
219 /* If 'preserveindent' and 'expandtab' are both set keep the original
220 * characters and allocate accordingly. We will fill the rest with spaces
221 * after the if (!curbuf->b_p_et) below. */
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000222 if (orig_char_len != -1)
Bram Moolenaar5002c292007-07-24 13:26:15 +0000223 {
224 newline = alloc(orig_char_len + size - ind_done + line_len);
225 if (newline == NULL)
226 return FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000227 todo = size - ind_done;
228 ind_len = orig_char_len + todo; /* Set total length of indent in
229 * characters, which may have been
230 * undercounted until now */
Bram Moolenaar5002c292007-07-24 13:26:15 +0000231 p = oldline;
232 s = newline;
233 while (orig_char_len > 0)
234 {
235 *s++ = *p++;
236 orig_char_len--;
237 }
Bram Moolenaar913626c2008-01-03 11:43:42 +0000238
Bram Moolenaar5002c292007-07-24 13:26:15 +0000239 /* Skip over any additional white space (useful when newindent is less
240 * than old) */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100241 while (VIM_ISWHITE(*p))
Bram Moolenaar913626c2008-01-03 11:43:42 +0000242 ++p;
Bram Moolenaarcc00b952007-08-11 12:32:57 +0000243
Bram Moolenaar5002c292007-07-24 13:26:15 +0000244 }
245 else
246 {
247 todo = size;
248 newline = alloc(ind_len + line_len);
249 if (newline == NULL)
250 return FALSE;
251 s = newline;
252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253
254 /* Put the characters in the new line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 /* if 'expandtab' isn't set: use TABs */
256 if (!curbuf->b_p_et)
257 {
258 /* If 'preserveindent' is set then reuse as much as possible of
259 * the existing indent structure for the new indent */
260 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
261 {
262 p = oldline;
263 ind_done = 0;
264
Bram Moolenaar1c465442017-03-12 20:10:05 +0100265 while (todo > 0 && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 {
267 if (*p == TAB)
268 {
269 tab_pad = (int)curbuf->b_p_ts
270 - (ind_done % (int)curbuf->b_p_ts);
271 /* stop if this tab will overshoot the target */
272 if (todo < tab_pad)
273 break;
274 todo -= tab_pad;
275 ind_done += tab_pad;
276 }
277 else
278 {
279 --todo;
280 ++ind_done;
281 }
282 *s++ = *p++;
283 }
284
285 /* Fill to next tabstop with a tab, if possible */
286 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
287 if (todo >= tab_pad)
288 {
289 *s++ = TAB;
290 todo -= tab_pad;
291 }
292
293 p = skipwhite(p);
294 }
295
296 while (todo >= (int)curbuf->b_p_ts)
297 {
298 *s++ = TAB;
299 todo -= (int)curbuf->b_p_ts;
300 }
301 }
302 while (todo > 0)
303 {
304 *s++ = ' ';
305 --todo;
306 }
307 mch_memmove(s, p, (size_t)line_len);
308
309 /* Replace the line (unless undo fails). */
310 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
311 {
312 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
313 if (flags & SIN_CHANGED)
314 changed_bytes(curwin->w_cursor.lnum, 0);
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200315 /* Correct saved cursor position if it is in this line. */
316 if (saved_cursor.lnum == curwin->w_cursor.lnum)
317 {
318 if (saved_cursor.col >= (colnr_T)(p - oldline))
319 /* cursor was after the indent, adjust for the number of
320 * bytes added/removed */
321 saved_cursor.col += ind_len - (colnr_T)(p - oldline);
322 else if (saved_cursor.col >= (colnr_T)(s - newline))
323 /* cursor was in the indent, and is now after it, put it back
324 * at the start of the indent (replacing spaces with TAB) */
325 saved_cursor.col = (colnr_T)(s - newline);
326 }
Bram Moolenaar5409c052005-03-18 20:27:04 +0000327 retval = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 }
329 else
330 vim_free(newline);
331
332 curwin->w_cursor.col = ind_len;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000333 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334}
335
336/*
337 * Copy the indent from ptr to the current line (and fill to size)
338 * Leaves the cursor on the first non-blank in the line.
339 * Returns TRUE if the line was changed.
340 */
341 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100342copy_indent(int size, char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343{
344 char_u *p = NULL;
345 char_u *line = NULL;
346 char_u *s;
347 int todo;
348 int ind_len;
349 int line_len = 0;
350 int tab_pad;
351 int ind_done;
352 int round;
353
354 /* Round 1: compute the number of characters needed for the indent
355 * Round 2: copy the characters. */
356 for (round = 1; round <= 2; ++round)
357 {
358 todo = size;
359 ind_len = 0;
360 ind_done = 0;
361 s = src;
362
363 /* Count/copy the usable portion of the source line */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100364 while (todo > 0 && VIM_ISWHITE(*s))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 {
366 if (*s == TAB)
367 {
368 tab_pad = (int)curbuf->b_p_ts
369 - (ind_done % (int)curbuf->b_p_ts);
370 /* Stop if this tab will overshoot the target */
371 if (todo < tab_pad)
372 break;
373 todo -= tab_pad;
374 ind_done += tab_pad;
375 }
376 else
377 {
378 --todo;
379 ++ind_done;
380 }
381 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000382 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 *p++ = *s;
384 ++s;
385 }
386
387 /* Fill to next tabstop with a tab, if possible */
388 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200389 if (todo >= tab_pad && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 {
391 todo -= tab_pad;
392 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000393 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 *p++ = TAB;
395 }
396
397 /* Add tabs required for indent */
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200398 while (todo >= (int)curbuf->b_p_ts && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 {
400 todo -= (int)curbuf->b_p_ts;
401 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000402 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 *p++ = TAB;
404 }
405
406 /* Count/add spaces required for indent */
407 while (todo > 0)
408 {
409 --todo;
410 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000411 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 *p++ = ' ';
413 }
414
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000415 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 {
417 /* Allocate memory for the result: the copied indent, new indent
418 * and the rest of the line. */
419 line_len = (int)STRLEN(ml_get_curline()) + 1;
420 line = alloc(ind_len + line_len);
421 if (line == NULL)
422 return FALSE;
423 p = line;
424 }
425 }
426
427 /* Append the original line */
428 mch_memmove(p, ml_get_curline(), (size_t)line_len);
429
430 /* Replace the line */
431 ml_replace(curwin->w_cursor.lnum, line, FALSE);
432
433 /* Put the cursor after the indent. */
434 curwin->w_cursor.col = ind_len;
435 return TRUE;
436}
437
438/*
439 * Return the indent of the current line after a number. Return -1 if no
440 * number was found. Used for 'n' in 'formatoptions': numbered list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000441 * Since a pattern is used it can actually handle more than numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 */
443 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100444get_number_indent(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 colnr_T col;
447 pos_T pos;
448
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200449 regmatch_T regmatch;
450 int lead_len = 0; /* length of comment leader */
451
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 if (lnum > curbuf->b_ml.ml_line_count)
453 return -1;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000454 pos.lnum = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200455
456#ifdef FEAT_COMMENTS
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200457 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
458 if ((State & INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200459 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000460#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200461 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
462 if (regmatch.regprog != NULL)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200463 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200464 regmatch.rm_ic = FALSE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200465
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200466 /* vim_regexec() expects a pointer to a line. This lets us
467 * start matching for the flp beyond any comment leader... */
468 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200469 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200470 pos.lnum = lnum;
471 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200472#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200473 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200474#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200475 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200476 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200477 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000478
479 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 getvcol(curwin, &pos, &col, NULL, NULL);
482 return (int)col;
483}
484
Bram Moolenaar597a4222014-06-25 14:39:50 +0200485#if defined(FEAT_LINEBREAK) || defined(PROTO)
486/*
487 * Return appropriate space number for breakindent, taking influencing
488 * parameters into account. Window must be specified, since it is not
489 * necessarily always the current one.
490 */
491 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100492get_breakindent_win(
493 win_T *wp,
494 char_u *line) /* start of the line */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200495{
496 static int prev_indent = 0; /* cached indent value */
497 static long prev_ts = 0L; /* cached tabstop value */
498 static char_u *prev_line = NULL; /* cached pointer to line */
Bram Moolenaar79518e22017-02-17 16:31:35 +0100499 static varnumber_T prev_tick = 0; /* changedtick of cached value */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200500 int bri = 0;
501 /* window width minus window margin space, i.e. what rests for text */
Bram Moolenaar02631462017-09-22 15:20:32 +0200502 const int eff_wwidth = wp->w_width
Bram Moolenaar597a4222014-06-25 14:39:50 +0200503 - ((wp->w_p_nu || wp->w_p_rnu)
504 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
505 ? number_width(wp) + 1 : 0);
506
507 /* used cached indent, unless pointer or 'tabstop' changed */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200508 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100509 || prev_tick != CHANGEDTICK(wp->w_buffer))
Bram Moolenaar597a4222014-06-25 14:39:50 +0200510 {
511 prev_line = line;
512 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100513 prev_tick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar597a4222014-06-25 14:39:50 +0200514 prev_indent = get_indent_str(line,
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200515 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
Bram Moolenaar597a4222014-06-25 14:39:50 +0200516 }
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200517 bri = prev_indent + wp->w_p_brishift;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200518
519 /* indent minus the length of the showbreak string */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200520 if (wp->w_p_brisbr)
521 bri -= vim_strsize(p_sbr);
522
523 /* Add offset for number column, if 'n' is in 'cpoptions' */
524 bri += win_col_off2(wp);
525
526 /* never indent past left window margin */
527 if (bri < 0)
528 bri = 0;
529 /* always leave at least bri_min characters on the left,
530 * if text width is sufficient */
531 else if (bri > eff_wwidth - wp->w_p_brimin)
532 bri = (eff_wwidth - wp->w_p_brimin < 0)
533 ? 0 : eff_wwidth - wp->w_p_brimin;
534
535 return bri;
536}
537#endif
538
539
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
541
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100542static int cin_is_cinword(char_u *line);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543
544/*
545 * Return TRUE if the string "line" starts with a word from 'cinwords'.
546 */
547 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100548cin_is_cinword(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549{
550 char_u *cinw;
551 char_u *cinw_buf;
552 int cinw_len;
553 int retval = FALSE;
554 int len;
555
556 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1;
557 cinw_buf = alloc((unsigned)cinw_len);
558 if (cinw_buf != NULL)
559 {
560 line = skipwhite(line);
561 for (cinw = curbuf->b_p_cinw; *cinw; )
562 {
563 len = copy_option_part(&cinw, cinw_buf, cinw_len, ",");
564 if (STRNCMP(line, cinw_buf, len) == 0
565 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1])))
566 {
567 retval = TRUE;
568 break;
569 }
570 }
571 vim_free(cinw_buf);
572 }
573 return retval;
574}
575#endif
576
577/*
578 * open_line: Add a new line below or above the current line.
579 *
580 * For VREPLACE mode, we only add a new line when we get to the end of the
581 * file, otherwise we just start replacing the next line.
582 *
583 * Caller must take care of undo. Since VREPLACE may affect any number of
584 * lines however, it may call u_save_cursor() again when starting to change a
585 * new line.
586 * "flags": OPENLINE_DELSPACES delete spaces after cursor
587 * OPENLINE_DO_COM format comments
588 * OPENLINE_KEEPTRAIL keep trailing spaces
589 * OPENLINE_MARKFIX adjust mark positions after the line break
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200590 * OPENLINE_COM_LIST format comments with list or 2nd line indent
591 *
592 * "second_line_indent": indent for after ^^D in Insert mode or if flag
593 * OPENLINE_COM_LIST
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 *
Bram Moolenaar24a2d722018-04-24 19:36:43 +0200595 * Return OK for success, FAIL for failure
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 */
597 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100598open_line(
599 int dir, /* FORWARD or BACKWARD */
600 int flags,
601 int second_line_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602{
603 char_u *saved_line; /* copy of the original line */
604 char_u *next_line = NULL; /* copy of the next line */
605 char_u *p_extra = NULL; /* what goes to next line */
606 int less_cols = 0; /* less columns for mark in new line */
607 int less_cols_off = 0; /* columns to skip for mark adjust */
608 pos_T old_cursor; /* old cursor position */
609 int newcol = 0; /* new cursor column */
610 int newindent = 0; /* auto-indent of the new line */
611 int n;
612 int trunc_line = FALSE; /* truncate current line afterwards */
Bram Moolenaar24a2d722018-04-24 19:36:43 +0200613 int retval = FAIL; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614#ifdef FEAT_COMMENTS
615 int extra_len = 0; /* length of p_extra string */
616 int lead_len; /* length of comment leader */
617 char_u *lead_flags; /* position in 'comments' for comment leader */
618 char_u *leader = NULL; /* copy of comment leader */
619#endif
620 char_u *allocated = NULL; /* allocated memory */
621#if defined(FEAT_SMARTINDENT) || defined(FEAT_VREPLACE) || defined(FEAT_LISP) \
622 || defined(FEAT_CINDENT) || defined(FEAT_COMMENTS)
623 char_u *p;
624#endif
625 int saved_char = NUL; /* init for GCC */
626#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
627 pos_T *pos;
628#endif
629#ifdef FEAT_SMARTINDENT
630 int do_si = (!p_paste && curbuf->b_p_si
631# ifdef FEAT_CINDENT
632 && !curbuf->b_p_cin
633# endif
Bram Moolenaar69a76fe2017-08-03 17:54:03 +0200634# ifdef FEAT_EVAL
635 && *curbuf->b_p_inde == NUL
636# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 );
638 int no_si = FALSE; /* reset did_si afterwards */
639 int first_char = NUL; /* init for GCC */
640#endif
641#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
642 int vreplace_mode;
643#endif
644 int did_append; /* appended a new line */
645 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */
646
647 /*
648 * make a copy of the current line so we can mess with it
649 */
650 saved_line = vim_strsave(ml_get_curline());
651 if (saved_line == NULL) /* out of memory! */
652 return FALSE;
653
654#ifdef FEAT_VREPLACE
655 if (State & VREPLACE_FLAG)
656 {
657 /*
658 * With VREPLACE we make a copy of the next line, which we will be
659 * starting to replace. First make the new line empty and let vim play
660 * with the indenting and comment leader to its heart's content. Then
661 * we grab what it ended up putting on the new line, put back the
662 * original line, and call ins_char() to put each new character onto
663 * the line, replacing what was there before and pushing the right
664 * stuff onto the replace stack. -- webb.
665 */
666 if (curwin->w_cursor.lnum < orig_line_count)
667 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
668 else
669 next_line = vim_strsave((char_u *)"");
670 if (next_line == NULL) /* out of memory! */
671 goto theend;
672
673 /*
674 * In VREPLACE mode, a NL replaces the rest of the line, and starts
675 * replacing the next line, so push all of the characters left on the
676 * line onto the replace stack. We'll push any other characters that
677 * might be replaced at the start of the next line (due to autoindent
678 * etc) a bit later.
679 */
680 replace_push(NUL); /* Call twice because BS over NL expects it */
681 replace_push(NUL);
682 p = saved_line + curwin->w_cursor.col;
683 while (*p != NUL)
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000684 {
685#ifdef FEAT_MBYTE
686 if (has_mbyte)
687 p += replace_push_mb(p);
688 else
689#endif
690 replace_push(*p++);
691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 saved_line[curwin->w_cursor.col] = NUL;
693 }
694#endif
695
696 if ((State & INSERT)
697#ifdef FEAT_VREPLACE
698 && !(State & VREPLACE_FLAG)
699#endif
700 )
701 {
702 p_extra = saved_line + curwin->w_cursor.col;
703#ifdef FEAT_SMARTINDENT
704 if (do_si) /* need first char after new line break */
705 {
706 p = skipwhite(p_extra);
707 first_char = *p;
708 }
709#endif
710#ifdef FEAT_COMMENTS
711 extra_len = (int)STRLEN(p_extra);
712#endif
713 saved_char = *p_extra;
714 *p_extra = NUL;
715 }
716
717 u_clearline(); /* cannot do "U" command when adding lines */
718#ifdef FEAT_SMARTINDENT
719 did_si = FALSE;
720#endif
721 ai_col = 0;
722
723 /*
724 * If we just did an auto-indent, then we didn't type anything on
725 * the prior line, and it should be truncated. Do this even if 'ai' is not
726 * set because automatically inserting a comment leader also sets did_ai.
727 */
728 if (dir == FORWARD && did_ai)
729 trunc_line = TRUE;
730
731 /*
732 * If 'autoindent' and/or 'smartindent' is set, try to figure out what
733 * indent to use for the new line.
734 */
735 if (curbuf->b_p_ai
736#ifdef FEAT_SMARTINDENT
737 || do_si
738#endif
739 )
740 {
741 /*
742 * count white space on current line
743 */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200744 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200745 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
746 newindent = second_line_indent; /* for ^^D command in insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747
748#ifdef FEAT_SMARTINDENT
749 /*
750 * Do smart indenting.
751 * In insert/replace mode (only when dir == FORWARD)
752 * we may move some text to the next line. If it starts with '{'
753 * don't add an indent. Fixes inserting a NL before '{' in line
754 * "if (condition) {"
755 */
756 if (!trunc_line && do_si && *saved_line != NUL
757 && (p_extra == NULL || first_char != '{'))
758 {
759 char_u *ptr;
760 char_u last_char;
761
762 old_cursor = curwin->w_cursor;
763 ptr = saved_line;
764# ifdef FEAT_COMMENTS
765 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200766 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 else
768 lead_len = 0;
769# endif
770 if (dir == FORWARD)
771 {
772 /*
773 * Skip preprocessor directives, unless they are
774 * recognised as comments.
775 */
776 if (
777# ifdef FEAT_COMMENTS
778 lead_len == 0 &&
779# endif
780 ptr[0] == '#')
781 {
782 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
783 ptr = ml_get(--curwin->w_cursor.lnum);
784 newindent = get_indent();
785 }
786# ifdef FEAT_COMMENTS
787 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200788 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 else
790 lead_len = 0;
791 if (lead_len > 0)
792 {
793 /*
794 * This case gets the following right:
795 * \*
796 * * A comment (read '\' as '/').
797 * *\
798 * #define IN_THE_WAY
799 * This should line up here;
800 */
801 p = skipwhite(ptr);
802 if (p[0] == '/' && p[1] == '*')
803 p++;
804 if (p[0] == '*')
805 {
806 for (p++; *p; p++)
807 {
808 if (p[0] == '/' && p[-1] == '*')
809 {
810 /*
811 * End of C comment, indent should line up
812 * with the line containing the start of
813 * the comment
814 */
815 curwin->w_cursor.col = (colnr_T)(p - ptr);
816 if ((pos = findmatch(NULL, NUL)) != NULL)
817 {
818 curwin->w_cursor.lnum = pos->lnum;
819 newindent = get_indent();
820 }
821 }
822 }
823 }
824 }
825 else /* Not a comment line */
826# endif
827 {
828 /* Find last non-blank in line */
829 p = ptr + STRLEN(ptr) - 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100830 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 --p;
832 last_char = *p;
833
834 /*
835 * find the character just before the '{' or ';'
836 */
837 if (last_char == '{' || last_char == ';')
838 {
839 if (p > ptr)
840 --p;
Bram Moolenaar1c465442017-03-12 20:10:05 +0100841 while (p > ptr && VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 --p;
843 }
844 /*
845 * Try to catch lines that are split over multiple
846 * lines. eg:
847 * if (condition &&
848 * condition) {
849 * Should line up here!
850 * }
851 */
852 if (*p == ')')
853 {
854 curwin->w_cursor.col = (colnr_T)(p - ptr);
855 if ((pos = findmatch(NULL, '(')) != NULL)
856 {
857 curwin->w_cursor.lnum = pos->lnum;
858 newindent = get_indent();
859 ptr = ml_get_curline();
860 }
861 }
862 /*
863 * If last character is '{' do indent, without
864 * checking for "if" and the like.
865 */
866 if (last_char == '{')
867 {
868 did_si = TRUE; /* do indent */
869 no_si = TRUE; /* don't delete it when '{' typed */
870 }
871 /*
872 * Look for "if" and the like, use 'cinwords'.
873 * Don't do this if the previous line ended in ';' or
874 * '}'.
875 */
876 else if (last_char != ';' && last_char != '}'
877 && cin_is_cinword(ptr))
878 did_si = TRUE;
879 }
880 }
881 else /* dir == BACKWARD */
882 {
883 /*
884 * Skip preprocessor directives, unless they are
885 * recognised as comments.
886 */
887 if (
888# ifdef FEAT_COMMENTS
889 lead_len == 0 &&
890# endif
891 ptr[0] == '#')
892 {
893 int was_backslashed = FALSE;
894
895 while ((ptr[0] == '#' || was_backslashed) &&
896 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
897 {
898 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
899 was_backslashed = TRUE;
900 else
901 was_backslashed = FALSE;
902 ptr = ml_get(++curwin->w_cursor.lnum);
903 }
904 if (was_backslashed)
905 newindent = 0; /* Got to end of file */
906 else
907 newindent = get_indent();
908 }
909 p = skipwhite(ptr);
910 if (*p == '}') /* if line starts with '}': do indent */
911 did_si = TRUE;
912 else /* can delete indent when '{' typed */
913 can_si_back = TRUE;
914 }
915 curwin->w_cursor = old_cursor;
916 }
917 if (do_si)
918 can_si = TRUE;
919#endif /* FEAT_SMARTINDENT */
920
921 did_ai = TRUE;
922 }
923
924#ifdef FEAT_COMMENTS
925 /*
926 * Find out if the current line starts with a comment leader.
927 * This may then be inserted in front of the new line.
928 */
929 end_comment_pending = NUL;
930 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200931 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 else
933 lead_len = 0;
934 if (lead_len > 0)
935 {
936 char_u *lead_repl = NULL; /* replaces comment leader */
937 int lead_repl_len = 0; /* length of *lead_repl */
938 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
939 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
940 char_u *comment_end = NULL; /* where lead_end has been found */
941 int extra_space = FALSE; /* append extra space */
942 int current_flag;
943 int require_blank = FALSE; /* requires blank after middle */
944 char_u *p2;
945
946 /*
947 * If the comment leader has the start, middle or end flag, it may not
948 * be used or may be replaced with the middle leader.
949 */
950 for (p = lead_flags; *p && *p != ':'; ++p)
951 {
952 if (*p == COM_BLANK)
953 {
954 require_blank = TRUE;
955 continue;
956 }
957 if (*p == COM_START || *p == COM_MIDDLE)
958 {
959 current_flag = *p;
960 if (*p == COM_START)
961 {
962 /*
963 * Doing "O" on a start of comment does not insert leader.
964 */
965 if (dir == BACKWARD)
966 {
967 lead_len = 0;
968 break;
969 }
970
971 /* find start of middle part */
972 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
973 require_blank = FALSE;
974 }
975
976 /*
977 * Isolate the strings of the middle and end leader.
978 */
979 while (*p && p[-1] != ':') /* find end of middle flags */
980 {
981 if (*p == COM_BLANK)
982 require_blank = TRUE;
983 ++p;
984 }
985 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
986
987 while (*p && p[-1] != ':') /* find end of end flags */
988 {
989 /* Check whether we allow automatic ending of comments */
990 if (*p == COM_AUTO_END)
991 end_comment_pending = -1; /* means we want to set it */
992 ++p;
993 }
994 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
995
996 if (end_comment_pending == -1) /* we can set it now */
997 end_comment_pending = lead_end[n - 1];
998
999 /*
1000 * If the end of the comment is in the same line, don't use
1001 * the comment leader.
1002 */
1003 if (dir == FORWARD)
1004 {
1005 for (p = saved_line + lead_len; *p; ++p)
1006 if (STRNCMP(p, lead_end, n) == 0)
1007 {
1008 comment_end = p;
1009 lead_len = 0;
1010 break;
1011 }
1012 }
1013
1014 /*
1015 * Doing "o" on a start of comment inserts the middle leader.
1016 */
1017 if (lead_len > 0)
1018 {
1019 if (current_flag == COM_START)
1020 {
1021 lead_repl = lead_middle;
1022 lead_repl_len = (int)STRLEN(lead_middle);
1023 }
1024
1025 /*
1026 * If we have hit RETURN immediately after the start
1027 * comment leader, then put a space after the middle
1028 * comment leader on the next line.
1029 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001030 if (!VIM_ISWHITE(saved_line[lead_len - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 && ((p_extra != NULL
1032 && (int)curwin->w_cursor.col == lead_len)
1033 || (p_extra == NULL
1034 && saved_line[lead_len] == NUL)
1035 || require_blank))
1036 extra_space = TRUE;
1037 }
1038 break;
1039 }
1040 if (*p == COM_END)
1041 {
1042 /*
1043 * Doing "o" on the end of a comment does not insert leader.
1044 * Remember where the end is, might want to use it to find the
1045 * start (for C-comments).
1046 */
1047 if (dir == FORWARD)
1048 {
1049 comment_end = skipwhite(saved_line);
1050 lead_len = 0;
1051 break;
1052 }
1053
1054 /*
1055 * Doing "O" on the end of a comment inserts the middle leader.
1056 * Find the string for the middle leader, searching backwards.
1057 */
1058 while (p > curbuf->b_p_com && *p != ',')
1059 --p;
1060 for (lead_repl = p; lead_repl > curbuf->b_p_com
1061 && lead_repl[-1] != ':'; --lead_repl)
1062 ;
1063 lead_repl_len = (int)(p - lead_repl);
1064
1065 /* We can probably always add an extra space when doing "O" on
1066 * the comment-end */
1067 extra_space = TRUE;
1068
1069 /* Check whether we allow automatic ending of comments */
1070 for (p2 = p; *p2 && *p2 != ':'; p2++)
1071 {
1072 if (*p2 == COM_AUTO_END)
1073 end_comment_pending = -1; /* means we want to set it */
1074 }
1075 if (end_comment_pending == -1)
1076 {
1077 /* Find last character in end-comment string */
1078 while (*p2 && *p2 != ',')
1079 p2++;
1080 end_comment_pending = p2[-1];
1081 }
1082 break;
1083 }
1084 if (*p == COM_FIRST)
1085 {
1086 /*
1087 * Comment leader for first line only: Don't repeat leader
1088 * when using "O", blank out leader when using "o".
1089 */
1090 if (dir == BACKWARD)
1091 lead_len = 0;
1092 else
1093 {
1094 lead_repl = (char_u *)"";
1095 lead_repl_len = 0;
1096 }
1097 break;
1098 }
1099 }
1100 if (lead_len)
1101 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001102 /* allocate buffer (may concatenate p_extra later) */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001103 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001104 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 allocated = leader; /* remember to free it later */
1106
1107 if (leader == NULL)
1108 lead_len = 0;
1109 else
1110 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001111 vim_strncpy(leader, saved_line, lead_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112
1113 /*
1114 * Replace leader with lead_repl, right or left adjusted
1115 */
1116 if (lead_repl != NULL)
1117 {
1118 int c = 0;
1119 int off = 0;
1120
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001121 for (p = lead_flags; *p != NUL && *p != ':'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 {
1123 if (*p == COM_RIGHT || *p == COM_LEFT)
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001124 c = *p++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 else if (VIM_ISDIGIT(*p) || *p == '-')
1126 off = getdigits(&p);
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001127 else
1128 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 }
1130 if (c == COM_RIGHT) /* right adjusted leader */
1131 {
1132 /* find last non-white in the leader to line up with */
1133 for (p = leader + lead_len - 1; p > leader
Bram Moolenaar1c465442017-03-12 20:10:05 +01001134 && VIM_ISWHITE(*p); --p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 ++p;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001137
1138#ifdef FEAT_MBYTE
1139 /* Compute the length of the replaced characters in
1140 * screen characters, not bytes. */
1141 {
1142 int repl_size = vim_strnsize(lead_repl,
1143 lead_repl_len);
1144 int old_size = 0;
1145 char_u *endp = p;
1146 int l;
1147
1148 while (old_size < repl_size && p > leader)
1149 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001150 MB_PTR_BACK(leader, p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001151 old_size += ptr2cells(p);
1152 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001153 l = lead_repl_len - (int)(endp - p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001154 if (l != 0)
1155 mch_memmove(endp + l, endp,
1156 (size_t)((leader + lead_len) - endp));
1157 lead_len += l;
1158 }
1159#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 if (p < leader + lead_repl_len)
1161 p = leader;
1162 else
1163 p -= lead_repl_len;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1166 if (p + lead_repl_len > leader + lead_len)
1167 p[lead_repl_len] = NUL;
1168
1169 /* blank-out any other chars from the old leader. */
1170 while (--p >= leader)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001171 {
1172#ifdef FEAT_MBYTE
1173 int l = mb_head_off(leader, p);
1174
1175 if (l > 1)
1176 {
1177 p -= l;
1178 if (ptr2cells(p) > 1)
1179 {
1180 p[1] = ' ';
1181 --l;
1182 }
1183 mch_memmove(p + 1, p + l + 1,
1184 (size_t)((leader + lead_len) - (p + l + 1)));
1185 lead_len -= l;
1186 *p = ' ';
1187 }
1188 else
1189#endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01001190 if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 }
1194 else /* left adjusted leader */
1195 {
1196 p = skipwhite(leader);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001197#ifdef FEAT_MBYTE
1198 /* Compute the length of the replaced characters in
1199 * screen characters, not bytes. Move the part that is
1200 * not to be overwritten. */
1201 {
1202 int repl_size = vim_strnsize(lead_repl,
1203 lead_repl_len);
1204 int i;
1205 int l;
1206
Bram Moolenaardc633cf2016-04-23 14:33:19 +02001207 for (i = 0; i < lead_len && p[i] != NUL; i += l)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001208 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001209 l = (*mb_ptr2len)(p + i);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001210 if (vim_strnsize(p, i + l) > repl_size)
1211 break;
1212 }
1213 if (i != lead_repl_len)
1214 {
1215 mch_memmove(p + lead_repl_len, p + i,
Bram Moolenaar2d7ff052009-11-17 15:08:26 +00001216 (size_t)(lead_len - i - (p - leader)));
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001217 lead_len += lead_repl_len - i;
1218 }
1219 }
1220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1222
1223 /* Replace any remaining non-white chars in the old
1224 * leader by spaces. Keep Tabs, the indent must
1225 * remain the same. */
1226 for (p += lead_repl_len; p < leader + lead_len; ++p)
Bram Moolenaar1c465442017-03-12 20:10:05 +01001227 if (!VIM_ISWHITE(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 {
1229 /* Don't put a space before a TAB. */
1230 if (p + 1 < leader + lead_len && p[1] == TAB)
1231 {
1232 --lead_len;
1233 mch_memmove(p, p + 1,
1234 (leader + lead_len) - p);
1235 }
1236 else
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001237 {
1238#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001239 int l = (*mb_ptr2len)(p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001240
1241 if (l > 1)
1242 {
1243 if (ptr2cells(p) > 1)
1244 {
1245 /* Replace a double-wide char with
1246 * two spaces */
1247 --l;
1248 *p++ = ' ';
1249 }
1250 mch_memmove(p + 1, p + l,
1251 (leader + lead_len) - p);
1252 lead_len -= l - 1;
1253 }
1254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 }
1258 *p = NUL;
1259 }
1260
1261 /* Recompute the indent, it may have changed. */
1262 if (curbuf->b_p_ai
1263#ifdef FEAT_SMARTINDENT
1264 || do_si
1265#endif
1266 )
Bram Moolenaar597a4222014-06-25 14:39:50 +02001267 newindent = get_indent_str(leader, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268
1269 /* Add the indent offset */
1270 if (newindent + off < 0)
1271 {
1272 off = -newindent;
1273 newindent = 0;
1274 }
1275 else
1276 newindent += off;
1277
1278 /* Correct trailing spaces for the shift, so that
1279 * alignment remains equal. */
1280 while (off > 0 && lead_len > 0
1281 && leader[lead_len - 1] == ' ')
1282 {
1283 /* Don't do it when there is a tab before the space */
1284 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1285 break;
1286 --lead_len;
1287 --off;
1288 }
1289
1290 /* If the leader ends in white space, don't add an
1291 * extra space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001292 if (lead_len > 0 && VIM_ISWHITE(leader[lead_len - 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 extra_space = FALSE;
1294 leader[lead_len] = NUL;
1295 }
1296
1297 if (extra_space)
1298 {
1299 leader[lead_len++] = ' ';
1300 leader[lead_len] = NUL;
1301 }
1302
1303 newcol = lead_len;
1304
1305 /*
1306 * if a new indent will be set below, remove the indent that
1307 * is in the comment leader
1308 */
1309 if (newindent
1310#ifdef FEAT_SMARTINDENT
1311 || did_si
1312#endif
1313 )
1314 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001315 while (lead_len && VIM_ISWHITE(*leader))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 {
1317 --lead_len;
1318 --newcol;
1319 ++leader;
1320 }
1321 }
1322
1323 }
1324#ifdef FEAT_SMARTINDENT
1325 did_si = can_si = FALSE;
1326#endif
1327 }
1328 else if (comment_end != NULL)
1329 {
1330 /*
1331 * We have finished a comment, so we don't use the leader.
1332 * If this was a C-comment and 'ai' or 'si' is set do a normal
1333 * indent to align with the line containing the start of the
1334 * comment.
1335 */
1336 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1337 (curbuf->b_p_ai
1338#ifdef FEAT_SMARTINDENT
1339 || do_si
1340#endif
1341 ))
1342 {
1343 old_cursor = curwin->w_cursor;
1344 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1345 if ((pos = findmatch(NULL, NUL)) != NULL)
1346 {
1347 curwin->w_cursor.lnum = pos->lnum;
1348 newindent = get_indent();
1349 }
1350 curwin->w_cursor = old_cursor;
1351 }
1352 }
1353 }
1354#endif
1355
1356 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */
1357 if (p_extra != NULL)
1358 {
1359 *p_extra = saved_char; /* restore char that NUL replaced */
1360
1361 /*
1362 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
1363 * non-blank.
1364 *
1365 * When in REPLACE mode, put the deleted blanks on the replace stack,
1366 * preceded by a NUL, so they can be put back when a BS is entered.
1367 */
1368 if (REPLACE_NORMAL(State))
1369 replace_push(NUL); /* end of extra blanks */
1370 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
1371 {
1372 while ((*p_extra == ' ' || *p_extra == '\t')
1373#ifdef FEAT_MBYTE
1374 && (!enc_utf8
1375 || !utf_iscomposing(utf_ptr2char(p_extra + 1)))
1376#endif
1377 )
1378 {
1379 if (REPLACE_NORMAL(State))
1380 replace_push(*p_extra);
1381 ++p_extra;
1382 ++less_cols_off;
1383 }
1384 }
1385 if (*p_extra != NUL)
1386 did_ai = FALSE; /* append some text, don't truncate now */
1387
1388 /* columns for marks adjusted for removed columns */
1389 less_cols = (int)(p_extra - saved_line);
1390 }
1391
1392 if (p_extra == NULL)
1393 p_extra = (char_u *)""; /* append empty line */
1394
1395#ifdef FEAT_COMMENTS
1396 /* concatenate leader and p_extra, if there is a leader */
1397 if (lead_len)
1398 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001399 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
1400 {
1401 int i;
Bram Moolenaar36105782012-06-14 20:59:25 +02001402 int padding = second_line_indent
1403 - (newindent + (int)STRLEN(leader));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001404
1405 /* Here whitespace is inserted after the comment char.
1406 * Below, set_indent(newindent, SIN_INSERT) will insert the
1407 * whitespace needed before the comment char. */
1408 for (i = 0; i < padding; i++)
1409 {
1410 STRCAT(leader, " ");
Bram Moolenaar5fb9ec52012-07-25 16:10:03 +02001411 less_cols--;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001412 newcol++;
1413 }
1414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 STRCAT(leader, p_extra);
1416 p_extra = leader;
1417 did_ai = TRUE; /* So truncating blanks works with comments */
1418 less_cols -= lead_len;
1419 }
1420 else
1421 end_comment_pending = NUL; /* turns out there was no leader */
1422#endif
1423
1424 old_cursor = curwin->w_cursor;
1425 if (dir == BACKWARD)
1426 --curwin->w_cursor.lnum;
1427#ifdef FEAT_VREPLACE
1428 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
1429#endif
1430 {
1431 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
1432 == FAIL)
1433 goto theend;
1434 /* Postpone calling changed_lines(), because it would mess up folding
Bram Moolenaar82faa252016-06-04 20:14:07 +02001435 * with markers.
1436 * Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01001437 * be marks there. But still needed in diff mode. */
1438 if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count
1439#ifdef FEAT_DIFF
1440 || curwin->w_p_diff
1441#endif
1442 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02001443 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 did_append = TRUE;
1445 }
1446#ifdef FEAT_VREPLACE
1447 else
1448 {
1449 /*
1450 * In VREPLACE mode we are starting to replace the next line.
1451 */
1452 curwin->w_cursor.lnum++;
1453 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1454 {
1455 /* In case we NL to a new line, BS to the previous one, and NL
1456 * again, we don't want to save the new line for undo twice.
1457 */
1458 (void)u_save_cursor(); /* errors are ignored! */
1459 vr_lines_changed++;
1460 }
1461 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1462 changed_bytes(curwin->w_cursor.lnum, 0);
1463 curwin->w_cursor.lnum--;
1464 did_append = FALSE;
1465 }
1466#endif
1467
1468 if (newindent
1469#ifdef FEAT_SMARTINDENT
1470 || did_si
1471#endif
1472 )
1473 {
1474 ++curwin->w_cursor.lnum;
1475#ifdef FEAT_SMARTINDENT
1476 if (did_si)
1477 {
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001478 int sw = (int)get_sw_value(curbuf);
Bram Moolenaar14f24742012-08-08 18:01:05 +02001479
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001481 newindent -= newindent % sw;
1482 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 }
1484#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001485 /* Copy the indent */
1486 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 {
1488 (void)copy_indent(newindent, saved_line);
1489
1490 /*
1491 * Set the 'preserveindent' option so that any further screwing
1492 * with the line doesn't entirely destroy our efforts to preserve
1493 * it. It gets restored at the function end.
1494 */
1495 curbuf->b_p_pi = TRUE;
1496 }
1497 else
1498 (void)set_indent(newindent, SIN_INSERT);
1499 less_cols -= curwin->w_cursor.col;
1500
1501 ai_col = curwin->w_cursor.col;
1502
1503 /*
1504 * In REPLACE mode, for each character in the new indent, there must
1505 * be a NUL on the replace stack, for when it is deleted with BS
1506 */
1507 if (REPLACE_NORMAL(State))
1508 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1509 replace_push(NUL);
1510 newcol += curwin->w_cursor.col;
1511#ifdef FEAT_SMARTINDENT
1512 if (no_si)
1513 did_si = FALSE;
1514#endif
1515 }
1516
1517#ifdef FEAT_COMMENTS
1518 /*
1519 * In REPLACE mode, for each character in the extra leader, there must be
1520 * a NUL on the replace stack, for when it is deleted with BS.
1521 */
1522 if (REPLACE_NORMAL(State))
1523 while (lead_len-- > 0)
1524 replace_push(NUL);
1525#endif
1526
1527 curwin->w_cursor = old_cursor;
1528
1529 if (dir == FORWARD)
1530 {
1531 if (trunc_line || (State & INSERT))
1532 {
1533 /* truncate current line at cursor */
1534 saved_line[curwin->w_cursor.col] = NUL;
1535 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1536 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1537 truncate_spaces(saved_line);
1538 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1539 saved_line = NULL;
1540 if (did_append)
1541 {
1542 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1543 curwin->w_cursor.lnum + 1, 1L);
1544 did_append = FALSE;
1545
1546 /* Move marks after the line break to the new line. */
1547 if (flags & OPENLINE_MARKFIX)
1548 mark_col_adjust(curwin->w_cursor.lnum,
1549 curwin->w_cursor.col + less_cols_off,
1550 1L, (long)-less_cols);
1551 }
1552 else
1553 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1554 }
1555
1556 /*
1557 * Put the cursor on the new line. Careful: the scrollup() above may
1558 * have moved w_cursor, we must use old_cursor.
1559 */
1560 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1561 }
1562 if (did_append)
1563 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1564
1565 curwin->w_cursor.col = newcol;
1566#ifdef FEAT_VIRTUALEDIT
1567 curwin->w_cursor.coladd = 0;
1568#endif
1569
1570#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1571 /*
1572 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1573 * fixthisline() from doing it (via change_indent()) by telling it we're in
1574 * normal INSERT mode.
1575 */
1576 if (State & VREPLACE_FLAG)
1577 {
1578 vreplace_mode = State; /* So we know to put things right later */
1579 State = INSERT;
1580 }
1581 else
1582 vreplace_mode = 0;
1583#endif
1584#ifdef FEAT_LISP
1585 /*
1586 * May do lisp indenting.
1587 */
1588 if (!p_paste
1589# ifdef FEAT_COMMENTS
1590 && leader == NULL
1591# endif
1592 && curbuf->b_p_lisp
1593 && curbuf->b_p_ai)
1594 {
1595 fixthisline(get_lisp_indent);
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001596 ai_col = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 }
1598#endif
1599#ifdef FEAT_CINDENT
1600 /*
1601 * May do indenting after opening a new line.
1602 */
1603 if (!p_paste
1604 && (curbuf->b_p_cin
1605# ifdef FEAT_EVAL
1606 || *curbuf->b_p_inde != NUL
1607# endif
1608 )
1609 && in_cinkeys(dir == FORWARD
1610 ? KEY_OPEN_FORW
1611 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1612 {
1613 do_c_expr_indent();
Bram Moolenaare2e69e42017-09-02 20:30:35 +02001614 ai_col = (colnr_T)getwhitecols_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 }
1616#endif
1617#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1618 if (vreplace_mode != 0)
1619 State = vreplace_mode;
1620#endif
1621
1622#ifdef FEAT_VREPLACE
1623 /*
1624 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1625 * original line, and inserts the new stuff char by char, pushing old stuff
1626 * onto the replace stack (via ins_char()).
1627 */
1628 if (State & VREPLACE_FLAG)
1629 {
1630 /* Put new line in p_extra */
1631 p_extra = vim_strsave(ml_get_curline());
1632 if (p_extra == NULL)
1633 goto theend;
1634
1635 /* Put back original line */
1636 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1637
1638 /* Insert new stuff into line again */
1639 curwin->w_cursor.col = 0;
1640#ifdef FEAT_VIRTUALEDIT
1641 curwin->w_cursor.coladd = 0;
1642#endif
1643 ins_bytes(p_extra); /* will call changed_bytes() */
1644 vim_free(p_extra);
1645 next_line = NULL;
1646 }
1647#endif
1648
Bram Moolenaar24a2d722018-04-24 19:36:43 +02001649 retval = OK; /* success! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650theend:
1651 curbuf->b_p_pi = saved_pi;
1652 vim_free(saved_line);
1653 vim_free(next_line);
1654 vim_free(allocated);
1655 return retval;
1656}
1657
1658#if defined(FEAT_COMMENTS) || defined(PROTO)
1659/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +02001660 * get_leader_len() returns the length in bytes of the prefix of the given
1661 * string which introduces a comment. If this string is not a comment then
1662 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 * When "flags" is not NULL, it is set to point to the flags of the recognized
1664 * comment leader.
1665 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001666 * If "include_space" is set, include trailing whitespace while calculating the
1667 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 */
1669 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001670get_leader_len(
1671 char_u *line,
1672 char_u **flags,
1673 int backward,
1674 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675{
1676 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001677 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 int got_com = FALSE;
1679 int found_one;
1680 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1681 char_u *string; /* pointer to comment string */
1682 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001683 int middle_match_len = 0;
1684 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001685 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686
Bram Moolenaar81340392012-06-06 16:12:59 +02001687 result = i = 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001688 while (VIM_ISWHITE(line[i])) /* leading white space is ignored */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 ++i;
1690
1691 /*
1692 * Repeat to match several nested comment strings.
1693 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001694 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 {
1696 /*
1697 * scan through the 'comments' option for a match
1698 */
1699 found_one = FALSE;
1700 for (list = curbuf->b_p_com; *list; )
1701 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001702 /* Get one option part into part_buf[]. Advance "list" to next
1703 * one. Put "string" at start of string. */
1704 if (!got_com && flags != NULL)
1705 *flags = list; /* remember where flags started */
1706 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1708 string = vim_strchr(part_buf, ':');
1709 if (string == NULL) /* missing ':', ignore this part */
1710 continue;
1711 *string++ = NUL; /* isolate flags from string */
1712
Bram Moolenaara4271d52011-05-10 13:38:27 +02001713 /* If we found a middle match previously, use that match when this
1714 * is not a middle or end. */
1715 if (middle_match_len != 0
1716 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1717 && vim_strchr(part_buf, COM_END) == NULL)
1718 break;
1719
1720 /* When we already found a nested comment, only accept further
1721 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1723 continue;
1724
Bram Moolenaara4271d52011-05-10 13:38:27 +02001725 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1727 continue;
1728
Bram Moolenaara4271d52011-05-10 13:38:27 +02001729 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 * When string starts with white space, must have some white space
1731 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001732 * TABs and spaces). */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001733 if (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001735 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001736 continue; /* missing white space */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001737 while (VIM_ISWHITE(string[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 ++string;
1739 }
1740 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1741 ;
1742 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001743 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744
Bram Moolenaara4271d52011-05-10 13:38:27 +02001745 /* When 'b' flag used, there must be white space or an
1746 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001748 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 continue;
1750
Bram Moolenaara4271d52011-05-10 13:38:27 +02001751 /* We have found a match, stop searching unless this is a middle
1752 * comment. The middle comment can be a substring of the end
1753 * comment in which case it's better to return the length of the
1754 * end comment and its flags. Thus we keep searching with middle
1755 * and end matches and use an end match if it matches better. */
1756 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1757 {
1758 if (middle_match_len == 0)
1759 {
1760 middle_match_len = j;
1761 saved_flags = prev_list;
1762 }
1763 continue;
1764 }
1765 if (middle_match_len != 0 && j > middle_match_len)
1766 /* Use this match instead of the middle match, since it's a
1767 * longer thus better match. */
1768 middle_match_len = 0;
1769
1770 if (middle_match_len == 0)
1771 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 found_one = TRUE;
1773 break;
1774 }
1775
Bram Moolenaara4271d52011-05-10 13:38:27 +02001776 if (middle_match_len != 0)
1777 {
1778 /* Use the previously found middle match after failing to find a
1779 * match with an end. */
1780 if (!got_com && flags != NULL)
1781 *flags = saved_flags;
1782 i += middle_match_len;
1783 found_one = TRUE;
1784 }
1785
1786 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 if (!found_one)
1788 break;
1789
Bram Moolenaar81340392012-06-06 16:12:59 +02001790 result = i;
1791
Bram Moolenaara4271d52011-05-10 13:38:27 +02001792 /* Include any trailing white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001793 while (VIM_ISWHITE(line[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 ++i;
1795
Bram Moolenaar81340392012-06-06 16:12:59 +02001796 if (include_space)
1797 result = i;
1798
Bram Moolenaara4271d52011-05-10 13:38:27 +02001799 /* If this comment doesn't nest, stop here. */
1800 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 if (vim_strchr(part_buf, COM_NEST) == NULL)
1802 break;
1803 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001804 return result;
1805}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001806
Bram Moolenaar81340392012-06-06 16:12:59 +02001807/*
1808 * Return the offset at which the last comment in line starts. If there is no
1809 * comment in the whole line, -1 is returned.
1810 *
1811 * When "flags" is not null, it is set to point to the flags describing the
1812 * recognized comment leader.
1813 */
1814 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001815get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +02001816{
1817 int result = -1;
1818 int i, j;
1819 int lower_check_bound = 0;
1820 char_u *string;
1821 char_u *com_leader;
1822 char_u *com_flags;
1823 char_u *list;
1824 int found_one;
1825 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1826
1827 /*
1828 * Repeat to match several nested comment strings.
1829 */
1830 i = (int)STRLEN(line);
1831 while (--i >= lower_check_bound)
1832 {
1833 /*
1834 * scan through the 'comments' option for a match
1835 */
1836 found_one = FALSE;
1837 for (list = curbuf->b_p_com; *list; )
1838 {
1839 char_u *flags_save = list;
1840
1841 /*
1842 * Get one option part into part_buf[]. Advance list to next one.
1843 * put string at start of string.
1844 */
1845 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1846 string = vim_strchr(part_buf, ':');
1847 if (string == NULL) /* If everything is fine, this cannot actually
1848 * happen. */
1849 {
1850 continue;
1851 }
1852 *string++ = NUL; /* Isolate flags from string. */
1853 com_leader = string;
1854
1855 /*
1856 * Line contents and string must match.
1857 * When string starts with white space, must have some white space
1858 * (but the amount does not need to match, there might be a mix of
1859 * TABs and spaces).
1860 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001861 if (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +02001862 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001863 if (i == 0 || !VIM_ISWHITE(line[i - 1]))
Bram Moolenaar81340392012-06-06 16:12:59 +02001864 continue;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001865 while (VIM_ISWHITE(string[0]))
Bram Moolenaar81340392012-06-06 16:12:59 +02001866 ++string;
1867 }
1868 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1869 /* do nothing */;
1870 if (string[j] != NUL)
1871 continue;
1872
1873 /*
1874 * When 'b' flag used, there must be white space or an
1875 * end-of-line after the string in the line.
1876 */
1877 if (vim_strchr(part_buf, COM_BLANK) != NULL
Bram Moolenaar1c465442017-03-12 20:10:05 +01001878 && !VIM_ISWHITE(line[i + j]) && line[i + j] != NUL)
Bram Moolenaar81340392012-06-06 16:12:59 +02001879 {
1880 continue;
1881 }
1882
1883 /*
1884 * We have found a match, stop searching.
1885 */
1886 found_one = TRUE;
1887
1888 if (flags)
1889 *flags = flags_save;
1890 com_flags = flags_save;
1891
1892 break;
1893 }
1894
1895 if (found_one)
1896 {
1897 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
1898 int len1, len2, off;
1899
1900 result = i;
1901 /*
1902 * If this comment nests, continue searching.
1903 */
1904 if (vim_strchr(part_buf, COM_NEST) != NULL)
1905 continue;
1906
1907 lower_check_bound = i;
1908
1909 /* Let's verify whether the comment leader found is a substring
1910 * of other comment leaders. If it is, let's adjust the
1911 * lower_check_bound so that we make sure that we have determined
1912 * the comment leader correctly.
1913 */
1914
Bram Moolenaar1c465442017-03-12 20:10:05 +01001915 while (VIM_ISWHITE(*com_leader))
Bram Moolenaar81340392012-06-06 16:12:59 +02001916 ++com_leader;
1917 len1 = (int)STRLEN(com_leader);
1918
1919 for (list = curbuf->b_p_com; *list; )
1920 {
1921 char_u *flags_save = list;
1922
1923 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
1924 if (flags_save == com_flags)
1925 continue;
1926 string = vim_strchr(part_buf2, ':');
1927 ++string;
Bram Moolenaar1c465442017-03-12 20:10:05 +01001928 while (VIM_ISWHITE(*string))
Bram Moolenaar81340392012-06-06 16:12:59 +02001929 ++string;
1930 len2 = (int)STRLEN(string);
1931 if (len2 == 0)
1932 continue;
1933
1934 /* Now we have to verify whether string ends with a substring
1935 * beginning the com_leader. */
1936 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
1937 {
1938 --off;
1939 if (!STRNCMP(string + off, com_leader, len2 - off))
1940 {
1941 if (i - off < lower_check_bound)
1942 lower_check_bound = i - off;
1943 }
1944 }
1945 }
1946 }
1947 }
1948 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949}
1950#endif
1951
1952/*
1953 * Return the number of window lines occupied by buffer line "lnum".
1954 */
1955 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001956plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957{
1958 return plines_win(curwin, lnum, TRUE);
1959}
1960
1961 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001962plines_win(
1963 win_T *wp,
1964 linenr_T lnum,
1965 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966{
1967#if defined(FEAT_DIFF) || defined(PROTO)
1968 /* Check for filler lines above this buffer line. When folded the result
1969 * is one line anyway. */
1970 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
1971}
1972
1973 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001974plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975{
1976 return plines_win_nofill(curwin, lnum, TRUE);
1977}
1978
1979 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001980plines_win_nofill(
1981 win_T *wp,
1982 linenr_T lnum,
1983 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984{
1985#endif
1986 int lines;
1987
1988 if (!wp->w_p_wrap)
1989 return 1;
1990
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 if (wp->w_width == 0)
1992 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993
1994#ifdef FEAT_FOLDING
1995 /* A folded lines is handled just like an empty line. */
1996 /* NOTE: Caller must handle lines that are MAYBE folded. */
1997 if (lineFolded(wp, lnum) == TRUE)
1998 return 1;
1999#endif
2000
2001 lines = plines_win_nofold(wp, lnum);
2002 if (winheight > 0 && lines > wp->w_height)
2003 return (int)wp->w_height;
2004 return lines;
2005}
2006
2007/*
2008 * Return number of window lines physical line "lnum" will occupy in window
2009 * "wp". Does not care about folding, 'wrap' or 'diff'.
2010 */
2011 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002012plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013{
2014 char_u *s;
2015 long col;
2016 int width;
2017
2018 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2019 if (*s == NUL) /* empty line */
2020 return 1;
2021 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
2022
2023 /*
2024 * If list mode is on, then the '$' at the end of the line may take up one
2025 * extra column.
2026 */
2027 if (wp->w_p_list && lcs_eol != NUL)
2028 col += 1;
2029
2030 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002031 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002033 width = wp->w_width - win_col_off(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 if (width <= 0)
2035 return 32000;
2036 if (col <= width)
2037 return 1;
2038 col -= width;
2039 width += win_col_off2(wp);
2040 return (col + (width - 1)) / width + 1;
2041}
2042
2043/*
2044 * Like plines_win(), but only reports the number of physical screen lines
2045 * used from the start of the line to the given column number.
2046 */
2047 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002048plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049{
2050 long col;
2051 char_u *s;
2052 int lines = 0;
2053 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002054 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055
2056#ifdef FEAT_DIFF
2057 /* Check for filler lines above this buffer line. When folded the result
2058 * is one line anyway. */
2059 lines = diff_check_fill(wp, lnum);
2060#endif
2061
2062 if (!wp->w_p_wrap)
2063 return lines + 1;
2064
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 if (wp->w_width == 0)
2066 return lines + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067
Bram Moolenaar597a4222014-06-25 14:39:50 +02002068 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069
2070 col = 0;
2071 while (*s != NUL && --column >= 0)
2072 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02002073 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002074 MB_PTR_ADV(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 }
2076
2077 /*
2078 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2079 * INSERT mode, then col must be adjusted so that it represents the last
2080 * screen position of the TAB. This only fixes an error when the TAB wraps
2081 * from one screen line to the next (when 'columns' is not a multiple of
2082 * 'ts') -- webb.
2083 */
2084 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02002085 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086
2087 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002088 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 */
Bram Moolenaar02631462017-09-22 15:20:32 +02002090 width = wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002091 if (width <= 0)
2092 return 9999;
2093
2094 lines += 1;
2095 if (col > width)
2096 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2097 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098}
2099
2100 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002101plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102{
2103 int count = 0;
2104
2105 while (first <= last)
2106 {
2107#ifdef FEAT_FOLDING
2108 int x;
2109
2110 /* Check if there are any really folded lines, but also included lines
2111 * that are maybe folded. */
2112 x = foldedCount(wp, first, NULL);
2113 if (x > 0)
2114 {
2115 ++count; /* count 1 for "+-- folded" line */
2116 first += x;
2117 }
2118 else
2119#endif
2120 {
2121#ifdef FEAT_DIFF
2122 if (first == wp->w_topline)
2123 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2124 else
2125#endif
2126 count += plines_win(wp, first, TRUE);
2127 ++first;
2128 }
2129 }
2130 return (count);
2131}
2132
2133#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO)
2134/*
2135 * Insert string "p" at the cursor position. Stops at a NUL byte.
2136 * Handles Replace mode and multi-byte characters.
2137 */
2138 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002139ins_bytes(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140{
2141 ins_bytes_len(p, (int)STRLEN(p));
2142}
2143#endif
2144
2145#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2146 || defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO)
2147/*
2148 * Insert string "p" with length "len" at the cursor position.
2149 * Handles Replace mode and multi-byte characters.
2150 */
2151 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002152ins_bytes_len(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153{
2154 int i;
2155# ifdef FEAT_MBYTE
2156 int n;
2157
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002158 if (has_mbyte)
2159 for (i = 0; i < len; i += n)
2160 {
2161 if (enc_utf8)
2162 /* avoid reading past p[len] */
2163 n = utfc_ptr2len_len(p + i, len - i);
2164 else
2165 n = (*mb_ptr2len)(p + i);
2166 ins_char_bytes(p + i, n);
2167 }
2168 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169# endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002170 for (i = 0; i < len; ++i)
2171 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172}
2173#endif
2174
2175/*
2176 * Insert or replace a single character at the cursor position.
2177 * When in REPLACE or VREPLACE mode, replace any existing character.
2178 * Caller must have prepared for undo.
2179 * For multi-byte characters we get the whole character, the caller must
2180 * convert bytes to a character.
2181 */
2182 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002183ins_char(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002185 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar560379d2017-01-21 22:50:00 +01002186 int n = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187
Bram Moolenaar6a8ede92017-01-22 19:49:12 +01002188#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 n = (*mb_char2bytes)(c, buf);
2190
2191 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2192 * Happens for CTRL-Vu9900. */
2193 if (buf[0] == 0)
2194 buf[0] = '\n';
Bram Moolenaar560379d2017-01-21 22:50:00 +01002195#else
2196 buf[0] = c;
2197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198
2199 ins_char_bytes(buf, n);
2200}
2201
2202 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002203ins_char_bytes(char_u *buf, int charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204{
2205 int c = buf[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 int newlen; /* nr of bytes inserted */
2207 int oldlen; /* nr of bytes deleted (0 when not replacing) */
2208 char_u *p;
2209 char_u *newp;
2210 char_u *oldp;
2211 int linelen; /* length of old line including NUL */
2212 colnr_T col;
2213 linenr_T lnum = curwin->w_cursor.lnum;
2214 int i;
2215
2216#ifdef FEAT_VIRTUALEDIT
2217 /* Break tabs if needed. */
2218 if (virtual_active() && curwin->w_cursor.coladd > 0)
2219 coladvance_force(getviscol());
2220#endif
2221
2222 col = curwin->w_cursor.col;
2223 oldp = ml_get(lnum);
2224 linelen = (int)STRLEN(oldp) + 1;
2225
2226 /* The lengths default to the values for when not replacing. */
2227 oldlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 newlen = charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229
2230 if (State & REPLACE_FLAG)
2231 {
2232#ifdef FEAT_VREPLACE
2233 if (State & VREPLACE_FLAG)
2234 {
2235 colnr_T new_vcol = 0; /* init for GCC */
2236 colnr_T vcol;
2237 int old_list;
2238#ifndef FEAT_MBYTE
2239 char_u buf[2];
2240#endif
2241
2242 /*
2243 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2244 * Returns the old value of list, so when finished,
2245 * curwin->w_p_list should be set back to this.
2246 */
2247 old_list = curwin->w_p_list;
2248 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2249 curwin->w_p_list = FALSE;
2250
2251 /*
2252 * In virtual replace mode each character may replace one or more
2253 * characters (zero if it's a TAB). Count the number of bytes to
2254 * be deleted to make room for the new character, counting screen
2255 * cells. May result in adding spaces to fill a gap.
2256 */
2257 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2258#ifndef FEAT_MBYTE
2259 buf[0] = c;
2260 buf[1] = NUL;
2261#endif
2262 new_vcol = vcol + chartabsize(buf, vcol);
2263 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2264 {
2265 vcol += chartabsize(oldp + col + oldlen, vcol);
2266 /* Don't need to remove a TAB that takes us to the right
2267 * position. */
2268 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2269 break;
2270#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002271 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272#else
2273 ++oldlen;
2274#endif
2275 /* Deleted a bit too much, insert spaces. */
2276 if (vcol > new_vcol)
2277 newlen += vcol - new_vcol;
2278 }
2279 curwin->w_p_list = old_list;
2280 }
2281 else
2282#endif
2283 if (oldp[col] != NUL)
2284 {
2285 /* normal replace */
2286#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002287 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288#else
2289 oldlen = 1;
2290#endif
2291 }
2292
2293
2294 /* Push the replaced bytes onto the replace stack, so that they can be
2295 * put back when BS is used. The bytes of a multi-byte character are
2296 * done the other way around, so that the first byte is popped off
2297 * first (it tells the byte length of the character). */
2298 replace_push(NUL);
2299 for (i = 0; i < oldlen; ++i)
2300 {
2301#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002302 if (has_mbyte)
2303 i += replace_push_mb(oldp + col + i) - 1;
2304 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002306 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 }
2308 }
2309
2310 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2311 if (newp == NULL)
2312 return;
2313
2314 /* Copy bytes before the cursor. */
2315 if (col > 0)
2316 mch_memmove(newp, oldp, (size_t)col);
2317
2318 /* Copy bytes after the changed character(s). */
2319 p = newp + col;
Bram Moolenaar9ad89c62017-10-26 22:04:04 +02002320 if (linelen > col + oldlen)
2321 mch_memmove(p + newlen, oldp + col + oldlen,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 (size_t)(linelen - col - oldlen));
2323
2324 /* Insert or overwrite the new character. */
2325#ifdef FEAT_MBYTE
2326 mch_memmove(p, buf, charlen);
2327 i = charlen;
2328#else
2329 *p = c;
2330 i = 1;
2331#endif
2332
2333 /* Fill with spaces when necessary. */
2334 while (i < newlen)
2335 p[i++] = ' ';
2336
2337 /* Replace the line in the buffer. */
2338 ml_replace(lnum, newp, FALSE);
2339
2340 /* mark the buffer as changed and prepare for displaying */
2341 changed_bytes(lnum, col);
2342
2343 /*
2344 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2345 * show the match for right parens and braces.
2346 */
2347 if (p_sm && (State & INSERT)
2348 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002349#ifdef FEAT_INS_EXPAND
2350 && !ins_compl_active()
2351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002353 {
2354#ifdef FEAT_MBYTE
2355 if (has_mbyte)
2356 showmatch(mb_ptr2char(buf));
2357 else
2358#endif
2359 showmatch(c);
2360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
2362#ifdef FEAT_RIGHTLEFT
2363 if (!p_ri || (State & REPLACE_FLAG))
2364#endif
2365 {
2366 /* Normal insert: move cursor right */
2367#ifdef FEAT_MBYTE
2368 curwin->w_cursor.col += charlen;
2369#else
2370 ++curwin->w_cursor.col;
2371#endif
2372 }
2373 /*
2374 * TODO: should try to update w_row here, to avoid recomputing it later.
2375 */
2376}
2377
2378/*
2379 * Insert a string at the cursor position.
2380 * Note: Does NOT handle Replace mode.
2381 * Caller must have prepared for undo.
2382 */
2383 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002384ins_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385{
2386 char_u *oldp, *newp;
2387 int newlen = (int)STRLEN(s);
2388 int oldlen;
2389 colnr_T col;
2390 linenr_T lnum = curwin->w_cursor.lnum;
2391
2392#ifdef FEAT_VIRTUALEDIT
2393 if (virtual_active() && curwin->w_cursor.coladd > 0)
2394 coladvance_force(getviscol());
2395#endif
2396
2397 col = curwin->w_cursor.col;
2398 oldp = ml_get(lnum);
2399 oldlen = (int)STRLEN(oldp);
2400
2401 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2402 if (newp == NULL)
2403 return;
2404 if (col > 0)
2405 mch_memmove(newp, oldp, (size_t)col);
2406 mch_memmove(newp + col, s, (size_t)newlen);
2407 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2408 ml_replace(lnum, newp, FALSE);
2409 changed_bytes(lnum, col);
2410 curwin->w_cursor.col += newlen;
2411}
2412
2413/*
2414 * Delete one character under the cursor.
2415 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2416 * Caller must have prepared for undo.
2417 *
2418 * return FAIL for failure, OK otherwise
2419 */
2420 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002421del_char(int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422{
2423#ifdef FEAT_MBYTE
2424 if (has_mbyte)
2425 {
2426 /* Make sure the cursor is at the start of a character. */
2427 mb_adjust_cursor();
2428 if (*ml_get_cursor() == NUL)
2429 return FAIL;
2430 return del_chars(1L, fixpos);
2431 }
2432#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002433 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434}
2435
2436#if defined(FEAT_MBYTE) || defined(PROTO)
2437/*
2438 * Like del_bytes(), but delete characters instead of bytes.
2439 */
2440 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002441del_chars(long count, int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442{
2443 long bytes = 0;
2444 long i;
2445 char_u *p;
2446 int l;
2447
2448 p = ml_get_cursor();
2449 for (i = 0; i < count && *p != NUL; ++i)
2450 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002451 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 bytes += l;
2453 p += l;
2454 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002455 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456}
2457#endif
2458
2459/*
2460 * Delete "count" bytes under the cursor.
2461 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2462 * Caller must have prepared for undo.
2463 *
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002464 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 */
2466 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002467del_bytes(
2468 long count,
2469 int fixpos_arg,
2470 int use_delcombine UNUSED) /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471{
2472 char_u *oldp, *newp;
2473 colnr_T oldlen;
2474 linenr_T lnum = curwin->w_cursor.lnum;
2475 colnr_T col = curwin->w_cursor.col;
2476 int was_alloced;
2477 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002478 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479
2480 oldp = ml_get(lnum);
2481 oldlen = (int)STRLEN(oldp);
2482
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002483 /* Can't do anything when the cursor is on the NUL after the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 if (col >= oldlen)
2485 return FAIL;
2486
Bram Moolenaar191f18b2018-02-04 16:38:47 +01002487 /* If "count" is zero there is nothing to do. */
2488 if (count == 0)
2489 return OK;
2490
2491 /* If "count" is negative the caller must be doing something wrong. */
2492 if (count < 1)
2493 {
2494 IEMSGN("E950: Invalid count for del_bytes(): %ld", count);
2495 return FAIL;
2496 }
2497
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498#ifdef FEAT_MBYTE
2499 /* If 'delcombine' is set and deleting (less than) one character, only
2500 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002501 if (p_deco && use_delcombine && enc_utf8
2502 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002504 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 int n;
2506
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002507 (void)utfc_ptr2char(oldp + col, cc);
2508 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 {
2510 /* Find the last composing char, there can be several. */
2511 n = col;
2512 do
2513 {
2514 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002515 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 n += count;
2517 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2518 fixpos = 0;
2519 }
2520 }
2521#endif
2522
2523 /*
2524 * When count is too big, reduce it.
2525 */
2526 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2527 if (movelen <= 1)
2528 {
2529 /*
2530 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002531 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2532 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002534 if (col > 0 && fixpos && restart_edit == 0
2535#ifdef FEAT_VIRTUALEDIT
2536 && (ve_flags & VE_ONEMORE) == 0
2537#endif
2538 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 {
2540 --curwin->w_cursor.col;
2541#ifdef FEAT_VIRTUALEDIT
2542 curwin->w_cursor.coladd = 0;
2543#endif
2544#ifdef FEAT_MBYTE
2545 if (has_mbyte)
2546 curwin->w_cursor.col -=
2547 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2548#endif
2549 }
2550 count = oldlen - col;
2551 movelen = 1;
2552 }
2553
2554 /*
2555 * If the old line has been allocated the deletion can be done in the
2556 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002557 * Can't do this when using Netbeans, because we would need to invoke
2558 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002559 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002562 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002563 was_alloced = FALSE;
2564 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002566 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 if (was_alloced)
2568 newp = oldp; /* use same allocated memory */
2569 else
2570 { /* need to allocate a new line */
2571 newp = alloc((unsigned)(oldlen + 1 - count));
2572 if (newp == NULL)
2573 return FAIL;
2574 mch_memmove(newp, oldp, (size_t)col);
2575 }
2576 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2577 if (!was_alloced)
2578 ml_replace(lnum, newp, FALSE);
2579
2580 /* mark the buffer as changed and prepare for displaying */
2581 changed_bytes(lnum, curwin->w_cursor.col);
2582
2583 return OK;
2584}
2585
2586/*
2587 * Delete from cursor to end of line.
2588 * Caller must have prepared for undo.
2589 *
2590 * return FAIL for failure, OK otherwise
2591 */
2592 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002593truncate_line(
2594 int fixpos) /* if TRUE fix the cursor position when done */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595{
2596 char_u *newp;
2597 linenr_T lnum = curwin->w_cursor.lnum;
2598 colnr_T col = curwin->w_cursor.col;
2599
2600 if (col == 0)
2601 newp = vim_strsave((char_u *)"");
2602 else
2603 newp = vim_strnsave(ml_get(lnum), col);
2604
2605 if (newp == NULL)
2606 return FAIL;
2607
2608 ml_replace(lnum, newp, FALSE);
2609
2610 /* mark the buffer as changed and prepare for displaying */
2611 changed_bytes(lnum, curwin->w_cursor.col);
2612
2613 /*
2614 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2615 */
2616 if (fixpos && curwin->w_cursor.col > 0)
2617 --curwin->w_cursor.col;
2618
2619 return OK;
2620}
2621
2622/*
2623 * Delete "nlines" lines at the cursor.
2624 * Saves the lines for undo first if "undo" is TRUE.
2625 */
2626 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002627del_lines(
2628 long nlines, /* number of lines to delete */
2629 int undo) /* if TRUE, prepare for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630{
2631 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002632 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633
2634 if (nlines <= 0)
2635 return;
2636
2637 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002638 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 return;
2640
2641 for (n = 0; n < nlines; )
2642 {
2643 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2644 break;
2645
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002646 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 ++n;
2648
2649 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002650 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 break;
2652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002654 /* Correct the cursor position before calling deleted_lines_mark(), it may
2655 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 curwin->w_cursor.col = 0;
2657 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002658
2659 /* adjust marks, mark the buffer as changed and prepare for displaying */
2660 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661}
2662
2663 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002664gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665{
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002666 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01002668 /* When searching columns is sometimes put at the end of a line. */
2669 if (pos->col == MAXCOL)
2670 return NUL;
2671 ptr = ml_get_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672#ifdef FEAT_MBYTE
2673 if (has_mbyte)
2674 return (*mb_ptr2char)(ptr);
2675#endif
2676 return (int)*ptr;
2677}
2678
2679 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002680gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681{
2682#ifdef FEAT_MBYTE
2683 if (has_mbyte)
2684 return (*mb_ptr2char)(ml_get_cursor());
2685#endif
2686 return (int)*ml_get_cursor();
2687}
2688
2689/*
2690 * Write a character at the current cursor position.
2691 * It is directly written into the block.
2692 */
2693 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002694pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695{
2696 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2697 + curwin->w_cursor.col) = c;
2698}
2699
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700/*
2701 * When extra == 0: Return TRUE if the cursor is before or on the first
2702 * non-blank in the line.
2703 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2704 * the line.
2705 */
2706 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002707inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708{
2709 char_u *ptr;
2710 colnr_T col;
2711
Bram Moolenaar1c465442017-03-12 20:10:05 +01002712 for (col = 0, ptr = ml_get_curline(); VIM_ISWHITE(*ptr); ++col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 ++ptr;
2714 if (col >= curwin->w_cursor.col + extra)
2715 return TRUE;
2716 else
2717 return FALSE;
2718}
2719
2720/*
2721 * Skip to next part of an option argument: Skip space and comma.
2722 */
2723 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002724skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725{
2726 if (*p == ',')
2727 ++p;
2728 while (*p == ' ')
2729 ++p;
2730 return p;
2731}
2732
2733/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002734 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 *
2736 * Most often called through changed_bytes() and changed_lines(), which also
2737 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002738 *
2739 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 */
2741 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002742changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743{
2744#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02002745 if (p_imst == IM_ON_THE_SPOT)
2746 {
2747 /* The text of the preediting area is inserted, but this doesn't
2748 * mean a change of the buffer yet. That is delayed until the
2749 * text is committed. (this means preedit becomes empty) */
2750 if (im_is_preediting() && !xim_changed_while_preediting)
2751 return;
2752 xim_changed_while_preediting = FALSE;
2753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754#endif
2755
2756 if (!curbuf->b_changed)
2757 {
2758 int save_msg_scroll = msg_scroll;
2759
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002760 /* Give a warning about changing a read-only file. This may also
2761 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002763
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 /* Create a swap file if that is wanted.
2765 * Don't do this for "nofile" and "nowrite" buffer types. */
2766 if (curbuf->b_may_swap
2767#ifdef FEAT_QUICKFIX
2768 && !bt_dontwrite(curbuf)
2769#endif
2770 )
2771 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002772 int save_need_wait_return = need_wait_return;
2773
2774 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 ml_open_file(curbuf);
2776
2777 /* The ml_open_file() can cause an ATTENTION message.
2778 * Wait two seconds, to make sure the user reads this unexpected
2779 * message. Since we could be anywhere, call wait_return() now,
2780 * and don't let the emsg() set msg_scroll. */
2781 if (need_wait_return && emsg_silent == 0)
2782 {
2783 out_flush();
2784 ui_delay(2000L, TRUE);
2785 wait_return(TRUE);
2786 msg_scroll = save_msg_scroll;
2787 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002788 else
2789 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002791 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002793 ++CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794}
2795
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002796/*
2797 * Internal part of changed(), no user interaction.
2798 */
2799 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002800changed_int(void)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002801{
2802 curbuf->b_changed = TRUE;
2803 ml_setflags(curbuf);
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002804 check_status(curbuf);
2805 redraw_tabline = TRUE;
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002806#ifdef FEAT_TITLE
2807 need_maketitle = TRUE; /* set window title later */
2808#endif
2809}
2810
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002811static void changedOneline(buf_T *buf, linenr_T lnum);
2812static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
2813static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814
2815/*
2816 * Changed bytes within a single line for the current buffer.
2817 * - marks the windows on this buffer to be redisplayed
2818 * - marks the buffer changed by calling changed()
2819 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002820 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 */
2822 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002823changed_bytes(linenr_T lnum, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002825 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002827
2828#ifdef FEAT_DIFF
2829 /* Diff highlighting in other diff windows may need to be updated too. */
2830 if (curwin->w_p_diff)
2831 {
2832 win_T *wp;
2833 linenr_T wlnum;
2834
Bram Moolenaar29323592016-07-24 22:04:11 +02002835 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002836 if (wp->w_p_diff && wp != curwin)
2837 {
2838 redraw_win_later(wp, VALID);
2839 wlnum = diff_lnum_win(lnum, wp);
2840 if (wlnum > 0)
2841 changedOneline(wp->w_buffer, wlnum);
2842 }
2843 }
2844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845}
2846
2847 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002848changedOneline(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002850 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 {
2852 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002853 if (lnum < buf->b_mod_top)
2854 buf->b_mod_top = lnum;
2855 else if (lnum >= buf->b_mod_bot)
2856 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 }
2858 else
2859 {
2860 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002861 buf->b_mod_set = TRUE;
2862 buf->b_mod_top = lnum;
2863 buf->b_mod_bot = lnum + 1;
2864 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 }
2866}
2867
2868/*
2869 * Appended "count" lines below line "lnum" in the current buffer.
2870 * Must be called AFTER the change and after mark_adjust().
2871 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2872 */
2873 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002874appended_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875{
2876 changed_lines(lnum + 1, 0, lnum + 1, count);
2877}
2878
2879/*
2880 * Like appended_lines(), but adjust marks first.
2881 */
2882 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002883appended_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884{
Bram Moolenaar82faa252016-06-04 20:14:07 +02002885 /* Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01002886 * be marks there. But it's still needed in diff mode. */
2887 if (lnum + count < curbuf->b_ml.ml_line_count
2888#ifdef FEAT_DIFF
2889 || curwin->w_p_diff
2890#endif
2891 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02002892 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 changed_lines(lnum + 1, 0, lnum + 1, count);
2894}
2895
2896/*
2897 * Deleted "count" lines at line "lnum" in the current buffer.
2898 * Must be called AFTER the change and after mark_adjust().
2899 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2900 */
2901 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002902deleted_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903{
2904 changed_lines(lnum, 0, lnum + count, -count);
2905}
2906
2907/*
2908 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002909 * Make sure the cursor is on a valid line before calling, a GUI callback may
2910 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 */
2912 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002913deleted_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914{
2915 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
2916 changed_lines(lnum, 0, lnum + count, -count);
2917}
2918
2919/*
2920 * Changed lines for the current buffer.
2921 * Must be called AFTER the change and after mark_adjust().
2922 * - mark the buffer changed by calling changed()
2923 * - mark the windows on this buffer to be redisplayed
2924 * - invalidate cached values
2925 * "lnum" is the first line that needs displaying, "lnume" the first line
2926 * below the changed lines (BEFORE the change).
2927 * When only inserting lines, "lnum" and "lnume" are equal.
2928 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002929 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 */
2931 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002932changed_lines(
2933 linenr_T lnum, /* first line with change */
2934 colnr_T col, /* column in first line with change */
2935 linenr_T lnume, /* line below last changed line */
2936 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002938 changed_lines_buf(curbuf, lnum, lnume, xtra);
2939
2940#ifdef FEAT_DIFF
2941 if (xtra == 0 && curwin->w_p_diff)
2942 {
2943 /* When the number of lines doesn't change then mark_adjust() isn't
2944 * called and other diff buffers still need to be marked for
2945 * displaying. */
2946 win_T *wp;
2947 linenr_T wlnum;
2948
Bram Moolenaar29323592016-07-24 22:04:11 +02002949 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002950 if (wp->w_p_diff && wp != curwin)
2951 {
2952 redraw_win_later(wp, VALID);
2953 wlnum = diff_lnum_win(lnum, wp);
2954 if (wlnum > 0)
2955 changed_lines_buf(wp->w_buffer, wlnum,
2956 lnume - lnum + wlnum, 0L);
2957 }
2958 }
2959#endif
2960
2961 changed_common(lnum, col, lnume, xtra);
2962}
2963
2964 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002965changed_lines_buf(
2966 buf_T *buf,
2967 linenr_T lnum, /* first line with change */
2968 linenr_T lnume, /* line below last changed line */
2969 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002970{
2971 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 {
2973 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002974 if (lnum < buf->b_mod_top)
2975 buf->b_mod_top = lnum;
2976 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 {
2978 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002979 buf->b_mod_bot += xtra;
2980 if (buf->b_mod_bot < lnum)
2981 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00002983 if (lnume + xtra > buf->b_mod_bot)
2984 buf->b_mod_bot = lnume + xtra;
2985 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 }
2987 else
2988 {
2989 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002990 buf->b_mod_set = TRUE;
2991 buf->b_mod_top = lnum;
2992 buf->b_mod_bot = lnume + xtra;
2993 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995}
2996
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002997/*
2998 * Common code for when a change is was made.
2999 * See changed_lines() for the arguments.
3000 * Careful: may trigger autocommands that reload the buffer.
3001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003003changed_common(
3004 linenr_T lnum,
3005 colnr_T col,
3006 linenr_T lnume,
3007 long xtra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008{
3009 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003010 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 int i;
3012#ifdef FEAT_JUMPLIST
3013 int cols;
3014 pos_T *p;
3015 int add;
3016#endif
3017
3018 /* mark the buffer as modified */
3019 changed();
3020
3021 /* set the '. mark */
3022 if (!cmdmod.keepjumps)
3023 {
3024 curbuf->b_last_change.lnum = lnum;
3025 curbuf->b_last_change.col = col;
3026
3027#ifdef FEAT_JUMPLIST
3028 /* Create a new entry if a new undo-able change was started or we
3029 * don't have an entry yet. */
3030 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3031 {
3032 if (curbuf->b_changelistlen == 0)
3033 add = TRUE;
3034 else
3035 {
3036 /* Don't create a new entry when the line number is the same
3037 * as the last one and the column is not too far away. Avoids
3038 * creating many entries for typing "xxxxx". */
3039 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3040 if (p->lnum != lnum)
3041 add = TRUE;
3042 else
3043 {
3044 cols = comp_textwidth(FALSE);
3045 if (cols == 0)
3046 cols = 79;
3047 add = (p->col + cols < col || col + cols < p->col);
3048 }
3049 }
3050 if (add)
3051 {
3052 /* This is the first of a new sequence of undo-able changes
3053 * and it's at some distance of the last change. Use a new
3054 * position in the changelist. */
3055 curbuf->b_new_change = FALSE;
3056
3057 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3058 {
3059 /* changelist is full: remove oldest entry */
3060 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3061 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3062 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003063 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 {
3065 /* Correct position in changelist for other windows on
3066 * this buffer. */
3067 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3068 --wp->w_changelistidx;
3069 }
3070 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003071 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 {
3073 /* For other windows, if the position in the changelist is
3074 * at the end it stays at the end. */
3075 if (wp->w_buffer == curbuf
3076 && wp->w_changelistidx == curbuf->b_changelistlen)
3077 ++wp->w_changelistidx;
3078 }
3079 ++curbuf->b_changelistlen;
3080 }
3081 }
3082 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3083 curbuf->b_last_change;
3084 /* The current window is always after the last change, so that "g,"
3085 * takes you back to it. */
3086 curwin->w_changelistidx = curbuf->b_changelistlen;
3087#endif
3088 }
3089
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003090 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 {
3092 if (wp->w_buffer == curbuf)
3093 {
3094 /* Mark this window to be redrawn later. */
3095 if (wp->w_redr_type < VALID)
3096 wp->w_redr_type = VALID;
3097
3098 /* Check if a change in the buffer has invalidated the cached
3099 * values for the cursor. */
3100#ifdef FEAT_FOLDING
3101 /*
3102 * Update the folds for this window. Can't postpone this, because
3103 * a following operator might work on the whole fold: ">>dd".
3104 */
3105 foldUpdate(wp, lnum, lnume + xtra - 1);
3106
3107 /* The change may cause lines above or below the change to become
3108 * included in a fold. Set lnum/lnume to the first/last line that
3109 * might be displayed differently.
3110 * Set w_cline_folded here as an efficient way to update it when
3111 * inserting lines just above a closed fold. */
3112 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3113 if (wp->w_cursor.lnum == lnum)
3114 wp->w_cline_folded = i;
3115 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3116 if (wp->w_cursor.lnum == lnume)
3117 wp->w_cline_folded = i;
3118
3119 /* If the changed line is in a range of previously folded lines,
3120 * compare with the first line in that range. */
3121 if (wp->w_cursor.lnum <= lnum)
3122 {
3123 i = find_wl_entry(wp, lnum);
3124 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3125 changed_line_abv_curs_win(wp);
3126 }
3127#endif
3128
3129 if (wp->w_cursor.lnum > lnum)
3130 changed_line_abv_curs_win(wp);
3131 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3132 changed_cline_bef_curs_win(wp);
3133 if (wp->w_botline >= lnum)
3134 {
3135 /* Assume that botline doesn't change (inserted lines make
3136 * other lines scroll down below botline). */
3137 approximate_botline_win(wp);
3138 }
3139
3140 /* Check if any w_lines[] entries have become invalid.
3141 * For entries below the change: Correct the lnums for
3142 * inserted/deleted lines. Makes it possible to stop displaying
3143 * after the change. */
3144 for (i = 0; i < wp->w_lines_valid; ++i)
3145 if (wp->w_lines[i].wl_valid)
3146 {
3147 if (wp->w_lines[i].wl_lnum >= lnum)
3148 {
3149 if (wp->w_lines[i].wl_lnum < lnume)
3150 {
3151 /* line included in change */
3152 wp->w_lines[i].wl_valid = FALSE;
3153 }
3154 else if (xtra != 0)
3155 {
3156 /* line below change */
3157 wp->w_lines[i].wl_lnum += xtra;
3158#ifdef FEAT_FOLDING
3159 wp->w_lines[i].wl_lastlnum += xtra;
3160#endif
3161 }
3162 }
3163#ifdef FEAT_FOLDING
3164 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3165 {
3166 /* change somewhere inside this range of folded lines,
3167 * may need to be redrawn */
3168 wp->w_lines[i].wl_valid = FALSE;
3169 }
3170#endif
3171 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003172
3173#ifdef FEAT_FOLDING
3174 /* Take care of side effects for setting w_topline when folds have
3175 * changed. Esp. when the buffer was changed in another window. */
3176 if (hasAnyFolding(wp))
3177 set_topline(wp, wp->w_topline);
3178#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003179 /* relative numbering may require updating more */
3180 if (wp->w_p_rnu)
3181 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 }
3183 }
3184
3185 /* Call update_screen() later, which checks out what needs to be redrawn,
3186 * since it notices b_mod_set and then uses b_mod_*. */
3187 if (must_redraw < VALID)
3188 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003189
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003190 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003191 if (lnum <= curwin->w_cursor.lnum
3192 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003193 last_cursormoved.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194}
3195
3196/*
3197 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3198 */
3199 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003200unchanged(
3201 buf_T *buf,
3202 int ff) /* also reset 'fileformat' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003204 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 {
3206 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003207 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 if (ff)
3209 save_file_ff(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003211 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212#ifdef FEAT_TITLE
3213 need_maketitle = TRUE; /* set window title later */
3214#endif
3215 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003216 ++CHANGEDTICK(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217#ifdef FEAT_NETBEANS_INTG
3218 netbeans_unmodified(buf);
3219#endif
3220}
3221
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222/*
3223 * check_status: called when the status bars for the buffer 'buf'
3224 * need to be updated
3225 */
3226 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003227check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228{
3229 win_T *wp;
3230
Bram Moolenaar29323592016-07-24 22:04:11 +02003231 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 if (wp->w_buffer == buf && wp->w_status_height)
3233 {
3234 wp->w_redr_status = TRUE;
3235 if (must_redraw < VALID)
3236 must_redraw = VALID;
3237 }
3238}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239
3240/*
3241 * If the file is readonly, give a warning message with the first change.
3242 * Don't do this for autocommands.
3243 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003244 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003246 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 */
3248 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003249change_warning(
3250 int col) /* column for message; non-zero when in insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 mode and 'showmode' is on */
3252{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003253 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3254
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 if (curbuf->b_did_warn == FALSE
3256 && curbufIsChanged() == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 && !autocmd_busy
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 && curbuf->b_p_ro)
3259 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003260 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003262 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 if (!curbuf->b_p_ro)
3264 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 /*
3266 * Do what msg() does, but with a column offset if the warning should
3267 * be after the mode message.
3268 */
3269 msg_start();
3270 if (msg_row == Rows - 1)
3271 msg_col = col;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003272 msg_source(HL_ATTR(HLF_W));
3273 MSG_PUTS_ATTR(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST);
Bram Moolenaar496c5262009-03-18 14:42:00 +00003274#ifdef FEAT_EVAL
3275 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 msg_clr_eos();
3278 (void)msg_end();
Bram Moolenaare5f2a072017-02-01 22:31:49 +01003279 if (msg_silent == 0 && !silent_mode
3280#ifdef FEAT_EVAL
3281 && time_for_testing != 1
3282#endif
3283 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 {
3285 out_flush();
3286 ui_delay(1000L, TRUE); /* give the user time to think about it */
3287 }
3288 curbuf->b_did_warn = TRUE;
3289 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3290 if (msg_row < Rows - 1)
3291 showmode();
3292 }
3293}
3294
3295/*
3296 * Ask for a reply from the user, a 'y' or a 'n'.
3297 * No other characters are accepted, the message is repeated until a valid
3298 * reply is entered or CTRL-C is hit.
3299 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3300 * from any buffers but directly from the user.
3301 *
3302 * return the 'y' or 'n'
3303 */
3304 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003305ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306{
3307 int r = ' ';
3308 int save_State = State;
3309
3310 if (exiting) /* put terminal in raw mode for this question */
3311 settmode(TMODE_RAW);
3312 ++no_wait_return;
3313#ifdef USE_ON_FLY_SCROLL
3314 dont_scroll = TRUE; /* disallow scrolling here */
3315#endif
3316 State = CONFIRM; /* mouse behaves like with :confirm */
3317#ifdef FEAT_MOUSE
3318 setmouse(); /* disables mouse for xterm */
3319#endif
3320 ++no_mapping;
3321 ++allow_keys; /* no mapping here, but recognize keys */
3322
3323 while (r != 'y' && r != 'n')
3324 {
3325 /* same highlighting as for wait_return */
Bram Moolenaar8820b482017-03-16 17:23:31 +01003326 smsg_attr(HL_ATTR(HLF_R), (char_u *)"%s (y/n)?", str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 if (direct)
3328 r = get_keystroke();
3329 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003330 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 if (r == Ctrl_C || r == ESC)
3332 r = 'n';
3333 msg_putchar(r); /* show what you typed */
3334 out_flush();
3335 }
3336 --no_wait_return;
3337 State = save_State;
3338#ifdef FEAT_MOUSE
3339 setmouse();
3340#endif
3341 --no_mapping;
3342 --allow_keys;
3343
3344 return r;
3345}
3346
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003347#if defined(FEAT_MOUSE) || defined(PROTO)
3348/*
3349 * Return TRUE if "c" is a mouse key.
3350 */
3351 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003352is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003353{
3354 return c == K_LEFTMOUSE
3355 || c == K_LEFTMOUSE_NM
3356 || c == K_LEFTDRAG
3357 || c == K_LEFTRELEASE
3358 || c == K_LEFTRELEASE_NM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01003359 || c == K_MOUSEMOVE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003360 || c == K_MIDDLEMOUSE
3361 || c == K_MIDDLEDRAG
3362 || c == K_MIDDLERELEASE
3363 || c == K_RIGHTMOUSE
3364 || c == K_RIGHTDRAG
3365 || c == K_RIGHTRELEASE
3366 || c == K_MOUSEDOWN
3367 || c == K_MOUSEUP
3368 || c == K_MOUSELEFT
3369 || c == K_MOUSERIGHT
3370 || c == K_X1MOUSE
3371 || c == K_X1DRAG
3372 || c == K_X1RELEASE
3373 || c == K_X2MOUSE
3374 || c == K_X2DRAG
3375 || c == K_X2RELEASE;
3376}
3377#endif
3378
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379/*
3380 * Get a key stroke directly from the user.
3381 * Ignores mouse clicks and scrollbar events, except a click for the left
3382 * button (used at the more prompt).
3383 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3384 * Disadvantage: typeahead is ignored.
3385 * Translates the interrupt character for unix to ESC.
3386 */
3387 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003388get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003390 char_u *buf = NULL;
3391 int buflen = 150;
3392 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 int len = 0;
3394 int n;
3395 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003396 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397
3398 mapped_ctrl_c = FALSE; /* mappings are not used here */
3399 for (;;)
3400 {
3401 cursor_on();
3402 out_flush();
3403
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003404 /* Leave some room for check_termcode() to insert a key code into (max
3405 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3406 * bytes. */
3407 maxlen = (buflen - 6 - len) / 3;
3408 if (buf == NULL)
3409 buf = alloc(buflen);
3410 else if (maxlen < 10)
3411 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003412 char_u *t_buf = buf;
3413
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003414 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003415 * escape sequence. */
3416 buflen += 100;
3417 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003418 if (buf == NULL)
3419 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003420 maxlen = (buflen - 6 - len) / 3;
3421 }
3422 if (buf == NULL)
3423 {
3424 do_outofmem_msg((long_u)buflen);
3425 return ESC; /* panic! */
3426 }
3427
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003429 * terminal code to complete. */
3430 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 if (n > 0)
3432 {
3433 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003434 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003436 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003438 else if (len > 0)
3439 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440
Bram Moolenaar4395a712006-09-05 18:57:57 +00003441 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003442 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003443 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003445
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003446 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003447 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003448 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003449 {
3450 /* Redrawing was postponed, do it now. */
3451 update_screen(0);
3452 setcursor(); /* put cursor back where it belongs */
3453 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003454 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003455 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003456 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003458 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 continue;
3460
3461 /* Handle modifier and/or special key code. */
3462 n = buf[0];
3463 if (n == K_SPECIAL)
3464 {
3465 n = TO_SPECIAL(buf[1], buf[2]);
3466 if (buf[1] == KS_MODIFIER
3467 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003468#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003469 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003470#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003471#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 || n == K_VER_SCROLLBAR
3473 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474#endif
3475 )
3476 {
3477 if (buf[1] == KS_MODIFIER)
3478 mod_mask = buf[2];
3479 len -= 3;
3480 if (len > 0)
3481 mch_memmove(buf, buf + 3, (size_t)len);
3482 continue;
3483 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003484 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 }
3486#ifdef FEAT_MBYTE
3487 if (has_mbyte)
3488 {
3489 if (MB_BYTE2LEN(n) > len)
3490 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003491 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 n = (*mb_ptr2char)(buf);
3493 }
3494#endif
3495#ifdef UNIX
3496 if (n == intr_char)
3497 n = ESC;
3498#endif
3499 break;
3500 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003501 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502
3503 mapped_ctrl_c = save_mapped_ctrl_c;
3504 return n;
3505}
3506
3507/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003508 * Get a number from the user.
3509 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 */
3511 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003512get_number(
3513 int colon, /* allow colon to abort */
3514 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515{
3516 int n = 0;
3517 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003518 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003520 if (mouse_used != NULL)
3521 *mouse_used = FALSE;
3522
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 /* When not printing messages, the user won't know what to type, return a
3524 * zero (as if CR was hit). */
3525 if (msg_silent != 0)
3526 return 0;
3527
3528#ifdef USE_ON_FLY_SCROLL
3529 dont_scroll = TRUE; /* disallow scrolling here */
3530#endif
3531 ++no_mapping;
3532 ++allow_keys; /* no mapping here, but recognize keys */
3533 for (;;)
3534 {
3535 windgoto(msg_row, msg_col);
3536 c = safe_vgetc();
3537 if (VIM_ISDIGIT(c))
3538 {
3539 n = n * 10 + c - '0';
3540 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003541 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 }
3543 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3544 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003545 if (typed > 0)
3546 {
3547 MSG_PUTS("\b \b");
3548 --typed;
3549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003552#ifdef FEAT_MOUSE
3553 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3554 {
3555 *mouse_used = TRUE;
3556 n = mouse_row + 1;
3557 break;
3558 }
3559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 else if (n == 0 && c == ':' && colon)
3561 {
3562 stuffcharReadbuff(':');
3563 if (!exmode_active)
3564 cmdline_row = msg_row;
3565 skip_redraw = TRUE; /* skip redraw once */
3566 do_redraw = FALSE;
3567 break;
3568 }
3569 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3570 break;
3571 }
3572 --no_mapping;
3573 --allow_keys;
3574 return n;
3575}
3576
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003577/*
3578 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003579 * When "mouse_used" is not NULL allow using the mouse and in that case return
3580 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003581 */
3582 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003583prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003584{
3585 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003586 int save_cmdline_row;
3587 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003588
3589 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003590 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003591 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003592 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003593 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003594
Bram Moolenaar203335e2006-09-03 14:35:42 +00003595 /* Set the state such that text can be selected/copied/pasted and we still
3596 * get mouse events. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003597 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003598 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003599 save_State = State;
Bram Moolenaarc9041072017-07-16 15:48:46 +02003600 State = ASKMORE; /* prevents a screen update when using a timer */
Bram Moolenaar73658312018-04-24 17:41:57 +02003601#ifdef FEAT_MOUSE
3602 /* May show different mouse shape. */
3603 setmouse();
3604#endif
3605
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003606
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003607 i = get_number(TRUE, mouse_used);
3608 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003609 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003610 /* don't call wait_return() now */
3611 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003612 cmdline_row = msg_row - 1;
3613 need_wait_return = FALSE;
3614 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003615 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003616 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003617 else
3618 cmdline_row = save_cmdline_row;
3619 State = save_State;
Bram Moolenaar73658312018-04-24 17:41:57 +02003620#ifdef FEAT_MOUSE
3621 /* May need to restore mouse shape. */
3622 setmouse();
3623#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003624
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003625 return i;
3626}
3627
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003629msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630{
3631 long pn;
3632
3633 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3635 return;
3636
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003637 /* We don't want to overwrite another important message, but do overwrite
3638 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3639 * then "put" reports the last action. */
3640 if (keep_msg != NULL && !keep_msg_more)
3641 return;
3642
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 if (n > 0)
3644 pn = n;
3645 else
3646 pn = -n;
3647
3648 if (pn > p_report)
3649 {
3650 if (pn == 1)
3651 {
3652 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003653 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3654 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003656 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3657 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 }
3659 else
3660 {
3661 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003662 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3663 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003665 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3666 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 }
3668 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003669 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 if (msg(msg_buf))
3671 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003672 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003673 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 }
3675 }
3676}
3677
3678/*
3679 * flush map and typeahead buffers and give a warning for an error
3680 */
3681 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003682beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683{
3684 if (emsg_silent == 0)
3685 {
3686 flush_buffers(FALSE);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003687 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 }
3689}
3690
3691/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003692 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 */
3694 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003695vim_beep(
3696 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697{
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003698#ifdef FEAT_EVAL
3699 called_vim_beep = TRUE;
3700#endif
3701
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 if (emsg_silent == 0)
3703 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003704 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3705 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003706#ifdef ELAPSED_FUNC
3707 static int did_init = FALSE;
3708 static ELAPSED_TYPE start_tv;
3709
3710 /* Only beep once per half a second, otherwise a sequence of beeps
3711 * would freeze Vim. */
3712 if (!did_init || ELAPSED_FUNC(start_tv) > 500)
3713 {
3714 did_init = TRUE;
3715 ELAPSED_INIT(start_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716#endif
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003717 if (p_vb
3718#ifdef FEAT_GUI
3719 /* While the GUI is starting up the termcap is set for
3720 * the GUI but the output still goes to a terminal. */
3721 && !(gui.in_use && gui.starting)
3722#endif
3723 )
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003724 {
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003725 out_str_cf(T_VB);
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003726#ifdef FEAT_VTP
3727 /* No restore color information, refresh the screen. */
3728 if (has_vtp_working() != 0
3729# ifdef FEAT_TERMGUICOLORS
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003730 && (p_tgc || (!p_tgc && t_colors >= 256))
Bram Moolenaarcafafb32018-02-22 21:07:09 +01003731# endif
3732 )
3733 {
3734 redraw_later(CLEAR);
3735 update_screen(0);
3736 redrawcmd();
3737 }
3738#endif
3739 }
Bram Moolenaar2e147ca2017-06-27 17:09:37 +02003740 else
3741 out_char(BELL);
3742#ifdef ELAPSED_FUNC
3743 }
3744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003746
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01003747 /* When 'debug' contains "beep" produce a message. If we are sourcing
3748 * a script or executing a function give the user a hint where the beep
3749 * comes from. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003750 if (vim_strchr(p_debug, 'e') != NULL)
3751 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01003752 msg_source(HL_ATTR(HLF_W));
3753 msg_attr((char_u *)_("Beep!"), HL_ATTR(HLF_W));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 }
3756}
3757
3758/*
3759 * To get the "real" home directory:
3760 * - get value of $HOME
3761 * For Unix:
3762 * - go to that directory
3763 * - do mch_dirname() to get the real name of that directory.
3764 * This also works with mounts and links.
3765 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3766 */
3767static char_u *homedir = NULL;
3768
3769 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003770init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771{
3772 char_u *var;
3773
Bram Moolenaar05159a02005-02-26 23:04:13 +00003774 /* In case we are called a second time (when 'encoding' changes). */
Bram Moolenaard23a8232018-02-10 18:45:26 +01003775 VIM_CLEAR(homedir);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003776
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777#ifdef VMS
3778 var = mch_getenv((char_u *)"SYS$LOGIN");
3779#else
3780 var = mch_getenv((char_u *)"HOME");
3781#endif
3782
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783#ifdef WIN3264
3784 /*
Bram Moolenaar48340b62017-08-29 22:08:53 +02003785 * Typically, $HOME is not defined on Windows, unless the user has
3786 * specifically defined it for Vim's sake. However, on Windows NT
3787 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3788 * each user. Try constructing $HOME from these.
3789 */
Bram Moolenaarb47a2592017-08-30 13:22:28 +02003790 if (var == NULL || *var == NUL)
Bram Moolenaar48340b62017-08-29 22:08:53 +02003791 {
3792 char_u *homedrive, *homepath;
3793
3794 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3795 homepath = mch_getenv((char_u *)"HOMEPATH");
3796 if (homepath == NULL || *homepath == NUL)
3797 homepath = (char_u *)"\\";
3798 if (homedrive != NULL
3799 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3800 {
3801 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3802 if (NameBuff[0] != NUL)
3803 var = NameBuff;
3804 }
3805 }
3806
3807 if (var == NULL)
3808 var = mch_getenv((char_u *)"USERPROFILE");
3809
3810 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 * Weird but true: $HOME may contain an indirect reference to another
3812 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3813 * when $HOME is being set.
3814 */
3815 if (var != NULL && *var == '%')
3816 {
3817 char_u *p;
3818 char_u *exp;
3819
3820 p = vim_strchr(var + 1, '%');
3821 if (p != NULL)
3822 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003823 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 exp = mch_getenv(NameBuff);
3825 if (exp != NULL && *exp != NUL
3826 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3827 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003828 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 var = NameBuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 }
3831 }
3832 }
3833
Bram Moolenaar48340b62017-08-29 22:08:53 +02003834 if (var != NULL && *var == NUL) /* empty is same as not set */
3835 var = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836
Bram Moolenaar48340b62017-08-29 22:08:53 +02003837# ifdef FEAT_MBYTE
Bram Moolenaar05159a02005-02-26 23:04:13 +00003838 if (enc_utf8 && var != NULL)
3839 {
3840 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003841 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003842
3843 /* Convert from active codepage to UTF-8. Other conversions are
3844 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003845 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003846 if (pp != NULL)
3847 {
3848 homedir = pp;
3849 return;
3850 }
3851 }
3852# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 /*
3855 * Default home dir is C:/
3856 * Best assumption we can make in such a situation.
3857 */
3858 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003859 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02003861
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 if (var != NULL)
3863 {
3864#ifdef UNIX
3865 /*
3866 * Change to the directory and get the actual path. This resolves
3867 * links. Don't do it when we can't return.
3868 */
3869 if (mch_dirname(NameBuff, MAXPATHL) == OK
3870 && mch_chdir((char *)NameBuff) == 0)
3871 {
3872 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3873 var = IObuff;
3874 if (mch_chdir((char *)NameBuff) != 0)
3875 EMSG(_(e_prev_dir));
3876 }
3877#endif
3878 homedir = vim_strsave(var);
3879 }
3880}
3881
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003882#if defined(EXITFREE) || defined(PROTO)
3883 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003884free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003885{
3886 vim_free(homedir);
3887}
Bram Moolenaar24305862012-08-15 14:05:05 +02003888
3889# ifdef FEAT_CMDL_COMPL
3890 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003891free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02003892{
3893 ga_clear_strings(&ga_users);
3894}
3895# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003896#endif
3897
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003899 * Call expand_env() and store the result in an allocated string.
3900 * This is not very memory efficient, this expects the result to be freed
3901 * again soon.
3902 */
3903 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003904expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003905{
3906 return expand_env_save_opt(src, FALSE);
3907}
3908
3909/*
3910 * Idem, but when "one" is TRUE handle the string as one file name, only
3911 * expand "~" at the start.
3912 */
3913 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003914expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003915{
3916 char_u *p;
3917
3918 p = alloc(MAXPATHL);
3919 if (p != NULL)
3920 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3921 return p;
3922}
3923
3924/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 * Expand environment variable with path name.
3926 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003927 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 * If anything fails no expansion is done and dst equals src.
3929 */
3930 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003931expand_env(
3932 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
3933 char_u *dst, /* where to put the result */
3934 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003936 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937}
3938
3939 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003940expand_env_esc(
3941 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
3942 char_u *dst, /* where to put the result */
3943 int dstlen, /* maximum length of the result */
3944 int esc, /* escape spaces in expanded variables */
3945 int one, /* "srcp" is one file name */
3946 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003948 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 char_u *tail;
3950 int c;
3951 char_u *var;
3952 int copy_char;
3953 int mustfree; /* var was allocated, need to free it later */
3954 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003955 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003957 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003958 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003959
3960 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 --dstlen; /* leave one char space for "\," */
3962 while (*src && dstlen > 0)
3963 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003964#ifdef FEAT_EVAL
3965 /* Skip over `=expr`. */
3966 if (src[0] == '`' && src[1] == '=')
3967 {
3968 size_t len;
3969
3970 var = src;
3971 src += 2;
3972 (void)skip_expr(&src);
3973 if (*src == '`')
3974 ++src;
3975 len = src - var;
3976 if (len > (size_t)dstlen)
3977 len = dstlen;
3978 vim_strncpy(dst, var, len);
3979 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02003980 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003981 continue;
3982 }
3983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003985 if ((*src == '$'
3986#ifdef VMS
3987 && at_start
3988#endif
3989 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003990#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 || *src == '%'
3992#endif
3993 || (*src == '~' && at_start))
3994 {
3995 mustfree = FALSE;
3996
3997 /*
3998 * The variable name is copied into dst temporarily, because it may
3999 * be a string in read-only memory and a NUL needs to be appended.
4000 */
4001 if (*src != '~') /* environment var */
4002 {
4003 tail = src + 1;
4004 var = dst;
4005 c = dstlen - 1;
4006
4007#ifdef UNIX
4008 /* Unix has ${var-name} type environment vars */
4009 if (*tail == '{' && !vim_isIDc('{'))
4010 {
4011 tail++; /* ignore '{' */
4012 while (c-- > 0 && *tail && *tail != '}')
4013 *var++ = *tail++;
4014 }
4015 else
4016#endif
4017 {
4018 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004019#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 || (*src == '%' && *tail != '%')
4021#endif
4022 ))
4023 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 }
4026 }
4027
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004028#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029# ifdef UNIX
4030 if (src[1] == '{' && *tail != '}')
4031# else
4032 if (*src == '%' && *tail != '%')
4033# endif
4034 var = NULL;
4035 else
4036 {
4037# ifdef UNIX
4038 if (src[1] == '{')
4039# else
4040 if (*src == '%')
4041#endif
4042 ++tail;
4043#endif
4044 *var = NUL;
4045 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004046#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 }
4048#endif
4049 }
4050 /* home directory */
4051 else if ( src[1] == NUL
4052 || vim_ispathsep(src[1])
4053 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4054 {
4055 var = homedir;
4056 tail = src + 1;
4057 }
4058 else /* user directory */
4059 {
4060#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4061 /*
4062 * Copy ~user to dst[], so we can put a NUL after it.
4063 */
4064 tail = src;
4065 var = dst;
4066 c = dstlen - 1;
4067 while ( c-- > 0
4068 && *tail
4069 && vim_isfilec(*tail)
4070 && !vim_ispathsep(*tail))
4071 *var++ = *tail++;
4072 *var = NUL;
4073# ifdef UNIX
4074 /*
4075 * If the system supports getpwnam(), use it.
4076 * Otherwise, or if getpwnam() fails, the shell is used to
4077 * expand ~user. This is slower and may fail if the shell
4078 * does not support ~user (old versions of /bin/sh).
4079 */
4080# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4081 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004082 /* Note: memory allocated by getpwnam() is never freed.
4083 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01004084 struct passwd *pw = (*dst == NUL)
4085 ? NULL : getpwnam((char *)dst + 1);
4086
4087 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 }
4089 if (var == NULL)
4090# endif
4091 {
4092 expand_T xpc;
4093
4094 ExpandInit(&xpc);
4095 xpc.xp_context = EXPAND_FILES;
4096 var = ExpandOne(&xpc, dst, NULL,
4097 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 mustfree = TRUE;
4099 }
4100
4101# else /* !UNIX, thus VMS */
4102 /*
4103 * USER_HOME is a comma-separated list of
4104 * directories to search for the user account in.
4105 */
4106 {
4107 char_u test[MAXPATHL], paths[MAXPATHL];
4108 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004109 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110
4111 STRCPY(paths, USER_HOME);
4112 next_path = paths;
4113 while (*next_path)
4114 {
4115 for (path = next_path; *next_path && *next_path != ',';
4116 next_path++);
4117 if (*next_path)
4118 *next_path++ = NUL;
4119 STRCPY(test, path);
4120 STRCAT(test, "/");
4121 STRCAT(test, dst + 1);
4122 if (mch_stat(test, &st) == 0)
4123 {
4124 var = alloc(STRLEN(test) + 1);
4125 STRCPY(var, test);
4126 mustfree = TRUE;
4127 break;
4128 }
4129 }
4130 }
4131# endif /* UNIX */
4132#else
4133 /* cannot expand user's home directory, so don't try */
4134 var = NULL;
4135 tail = (char_u *)""; /* for gcc */
4136#endif /* UNIX || VMS */
4137 }
4138
4139#ifdef BACKSLASH_IN_FILENAME
4140 /* If 'shellslash' is set change backslashes to forward slashes.
4141 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4142 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4143 {
4144 char_u *p = vim_strsave(var);
4145
4146 if (p != NULL)
4147 {
4148 if (mustfree)
4149 vim_free(var);
4150 var = p;
4151 mustfree = TRUE;
4152 forward_slash(var);
4153 }
4154 }
4155#endif
4156
4157 /* If "var" contains white space, escape it with a backslash.
4158 * Required for ":e ~/tt" when $HOME includes a space. */
4159 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4160 {
4161 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4162
4163 if (p != NULL)
4164 {
4165 if (mustfree)
4166 vim_free(var);
4167 var = p;
4168 mustfree = TRUE;
4169 }
4170 }
4171
4172 if (var != NULL && *var != NUL
4173 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4174 {
4175 STRCPY(dst, var);
4176 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004177 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 /* if var[] ends in a path separator and tail[] starts
4179 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004180 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4182 && dst[-1] != ':'
4183#endif
4184 && vim_ispathsep(*tail))
4185 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004186 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 src = tail;
4188 copy_char = FALSE;
4189 }
4190 if (mustfree)
4191 vim_free(var);
4192 }
4193
4194 if (copy_char) /* copy at least one char */
4195 {
4196 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004197 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004198 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4199 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 */
4201 at_start = FALSE;
4202 if (src[0] == '\\' && src[1] != NUL)
4203 {
4204 *dst++ = *src++;
4205 --dstlen;
4206 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004207 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 at_start = TRUE;
Bram Moolenaar1c864092017-08-06 18:15:45 +02004209 if (dstlen > 0)
4210 {
4211 *dst++ = *src++;
4212 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004213
Bram Moolenaar1c864092017-08-06 18:15:45 +02004214 if (startstr != NULL && src - startstr_len >= srcp
4215 && STRNCMP(src - startstr_len, startstr,
4216 startstr_len) == 0)
4217 at_start = TRUE;
4218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 }
Bram Moolenaar1c864092017-08-06 18:15:45 +02004220
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 }
4222 *dst = NUL;
4223}
4224
4225/*
4226 * Vim's version of getenv().
4227 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004228 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004229 * "mustfree" is set to TRUE when returned is allocated, it must be
4230 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 */
4232 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004233vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234{
4235 char_u *p;
4236 char_u *pend;
4237 int vimruntime;
4238
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004239#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 /* use "C:/" when $HOME is not set */
4241 if (STRCMP(name, "HOME") == 0)
4242 return homedir;
4243#endif
4244
4245 p = mch_getenv(name);
4246 if (p != NULL && *p == NUL) /* empty is the same as not set */
4247 p = NULL;
4248
4249 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004250 {
4251#if defined(FEAT_MBYTE) && defined(WIN3264)
4252 if (enc_utf8)
4253 {
4254 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004255 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004256
4257 /* Convert from active codepage to UTF-8. Other conversions are
4258 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004259 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004260 if (pp != NULL)
4261 {
4262 p = pp;
4263 *mustfree = TRUE;
4264 }
4265 }
4266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269
4270 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4271 if (!vimruntime && STRCMP(name, "VIM") != 0)
4272 return NULL;
4273
4274 /*
4275 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4276 * Don't do this when default_vimruntime_dir is non-empty.
4277 */
4278 if (vimruntime
4279#ifdef HAVE_PATHDEF
4280 && *default_vimruntime_dir == NUL
4281#endif
4282 )
4283 {
4284 p = mch_getenv((char_u *)"VIM");
4285 if (p != NULL && *p == NUL) /* empty is the same as not set */
4286 p = NULL;
4287 if (p != NULL)
4288 {
4289 p = vim_version_dir(p);
4290 if (p != NULL)
4291 *mustfree = TRUE;
4292 else
4293 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004294
4295#if defined(FEAT_MBYTE) && defined(WIN3264)
4296 if (enc_utf8)
4297 {
4298 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004299 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004300
4301 /* Convert from active codepage to UTF-8. Other conversions
4302 * are not done, because they would fail for non-ASCII
4303 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004304 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004305 if (pp != NULL)
4306 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004307 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004308 vim_free(p);
4309 p = pp;
4310 *mustfree = TRUE;
4311 }
4312 }
4313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 }
4315 }
4316
4317 /*
4318 * When expanding $VIM or $VIMRUNTIME fails, try using:
4319 * - the directory name from 'helpfile' (unless it contains '$')
4320 * - the executable name from argv[0]
4321 */
4322 if (p == NULL)
4323 {
4324 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4325 p = p_hf;
4326#ifdef USE_EXE_NAME
4327 /*
4328 * Use the name of the executable, obtained from argv[0].
4329 */
4330 else
4331 p = exe_name;
4332#endif
4333 if (p != NULL)
4334 {
4335 /* remove the file name */
4336 pend = gettail(p);
4337
4338 /* remove "doc/" from 'helpfile', if present */
4339 if (p == p_hf)
4340 pend = remove_tail(p, pend, (char_u *)"doc");
4341
4342#ifdef USE_EXE_NAME
4343# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004344 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 if (p == exe_name)
4346 {
4347 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004348 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004350 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4351 if (pend1 != pend)
4352 {
4353 pnew = alloc((unsigned)(pend1 - p) + 15);
4354 if (pnew != NULL)
4355 {
4356 STRNCPY(pnew, p, (pend1 - p));
4357 STRCPY(pnew + (pend1 - p), "Resources/vim");
4358 p = pnew;
4359 pend = p + STRLEN(p);
4360 }
4361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 }
4363# endif
4364 /* remove "src/" from exe_name, if present */
4365 if (p == exe_name)
4366 pend = remove_tail(p, pend, (char_u *)"src");
4367#endif
4368
4369 /* for $VIM, remove "runtime/" or "vim54/", if present */
4370 if (!vimruntime)
4371 {
4372 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4373 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4374 }
4375
4376 /* remove trailing path separator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004377 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 --pend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004380#ifdef MACOS_X
4381 if (p == exe_name || p == p_hf)
4382#endif
4383 /* check that the result is a directory name */
4384 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385
4386 if (p != NULL && !mch_isdir(p))
Bram Moolenaard23a8232018-02-10 18:45:26 +01004387 VIM_CLEAR(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 else
4389 {
4390#ifdef USE_EXE_NAME
4391 /* may add "/vim54" or "/runtime" if it exists */
4392 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4393 {
4394 vim_free(p);
4395 p = pend;
4396 }
4397#endif
4398 *mustfree = TRUE;
4399 }
4400 }
4401 }
4402
4403#ifdef HAVE_PATHDEF
4404 /* When there is a pathdef.c file we can use default_vim_dir and
4405 * default_vimruntime_dir */
4406 if (p == NULL)
4407 {
4408 /* Only use default_vimruntime_dir when it is not empty */
4409 if (vimruntime && *default_vimruntime_dir != NUL)
4410 {
4411 p = default_vimruntime_dir;
4412 *mustfree = FALSE;
4413 }
4414 else if (*default_vim_dir != NUL)
4415 {
4416 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4417 *mustfree = TRUE;
4418 else
4419 {
4420 p = default_vim_dir;
4421 *mustfree = FALSE;
4422 }
4423 }
4424 }
4425#endif
4426
4427 /*
4428 * Set the environment variable, so that the new value can be found fast
4429 * next time, and others can also use it (e.g. Perl).
4430 */
4431 if (p != NULL)
4432 {
4433 if (vimruntime)
4434 {
4435 vim_setenv((char_u *)"VIMRUNTIME", p);
4436 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 }
4438 else
4439 {
4440 vim_setenv((char_u *)"VIM", p);
4441 didset_vim = TRUE;
4442 }
4443 }
4444 return p;
4445}
4446
4447/*
4448 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4449 * Return NULL if not, return its name in allocated memory otherwise.
4450 */
4451 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004452vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453{
4454 char_u *p;
4455
4456 if (vimdir == NULL || *vimdir == NUL)
4457 return NULL;
4458 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4459 if (p != NULL && mch_isdir(p))
4460 return p;
4461 vim_free(p);
4462 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4463 if (p != NULL && mch_isdir(p))
4464 return p;
4465 vim_free(p);
4466 return NULL;
4467}
4468
4469/*
4470 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4471 * the length of "name/". Otherwise return "pend".
4472 */
4473 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004474remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475{
4476 int len = (int)STRLEN(name) + 1;
4477 char_u *newend = pend - len;
4478
4479 if (newend >= p
4480 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004481 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 return newend;
4483 return pend;
4484}
4485
Bram Moolenaar137374f2018-05-13 15:59:50 +02004486 void
4487vim_unsetenv(char_u *var)
4488{
4489#ifdef HAVE_UNSETENV
4490 unsetenv((char *)var);
4491#else
Bram Moolenaar1af6a4b2018-05-14 22:58:34 +02004492 vim_setenv(var, (char_u *)"");
Bram Moolenaar137374f2018-05-13 15:59:50 +02004493#endif
4494}
4495
4496
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 * Our portable version of setenv.
4499 */
4500 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004501vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502{
4503#ifdef HAVE_SETENV
4504 mch_setenv((char *)name, (char *)val, 1);
4505#else
4506 char_u *envbuf;
4507
4508 /*
4509 * Putenv does not copy the string, it has to remain
4510 * valid. The allocated memory will never be freed.
4511 */
4512 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4513 if (envbuf != NULL)
4514 {
4515 sprintf((char *)envbuf, "%s=%s", name, val);
4516 putenv((char *)envbuf);
4517 }
4518#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004519#ifdef FEAT_GETTEXT
4520 /*
4521 * When setting $VIMRUNTIME adjust the directory to find message
4522 * translations to $VIMRUNTIME/lang.
4523 */
4524 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4525 {
4526 char_u *buf = concat_str(val, (char_u *)"/lang");
4527
4528 if (buf != NULL)
4529 {
4530 bindtextdomain(VIMPACKAGE, (char *)buf);
4531 vim_free(buf);
4532 }
4533 }
4534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535}
4536
4537#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4538/*
4539 * Function given to ExpandGeneric() to obtain an environment variable name.
4540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004542get_env_name(
4543 expand_T *xp UNUSED,
4544 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545{
Bram Moolenaard0573012017-10-28 21:11:06 +02004546# if defined(AMIGA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 /*
Bram Moolenaard0573012017-10-28 21:11:06 +02004548 * No environ[] on the Amiga.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 */
4550 return NULL;
4551# else
4552# ifndef __WIN32__
4553 /* Borland C++ 5.2 has this in a header file. */
4554 extern char **environ;
4555# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004556# define ENVNAMELEN 100
4557 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 char_u *str;
4559 int n;
4560
4561 str = (char_u *)environ[idx];
4562 if (str == NULL)
4563 return NULL;
4564
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004565 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 {
4567 if (str[n] == '=' || str[n] == NUL)
4568 break;
4569 name[n] = str[n];
4570 }
4571 name[n] = NUL;
4572 return name;
4573# endif
4574}
Bram Moolenaar24305862012-08-15 14:05:05 +02004575
4576/*
4577 * Find all user names for user completion.
4578 * Done only once and then cached.
4579 */
4580 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004581init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004582{
Bram Moolenaar24305862012-08-15 14:05:05 +02004583 static int lazy_init_done = FALSE;
4584
4585 if (lazy_init_done)
4586 return;
4587
4588 lazy_init_done = TRUE;
4589 ga_init2(&ga_users, sizeof(char_u *), 20);
4590
4591# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4592 {
4593 char_u* user;
4594 struct passwd* pw;
4595
4596 setpwent();
4597 while ((pw = getpwent()) != NULL)
4598 /* pw->pw_name shouldn't be NULL but just in case... */
4599 if (pw->pw_name != NULL)
4600 {
4601 if (ga_grow(&ga_users, 1) == FAIL)
4602 break;
4603 user = vim_strsave((char_u*)pw->pw_name);
4604 if (user == NULL)
4605 break;
4606 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4607 }
4608 endpwent();
4609 }
Bram Moolenaar828c3d72018-06-19 18:58:07 +02004610# elif defined(WIN3264)
4611 {
4612 char_u* user;
4613 DWORD nusers = 0, ntotal = 0, i;
4614 PUSER_INFO_0 uinfo;
4615
4616 if (NetUserEnum(NULL, 0, 0, (LPBYTE *) &uinfo, MAX_PREFERRED_LENGTH,
4617 &nusers, &ntotal, NULL) == NERR_Success)
4618 {
4619 for (i = 0; i < nusers; i++)
4620 {
4621 if (ga_grow(&ga_users, 1) == FAIL)
4622 break;
4623 user = utf16_to_enc(uinfo[i].usri0_name, NULL);
4624 if (user == NULL)
4625 break;
4626 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4627 }
4628
4629 NetApiBufferFree(uinfo);
4630 }
4631 }
Bram Moolenaar24305862012-08-15 14:05:05 +02004632# endif
4633}
4634
4635/*
4636 * Function given to ExpandGeneric() to obtain an user names.
4637 */
4638 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004639get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004640{
4641 init_users();
4642 if (idx < ga_users.ga_len)
4643 return ((char_u **)ga_users.ga_data)[idx];
4644 return NULL;
4645}
4646
4647/*
4648 * Check whether name matches a user name. Return:
4649 * 0 if name does not match any user name.
4650 * 1 if name partially matches the beginning of a user name.
4651 * 2 is name fully matches a user name.
4652 */
Bram Moolenaar9b578142016-01-30 19:39:49 +01004653int match_user(char_u* name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004654{
4655 int i;
4656 int n = (int)STRLEN(name);
4657 int result = 0;
4658
4659 init_users();
4660 for (i = 0; i < ga_users.ga_len; i++)
4661 {
4662 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4663 return 2; /* full match */
4664 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4665 result = 1; /* partial match */
4666 }
4667 return result;
4668}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669#endif
4670
4671/*
4672 * Replace home directory by "~" in each space or comma separated file name in
4673 * 'src'.
4674 * If anything fails (except when out of space) dst equals src.
4675 */
4676 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004677home_replace(
4678 buf_T *buf, /* when not NULL, check for help files */
4679 char_u *src, /* input file name */
4680 char_u *dst, /* where to put the result */
4681 int dstlen, /* maximum length of the result */
4682 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 spaces and commas in the file name. */
4684{
4685 size_t dirlen = 0, envlen = 0;
4686 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004687 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 char_u *p;
4689
4690 if (src == NULL)
4691 {
4692 *dst = NUL;
4693 return;
4694 }
4695
4696 /*
4697 * If the file is a help file, remove the path completely.
4698 */
4699 if (buf != NULL && buf->b_help)
4700 {
Bram Moolenaar0af2d322017-08-05 23:00:53 +02004701 vim_snprintf((char *)dst, dstlen, "%s", gettail(src));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 return;
4703 }
4704
4705 /*
4706 * We check both the value of the $HOME environment variable and the
4707 * "real" home directory.
4708 */
4709 if (homedir != NULL)
4710 dirlen = STRLEN(homedir);
4711
4712#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004713 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004715 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4716#endif
Bram Moolenaar48340b62017-08-29 22:08:53 +02004717#ifdef WIN3264
4718 if (homedir_env == NULL)
4719 homedir_env_orig = homedir_env = mch_getenv((char_u *)"USERPROFILE");
4720#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004721 /* Empty is the same as not set. */
4722 if (homedir_env != NULL && *homedir_env == NUL)
4723 homedir_env = NULL;
4724
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004725#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004726 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004727 {
4728 int usedlen = 0;
4729 int flen;
4730 char_u *fbuf = NULL;
4731
4732 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004733 (void)modify_fname((char_u *)":p", &usedlen,
4734 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004735 flen = (int)STRLEN(homedir_env);
4736 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4737 /* Remove the trailing / that is added to a directory. */
4738 homedir_env[flen - 1] = NUL;
4739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740#endif
4741
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 if (homedir_env != NULL)
4743 envlen = STRLEN(homedir_env);
4744
4745 if (!one)
4746 src = skipwhite(src);
4747 while (*src && dstlen > 0)
4748 {
4749 /*
4750 * Here we are at the beginning of a file name.
4751 * First, check to see if the beginning of the file name matches
4752 * $HOME or the "real" home directory. Check that there is a '/'
4753 * after the match (so that if e.g. the file is "/home/pieter/bla",
4754 * and the home directory is "/home/piet", the file does not end up
4755 * as "~er/bla" (which would seem to indicate the file "bla" in user
4756 * er's home directory)).
4757 */
4758 p = homedir;
4759 len = dirlen;
4760 for (;;)
4761 {
4762 if ( len
4763 && fnamencmp(src, p, len) == 0
4764 && (vim_ispathsep(src[len])
4765 || (!one && (src[len] == ',' || src[len] == ' '))
4766 || src[len] == NUL))
4767 {
4768 src += len;
4769 if (--dstlen > 0)
4770 *dst++ = '~';
4771
4772 /*
4773 * If it's just the home directory, add "/".
4774 */
4775 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4776 *dst++ = '/';
4777 break;
4778 }
4779 if (p == homedir_env)
4780 break;
4781 p = homedir_env;
4782 len = envlen;
4783 }
4784
4785 /* if (!one) skip to separator: space or comma */
4786 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4787 *dst++ = *src++;
4788 /* skip separator */
4789 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4790 *dst++ = *src++;
4791 }
4792 /* if (dstlen == 0) out of space, what to do??? */
4793
4794 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004795
4796 if (homedir_env != homedir_env_orig)
4797 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798}
4799
4800/*
4801 * Like home_replace, store the replaced string in allocated memory.
4802 * When something fails, NULL is returned.
4803 */
4804 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004805home_replace_save(
4806 buf_T *buf, /* when not NULL, check for help files */
4807 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808{
4809 char_u *dst;
4810 unsigned len;
4811
4812 len = 3; /* space for "~/" and trailing NUL */
4813 if (src != NULL) /* just in case */
4814 len += (unsigned)STRLEN(src);
4815 dst = alloc(len);
4816 if (dst != NULL)
4817 home_replace(buf, src, dst, len, TRUE);
4818 return dst;
4819}
4820
4821/*
4822 * Compare two file names and return:
4823 * FPC_SAME if they both exist and are the same file.
4824 * FPC_SAMEX if they both don't exist and have the same file name.
4825 * FPC_DIFF if they both exist and are different files.
4826 * FPC_NOTX if they both don't exist.
4827 * FPC_DIFFX if one of them doesn't exist.
4828 * For the first name environment variables are expanded
4829 */
4830 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004831fullpathcmp(
4832 char_u *s1,
4833 char_u *s2,
4834 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835{
4836#ifdef UNIX
4837 char_u exp1[MAXPATHL];
4838 char_u full1[MAXPATHL];
4839 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02004840 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 int r1, r2;
4842
4843 expand_env(s1, exp1, MAXPATHL);
4844 r1 = mch_stat((char *)exp1, &st1);
4845 r2 = mch_stat((char *)s2, &st2);
4846 if (r1 != 0 && r2 != 0)
4847 {
4848 /* if mch_stat() doesn't work, may compare the names */
4849 if (checkname)
4850 {
4851 if (fnamecmp(exp1, s2) == 0)
4852 return FPC_SAMEX;
4853 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4854 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4855 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4856 return FPC_SAMEX;
4857 }
4858 return FPC_NOTX;
4859 }
4860 if (r1 != 0 || r2 != 0)
4861 return FPC_DIFFX;
4862 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4863 return FPC_SAME;
4864 return FPC_DIFF;
4865#else
4866 char_u *exp1; /* expanded s1 */
4867 char_u *full1; /* full path of s1 */
4868 char_u *full2; /* full path of s2 */
4869 int retval = FPC_DIFF;
4870 int r1, r2;
4871
4872 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4873 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4874 {
4875 full1 = exp1 + MAXPATHL;
4876 full2 = full1 + MAXPATHL;
4877
4878 expand_env(s1, exp1, MAXPATHL);
4879 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4880 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4881
4882 /* If vim_FullName() fails, the file probably doesn't exist. */
4883 if (r1 != OK && r2 != OK)
4884 {
4885 if (checkname && fnamecmp(exp1, s2) == 0)
4886 retval = FPC_SAMEX;
4887 else
4888 retval = FPC_NOTX;
4889 }
4890 else if (r1 != OK || r2 != OK)
4891 retval = FPC_DIFFX;
4892 else if (fnamecmp(full1, full2))
4893 retval = FPC_DIFF;
4894 else
4895 retval = FPC_SAME;
4896 vim_free(exp1);
4897 }
4898 return retval;
4899#endif
4900}
4901
4902/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004903 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004904 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004905 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 */
4907 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004908gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909{
4910 char_u *p1, *p2;
4911
4912 if (fname == NULL)
4913 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01004914 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01004916 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 p1 = p2 + 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004918 MB_PTR_ADV(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 }
4920 return p1;
4921}
4922
Bram Moolenaar31710262010-08-13 13:36:15 +02004923#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004924static char_u *gettail_dir(char_u *fname);
Bram Moolenaar31710262010-08-13 13:36:15 +02004925
4926/*
4927 * Return the end of the directory name, on the first path
4928 * separator:
4929 * "/path/file", "/path/dir/", "/path//dir", "/file"
4930 * ^ ^ ^ ^
4931 */
4932 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004933gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02004934{
4935 char_u *dir_end = fname;
4936 char_u *next_dir_end = fname;
4937 int look_for_sep = TRUE;
4938 char_u *p;
4939
4940 for (p = fname; *p != NUL; )
4941 {
4942 if (vim_ispathsep(*p))
4943 {
4944 if (look_for_sep)
4945 {
4946 next_dir_end = p;
4947 look_for_sep = FALSE;
4948 }
4949 }
4950 else
4951 {
4952 if (!look_for_sep)
4953 dir_end = next_dir_end;
4954 look_for_sep = TRUE;
4955 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004956 MB_PTR_ADV(p);
Bram Moolenaar31710262010-08-13 13:36:15 +02004957 }
4958 return dir_end;
4959}
4960#endif
4961
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004963 * Get pointer to tail of "fname", including path separators. Putting a NUL
4964 * here leaves the directory name. Takes care of "c:/" and "//".
4965 * Always returns a valid pointer.
4966 */
4967 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004968gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004969{
4970 char_u *p;
4971 char_u *t;
4972
4973 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4974 t = gettail(fname);
4975 while (t > p && after_pathsep(fname, t))
4976 --t;
4977#ifdef VMS
4978 /* path separator is part of the path */
4979 ++t;
4980#endif
4981 return t;
4982}
4983
4984/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 * get the next path component (just after the next path separator).
4986 */
4987 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004988getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989{
4990 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004991 MB_PTR_ADV(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 if (*fname)
4993 ++fname;
4994 return fname;
4995}
4996
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997/*
4998 * Get a pointer to one character past the head of a path name.
4999 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
5000 * If there is no head, path is returned.
5001 */
5002 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005003get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004{
5005 char_u *retval;
5006
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005007#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 /* may skip "c:" */
5009 if (isalpha(path[0]) && path[1] == ':')
5010 retval = path + 2;
5011 else
5012 retval = path;
5013#else
5014# if defined(AMIGA)
5015 /* may skip "label:" */
5016 retval = vim_strchr(path, ':');
5017 if (retval == NULL)
5018 retval = path;
5019# else /* Unix */
5020 retval = path;
5021# endif
5022#endif
5023
5024 while (vim_ispathsep(*retval))
5025 ++retval;
5026
5027 return retval;
5028}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029
5030/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01005031 * Return TRUE if 'c' is a path separator.
5032 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 */
5034 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005035vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036{
Bram Moolenaare60acc12011-05-10 16:41:25 +02005037#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02005039#else
5040# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005042# else
5043# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
5045 return (c == ':' || c == '[' || c == ']' || c == '/'
5046 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02005047# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005049# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02005051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052}
5053
Bram Moolenaar69c35002013-11-04 02:54:12 +01005054/*
5055 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
5056 */
5057 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005058vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01005059{
5060 return vim_ispathsep(c)
5061#ifdef BACKSLASH_IN_FILENAME
5062 && c != ':'
5063#endif
5064 ;
5065}
5066
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5068/*
5069 * return TRUE if 'c' is a path list separator.
5070 */
5071 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005072vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073{
5074#ifdef UNIX
5075 return (c == ':');
5076#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005077 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078#endif
5079}
5080#endif
5081
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005082/*
5083 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5084 * It's done in-place.
5085 */
5086 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005087shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005088{
5089 char_u *tail, *s, *d;
5090 int skip = FALSE;
5091
5092 tail = gettail(str);
5093 d = str;
5094 for (s = str; ; ++s)
5095 {
5096 if (s >= tail) /* copy the whole tail */
5097 {
5098 *d++ = *s;
5099 if (*s == NUL)
5100 break;
5101 }
5102 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5103 {
5104 *d++ = *s;
5105 skip = FALSE;
5106 }
5107 else if (!skip)
5108 {
5109 *d++ = *s; /* copy next char */
5110 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5111 skip = TRUE;
5112# ifdef FEAT_MBYTE
5113 if (has_mbyte)
5114 {
5115 int l = mb_ptr2len(s);
5116
5117 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005118 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005119 }
5120# endif
5121 }
5122 }
5123}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005124
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005125/*
5126 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5127 * Also returns TRUE if there is no directory name.
5128 * "fname" must be writable!.
5129 */
5130 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005131dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005132{
5133 char_u *p;
5134 int c;
5135 int retval;
5136
5137 p = gettail_sep(fname);
5138 if (p == fname)
5139 return TRUE;
5140 c = *p;
5141 *p = NUL;
5142 retval = mch_isdir(fname);
5143 *p = c;
5144 return retval;
5145}
5146
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005148 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5149 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 */
5151 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005152vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005154#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005156#else
5157 if (p_fic)
5158 return MB_STRICMP(x, y);
5159 return STRCMP(x, y);
5160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161}
5162
5163 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005164vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005166#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005167 char_u *px = x;
5168 char_u *py = y;
5169 int cx = NUL;
5170 int cy = NUL;
5171
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005172 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005174 cx = PTR2CHAR(px);
5175 cy = PTR2CHAR(py);
5176 if (cx == NUL || cy == NUL
5177 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5178 && !(cx == '/' && cy == '\\')
5179 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005181 len -= MB_PTR2LEN(px);
5182 px += MB_PTR2LEN(px);
5183 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 }
5185 if (len == 0)
5186 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005187 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005188#else
5189 if (p_fic)
5190 return MB_STRNICMP(x, y, len);
5191 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005193}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194
5195/*
5196 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005197 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 */
5199 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005200concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201{
5202 char_u *dest;
5203
5204 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5205 if (dest != NULL)
5206 {
5207 STRCPY(dest, fname1);
5208 if (sep)
5209 add_pathsep(dest);
5210 STRCAT(dest, fname2);
5211 }
5212 return dest;
5213}
5214
Bram Moolenaard6754642005-01-17 22:18:45 +00005215/*
5216 * Concatenate two strings and return the result in allocated memory.
5217 * Returns NULL when out of memory.
5218 */
5219 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005220concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005221{
5222 char_u *dest;
5223 size_t l = STRLEN(str1);
5224
5225 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5226 if (dest != NULL)
5227 {
5228 STRCPY(dest, str1);
5229 STRCPY(dest + l, str2);
5230 }
5231 return dest;
5232}
Bram Moolenaard6754642005-01-17 22:18:45 +00005233
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234/*
5235 * Add a path separator to a file name, unless it already ends in a path
5236 * separator.
5237 */
5238 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005239add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005241 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 STRCAT(p, PATHSEPSTR);
5243}
5244
5245/*
5246 * FullName_save - Make an allocated copy of a full file name.
5247 * Returns NULL when out of memory.
5248 */
5249 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005250FullName_save(
5251 char_u *fname,
5252 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005253 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254{
5255 char_u *buf;
5256 char_u *new_fname = NULL;
5257
5258 if (fname == NULL)
5259 return NULL;
5260
5261 buf = alloc((unsigned)MAXPATHL);
5262 if (buf != NULL)
5263 {
5264 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5265 new_fname = vim_strsave(buf);
5266 else
5267 new_fname = vim_strsave(fname);
5268 vim_free(buf);
5269 }
5270 return new_fname;
5271}
5272
5273#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5274
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005275static char_u *skip_string(char_u *p);
5276static pos_T *ind_find_start_comment(void);
Bram Moolenaardde81312017-08-26 17:49:01 +02005277static pos_T *ind_find_start_CORS(linenr_T *is_raw);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005278static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279
5280/*
5281 * Find the start of a comment, not knowing if we are in a comment right now.
5282 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005283 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005285 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005286ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005287{
5288 return find_start_comment(curbuf->b_ind_maxcomment);
5289}
5290
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005292find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293{
5294 pos_T *pos;
5295 char_u *line;
5296 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005297 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005299 for (;;)
5300 {
5301 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5302 if (pos == NULL)
5303 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005305 /*
5306 * Check if the comment start we found is inside a string.
5307 * If it is then restrict the search to below this line and try again.
5308 */
5309 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005310 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005311 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005312 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005313 break;
5314 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5315 if (cur_maxcomment <= 0)
5316 {
5317 pos = NULL;
5318 break;
5319 }
5320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 return pos;
5322}
5323
5324/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005325 * Find the start of a comment or raw string, not knowing if we are in a
5326 * comment or raw string right now.
5327 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaardde81312017-08-26 17:49:01 +02005328 * If is_raw is given and returns start of raw_string, sets it to true.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005329 * Return NULL when not inside a comment or raw string.
5330 * "CORS" -> Comment Or Raw String
5331 */
5332 static pos_T *
Bram Moolenaardde81312017-08-26 17:49:01 +02005333ind_find_start_CORS(linenr_T *is_raw) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005334{
Bram Moolenaar089af182015-10-07 11:41:49 +02005335 static pos_T comment_pos_copy;
5336 pos_T *comment_pos;
5337 pos_T *rs_pos;
5338
5339 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5340 if (comment_pos != NULL)
5341 {
5342 /* Need to make a copy of the static pos in findmatchlimit(),
5343 * calling find_start_rawstring() may change it. */
5344 comment_pos_copy = *comment_pos;
5345 comment_pos = &comment_pos_copy;
5346 }
5347 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005348
5349 /* If comment_pos is before rs_pos the raw string is inside the comment.
5350 * If rs_pos is before comment_pos the comment is inside the raw string. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005351 if (comment_pos == NULL || (rs_pos != NULL
5352 && LT_POS(*rs_pos, *comment_pos)))
Bram Moolenaardde81312017-08-26 17:49:01 +02005353 {
5354 if (is_raw != NULL && rs_pos != NULL)
5355 *is_raw = rs_pos->lnum;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005356 return rs_pos;
Bram Moolenaardde81312017-08-26 17:49:01 +02005357 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005358 return comment_pos;
5359}
5360
5361/*
5362 * Find the start of a raw string, not knowing if we are in one right now.
5363 * Search starts at w_cursor.lnum and goes backwards.
5364 * Return NULL when not inside a raw string.
5365 */
5366 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005367find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005368{
5369 pos_T *pos;
5370 char_u *line;
5371 char_u *p;
5372 int cur_maxcomment = ind_maxcomment;
5373
5374 for (;;)
5375 {
5376 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5377 if (pos == NULL)
5378 break;
5379
5380 /*
5381 * Check if the raw string start we found is inside a string.
5382 * If it is then restrict the search to below this line and try again.
5383 */
5384 line = ml_get(pos->lnum);
5385 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5386 p = skip_string(p);
5387 if ((colnr_T)(p - line) <= pos->col)
5388 break;
5389 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5390 if (cur_maxcomment <= 0)
5391 {
5392 pos = NULL;
5393 break;
5394 }
5395 }
5396 return pos;
5397}
5398
5399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 * Skip to the end of a "string" and a 'c' character.
5401 * If there is no string or character, return argument unmodified.
5402 */
5403 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005404skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405{
5406 int i;
5407
5408 /*
5409 * We loop, because strings may be concatenated: "date""time".
5410 */
5411 for ( ; ; ++p)
5412 {
5413 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5414 {
5415 if (!p[1]) /* ' at end of line */
5416 break;
5417 i = 2;
5418 if (p[1] == '\\') /* '\n' or '\000' */
5419 {
5420 ++i;
5421 while (vim_isdigit(p[i - 1])) /* '\000' */
5422 ++i;
5423 }
5424 if (p[i] == '\'') /* check for trailing ' */
5425 {
5426 p += i;
5427 continue;
5428 }
5429 }
5430 else if (p[0] == '"') /* start of string */
5431 {
5432 for (++p; p[0]; ++p)
5433 {
5434 if (p[0] == '\\' && p[1] != NUL)
5435 ++p;
5436 else if (p[0] == '"') /* end of string */
5437 break;
5438 }
5439 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005440 continue; /* continue for another string */
5441 }
5442 else if (p[0] == 'R' && p[1] == '"')
5443 {
5444 /* Raw string: R"[delim](...)[delim]" */
5445 char_u *delim = p + 2;
5446 char_u *paren = vim_strchr(delim, '(');
5447
5448 if (paren != NULL)
5449 {
5450 size_t delim_len = paren - delim;
5451
5452 for (p += 3; *p; ++p)
5453 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5454 && p[delim_len + 1] == '"')
5455 {
5456 p += delim_len + 1;
5457 break;
5458 }
5459 if (p[0] == '"')
5460 continue; /* continue for another string */
5461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 }
5463 break; /* no string found */
5464 }
5465 if (!*p)
5466 --p; /* backup from NUL */
5467 return p;
5468}
5469#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5470
5471#if defined(FEAT_CINDENT) || defined(PROTO)
5472
5473/*
5474 * Do C or expression indenting on the current line.
5475 */
5476 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005477do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478{
5479# ifdef FEAT_EVAL
5480 if (*curbuf->b_p_inde != NUL)
5481 fixthisline(get_expr_indent);
5482 else
5483# endif
5484 fixthisline(get_c_indent);
5485}
5486
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005487/* Find result cache for cpp_baseclass */
5488typedef struct {
5489 int found;
5490 lpos_T lpos;
5491} cpp_baseclass_cache_T;
5492
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493/*
5494 * Functions for C-indenting.
5495 * Most of this originally comes from Eric Fischer.
5496 */
5497/*
5498 * Below "XXX" means that this function may unlock the current line.
5499 */
5500
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005501static char_u *cin_skipcomment(char_u *);
5502static int cin_nocode(char_u *);
5503static pos_T *find_line_comment(void);
5504static int cin_has_js_key(char_u *text);
5505static int cin_islabel_skip(char_u **);
5506static int cin_isdefault(char_u *);
5507static char_u *after_label(char_u *l);
5508static int get_indent_nolabel(linenr_T lnum);
5509static int skip_label(linenr_T, char_u **pp);
5510static int cin_first_id_amount(void);
5511static int cin_get_equal_amount(linenr_T lnum);
5512static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005513static int cin_iscomment(char_u *);
5514static int cin_islinecomment(char_u *);
5515static int cin_isterminated(char_u *, int, int);
5516static int cin_isinit(void);
5517static int cin_isfuncdecl(char_u **, linenr_T, linenr_T);
5518static int cin_isif(char_u *);
5519static int cin_iselse(char_u *);
5520static int cin_isdo(char_u *);
5521static int cin_iswhileofdo(char_u *, linenr_T);
5522static int cin_is_if_for_while_before_offset(char_u *line, int *poffset);
5523static int cin_iswhileofdo_end(int terminated);
5524static int cin_isbreak(char_u *);
5525static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached);
5526static int get_baseclass_amount(int col);
5527static int cin_ends_in(char_u *, char_u *, char_u *);
5528static int cin_starts_with(char_u *s, char *word);
5529static int cin_skip2pos(pos_T *trypos);
5530static pos_T *find_start_brace(void);
5531static pos_T *find_match_paren(int);
5532static pos_T *find_match_char(int c, int ind_maxparen);
5533static int corr_ind_maxparen(pos_T *startpos);
5534static int find_last_paren(char_u *l, int start, int end);
5535static int find_match(int lookfor, linenr_T ourscope);
5536static int cin_is_cpp_namespace(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537
5538/*
5539 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005540 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 */
5542 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005543cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544{
5545 while (*s)
5546 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005547 char_u *prev_s = s;
5548
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005550
5551 /* Perl/shell # comment comment continues until eol. Require a space
5552 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005553 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005554 {
5555 s += STRLEN(s);
5556 break;
5557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 if (*s != '/')
5559 break;
5560 ++s;
5561 if (*s == '/') /* slash-slash comment continues till eol */
5562 {
5563 s += STRLEN(s);
5564 break;
5565 }
5566 if (*s != '*')
5567 break;
5568 for (++s; *s; ++s) /* skip slash-star comment */
5569 if (s[0] == '*' && s[1] == '/')
5570 {
5571 s += 2;
5572 break;
5573 }
5574 }
5575 return s;
5576}
5577
5578/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005579 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 * not considered code.
5581 */
5582 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005583cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584{
5585 return *cin_skipcomment(s) == NUL;
5586}
5587
5588/*
5589 * Check previous lines for a "//" line comment, skipping over blank lines.
5590 */
5591 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005592find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593{
5594 static pos_T pos;
5595 char_u *line;
5596 char_u *p;
5597
5598 pos = curwin->w_cursor;
5599 while (--pos.lnum > 0)
5600 {
5601 line = ml_get(pos.lnum);
5602 p = skipwhite(line);
5603 if (cin_islinecomment(p))
5604 {
5605 pos.col = (int)(p - line);
5606 return &pos;
5607 }
5608 if (*p != NUL)
5609 break;
5610 }
5611 return NULL;
5612}
5613
5614/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005615 * Return TRUE if "text" starts with "key:".
5616 */
5617 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005618cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005619{
5620 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005621 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005622
5623 if (*s == '\'' || *s == '"')
5624 {
5625 /* can be 'key': or "key": */
5626 quote = *s;
5627 ++s;
5628 }
5629 if (!vim_isIDc(*s)) /* need at least one ID character */
5630 return FALSE;
5631
5632 while (vim_isIDc(*s))
5633 ++s;
5634 if (*s == quote)
5635 ++s;
5636
5637 s = cin_skipcomment(s);
5638
5639 /* "::" is not a label, it's C++ */
5640 return (*s == ':' && s[1] != ':');
5641}
5642
5643/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005645 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 */
5647 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005648cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649{
5650 if (!vim_isIDc(**s)) /* need at least one ID character */
5651 return FALSE;
5652
5653 while (vim_isIDc(**s))
5654 (*s)++;
5655
5656 *s = cin_skipcomment(*s);
5657
5658 /* "::" is not a label, it's C++ */
5659 return (**s == ':' && *++*s != ':');
5660}
5661
5662/*
5663 * Recognize a label: "label:".
5664 * Note: curwin->w_cursor must be where we are looking for the label.
5665 */
5666 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005667cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668{
5669 char_u *s;
5670
5671 s = cin_skipcomment(ml_get_curline());
5672
5673 /*
5674 * Exclude "default" from labels, since it should be indented
5675 * like a switch label. Same for C++ scope declarations.
5676 */
5677 if (cin_isdefault(s))
5678 return FALSE;
5679 if (cin_isscopedecl(s))
5680 return FALSE;
5681
5682 if (cin_islabel_skip(&s))
5683 {
5684 /*
5685 * Only accept a label if the previous line is terminated or is a case
5686 * label.
5687 */
5688 pos_T cursor_save;
5689 pos_T *trypos;
5690 char_u *line;
5691
5692 cursor_save = curwin->w_cursor;
5693 while (curwin->w_cursor.lnum > 1)
5694 {
5695 --curwin->w_cursor.lnum;
5696
5697 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005698 * If we're in a comment or raw string now, skip to the start of
5699 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 */
5701 curwin->w_cursor.col = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02005702 if ((trypos = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 curwin->w_cursor = *trypos;
5704
5705 line = ml_get_curline();
5706 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5707 continue;
5708 if (*(line = cin_skipcomment(line)) == NUL)
5709 continue;
5710
5711 curwin->w_cursor = cursor_save;
5712 if (cin_isterminated(line, TRUE, FALSE)
5713 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005714 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 || (cin_islabel_skip(&line) && cin_nocode(line)))
5716 return TRUE;
5717 return FALSE;
5718 }
5719 curwin->w_cursor = cursor_save;
5720 return TRUE; /* label at start of file??? */
5721 }
5722 return FALSE;
5723}
5724
5725/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005726 * Recognize structure initialization and enumerations:
5727 * "[typedef] [static|public|protected|private] enum"
5728 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 */
5730 static int
5731cin_isinit(void)
5732{
5733 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005734 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735
5736 s = cin_skipcomment(ml_get_curline());
5737
Bram Moolenaar75342212013-03-07 13:13:52 +01005738 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 s = cin_skipcomment(s + 7);
5740
Bram Moolenaar75342212013-03-07 13:13:52 +01005741 for (;;)
5742 {
5743 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005744
Bram Moolenaar75342212013-03-07 13:13:52 +01005745 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5746 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005747 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005748 if (cin_starts_with(s, skip[i]))
5749 {
5750 s = cin_skipcomment(s + l);
5751 l = 0;
5752 break;
5753 }
5754 }
5755 if (l != 0)
5756 break;
5757 }
5758
5759 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 return TRUE;
5761
5762 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5763 return TRUE;
5764
5765 return FALSE;
5766}
5767
5768/*
5769 * Recognize a switch label: "case .*:" or "default:".
5770 */
5771 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005772cin_iscase(
5773 char_u *s,
5774 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775{
5776 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005777 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 {
5779 for (s += 4; *s; ++s)
5780 {
5781 s = cin_skipcomment(s);
5782 if (*s == ':')
5783 {
5784 if (s[1] == ':') /* skip over "::" for C++ */
5785 ++s;
5786 else
5787 return TRUE;
5788 }
5789 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005790 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5792 return FALSE; /* stop at comment */
5793 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005794 {
5795 /* JS etc. */
5796 if (strict)
5797 return FALSE; /* stop at string */
5798 else
5799 return TRUE;
5800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 }
5802 return FALSE;
5803 }
5804
5805 if (cin_isdefault(s))
5806 return TRUE;
5807 return FALSE;
5808}
5809
5810/*
5811 * Recognize a "default" switch label.
5812 */
5813 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005814cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815{
5816 return (STRNCMP(s, "default", 7) == 0
5817 && *(s = cin_skipcomment(s + 7)) == ':'
5818 && s[1] != ':');
5819}
5820
5821/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005822 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 */
5824 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005825cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826{
5827 int i;
5828
5829 s = cin_skipcomment(s);
5830 if (STRNCMP(s, "public", 6) == 0)
5831 i = 6;
5832 else if (STRNCMP(s, "protected", 9) == 0)
5833 i = 9;
5834 else if (STRNCMP(s, "private", 7) == 0)
5835 i = 7;
5836 else
5837 return FALSE;
5838 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5839}
5840
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005841/* Maximum number of lines to search back for a "namespace" line. */
5842#define FIND_NAMESPACE_LIM 20
5843
5844/*
5845 * Recognize a "namespace" scope declaration.
5846 */
5847 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005848cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005849{
5850 char_u *p;
5851 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005852 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005853
5854 s = cin_skipcomment(s);
5855 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5856 {
5857 p = cin_skipcomment(skipwhite(s + 9));
5858 while (*p != NUL)
5859 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01005860 if (VIM_ISWHITE(*p))
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005861 {
5862 has_name = TRUE; /* found end of a name */
5863 p = cin_skipcomment(skipwhite(p));
5864 }
5865 else if (*p == '{')
5866 {
5867 break;
5868 }
5869 else if (vim_iswordc(*p))
5870 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005871 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005872 if (has_name)
5873 return FALSE; /* word character after skipping past name */
5874 ++p;
5875 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005876 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
5877 {
5878 if (!has_name_start || has_name)
5879 return FALSE;
5880 /* C++ 17 nested namespace */
5881 p += 3;
5882 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005883 else
5884 {
5885 return FALSE;
5886 }
5887 }
5888 return TRUE;
5889 }
5890 return FALSE;
5891}
5892
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893/*
Bram Moolenaar7720ba82017-03-08 22:19:26 +01005894 * Recognize a `extern "C"` or `extern "C++"` linkage specifications.
5895 */
5896 static int
5897cin_is_cpp_extern_c(char_u *s)
5898{
5899 char_u *p;
5900 int has_string_literal = FALSE;
5901
5902 s = cin_skipcomment(s);
5903 if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
5904 {
5905 p = cin_skipcomment(skipwhite(s + 6));
5906 while (*p != NUL)
5907 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01005908 if (VIM_ISWHITE(*p))
Bram Moolenaar7720ba82017-03-08 22:19:26 +01005909 {
5910 p = cin_skipcomment(skipwhite(p));
5911 }
5912 else if (*p == '{')
5913 {
5914 break;
5915 }
5916 else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
5917 {
5918 if (has_string_literal)
5919 return FALSE;
5920 has_string_literal = TRUE;
5921 p += 3;
5922 }
5923 else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
5924 && p[4] == '"')
5925 {
5926 if (has_string_literal)
5927 return FALSE;
5928 has_string_literal = TRUE;
5929 p += 5;
5930 }
5931 else
5932 {
5933 return FALSE;
5934 }
5935 }
5936 return has_string_literal ? TRUE : FALSE;
5937 }
5938 return FALSE;
5939}
5940
5941/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 * Return a pointer to the first non-empty non-comment character after a ':'.
5943 * Return NULL if not found.
5944 * case 234: a = b;
5945 * ^
5946 */
5947 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005948after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949{
5950 for ( ; *l; ++l)
5951 {
5952 if (*l == ':')
5953 {
5954 if (l[1] == ':') /* skip over "::" for C++ */
5955 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005956 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 break;
5958 }
5959 else if (*l == '\'' && l[1] && l[2] == '\'')
5960 l += 2; /* skip over 'x' */
5961 }
5962 if (*l == NUL)
5963 return NULL;
5964 l = cin_skipcomment(l + 1);
5965 if (*l == NUL)
5966 return NULL;
5967 return l;
5968}
5969
5970/*
5971 * Get indent of line "lnum", skipping a label.
5972 * Return 0 if there is nothing after the label.
5973 */
5974 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005975get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976{
5977 char_u *l;
5978 pos_T fp;
5979 colnr_T col;
5980 char_u *p;
5981
5982 l = ml_get(lnum);
5983 p = after_label(l);
5984 if (p == NULL)
5985 return 0;
5986
5987 fp.col = (colnr_T)(p - l);
5988 fp.lnum = lnum;
5989 getvcol(curwin, &fp, &col, NULL, NULL);
5990 return (int)col;
5991}
5992
5993/*
5994 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005995 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 * label: if (asdf && asdfasdf)
5997 * ^
5998 */
5999 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006000skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001{
6002 char_u *l;
6003 int amount;
6004 pos_T cursor_save;
6005
6006 cursor_save = curwin->w_cursor;
6007 curwin->w_cursor.lnum = lnum;
6008 l = ml_get_curline();
6009 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006010 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 {
6012 amount = get_indent_nolabel(lnum);
6013 l = after_label(ml_get_curline());
6014 if (l == NULL) /* just in case */
6015 l = ml_get_curline();
6016 }
6017 else
6018 {
6019 amount = get_indent();
6020 l = ml_get_curline();
6021 }
6022 *pp = l;
6023
6024 curwin->w_cursor = cursor_save;
6025 return amount;
6026}
6027
6028/*
6029 * Return the indent of the first variable name after a type in a declaration.
6030 * int a, indent of "a"
6031 * static struct foo b, indent of "b"
6032 * enum bla c, indent of "c"
6033 * Returns zero when it doesn't look like a declaration.
6034 */
6035 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006036cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037{
6038 char_u *line, *p, *s;
6039 int len;
6040 pos_T fp;
6041 colnr_T col;
6042
6043 line = ml_get_curline();
6044 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006045 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 if (len == 6 && STRNCMP(p, "static", 6) == 0)
6047 {
6048 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006049 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 }
6051 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
6052 p = skipwhite(p + 6);
6053 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
6054 p = skipwhite(p + 4);
6055 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
6056 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
6057 {
6058 s = skipwhite(p + len);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006059 if ((STRNCMP(s, "int", 3) == 0 && VIM_ISWHITE(s[3]))
6060 || (STRNCMP(s, "long", 4) == 0 && VIM_ISWHITE(s[4]))
6061 || (STRNCMP(s, "short", 5) == 0 && VIM_ISWHITE(s[5]))
6062 || (STRNCMP(s, "char", 4) == 0 && VIM_ISWHITE(s[4])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 p = s;
6064 }
6065 for (len = 0; vim_isIDc(p[len]); ++len)
6066 ;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006067 if (len == 0 || !VIM_ISWHITE(p[len]) || cin_nocode(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 return 0;
6069
6070 p = skipwhite(p + len);
6071 fp.lnum = curwin->w_cursor.lnum;
6072 fp.col = (colnr_T)(p - line);
6073 getvcol(curwin, &fp, &col, NULL, NULL);
6074 return (int)col;
6075}
6076
6077/*
6078 * Return the indent of the first non-blank after an equal sign.
6079 * char *foo = "here";
6080 * Return zero if no (useful) equal sign found.
6081 * Return -1 if the line above "lnum" ends in a backslash.
6082 * foo = "asdf\
6083 * asdf\
6084 * here";
6085 */
6086 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006087cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088{
6089 char_u *line;
6090 char_u *s;
6091 colnr_T col;
6092 pos_T fp;
6093
6094 if (lnum > 1)
6095 {
6096 line = ml_get(lnum - 1);
6097 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6098 return -1;
6099 }
6100
6101 line = s = ml_get(lnum);
6102 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
6103 {
6104 if (cin_iscomment(s)) /* ignore comments */
6105 s = cin_skipcomment(s);
6106 else
6107 ++s;
6108 }
6109 if (*s != '=')
6110 return 0;
6111
6112 s = skipwhite(s + 1);
6113 if (cin_nocode(s))
6114 return 0;
6115
6116 if (*s == '"') /* nice alignment for continued strings */
6117 ++s;
6118
6119 fp.lnum = lnum;
6120 fp.col = (colnr_T)(s - line);
6121 getvcol(curwin, &fp, &col, NULL, NULL);
6122 return (int)col;
6123}
6124
6125/*
6126 * Recognize a preprocessor statement: Any line that starts with '#'.
6127 */
6128 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006129cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006131 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 return TRUE;
6133 return FALSE;
6134}
6135
6136/*
6137 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6138 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6139 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006140 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 */
6142 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006143cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144{
6145 char_u *line = *pp;
6146 linenr_T lnum = *lnump;
6147 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006148 int candidate_amount = *amount;
6149
6150 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6151 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006153 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 {
6155 if (cin_ispreproc(line))
6156 {
6157 retval = TRUE;
6158 *lnump = lnum;
6159 break;
6160 }
6161 if (lnum == 1)
6162 break;
6163 line = ml_get(--lnum);
6164 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6165 break;
6166 }
6167
6168 if (lnum != *lnump)
6169 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006170 if (retval)
6171 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 return retval;
6173}
6174
6175/*
6176 * Recognize the start of a C or C++ comment.
6177 */
6178 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006179cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180{
6181 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6182}
6183
6184/*
6185 * Recognize the start of a "//" comment.
6186 */
6187 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006188cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189{
6190 return (p[0] == '/' && p[1] == '/');
6191}
6192
6193/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006194 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6195 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006197 * If a line begins with an "else", only consider it terminated if no unmatched
6198 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 * Return the character terminating the line (ending char's have precedence if
6200 * both apply in order to determine initializations).
6201 */
6202 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006203cin_isterminated(
6204 char_u *s,
6205 int incl_open, /* include '{' at the end as terminator */
6206 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006208 char_u found_start = 0;
6209 unsigned n_open = 0;
6210 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211
6212 s = cin_skipcomment(s);
6213
6214 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6215 found_start = *s;
6216
Bram Moolenaar496f9512011-05-19 16:35:09 +02006217 if (!found_start)
6218 is_else = cin_iselse(s);
6219
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 while (*s)
6221 {
6222 /* skip over comments, "" strings and 'c'haracters */
6223 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006224 if (*s == '}' && n_open > 0)
6225 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006226 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006227 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 && cin_nocode(s + 1))
6229 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006230 else if (*s == '{')
6231 {
6232 if (incl_open && cin_nocode(s + 1))
6233 return *s;
6234 else
6235 ++n_open;
6236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237
6238 if (*s)
6239 s++;
6240 }
6241 return found_start;
6242}
6243
6244/*
6245 * Recognize the basic picture of a function declaration -- it needs to
6246 * have an open paren somewhere and a close paren at the end of the line and
6247 * no semicolons anywhere.
6248 * When a line ends in a comma we continue looking in the next line.
6249 * "sp" points to a string with the line. When looking at other lines it must
6250 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006251 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006252 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 */
6254 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006255cin_isfuncdecl(
6256 char_u **sp,
6257 linenr_T first_lnum,
6258 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259{
6260 char_u *s;
6261 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006262 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006264 pos_T *trypos;
6265 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266
6267 if (sp == NULL)
6268 s = ml_get(lnum);
6269 else
6270 s = *sp;
6271
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006272 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006273 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006274 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006275 {
6276 lnum = trypos->lnum;
6277 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006278 {
6279 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006280 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006281 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006282
6283 s = ml_get(lnum);
6284 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006285 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006286
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006287 /* Ignore line starting with #. */
6288 if (cin_ispreproc(s))
6289 return FALSE;
6290
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6292 {
6293 if (cin_iscomment(s)) /* ignore comments */
6294 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006295 else if (*s == ':')
6296 {
6297 if (*(s + 1) == ':')
6298 s += 2;
6299 else
6300 /* To avoid a mistake in the following situation:
6301 * A::A(int a, int b)
6302 * : a(0) // <--not a function decl
6303 * , b(0)
6304 * {...
6305 */
6306 return FALSE;
6307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 else
6309 ++s;
6310 }
6311 if (*s != '(')
6312 return FALSE; /* ';', ' or " before any () or no '(' */
6313
6314 while (*s && *s != ';' && *s != '\'' && *s != '"')
6315 {
6316 if (*s == ')' && cin_nocode(s + 1))
6317 {
6318 /* ')' at the end: may have found a match
6319 * Check for he previous line not to end in a backslash:
6320 * #if defined(x) && \
6321 * defined(y)
6322 */
6323 lnum = first_lnum - 1;
6324 s = ml_get(lnum);
6325 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6326 retval = TRUE;
6327 goto done;
6328 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006329 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006331 int comma = (*s == ',');
6332
6333 /* ',' at the end: continue looking in the next line.
6334 * At the end: check for ',' in the next line, for this style:
6335 * func(arg1
6336 * , arg2) */
6337 for (;;)
6338 {
6339 if (lnum >= curbuf->b_ml.ml_line_count)
6340 break;
6341 s = ml_get(++lnum);
6342 if (!cin_ispreproc(s))
6343 break;
6344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 if (lnum >= curbuf->b_ml.ml_line_count)
6346 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006347 /* Require a comma at end of the line or a comma or ')' at the
6348 * start of next line. */
6349 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006350 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006351 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006352 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 }
6354 else if (cin_iscomment(s)) /* ignore comments */
6355 s = cin_skipcomment(s);
6356 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006357 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006359 just_started = FALSE;
6360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 }
6362
6363done:
6364 if (lnum != first_lnum && sp != NULL)
6365 *sp = ml_get(first_lnum);
6366
6367 return retval;
6368}
6369
6370 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006371cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006373 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374}
6375
6376 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006377cin_iselse(
6378 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379{
6380 if (*p == '}') /* accept "} else" */
6381 p = cin_skipcomment(p + 1);
6382 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6383}
6384
6385 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006386cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387{
6388 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6389}
6390
6391/*
6392 * Check if this is a "while" that should have a matching "do".
6393 * We only accept a "while (condition) ;", with only white space between the
6394 * ')' and ';'. The condition may be spread over several lines.
6395 */
6396 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006397cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398{
6399 pos_T cursor_save;
6400 pos_T *trypos;
6401 int retval = FALSE;
6402
6403 p = cin_skipcomment(p);
6404 if (*p == '}') /* accept "} while (cond);" */
6405 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006406 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 {
6408 cursor_save = curwin->w_cursor;
6409 curwin->w_cursor.lnum = lnum;
6410 curwin->w_cursor.col = 0;
6411 p = ml_get_curline();
6412 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6413 {
6414 ++p;
6415 ++curwin->w_cursor.col;
6416 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006417 if ((trypos = findmatchlimit(NULL, 0, 0,
6418 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6420 retval = TRUE;
6421 curwin->w_cursor = cursor_save;
6422 }
6423 return retval;
6424}
6425
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006426/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006427 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006428 * Return 0 if there is none.
6429 * Otherwise return !0 and update "*poffset" to point to the place where the
6430 * string was found.
6431 */
6432 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006433cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006434{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006435 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006436
6437 if (offset-- < 2)
6438 return 0;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006439 while (offset > 2 && VIM_ISWHITE(line[offset]))
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006440 --offset;
6441
6442 offset -= 1;
6443 if (!STRNCMP(line + offset, "if", 2))
6444 goto probablyFound;
6445
6446 if (offset >= 1)
6447 {
6448 offset -= 1;
6449 if (!STRNCMP(line + offset, "for", 3))
6450 goto probablyFound;
6451
6452 if (offset >= 2)
6453 {
6454 offset -= 2;
6455 if (!STRNCMP(line + offset, "while", 5))
6456 goto probablyFound;
6457 }
6458 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006459 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006460
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006461probablyFound:
6462 if (!offset || !vim_isIDc(line[offset - 1]))
6463 {
6464 *poffset = offset;
6465 return 1;
6466 }
6467 return 0;
6468}
6469
6470/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006471 * Return TRUE if we are at the end of a do-while.
6472 * do
6473 * nothing;
6474 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006475 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006476 * Adjust the cursor to the line with "while".
6477 */
6478 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006479cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006480{
6481 char_u *line;
6482 char_u *p;
6483 char_u *s;
6484 pos_T *trypos;
6485 int i;
6486
6487 if (terminated != ';') /* there must be a ';' at the end */
6488 return FALSE;
6489
6490 p = line = ml_get_curline();
6491 while (*p != NUL)
6492 {
6493 p = cin_skipcomment(p);
6494 if (*p == ')')
6495 {
6496 s = skipwhite(p + 1);
6497 if (*s == ';' && cin_nocode(s + 1))
6498 {
6499 /* Found ");" at end of the line, now check there is "while"
6500 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006501 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006502 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006503 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006504 if (trypos != NULL)
6505 {
6506 s = cin_skipcomment(ml_get(trypos->lnum));
6507 if (*s == '}') /* accept "} while (cond);" */
6508 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006509 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006510 {
6511 curwin->w_cursor.lnum = trypos->lnum;
6512 return TRUE;
6513 }
6514 }
6515
6516 /* Searching may have made "line" invalid, get it again. */
6517 line = ml_get_curline();
6518 p = line + i;
6519 }
6520 }
6521 if (*p != NUL)
6522 ++p;
6523 }
6524 return FALSE;
6525}
6526
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006528cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529{
6530 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6531}
6532
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006533/*
6534 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 * constructor-initialization. eg:
6536 *
6537 * class MyClass :
6538 * baseClass <-- here
6539 * class MyClass : public baseClass,
6540 * anotherBaseClass <-- here (should probably lineup ??)
6541 * MyClass::MyClass(...) :
6542 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006543 *
6544 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 */
6546 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006547cin_is_cpp_baseclass(
6548 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006550 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 char_u *s;
6552 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006553 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006554 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006556 if (pos->lnum <= lnum)
6557 return cached->found; /* Use the cached result */
6558
6559 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006561 s = skipwhite(line);
6562 if (*s == '#') /* skip #define FOO x ? (x) : x */
6563 return FALSE;
6564 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 if (*s == NUL)
6566 return FALSE;
6567
6568 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6569
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006570 /* Search for a line starting with '#', empty, ending in ';' or containing
6571 * '{' or '}' and start below it. This handles the following situations:
6572 * a = cond ?
6573 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006574 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006575 * func::foo()
6576 * : something
6577 * {}
6578 * Foo::Foo (int one, int two)
6579 * : something(4),
6580 * somethingelse(3)
6581 * {}
6582 */
6583 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006585 line = ml_get(lnum - 1);
6586 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006587 if (*s == '#' || *s == NUL)
6588 break;
6589 while (*s != NUL)
6590 {
6591 s = cin_skipcomment(s);
6592 if (*s == '{' || *s == '}'
6593 || (*s == ';' && cin_nocode(s + 1)))
6594 break;
6595 if (*s != NUL)
6596 ++s;
6597 }
6598 if (*s != NUL)
6599 break;
6600 --lnum;
6601 }
6602
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006603 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006604 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006605 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006606 for (;;)
6607 {
6608 if (*s == NUL)
6609 {
6610 if (lnum == curwin->w_cursor.lnum)
6611 break;
6612 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006613 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006614 s = line;
6615 }
6616 if (s == line)
6617 {
6618 /* don't recognize "case (foo):" as a baseclass */
6619 if (cin_iscase(s, FALSE))
6620 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006621 s = cin_skipcomment(line);
6622 if (*s == NUL)
6623 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006624 }
6625
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006626 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006627 s = skip_string(s) + 1;
6628 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 {
6630 if (s[1] == ':')
6631 {
6632 /* skip double colon. It can't be a constructor
6633 * initialization any more */
6634 lookfor_ctor_init = FALSE;
6635 s = cin_skipcomment(s + 2);
6636 }
6637 else if (lookfor_ctor_init || class_or_struct)
6638 {
6639 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006640 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 cpp_base_class = TRUE;
6642 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006643 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 s = cin_skipcomment(s + 1);
6645 }
6646 else
6647 s = cin_skipcomment(s + 1);
6648 }
6649 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6650 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6651 {
6652 class_or_struct = TRUE;
6653 lookfor_ctor_init = FALSE;
6654
6655 if (*s == 'c')
6656 s = cin_skipcomment(s + 5);
6657 else
6658 s = cin_skipcomment(s + 6);
6659 }
6660 else
6661 {
6662 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6663 {
6664 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6665 }
6666 else if (s[0] == ')')
6667 {
6668 /* Constructor-initialization is assumed if we come across
6669 * something like "):" */
6670 class_or_struct = FALSE;
6671 lookfor_ctor_init = TRUE;
6672 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006673 else if (s[0] == '?')
6674 {
6675 /* Avoid seeing '() :' after '?' as constructor init. */
6676 return FALSE;
6677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 else if (!vim_isIDc(s[0]))
6679 {
6680 /* if it is not an identifier, we are wrong */
6681 class_or_struct = FALSE;
6682 lookfor_ctor_init = FALSE;
6683 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006684 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 {
6686 /* it can't be a constructor-initialization any more */
6687 lookfor_ctor_init = FALSE;
6688
6689 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006690 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006691 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 }
6693
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006694 /* When the line ends in a comma don't align with it. */
6695 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006696 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006697
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 s = cin_skipcomment(s + 1);
6699 }
6700 }
6701
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006702 cached->found = cpp_base_class;
6703 if (cpp_base_class)
6704 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 return cpp_base_class;
6706}
6707
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006708 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006709get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006710{
6711 int amount;
6712 colnr_T vcol;
6713 pos_T *trypos;
6714
6715 if (col == 0)
6716 {
6717 amount = get_indent();
6718 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006719 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006720 amount = get_indent_lnum(trypos->lnum); /* XXX */
6721 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006722 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006723 }
6724 else
6725 {
6726 curwin->w_cursor.col = col;
6727 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6728 amount = (int)vcol;
6729 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006730 if (amount < curbuf->b_ind_cpp_baseclass)
6731 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006732 return amount;
6733}
6734
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735/*
6736 * Return TRUE if string "s" ends with the string "find", possibly followed by
6737 * white space and comments. Skip strings and comments.
6738 * Ignore "ignore" after "find" if it's not NULL.
6739 */
6740 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006741cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742{
6743 char_u *p = s;
6744 char_u *r;
6745 int len = (int)STRLEN(find);
6746
6747 while (*p != NUL)
6748 {
6749 p = cin_skipcomment(p);
6750 if (STRNCMP(p, find, len) == 0)
6751 {
6752 r = skipwhite(p + len);
6753 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6754 r = skipwhite(r + STRLEN(ignore));
6755 if (cin_nocode(r))
6756 return TRUE;
6757 }
6758 if (*p != NUL)
6759 ++p;
6760 }
6761 return FALSE;
6762}
6763
6764/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006765 * Return TRUE when "s" starts with "word" and then a non-ID character.
6766 */
6767 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006768cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006769{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006770 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006771
6772 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6773}
6774
6775/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 * Skip strings, chars and comments until at or past "trypos".
6777 * Return the column found.
6778 */
6779 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006780cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781{
6782 char_u *line;
6783 char_u *p;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006784 char_u *new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785
6786 p = line = ml_get(trypos->lnum);
6787 while (*p && (colnr_T)(p - line) < trypos->col)
6788 {
6789 if (cin_iscomment(p))
6790 p = cin_skipcomment(p);
6791 else
6792 {
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006793 new_p = skip_string(p);
6794 if (new_p == p)
6795 ++p;
6796 else
6797 p = new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 }
6799 }
6800 return (int)(p - line);
6801}
6802
6803/*
6804 * Find the '{' at the start of the block we are in.
6805 * Return NULL if no match found.
6806 * Ignore a '{' that is in a comment, makes indenting the next three lines
6807 * work. */
6808/* foo() */
6809/* { */
6810/* } */
6811
6812 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006813find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814{
6815 pos_T cursor_save;
6816 pos_T *trypos;
6817 pos_T *pos;
6818 static pos_T pos_copy;
6819
6820 cursor_save = curwin->w_cursor;
6821 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6822 {
6823 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6824 trypos = &pos_copy;
6825 curwin->w_cursor = *trypos;
6826 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006827 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaardde81312017-08-26 17:49:01 +02006829 && (pos = ind_find_start_CORS(NULL)) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 break;
6831 if (pos != NULL)
6832 curwin->w_cursor.lnum = pos->lnum;
6833 }
6834 curwin->w_cursor = cursor_save;
6835 return trypos;
6836}
6837
6838/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006839 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006840 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 */
6842 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006843find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006845 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006846}
6847
6848 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006849find_match_char (int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006850{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 pos_T cursor_save;
6852 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006853 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006854 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855
6856 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006857 ind_maxp_wk = ind_maxparen;
6858retry:
6859 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 {
6861 /* check if the ( is in a // comment */
6862 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01006863 {
6864 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
6865 if (ind_maxp_wk > 0)
6866 {
6867 curwin->w_cursor = *trypos;
6868 curwin->w_cursor.col = 0; /* XXX */
6869 goto retry;
6870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 else
6874 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01006875 pos_T *trypos_wk;
6876
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6878 trypos = &pos_copy;
6879 curwin->w_cursor = *trypos;
Bram Moolenaardde81312017-08-26 17:49:01 +02006880 if ((trypos_wk = ind_find_start_CORS(NULL)) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01006881 {
6882 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
6883 - trypos_wk->lnum);
6884 if (ind_maxp_wk > 0)
6885 {
6886 curwin->w_cursor = *trypos_wk;
6887 goto retry;
6888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 }
6892 }
6893 curwin->w_cursor = cursor_save;
6894 return trypos;
6895}
6896
6897/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006898 * Find the matching '(', ignoring it if it is in a comment or before an
6899 * unmatched {.
6900 * Return NULL if no match found.
6901 */
6902 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006903find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02006904{
6905 pos_T *trypos = find_match_paren(ind_maxparen);
6906
6907 if (trypos != NULL)
6908 {
6909 pos_T *tryposBrace = find_start_brace();
6910
6911 /* If both an unmatched '(' and '{' is found. Ignore the '('
6912 * position if the '{' is further down. */
6913 if (tryposBrace != NULL
6914 && (trypos->lnum != tryposBrace->lnum
6915 ? trypos->lnum < tryposBrace->lnum
6916 : trypos->col < tryposBrace->col))
6917 trypos = NULL;
6918 }
6919 return trypos;
6920}
6921
6922/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 * Return ind_maxparen corrected for the difference in line number between the
6924 * cursor position and "startpos". This makes sure that searching for a
6925 * matching paren above the cursor line doesn't find a match because of
6926 * looking a few lines further.
6927 */
6928 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006929corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930{
6931 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6932
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006933 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
6934 return curbuf->b_ind_maxparen - (int)n;
6935 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936}
6937
6938/*
6939 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006940 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 */
6942 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006943find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944{
6945 int i;
6946 int retval = FALSE;
6947 int open_count = 0;
6948
6949 curwin->w_cursor.col = 0; /* default is start of line */
6950
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006951 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 {
6953 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6954 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6955 if (l[i] == start)
6956 ++open_count;
6957 else if (l[i] == end)
6958 {
6959 if (open_count > 0)
6960 --open_count;
6961 else
6962 {
6963 curwin->w_cursor.col = i;
6964 retval = TRUE;
6965 }
6966 }
6967 }
6968 return retval;
6969}
6970
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006971/*
6972 * Parse 'cinoptions' and set the values in "curbuf".
6973 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
6974 */
6975 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006976parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006977{
6978 char_u *p;
6979 char_u *l;
6980 char_u *digits;
6981 int n;
6982 int divider;
6983 int fraction = 0;
6984 int sw = (int)get_sw_value(buf);
6985
6986 /*
6987 * Set the default values.
6988 */
6989 /* Spaces from a block's opening brace the prevailing indent for that
6990 * block should be. */
6991 buf->b_ind_level = sw;
6992
6993 /* Spaces from the edge of the line an open brace that's at the end of a
6994 * line is imagined to be. */
6995 buf->b_ind_open_imag = 0;
6996
6997 /* Spaces from the prevailing indent for a line that is not preceded by
6998 * an opening brace. */
6999 buf->b_ind_no_brace = 0;
7000
7001 /* Column where the first { of a function should be located }. */
7002 buf->b_ind_first_open = 0;
7003
7004 /* Spaces from the prevailing indent a leftmost open brace should be
7005 * located. */
7006 buf->b_ind_open_extra = 0;
7007
7008 /* Spaces from the matching open brace (real location for one at the left
7009 * edge; imaginary location from one that ends a line) the matching close
7010 * brace should be located. */
7011 buf->b_ind_close_extra = 0;
7012
7013 /* Spaces from the edge of the line an open brace sitting in the leftmost
7014 * column is imagined to be. */
7015 buf->b_ind_open_left_imag = 0;
7016
7017 /* Spaces jump labels should be shifted to the left if N is non-negative,
7018 * otherwise the jump label will be put to column 1. */
7019 buf->b_ind_jump_label = -1;
7020
7021 /* Spaces from the switch() indent a "case xx" label should be located. */
7022 buf->b_ind_case = sw;
7023
7024 /* Spaces from the "case xx:" code after a switch() should be located. */
7025 buf->b_ind_case_code = sw;
7026
7027 /* Lineup break at end of case in switch() with case label. */
7028 buf->b_ind_case_break = 0;
7029
7030 /* Spaces from the class declaration indent a scope declaration label
7031 * should be located. */
7032 buf->b_ind_scopedecl = sw;
7033
7034 /* Spaces from the scope declaration label code should be located. */
7035 buf->b_ind_scopedecl_code = sw;
7036
7037 /* Amount K&R-style parameters should be indented. */
7038 buf->b_ind_param = sw;
7039
7040 /* Amount a function type spec should be indented. */
7041 buf->b_ind_func_type = sw;
7042
7043 /* Amount a cpp base class declaration or constructor initialization
7044 * should be indented. */
7045 buf->b_ind_cpp_baseclass = sw;
7046
7047 /* additional spaces beyond the prevailing indent a continuation line
7048 * should be located. */
7049 buf->b_ind_continuation = sw;
7050
7051 /* Spaces from the indent of the line with an unclosed parentheses. */
7052 buf->b_ind_unclosed = sw * 2;
7053
7054 /* Spaces from the indent of the line with an unclosed parentheses, which
7055 * itself is also unclosed. */
7056 buf->b_ind_unclosed2 = sw;
7057
7058 /* Suppress ignoring spaces from the indent of a line starting with an
7059 * unclosed parentheses. */
7060 buf->b_ind_unclosed_noignore = 0;
7061
7062 /* If the opening paren is the last nonwhite character on the line, and
7063 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
7064 * context (for very long lines). */
7065 buf->b_ind_unclosed_wrapped = 0;
7066
7067 /* Suppress ignoring white space when lining up with the character after
7068 * an unclosed parentheses. */
7069 buf->b_ind_unclosed_whiteok = 0;
7070
7071 /* Indent a closing parentheses under the line start of the matching
7072 * opening parentheses. */
7073 buf->b_ind_matching_paren = 0;
7074
7075 /* Indent a closing parentheses under the previous line. */
7076 buf->b_ind_paren_prev = 0;
7077
7078 /* Extra indent for comments. */
7079 buf->b_ind_comment = 0;
7080
7081 /* Spaces from the comment opener when there is nothing after it. */
7082 buf->b_ind_in_comment = 3;
7083
7084 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
7085 * after the comment opener. */
7086 buf->b_ind_in_comment2 = 0;
7087
7088 /* Max lines to search for an open paren. */
7089 buf->b_ind_maxparen = 20;
7090
7091 /* Max lines to search for an open comment. */
7092 buf->b_ind_maxcomment = 70;
7093
7094 /* Handle braces for java code. */
7095 buf->b_ind_java = 0;
7096
7097 /* Not to confuse JS object properties with labels. */
7098 buf->b_ind_js = 0;
7099
7100 /* Handle blocked cases correctly. */
7101 buf->b_ind_keep_case_label = 0;
7102
7103 /* Handle C++ namespace. */
7104 buf->b_ind_cpp_namespace = 0;
7105
7106 /* Handle continuation lines containing conditions of if(), for() and
7107 * while(). */
7108 buf->b_ind_if_for_while = 0;
7109
Bram Moolenaar6b643942017-03-05 19:44:06 +01007110 /* indentation for # comments */
7111 buf->b_ind_hash_comment = 0;
7112
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007113 /* Handle C++ extern "C" or "C++" */
7114 buf->b_ind_cpp_extern_c = 0;
7115
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007116 for (p = buf->b_p_cino; *p; )
7117 {
7118 l = p++;
7119 if (*p == '-')
7120 ++p;
7121 digits = p; /* remember where the digits start */
7122 n = getdigits(&p);
7123 divider = 0;
7124 if (*p == '.') /* ".5s" means a fraction */
7125 {
7126 fraction = atol((char *)++p);
7127 while (VIM_ISDIGIT(*p))
7128 {
7129 ++p;
7130 if (divider)
7131 divider *= 10;
7132 else
7133 divider = 10;
7134 }
7135 }
7136 if (*p == 's') /* "2s" means two times 'shiftwidth' */
7137 {
7138 if (p == digits)
7139 n = sw; /* just "s" is one 'shiftwidth' */
7140 else
7141 {
7142 n *= sw;
7143 if (divider)
7144 n += (sw * fraction + divider / 2) / divider;
7145 }
7146 ++p;
7147 }
7148 if (l[1] == '-')
7149 n = -n;
7150
7151 /* When adding an entry here, also update the default 'cinoptions' in
7152 * doc/indent.txt, and add explanation for it! */
7153 switch (*l)
7154 {
7155 case '>': buf->b_ind_level = n; break;
7156 case 'e': buf->b_ind_open_imag = n; break;
7157 case 'n': buf->b_ind_no_brace = n; break;
7158 case 'f': buf->b_ind_first_open = n; break;
7159 case '{': buf->b_ind_open_extra = n; break;
7160 case '}': buf->b_ind_close_extra = n; break;
7161 case '^': buf->b_ind_open_left_imag = n; break;
7162 case 'L': buf->b_ind_jump_label = n; break;
7163 case ':': buf->b_ind_case = n; break;
7164 case '=': buf->b_ind_case_code = n; break;
7165 case 'b': buf->b_ind_case_break = n; break;
7166 case 'p': buf->b_ind_param = n; break;
7167 case 't': buf->b_ind_func_type = n; break;
7168 case '/': buf->b_ind_comment = n; break;
7169 case 'c': buf->b_ind_in_comment = n; break;
7170 case 'C': buf->b_ind_in_comment2 = n; break;
7171 case 'i': buf->b_ind_cpp_baseclass = n; break;
7172 case '+': buf->b_ind_continuation = n; break;
7173 case '(': buf->b_ind_unclosed = n; break;
7174 case 'u': buf->b_ind_unclosed2 = n; break;
7175 case 'U': buf->b_ind_unclosed_noignore = n; break;
7176 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7177 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7178 case 'm': buf->b_ind_matching_paren = n; break;
7179 case 'M': buf->b_ind_paren_prev = n; break;
7180 case ')': buf->b_ind_maxparen = n; break;
7181 case '*': buf->b_ind_maxcomment = n; break;
7182 case 'g': buf->b_ind_scopedecl = n; break;
7183 case 'h': buf->b_ind_scopedecl_code = n; break;
7184 case 'j': buf->b_ind_java = n; break;
7185 case 'J': buf->b_ind_js = n; break;
7186 case 'l': buf->b_ind_keep_case_label = n; break;
7187 case '#': buf->b_ind_hash_comment = n; break;
7188 case 'N': buf->b_ind_cpp_namespace = n; break;
7189 case 'k': buf->b_ind_if_for_while = n; break;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007190 case 'E': buf->b_ind_cpp_extern_c = n; break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007191 }
7192 if (*p == ',')
7193 ++p;
7194 }
7195}
7196
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007197/*
7198 * Return the desired indent for C code.
7199 * Return -1 if the indent should be left alone (inside a raw string).
7200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007202get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 pos_T cur_curpos;
7205 int amount;
7206 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007207 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 colnr_T col;
7209 char_u *theline;
7210 char_u *linecopy;
7211 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007212 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007214 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 pos_T our_paren_pos;
7216 char_u *start;
7217 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007218#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219#define BRACE_AT_START 2 /* '{' is at start of line */
7220#define BRACE_AT_END 3 /* '{' is at end of line */
7221 linenr_T ourscope;
7222 char_u *l;
7223 char_u *look;
7224 char_u terminated;
7225 int lookfor;
7226#define LOOKFOR_INITIAL 0
7227#define LOOKFOR_IF 1
7228#define LOOKFOR_DO 2
7229#define LOOKFOR_CASE 3
7230#define LOOKFOR_ANY 4
7231#define LOOKFOR_TERM 5
7232#define LOOKFOR_UNTERM 6
7233#define LOOKFOR_SCOPEDECL 7
7234#define LOOKFOR_NOBREAK 8
7235#define LOOKFOR_CPP_BASECLASS 9
7236#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007237#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007238#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239
7240 int whilelevel;
7241 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 int n;
7243 int iscase;
7244 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007245 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007247 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007248 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007249 int js_cur_has_key = 0;
Bram Moolenaardde81312017-08-26 17:49:01 +02007250 linenr_T raw_string_start = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007251 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007253 /* make a copy, value is changed below */
7254 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255
7256 /* remember where the cursor was when we started */
7257 cur_curpos = curwin->w_cursor;
7258
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007259 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007260 if (cur_curpos.lnum == 1)
7261 return 0;
7262
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 /* Get a copy of the current contents of the line.
7264 * This is required, because only the most recent line obtained with
7265 * ml_get is valid! */
7266 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7267 if (linecopy == NULL)
7268 return 0;
7269
7270 /*
7271 * In insert mode and the cursor is on a ')' truncate the line at the
7272 * cursor position. We don't want to line up with the matching '(' when
7273 * inserting new stuff.
7274 * For unknown reasons the cursor might be past the end of the line, thus
7275 * check for that.
7276 */
7277 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007278 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 && linecopy[curwin->w_cursor.col] == ')')
7280 linecopy[curwin->w_cursor.col] = NUL;
7281
7282 theline = skipwhite(linecopy);
7283
7284 /* move the cursor to the start of the line */
7285
7286 curwin->w_cursor.col = 0;
7287
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007288 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007289
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007291 * If we are inside a raw string don't change the indent.
7292 * Ignore a raw string inside a comment.
7293 */
7294 comment_pos = ind_find_start_comment();
7295 if (comment_pos != NULL)
7296 {
7297 /* findmatchlimit() static pos is overwritten, make a copy */
7298 tryposCopy = *comment_pos;
7299 comment_pos = &tryposCopy;
7300 }
7301 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01007302 if (trypos != NULL && (comment_pos == NULL
7303 || LT_POS(*trypos, *comment_pos)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007304 {
7305 amount = -1;
7306 goto laterend;
7307 }
7308
7309 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 * #defines and so on always go at the left when included in 'cinkeys'.
7311 */
7312 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007313 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007314 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007315 goto theend;
7316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317
7318 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007319 * Is it a non-case label? Then that goes at the left margin too unless:
7320 * - JS flag is set.
7321 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007323 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007324 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 {
7326 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007327 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 }
7329
7330 /*
7331 * If we're inside a "//" comment and there is a "//" comment in a
7332 * previous line, lineup with that one.
7333 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007334 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 && (trypos = find_line_comment()) != NULL) /* XXX */
7336 {
7337 /* find how indented the line beginning the comment is */
7338 getvcol(curwin, trypos, &col, NULL, NULL);
7339 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007340 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 }
7342
7343 /*
7344 * If we're inside a comment and not looking at the start of the
7345 * comment, try using the 'comments' option.
7346 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007347 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 {
7349 int lead_start_len = 2;
7350 int lead_middle_len = 1;
7351 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7352 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7353 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7354 char_u *p;
7355 int start_align = 0;
7356 int start_off = 0;
7357 int done = FALSE;
7358
7359 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007360 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007362 *lead_start = NUL;
7363 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364
7365 p = curbuf->b_p_com;
7366 while (*p != NUL)
7367 {
7368 int align = 0;
7369 int off = 0;
7370 int what = 0;
7371
7372 while (*p != NUL && *p != ':')
7373 {
7374 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7375 what = *p++;
7376 else if (*p == COM_LEFT || *p == COM_RIGHT)
7377 align = *p++;
7378 else if (VIM_ISDIGIT(*p) || *p == '-')
7379 off = getdigits(&p);
7380 else
7381 ++p;
7382 }
7383
7384 if (*p == ':')
7385 ++p;
7386 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7387 if (what == COM_START)
7388 {
7389 STRCPY(lead_start, lead_end);
7390 lead_start_len = (int)STRLEN(lead_start);
7391 start_off = off;
7392 start_align = align;
7393 }
7394 else if (what == COM_MIDDLE)
7395 {
7396 STRCPY(lead_middle, lead_end);
7397 lead_middle_len = (int)STRLEN(lead_middle);
7398 }
7399 else if (what == COM_END)
7400 {
7401 /* If our line starts with the middle comment string, line it
7402 * up with the comment opener per the 'comments' option. */
7403 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7404 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7405 {
7406 done = TRUE;
7407 if (curwin->w_cursor.lnum > 1)
7408 {
7409 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007410 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 * the middle comment string matches in the previous
7412 * line, use the indent of that line. XXX */
7413 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7414 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7415 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7416 else if (STRNCMP(look, lead_middle,
7417 lead_middle_len) == 0)
7418 {
7419 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7420 break;
7421 }
7422 /* If the start comment string doesn't match with the
7423 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007424 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 lead_start, lead_start_len) != 0)
7426 continue;
7427 }
7428 if (start_off != 0)
7429 amount += start_off;
7430 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007431 amount += vim_strsize(lead_start)
7432 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 break;
7434 }
7435
7436 /* If our line starts with the end comment string, line it up
7437 * with the middle comment */
7438 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7439 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7440 {
7441 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7442 /* XXX */
7443 if (off != 0)
7444 amount += off;
7445 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007446 amount += vim_strsize(lead_start)
7447 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 done = TRUE;
7449 break;
7450 }
7451 }
7452 }
7453
7454 /* If our line starts with an asterisk, line up with the
7455 * asterisk in the comment opener; otherwise, line up
7456 * with the first character of the comment text.
7457 */
7458 if (done)
7459 ;
7460 else if (theline[0] == '*')
7461 amount += 1;
7462 else
7463 {
7464 /*
7465 * If we are more than one line away from the comment opener, take
7466 * the indent of the previous non-empty line. If 'cino' has "CO"
7467 * and we are just below the comment opener and there are any
7468 * white characters after it line up with the text after it;
7469 * otherwise, add the amount specified by "c" in 'cino'
7470 */
7471 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007472 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 {
7474 if (linewhite(lnum)) /* skip blank lines */
7475 continue;
7476 amount = get_indent_lnum(lnum); /* XXX */
7477 break;
7478 }
7479 if (amount == -1) /* use the comment opener */
7480 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007481 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007483 start = ml_get(comment_pos->lnum);
7484 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007486 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007488 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007490 if (curbuf->b_ind_in_comment2 || *look == NUL)
7491 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 }
7493 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007494 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 }
7496
7497 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007498 * Are we looking at a ']' that has a match?
7499 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007500 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007501 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7502 {
7503 /* align with the line containing the '['. */
7504 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007505 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007506 }
7507
7508 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 * Are we inside parentheses or braces?
7510 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007511 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007512 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007513 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 || trypos != NULL)
7515 {
7516 if (trypos != NULL && tryposBrace != NULL)
7517 {
7518 /* Both an unmatched '(' and '{' is found. Use the one which is
7519 * closer to the current cursor position, set the other to NULL. */
7520 if (trypos->lnum != tryposBrace->lnum
7521 ? trypos->lnum < tryposBrace->lnum
7522 : trypos->col < tryposBrace->col)
7523 trypos = NULL;
7524 else
7525 tryposBrace = NULL;
7526 }
7527
7528 if (trypos != NULL)
7529 {
7530 /*
7531 * If the matching paren is more than one line away, use the indent of
7532 * a previous non-empty line that matches the same paren.
7533 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007534 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007536 /* Line up with the start of the matching paren line. */
7537 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7538 }
7539 else
7540 {
7541 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007542 our_paren_pos = *trypos;
7543 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007545 l = skipwhite(ml_get(lnum));
7546 if (cin_nocode(l)) /* skip comment lines */
7547 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007548 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007549 continue; /* ignore #define, #if, etc. */
7550 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007552 /* Skip a comment or raw string. XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02007553 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007554 {
7555 lnum = trypos->lnum + 1;
7556 continue;
7557 }
7558
7559 /* XXX */
7560 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007561 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007562 && trypos->lnum == our_paren_pos.lnum
7563 && trypos->col == our_paren_pos.col)
7564 {
7565 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007567 if (theline[0] == ')')
7568 {
7569 if (our_paren_pos.lnum != lnum
7570 && cur_amount > amount)
7571 cur_amount = amount;
7572 amount = -1;
7573 }
7574 break;
7575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 }
7577 }
7578
7579 /*
7580 * Line up with line where the matching paren is. XXX
7581 * If the line starts with a '(' or the indent for unclosed
7582 * parentheses is zero, line up with the unclosed parentheses.
7583 */
7584 if (amount == -1)
7585 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007586 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007587 int is_if_for_while = 0;
7588
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007589 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007590 {
7591 /* Look for the outermost opening parenthesis on this line
7592 * and check whether it belongs to an "if", "for" or "while". */
7593
7594 pos_T cursor_save = curwin->w_cursor;
7595 pos_T outermost;
7596 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007597
7598 trypos = &our_paren_pos;
7599 do {
7600 outermost = *trypos;
7601 curwin->w_cursor.lnum = outermost.lnum;
7602 curwin->w_cursor.col = outermost.col;
7603
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007604 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007605 } while (trypos && trypos->lnum == outermost.lnum);
7606
7607 curwin->w_cursor = cursor_save;
7608
7609 line = ml_get(outermost.lnum);
7610
7611 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007612 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007613 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007614
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007615 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007616 look = skipwhite(look);
7617 if (*look == '(')
7618 {
7619 linenr_T save_lnum = curwin->w_cursor.lnum;
7620 char_u *line;
7621 int look_col;
7622
7623 /* Ignore a '(' in front of the line that has a match before
7624 * our matching '('. */
7625 curwin->w_cursor.lnum = our_paren_pos.lnum;
7626 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007627 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007628 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007629 if ((trypos = findmatchlimit(NULL, ')', 0,
7630 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007631 != NULL
7632 && trypos->lnum == our_paren_pos.lnum
7633 && trypos->col < our_paren_pos.col)
7634 ignore_paren_col = trypos->col + 1;
7635
7636 curwin->w_cursor.lnum = save_lnum;
7637 look = ml_get(our_paren_pos.lnum) + look_col;
7638 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007639 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7640 && is_if_for_while == 0)
7641 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007642 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 {
7644 /*
7645 * If we're looking at a close paren, line up right there;
7646 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007647 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 * the last nonwhite character of the line, use either the
7649 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007650 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 * lines).
7652 */
7653 if (theline[0] != ')')
7654 {
7655 cur_amount = MAXCOL;
7656 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007657 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 && cin_ends_in(l, (char_u *)"(", NULL))
7659 {
7660 /* look for opening unmatched paren, indent one level
7661 * for each additional level */
7662 n = 1;
7663 for (col = 0; col < our_paren_pos.col; ++col)
7664 {
7665 switch (l[col])
7666 {
7667 case '(':
7668 case '{': ++n;
7669 break;
7670
7671 case ')':
7672 case '}': if (n > 1)
7673 --n;
7674 break;
7675 }
7676 }
7677
7678 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007679 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007681 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 our_paren_pos.col++;
7683 else
7684 {
7685 col = our_paren_pos.col + 1;
Bram Moolenaar1c465442017-03-12 20:10:05 +01007686 while (VIM_ISWHITE(l[col]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 col++;
7688 if (l[col] != NUL) /* In case of trailing space */
7689 our_paren_pos.col = col;
7690 else
7691 our_paren_pos.col++;
7692 }
7693 }
7694
7695 /*
7696 * Find how indented the paren is, or the character after it
7697 * if we did the above "if".
7698 */
7699 if (our_paren_pos.col > 0)
7700 {
7701 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7702 if (cur_amount > (int)col)
7703 cur_amount = col;
7704 }
7705 }
7706
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007707 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 {
7709 /* Line up with the start of the matching paren line. */
7710 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007711 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7712 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007713 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 {
7715 if (cur_amount != MAXCOL)
7716 amount = cur_amount;
7717 }
7718 else
7719 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007720 /* Add b_ind_unclosed2 for each '(' before our matching one,
7721 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007723 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 {
7725 --our_paren_pos.col;
7726 switch (*ml_get_pos(&our_paren_pos))
7727 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007728 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 col = our_paren_pos.col;
7730 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007731 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 col = MAXCOL;
7733 break;
7734 }
7735 }
7736
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007737 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 * braces */
7739 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007740 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 else
7742 {
7743 curwin->w_cursor.lnum = our_paren_pos.lnum;
7744 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007745 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7746 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007747 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007749 {
7750 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007751 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007752 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007753 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 }
7756 /*
7757 * For a line starting with ')' use the minimum of the two
7758 * positions, to avoid giving it more indent than the previous
7759 * lines:
7760 * func_long_name( if (x
7761 * arg && yy
7762 * ) ^ not here ) ^ not here
7763 */
7764 if (cur_amount < amount)
7765 amount = cur_amount;
7766 }
7767 }
7768
7769 /* add extra indent for a comment */
7770 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007771 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 else
7774 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007775 /*
7776 * We are inside braces, there is a { before this line at the position
7777 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007778 * Make a copy of tryposBrace, it may point to pos_copy inside
7779 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007780 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007781 tryposCopy = *tryposBrace;
7782 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 ourscope = trypos->lnum;
7785 start = ml_get(ourscope);
7786
7787 /*
7788 * Now figure out how indented the line is in general.
7789 * If the brace was at the start of the line, we use that;
7790 * otherwise, check out the indentation of the line as
7791 * a whole and then add the "imaginary indent" to that.
7792 */
7793 look = skipwhite(start);
7794 if (*look == '{')
7795 {
7796 getvcol(curwin, trypos, &col, NULL, NULL);
7797 amount = col;
7798 if (*start == '{')
7799 start_brace = BRACE_IN_COL0;
7800 else
7801 start_brace = BRACE_AT_START;
7802 }
7803 else
7804 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007805 /* That opening brace might have been on a continuation
7806 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 curwin->w_cursor.lnum = ourscope;
7808
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007809 /* Position the cursor over the rightmost paren, so that
7810 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 lnum = ourscope;
7812 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007813 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7814 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 lnum = trypos->lnum;
7816
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007817 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 * case 1: if (asdf &&
7819 * ldfd) {
7820 * }
7821 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007822 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7823 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007825 else if (curbuf->b_ind_js)
7826 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007828 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829
7830 start_brace = BRACE_AT_END;
7831 }
7832
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007833 /* For Javascript check if the line starts with "key:". */
7834 if (curbuf->b_ind_js)
7835 js_cur_has_key = cin_has_js_key(theline);
7836
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007838 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 * we want to be. otherwise, add the amount of room
7840 * that an indent is supposed to be.
7841 */
7842 if (theline[0] == '}')
7843 {
7844 /*
7845 * they may want closing braces to line up with something
7846 * other than the open brace. indulge them, if so.
7847 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007848 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 }
7850 else
7851 {
7852 /*
7853 * If we're looking at an "else", try to find an "if"
7854 * to match it with.
7855 * If we're looking at a "while", try to find a "do"
7856 * to match it with.
7857 */
7858 lookfor = LOOKFOR_INITIAL;
7859 if (cin_iselse(theline))
7860 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007861 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 lookfor = LOOKFOR_DO;
7863 if (lookfor != LOOKFOR_INITIAL)
7864 {
7865 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007866 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 {
7868 amount = get_indent(); /* XXX */
7869 goto theend;
7870 }
7871 }
7872
7873 /*
7874 * We get here if we are not on an "while-of-do" or "else" (or
7875 * failed to find a matching "if").
7876 * Search backwards for something to line up with.
7877 * First set amount for when we don't find anything.
7878 */
7879
7880 /*
7881 * if the '{' is _really_ at the left margin, use the imaginary
7882 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007883 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 */
7885
7886 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7887 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007888 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007889 lookfor_cpp_namespace = TRUE;
7890 }
7891 else if (start_brace == BRACE_AT_START &&
7892 lookfor_cpp_namespace) /* '{' is at start */
7893 {
7894
7895 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 }
7897 else
7898 {
7899 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007900 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007901 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007902
7903 l = skipwhite(ml_get_curline());
7904 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007905 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007906 else if (cin_is_cpp_extern_c(l))
7907 amount += curbuf->b_ind_cpp_extern_c;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 else
7910 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007911 /* Compensate for adding b_ind_open_extra later. */
7912 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 if (amount < 0)
7914 amount = 0;
7915 }
7916 }
7917
7918 lookfor_break = FALSE;
7919
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007920 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 {
7922 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007923 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 }
7925 else if (cin_isscopedecl(theline)) /* private:, ... */
7926 {
7927 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007928 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 }
7930 else
7931 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007932 if (curbuf->b_ind_case_break && cin_isbreak(theline))
7933 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 lookfor_break = TRUE;
7935
7936 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007937 /* b_ind_level from start of block */
7938 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 }
7940 scope_amount = amount;
7941 whilelevel = 0;
7942
7943 /*
7944 * Search backwards. If we find something we recognize, line up
7945 * with that.
7946 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007947 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 * the usual amount relative to the conditional
7949 * that opens the block.
7950 */
7951 curwin->w_cursor = cur_curpos;
7952 for (;;)
7953 {
7954 curwin->w_cursor.lnum--;
7955 curwin->w_cursor.col = 0;
7956
7957 /*
7958 * If we went all the way back to the start of our scope, line
7959 * up with it.
7960 */
7961 if (curwin->w_cursor.lnum <= ourscope)
7962 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007963 /* We reached end of scope:
7964 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007966 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 * don't add ind_continuation, otherwise it is a variable
7968 * declaration:
7969 * int x,
7970 * here; <-- add ind_continuation
7971 */
7972 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7973 {
7974 if (curwin->w_cursor.lnum == 0
7975 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007976 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007978 /* nothing found (abuse curbuf->b_ind_maxparen as
7979 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 * initialization) */
7981 if (cont_amount > 0)
7982 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007983 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 amount += ind_continuation;
7985 break;
7986 }
7987
7988 l = ml_get_curline();
7989
7990 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007991 * If we're in a comment or raw string now, skip to
7992 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 */
Bram Moolenaardde81312017-08-26 17:49:01 +02007994 trypos = ind_find_start_CORS(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 if (trypos != NULL)
7996 {
7997 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007998 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 continue;
8000 }
8001
8002 /*
8003 * Skip preprocessor directives and blank lines.
8004 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008005 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8006 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 continue;
8008
8009 if (cin_nocode(l))
8010 continue;
8011
8012 terminated = cin_isterminated(l, FALSE, TRUE);
8013
8014 /*
8015 * If we are at top level and the line looks like a
8016 * function declaration, we are done
8017 * (it's a variable declaration).
8018 */
8019 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008020 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 {
8022 /* if the line is terminated with another ','
8023 * it is a continued variable initialization.
8024 * don't add extra indent.
8025 * TODO: does not work, if a function
8026 * declaration is split over multiple lines:
8027 * cin_isfuncdecl returns FALSE then.
8028 */
8029 if (terminated == ',')
8030 break;
8031
8032 /* if it es a enum declaration or an assignment,
8033 * we are done.
8034 */
8035 if (terminated != ';' && cin_isinit())
8036 break;
8037
8038 /* nothing useful found */
8039 if (terminated == 0 || terminated == '{')
8040 continue;
8041 }
8042
8043 if (terminated != ';')
8044 {
8045 /* Skip parens and braces. Position the cursor
8046 * over the rightmost paren, so that matching it
8047 * will take us back to the start of the line.
8048 */ /* XXX */
8049 trypos = NULL;
8050 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008051 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008052 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053
8054 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008055 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056
8057 if (trypos != NULL)
8058 {
8059 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008060 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 continue;
8062 }
8063 }
8064
8065 /* it's a variable declaration, add indentation
8066 * like in
8067 * int a,
8068 * b;
8069 */
8070 if (cont_amount > 0)
8071 amount = cont_amount;
8072 else
8073 amount += ind_continuation;
8074 }
8075 else if (lookfor == LOOKFOR_UNTERM)
8076 {
8077 if (cont_amount > 0)
8078 amount = cont_amount;
8079 else
8080 amount += ind_continuation;
8081 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008082 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008083 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02008084 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008085 && lookfor != LOOKFOR_CPP_BASECLASS
8086 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02008087 {
8088 amount = scope_amount;
8089 if (theline[0] == '{')
8090 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008091 amount += curbuf->b_ind_open_extra;
8092 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008093 }
8094 }
8095
8096 if (lookfor_cpp_namespace)
8097 {
8098 /*
8099 * Looking for C++ namespace, need to look further
8100 * back.
8101 */
8102 if (curwin->w_cursor.lnum == ourscope)
8103 continue;
8104
8105 if (curwin->w_cursor.lnum == 0
8106 || curwin->w_cursor.lnum
8107 < ourscope - FIND_NAMESPACE_LIM)
8108 break;
8109
8110 l = ml_get_curline();
8111
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008112 /* If we're in a comment or raw string now, skip
8113 * to the start of it. */
Bram Moolenaardde81312017-08-26 17:49:01 +02008114 trypos = ind_find_start_CORS(NULL);
Bram Moolenaare79d1532011-10-04 18:03:47 +02008115 if (trypos != NULL)
8116 {
8117 curwin->w_cursor.lnum = trypos->lnum + 1;
8118 curwin->w_cursor.col = 0;
8119 continue;
8120 }
8121
8122 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008123 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8124 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02008125 continue;
8126
8127 /* Finally the actual check for "namespace". */
8128 if (cin_is_cpp_namespace(l))
8129 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008130 amount += curbuf->b_ind_cpp_namespace
8131 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008132 break;
8133 }
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008134 else if (cin_is_cpp_extern_c(l))
8135 {
8136 amount += curbuf->b_ind_cpp_extern_c
8137 - added_to_amount;
8138 break;
8139 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008140
8141 if (cin_nocode(l))
8142 continue;
8143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 }
8145 break;
8146 }
8147
8148 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008149 * If we're in a comment or raw string now, skip to the start
8150 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02008152 if ((trypos = ind_find_start_CORS(&raw_string_start)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 {
8154 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008155 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 continue;
8157 }
8158
8159 l = ml_get_curline();
8160
8161 /*
8162 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008163 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008165 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 if (iscase || cin_isscopedecl(l))
8167 {
8168 /* we are only looking for cpp base class
8169 * declaration/initialization any longer */
8170 if (lookfor == LOOKFOR_CPP_BASECLASS)
8171 break;
8172
8173 /* When looking for a "do" we are not interested in
8174 * labels. */
8175 if (whilelevel > 0)
8176 continue;
8177
8178 /*
8179 * case xx:
8180 * c = 99 + <- this indent plus continuation
8181 *-> here;
8182 */
8183 if (lookfor == LOOKFOR_UNTERM
8184 || lookfor == LOOKFOR_ENUM_OR_INIT)
8185 {
8186 if (cont_amount > 0)
8187 amount = cont_amount;
8188 else
8189 amount += ind_continuation;
8190 break;
8191 }
8192
8193 /*
8194 * case xx: <- line up with this case
8195 * x = 333;
8196 * case yy:
8197 */
8198 if ( (iscase && lookfor == LOOKFOR_CASE)
8199 || (iscase && lookfor_break)
8200 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8201 {
8202 /*
8203 * Check that this case label is not for another
8204 * switch()
8205 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008206 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008207 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 {
8209 amount = get_indent(); /* XXX */
8210 break;
8211 }
8212 continue;
8213 }
8214
8215 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8216
8217 /*
8218 * case xx: if (cond) <- line up with this if
8219 * y = y + 1;
8220 * -> s = 99;
8221 *
8222 * case xx:
8223 * if (cond) <- line up with this line
8224 * y = y + 1;
8225 * -> s = 99;
8226 */
8227 if (lookfor == LOOKFOR_TERM)
8228 {
8229 if (n)
8230 amount = n;
8231
8232 if (!lookfor_break)
8233 break;
8234 }
8235
8236 /*
8237 * case xx: x = x + 1; <- line up with this x
8238 * -> y = y + 1;
8239 *
8240 * case xx: if (cond) <- line up with this if
8241 * -> y = y + 1;
8242 */
8243 if (n)
8244 {
8245 amount = n;
8246 l = after_label(ml_get_curline());
8247 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008248 {
8249 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008250 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008251 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008252 amount += curbuf->b_ind_level
8253 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 break;
8256 }
8257
8258 /*
8259 * Try to get the indent of a statement before the switch
8260 * label. If nothing is found, line up relative to the
8261 * switch label.
8262 * break; <- may line up with this line
8263 * case xx:
8264 * -> y = 1;
8265 */
8266 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008267 ? curbuf->b_ind_case_code
8268 : curbuf->b_ind_scopedecl_code);
8269 lookfor = curbuf->b_ind_case_break
8270 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 continue;
8272 }
8273
8274 /*
8275 * Looking for a switch() label or C++ scope declaration,
8276 * ignore other lines, skip {}-blocks.
8277 */
8278 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8279 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008280 if (find_last_paren(l, '{', '}')
8281 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008282 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008284 curwin->w_cursor.col = 0;
8285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 continue;
8287 }
8288
8289 /*
8290 * Ignore jump labels with nothing after them.
8291 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008292 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 {
8294 l = after_label(ml_get_curline());
8295 if (l == NULL || cin_nocode(l))
8296 continue;
8297 }
8298
8299 /*
8300 * Ignore #defines, #if, etc.
8301 * Ignore comment and empty lines.
8302 * (need to get the line again, cin_islabel() may have
8303 * unlocked it)
8304 */
8305 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008306 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 || cin_nocode(l))
8308 continue;
8309
8310 /*
8311 * Are we at the start of a cpp base class declaration or
8312 * constructor initialization?
8313 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008314 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008315 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008316 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008317 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008318 l = ml_get_curline();
8319 }
8320 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 {
8322 if (lookfor == LOOKFOR_UNTERM)
8323 {
8324 if (cont_amount > 0)
8325 amount = cont_amount;
8326 else
8327 amount += ind_continuation;
8328 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008329 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008331 /* Need to find start of the declaration. */
8332 lookfor = LOOKFOR_UNTERM;
8333 ind_continuation = 0;
8334 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 }
8336 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008337 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008338 amount = get_baseclass_amount(
8339 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 break;
8341 }
8342 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8343 {
8344 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008345 * declaration or initialization before the opening brace.
8346 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 if (cin_isterminated(l, TRUE, FALSE))
8348 break;
8349 else
8350 continue;
8351 }
8352
8353 /*
8354 * What happens next depends on the line being terminated.
8355 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008356 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 * 123,
8358 * sizeof
8359 * here
8360 * Otherwise check whether it is a enumeration or structure
8361 * initialisation (not indented) or a variable declaration
8362 * (indented).
8363 */
8364 terminated = cin_isterminated(l, FALSE, TRUE);
8365
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008366 if (js_cur_has_key)
8367 {
8368 js_cur_has_key = 0; /* only check the first line */
8369 if (curbuf->b_ind_js && terminated == ',')
8370 {
8371 /* For Javascript we might be inside an object:
8372 * key: something, <- align with this
8373 * key: something
8374 * or:
8375 * key: something + <- align with this
8376 * something,
8377 * key: something
8378 */
8379 lookfor = LOOKFOR_JS_KEY;
8380 }
8381 }
8382 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8383 {
8384 amount = get_indent();
8385 break;
8386 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008387 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008388 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008389 if (tryposBrace != NULL && tryposBrace->lnum
8390 >= curwin->w_cursor.lnum)
8391 break;
8392 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008393 /* line below current line is the one that starts a
8394 * (possibly broken) line ending in a comma. */
8395 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008396 else
8397 {
8398 amount = get_indent();
8399 if (curwin->w_cursor.lnum - 1 == ourscope)
8400 /* line above is start of the scope, thus current
8401 * line is the one that stars a (possibly broken)
8402 * line ending in a comma. */
8403 break;
8404 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008405 }
8406
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8408 && terminated == ','))
8409 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008410 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8411 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008412 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 /*
8414 * if we're in the middle of a paren thing,
8415 * go back to the line that starts it so
8416 * we can get the right prevailing indent
8417 * if ( foo &&
8418 * bar )
8419 */
8420 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008421 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008423 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 */
8425 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008426 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008427 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8428 || (trypos->lnum == tryposBrace->lnum
8429 && trypos->col < tryposBrace->col)))
8430 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431
8432 /*
8433 * If we are looking for ',', we also look for matching
8434 * braces.
8435 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008436 if (trypos == NULL && terminated == ','
8437 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008438 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439
8440 if (trypos != NULL)
8441 {
8442 /*
8443 * Check if we are on a case label now. This is
8444 * handled above.
8445 * case xx: if ( asdf &&
8446 * asdf)
8447 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008448 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008450 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 {
8452 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008453 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 continue;
8455 }
8456 }
8457
8458 /*
8459 * Skip over continuation lines to find the one to get the
8460 * indent from
8461 * char *usethis = "bla\
8462 * bla",
8463 * here;
8464 */
8465 if (terminated == ',')
8466 {
8467 while (curwin->w_cursor.lnum > 1)
8468 {
8469 l = ml_get(curwin->w_cursor.lnum - 1);
8470 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8471 break;
8472 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008473 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 }
8475 }
8476
8477 /*
8478 * Get indent and pointer to text for current line,
8479 * ignoring any jump label. XXX
8480 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008481 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008482 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008483 else
8484 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 /*
8486 * If this is just above the line we are indenting, and it
8487 * starts with a '{', line it up with this line.
8488 * while (not)
8489 * -> {
8490 * }
8491 */
8492 if (terminated != ',' && lookfor != LOOKFOR_TERM
8493 && theline[0] == '{')
8494 {
8495 amount = cur_amount;
8496 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008497 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 * doesn't start with a '{', which must have a match
8499 * in the same line (scope is the same). Probably:
8500 * { 1, 2 },
8501 * -> { 3, 4 }
8502 */
8503 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008504 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008506 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 {
8508 /* have to look back, whether it is a cpp base
8509 * class declaration or initialization */
8510 lookfor = LOOKFOR_CPP_BASECLASS;
8511 continue;
8512 }
8513 break;
8514 }
8515
8516 /*
8517 * Check if we are after an "if", "while", etc.
8518 * Also allow " } else".
8519 */
8520 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8521 {
8522 /*
8523 * Found an unterminated line after an if (), line up
8524 * with the last one.
8525 * if (cond)
8526 * 100 +
8527 * -> here;
8528 */
8529 if (lookfor == LOOKFOR_UNTERM
8530 || lookfor == LOOKFOR_ENUM_OR_INIT)
8531 {
8532 if (cont_amount > 0)
8533 amount = cont_amount;
8534 else
8535 amount += ind_continuation;
8536 break;
8537 }
8538
8539 /*
8540 * If this is just above the line we are indenting, we
8541 * are finished.
8542 * while (not)
8543 * -> here;
8544 * Otherwise this indent can be used when the line
8545 * before this is terminated.
8546 * yyy;
8547 * if (stat)
8548 * while (not)
8549 * xxx;
8550 * -> here;
8551 */
8552 amount = cur_amount;
8553 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008554 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 if (lookfor != LOOKFOR_TERM)
8556 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008557 amount += curbuf->b_ind_level
8558 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 break;
8560 }
8561
8562 /*
8563 * Special trick: when expecting the while () after a
8564 * do, line up with the while()
8565 * do
8566 * x = 1;
8567 * -> here
8568 */
8569 l = skipwhite(ml_get_curline());
8570 if (cin_isdo(l))
8571 {
8572 if (whilelevel == 0)
8573 break;
8574 --whilelevel;
8575 }
8576
8577 /*
8578 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008579 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 * Need to use the scope of this "else". XXX
8581 * If whilelevel != 0 continue looking for a "do {".
8582 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008583 if (cin_iselse(l) && whilelevel == 0)
8584 {
8585 /* If we're looking at "} else", let's make sure we
8586 * find the opening brace of the enclosing scope,
8587 * not the one from "if () {". */
8588 if (*l == '}')
8589 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008590 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008591
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008592 if ((trypos = find_start_brace()) == NULL
8593 || find_match(LOOKFOR_IF, trypos->lnum)
8594 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008595 break;
8596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 }
8598
8599 /*
8600 * If we're below an unterminated line that is not an
8601 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008602 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 * the line before this one.
8604 */
8605 else
8606 {
8607 /*
8608 * Found two unterminated lines on a row, line up with
8609 * the last one.
8610 * c = 99 +
8611 * 100 +
8612 * -> here;
8613 */
8614 if (lookfor == LOOKFOR_UNTERM)
8615 {
8616 /* When line ends in a comma add extra indent */
8617 if (terminated == ',')
8618 amount += ind_continuation;
8619 break;
8620 }
8621
8622 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8623 {
8624 /* Found two lines ending in ',', lineup with the
8625 * lowest one, but check for cpp base class
8626 * declaration/initialization, if it is an
8627 * opening brace or we are looking just for
8628 * enumerations/initializations. */
8629 if (terminated == ',')
8630 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008631 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 break;
8633
8634 lookfor = LOOKFOR_CPP_BASECLASS;
8635 continue;
8636 }
8637
8638 /* Ignore unterminated lines in between, but
8639 * reduce indent. */
8640 if (amount > cur_amount)
8641 amount = cur_amount;
8642 }
8643 else
8644 {
8645 /*
8646 * Found first unterminated line on a row, may
8647 * line up with this line, remember its indent
8648 * 100 +
8649 * -> here;
8650 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008651 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008653
8654 n = (int)STRLEN(l);
8655 if (terminated == ',' && (*skipwhite(l) == ']'
8656 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008657 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658
8659 /*
8660 * If previous line ends in ',', check whether we
8661 * are in an initialization or enum
8662 * struct xxx =
8663 * {
8664 * sizeof a,
8665 * 124 };
8666 * or a normal possible continuation line.
8667 * but only, of no other statement has been found
8668 * yet.
8669 */
8670 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8671 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008672 if (curbuf->b_ind_js)
8673 {
8674 /* Search for a line ending in a comma
8675 * and line up with the line below it
8676 * (could be the current line).
8677 * some = [
8678 * 1, <- line up here
8679 * 2,
8680 * some = [
8681 * 3 + <- line up here
8682 * 4 *
8683 * 5,
8684 * 6,
8685 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008686 if (cin_iscomment(skipwhite(l)))
8687 break;
8688 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008689 trypos = find_match_char('[',
8690 curbuf->b_ind_maxparen);
8691 if (trypos != NULL)
8692 {
8693 if (trypos->lnum
8694 == curwin->w_cursor.lnum - 1)
8695 {
8696 /* Current line is first inside
8697 * [], line up with it. */
8698 break;
8699 }
8700 ourscope = trypos->lnum;
8701 }
8702 }
8703 else
8704 {
8705 lookfor = LOOKFOR_ENUM_OR_INIT;
8706 cont_amount = cin_first_id_amount();
8707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 }
8709 else
8710 {
8711 if (lookfor == LOOKFOR_INITIAL
8712 && *l != NUL
8713 && l[STRLEN(l) - 1] == '\\')
8714 /* XXX */
8715 cont_amount = cin_get_equal_amount(
8716 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008717 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008718 && lookfor != LOOKFOR_JS_KEY
Bram Moolenaardde81312017-08-26 17:49:01 +02008719 && lookfor != LOOKFOR_COMMA
8720 && raw_string_start != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 lookfor = LOOKFOR_UNTERM;
8722 }
8723 }
8724 }
8725 }
8726
8727 /*
8728 * Check if we are after a while (cond);
8729 * If so: Ignore until the matching "do".
8730 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008731 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 {
8733 /*
8734 * Found an unterminated line after a while ();, line up
8735 * with the last one.
8736 * while (cond);
8737 * 100 + <- line up with this one
8738 * -> here;
8739 */
8740 if (lookfor == LOOKFOR_UNTERM
8741 || lookfor == LOOKFOR_ENUM_OR_INIT)
8742 {
8743 if (cont_amount > 0)
8744 amount = cont_amount;
8745 else
8746 amount += ind_continuation;
8747 break;
8748 }
8749
8750 if (whilelevel == 0)
8751 {
8752 lookfor = LOOKFOR_TERM;
8753 amount = get_indent(); /* XXX */
8754 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008755 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 }
8757 ++whilelevel;
8758 }
8759
8760 /*
8761 * We are after a "normal" statement.
8762 * If we had another statement we can stop now and use the
8763 * indent of that other statement.
8764 * Otherwise the indent of the current statement may be used,
8765 * search backwards for the next "normal" statement.
8766 */
8767 else
8768 {
8769 /*
8770 * Skip single break line, if before a switch label. It
8771 * may be lined up with the case label.
8772 */
8773 if (lookfor == LOOKFOR_NOBREAK
8774 && cin_isbreak(skipwhite(ml_get_curline())))
8775 {
8776 lookfor = LOOKFOR_ANY;
8777 continue;
8778 }
8779
8780 /*
8781 * Handle "do {" line.
8782 */
8783 if (whilelevel > 0)
8784 {
8785 l = cin_skipcomment(ml_get_curline());
8786 if (cin_isdo(l))
8787 {
8788 amount = get_indent(); /* XXX */
8789 --whilelevel;
8790 continue;
8791 }
8792 }
8793
8794 /*
8795 * Found a terminated line above an unterminated line. Add
8796 * the amount for a continuation line.
8797 * x = 1;
8798 * y = foo +
8799 * -> here;
8800 * or
8801 * int x = 1;
8802 * int foo,
8803 * -> here;
8804 */
8805 if (lookfor == LOOKFOR_UNTERM
8806 || lookfor == LOOKFOR_ENUM_OR_INIT)
8807 {
8808 if (cont_amount > 0)
8809 amount = cont_amount;
8810 else
8811 amount += ind_continuation;
8812 break;
8813 }
8814
8815 /*
8816 * Found a terminated line above a terminated line or "if"
8817 * etc. line. Use the amount of the line below us.
8818 * x = 1; x = 1;
8819 * if (asdf) y = 2;
8820 * while (asdf) ->here;
8821 * here;
8822 * ->foo;
8823 */
8824 if (lookfor == LOOKFOR_TERM)
8825 {
8826 if (!lookfor_break && whilelevel == 0)
8827 break;
8828 }
8829
8830 /*
8831 * First line above the one we're indenting is terminated.
8832 * To know what needs to be done look further backward for
8833 * a terminated line.
8834 */
8835 else
8836 {
8837 /*
8838 * position the cursor over the rightmost paren, so
8839 * that matching it will take us back to the start of
8840 * the line. Helps for:
8841 * func(asdr,
8842 * asdfasdf);
8843 * here;
8844 */
8845term_again:
8846 l = ml_get_curline();
8847 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008848 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008849 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 {
8851 /*
8852 * Check if we are on a case label now. This is
8853 * handled above.
8854 * case xx: if ( asdf &&
8855 * asdf)
8856 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008857 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008859 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 {
8861 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008862 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 continue;
8864 }
8865 }
8866
8867 /* When aligning with the case statement, don't align
8868 * with a statement after it.
8869 * case 1: { <-- don't use this { position
8870 * stat;
8871 * }
8872 * case 2:
8873 * stat;
8874 * }
8875 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008876 iscase = (curbuf->b_ind_keep_case_label
8877 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878
8879 /*
8880 * Get indent and pointer to text for current line,
8881 * ignoring any jump label.
8882 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008883 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884
8885 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008886 amount += curbuf->b_ind_open_extra;
8887 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008888 l = skipwhite(l);
8889 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008890 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8892
8893 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008894 * When a terminated line starts with "else" skip to
8895 * the matching "if":
8896 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008897 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008898 * Need to use the scope of this "else". XXX
8899 * If whilelevel != 0 continue looking for a "do {".
8900 */
8901 if (lookfor == LOOKFOR_TERM
8902 && *l != '}'
8903 && cin_iselse(l)
8904 && whilelevel == 0)
8905 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008906 if ((trypos = find_start_brace()) == NULL
8907 || find_match(LOOKFOR_IF, trypos->lnum)
8908 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008909 break;
8910 continue;
8911 }
8912
8913 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 * If we're at the end of a block, skip to the start of
8915 * that block.
8916 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008917 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008918 if (find_last_paren(l, '{', '}') /* XXX */
8919 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008921 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 /* if not "else {" check for terminated again */
8923 /* but skip block for "} else {" */
8924 l = cin_skipcomment(ml_get_curline());
8925 if (*l == '}' || !cin_iselse(l))
8926 goto term_again;
8927 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008928 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 }
8930 }
8931 }
8932 }
8933 }
8934 }
8935
8936 /* add extra indent for a comment */
8937 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008938 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008939
8940 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008941 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
8942 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008943
8944 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008946
8947 /*
8948 * ok -- we're not inside any sort of structure at all!
8949 *
8950 * This means we're at the top level, and everything should
8951 * basically just match where the previous line is, except
8952 * for the lines immediately following a function declaration,
8953 * which are K&R-style parameters and need to be indented.
8954 *
8955 * if our line starts with an open brace, forget about any
8956 * prevailing indent and make sure it looks like the start
8957 * of a function
8958 */
8959
8960 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008962 amount = curbuf->b_ind_first_open;
8963 goto theend;
8964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008966 /*
8967 * If the NEXT line is a function declaration, the current
8968 * line needs to be indented as a function type spec.
8969 * Don't do this if the current line looks like a comment or if the
8970 * current line is terminated, ie. ends in ';', or if the current line
8971 * contains { or }: "void f() {\n if (1)"
8972 */
8973 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8974 && !cin_nocode(theline)
8975 && vim_strchr(theline, '{') == NULL
8976 && vim_strchr(theline, '}') == NULL
8977 && !cin_ends_in(theline, (char_u *)":", NULL)
8978 && !cin_ends_in(theline, (char_u *)",", NULL)
8979 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
8980 cur_curpos.lnum + 1)
8981 && !cin_isterminated(theline, FALSE, TRUE))
8982 {
8983 amount = curbuf->b_ind_func_type;
8984 goto theend;
8985 }
8986
8987 /* search backwards until we find something we recognize */
8988 amount = 0;
8989 curwin->w_cursor = cur_curpos;
8990 while (curwin->w_cursor.lnum > 1)
8991 {
8992 curwin->w_cursor.lnum--;
8993 curwin->w_cursor.col = 0;
8994
8995 l = ml_get_curline();
8996
8997 /*
8998 * If we're in a comment or raw string now, skip to the start
8999 * of it.
9000 */ /* XXX */
Bram Moolenaardde81312017-08-26 17:49:01 +02009001 if ((trypos = ind_find_start_CORS(NULL)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009003 curwin->w_cursor.lnum = trypos->lnum + 1;
9004 curwin->w_cursor.col = 0;
9005 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 }
9007
9008 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009009 * Are we at the start of a cpp base class declaration or
9010 * constructor initialization?
9011 */ /* XXX */
9012 n = FALSE;
9013 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009015 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
9016 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009018 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009020 /* XXX */
9021 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
9022 break;
9023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009025 /*
9026 * Skip preprocessor directives and blank lines.
9027 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009028 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009029 continue;
9030
9031 if (cin_nocode(l))
9032 continue;
9033
9034 /*
9035 * If the previous line ends in ',', use one level of
9036 * indentation:
9037 * int foo,
9038 * bar;
9039 * do this before checking for '}' in case of eg.
9040 * enum foobar
9041 * {
9042 * ...
9043 * } foo,
9044 * bar;
9045 */
9046 n = 0;
9047 if (cin_ends_in(l, (char_u *)",", NULL)
9048 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
9049 {
9050 /* take us back to opening paren */
9051 if (find_last_paren(l, '(', ')')
9052 && (trypos = find_match_paren(
9053 curbuf->b_ind_maxparen)) != NULL)
9054 curwin->w_cursor = *trypos;
9055
9056 /* For a line ending in ',' that is a continuation line go
9057 * back to the first line with a backslash:
9058 * char *foo = "bla\
9059 * bla",
9060 * here;
9061 */
9062 while (n == 0 && curwin->w_cursor.lnum > 1)
9063 {
9064 l = ml_get(curwin->w_cursor.lnum - 1);
9065 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
9066 break;
9067 --curwin->w_cursor.lnum;
9068 curwin->w_cursor.col = 0;
9069 }
9070
9071 amount = get_indent(); /* XXX */
9072
9073 if (amount == 0)
9074 amount = cin_first_id_amount();
9075 if (amount == 0)
9076 amount = ind_continuation;
9077 break;
9078 }
9079
9080 /*
9081 * If the line looks like a function declaration, and we're
9082 * not in a comment, put it the left margin.
9083 */
9084 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
9085 break;
9086 l = ml_get_curline();
9087
9088 /*
9089 * Finding the closing '}' of a previous function. Put
9090 * current line at the left margin. For when 'cino' has "fs".
9091 */
9092 if (*skipwhite(l) == '}')
9093 break;
9094
9095 /* (matching {)
9096 * If the previous line ends on '};' (maybe followed by
9097 * comments) align at column 0. For example:
9098 * char *string_array[] = { "foo",
9099 * / * x * / "b};ar" }; / * foobar * /
9100 */
9101 if (cin_ends_in(l, (char_u *)"};", NULL))
9102 break;
9103
9104 /*
9105 * If the previous line ends on '[' we are probably in an
9106 * array constant:
9107 * something = [
9108 * 234, <- extra indent
9109 */
9110 if (cin_ends_in(l, (char_u *)"[", NULL))
9111 {
9112 amount = get_indent() + ind_continuation;
9113 break;
9114 }
9115
9116 /*
9117 * Find a line only has a semicolon that belongs to a previous
9118 * line ending in '}', e.g. before an #endif. Don't increase
9119 * indent then.
9120 */
9121 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
9122 {
9123 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124
9125 while (curwin->w_cursor.lnum > 1)
9126 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009127 look = ml_get(--curwin->w_cursor.lnum);
9128 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009129 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009131 }
9132 if (curwin->w_cursor.lnum > 0
9133 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009136 curwin->w_cursor = curpos_save;
9137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009139 /*
9140 * If the PREVIOUS line is a function declaration, the current
9141 * line (and the ones that follow) needs to be indented as
9142 * parameters.
9143 */
9144 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
9145 {
9146 amount = curbuf->b_ind_param;
9147 break;
9148 }
9149
9150 /*
9151 * If the previous line ends in ';' and the line before the
9152 * previous line ends in ',' or '\', ident to column zero:
9153 * int foo,
9154 * bar;
9155 * indent_to_0 here;
9156 */
9157 if (cin_ends_in(l, (char_u *)";", NULL))
9158 {
9159 l = ml_get(curwin->w_cursor.lnum - 1);
9160 if (cin_ends_in(l, (char_u *)",", NULL)
9161 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9162 break;
9163 l = ml_get_curline();
9164 }
9165
9166 /*
9167 * Doesn't look like anything interesting -- so just
9168 * use the indent of this line.
9169 *
9170 * Position the cursor over the rightmost paren, so that
9171 * matching it will take us back to the start of the line.
9172 */
9173 find_last_paren(l, '(', ')');
9174
9175 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9176 curwin->w_cursor = *trypos;
9177 amount = get_indent(); /* XXX */
9178 break;
9179 }
9180
9181 /* add extra indent for a comment */
9182 if (cin_iscomment(theline))
9183 amount += curbuf->b_ind_comment;
9184
9185 /* add extra indent if the previous line ended in a backslash:
9186 * "asdfasdf\
9187 * here";
9188 * char *foo = "asdf\
9189 * here";
9190 */
9191 if (cur_curpos.lnum > 1)
9192 {
9193 l = ml_get(cur_curpos.lnum - 1);
9194 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9195 {
9196 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9197 if (cur_amount > 0)
9198 amount = cur_amount;
9199 else if (cur_amount == 0)
9200 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 }
9202 }
9203
9204theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009205 if (amount < 0)
9206 amount = 0;
9207
9208laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 /* put the cursor back where it belongs */
9210 curwin->w_cursor = cur_curpos;
9211
9212 vim_free(linecopy);
9213
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 return amount;
9215}
9216
9217 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009218find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219{
9220 char_u *look;
9221 pos_T *theirscope;
9222 char_u *mightbeif;
9223 int elselevel;
9224 int whilelevel;
9225
9226 if (lookfor == LOOKFOR_IF)
9227 {
9228 elselevel = 1;
9229 whilelevel = 0;
9230 }
9231 else
9232 {
9233 elselevel = 0;
9234 whilelevel = 1;
9235 }
9236
9237 curwin->w_cursor.col = 0;
9238
9239 while (curwin->w_cursor.lnum > ourscope + 1)
9240 {
9241 curwin->w_cursor.lnum--;
9242 curwin->w_cursor.col = 0;
9243
9244 look = cin_skipcomment(ml_get_curline());
9245 if (cin_iselse(look)
9246 || cin_isif(look)
9247 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009248 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 {
9250 /*
9251 * if we've gone outside the braces entirely,
9252 * we must be out of scope...
9253 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009254 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 if (theirscope == NULL)
9256 break;
9257
9258 /*
9259 * and if the brace enclosing this is further
9260 * back than the one enclosing the else, we're
9261 * out of luck too.
9262 */
9263 if (theirscope->lnum < ourscope)
9264 break;
9265
9266 /*
9267 * and if they're enclosed in a *deeper* brace,
9268 * then we can ignore it because it's in a
9269 * different scope...
9270 */
9271 if (theirscope->lnum > ourscope)
9272 continue;
9273
9274 /*
9275 * if it was an "else" (that's not an "else if")
9276 * then we need to go back to another if, so
9277 * increment elselevel
9278 */
9279 look = cin_skipcomment(ml_get_curline());
9280 if (cin_iselse(look))
9281 {
9282 mightbeif = cin_skipcomment(look + 4);
9283 if (!cin_isif(mightbeif))
9284 ++elselevel;
9285 continue;
9286 }
9287
9288 /*
9289 * if it was a "while" then we need to go back to
9290 * another "do", so increment whilelevel. XXX
9291 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009292 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293 {
9294 ++whilelevel;
9295 continue;
9296 }
9297
9298 /* If it's an "if" decrement elselevel */
9299 look = cin_skipcomment(ml_get_curline());
9300 if (cin_isif(look))
9301 {
9302 elselevel--;
9303 /*
9304 * When looking for an "if" ignore "while"s that
9305 * get in the way.
9306 */
9307 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9308 whilelevel = 0;
9309 }
9310
9311 /* If it's a "do" decrement whilelevel */
9312 if (cin_isdo(look))
9313 whilelevel--;
9314
9315 /*
9316 * if we've used up all the elses, then
9317 * this must be the if that we want!
9318 * match the indent level of that if.
9319 */
9320 if (elselevel <= 0 && whilelevel <= 0)
9321 {
9322 return OK;
9323 }
9324 }
9325 }
9326 return FAIL;
9327}
9328
9329# if defined(FEAT_EVAL) || defined(PROTO)
9330/*
9331 * Get indent level from 'indentexpr'.
9332 */
9333 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009334get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335{
Bram Moolenaar97db5542017-04-21 23:18:26 +02009336 int indent = -1;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009337 char_u *inde_copy;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009338 pos_T save_pos;
9339 colnr_T save_curswant;
9340 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009342 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9343 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009345 /* Save and restore cursor position and curswant, in case it was changed
9346 * via :normal commands */
9347 save_pos = curwin->w_cursor;
9348 save_curswant = curwin->w_curswant;
9349 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009351 if (use_sandbox)
9352 ++sandbox;
9353 ++textlock;
Bram Moolenaara701b3b2017-04-20 22:57:27 +02009354
9355 /* Need to make a copy, the 'indentexpr' option could be changed while
9356 * evaluating it. */
9357 inde_copy = vim_strsave(curbuf->b_p_inde);
9358 if (inde_copy != NULL)
9359 {
9360 indent = (int)eval_to_number(inde_copy);
9361 vim_free(inde_copy);
9362 }
9363
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009364 if (use_sandbox)
9365 --sandbox;
9366 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367
9368 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9369 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9370 * command. */
9371 save_State = State;
9372 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009373 curwin->w_cursor = save_pos;
9374 curwin->w_curswant = save_curswant;
9375 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 check_cursor();
9377 State = save_State;
9378
9379 /* If there is an error, just keep the current indent. */
9380 if (indent < 0)
9381 indent = get_indent();
9382
9383 return indent;
9384}
9385# endif
9386
9387#endif /* FEAT_CINDENT */
9388
9389#if defined(FEAT_LISP) || defined(PROTO)
9390
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009391static int lisp_match(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392
9393 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009394lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395{
9396 char_u buf[LSIZE];
9397 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009398 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399
9400 while (*word != NUL)
9401 {
9402 (void)copy_option_part(&word, buf, LSIZE, ",");
9403 len = (int)STRLEN(buf);
9404 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9405 return TRUE;
9406 }
9407 return FALSE;
9408}
9409
9410/*
9411 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9412 * The incompatible newer method is quite a bit better at indenting
9413 * code in lisp-like languages than the traditional one; it's still
9414 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9415 *
9416 * TODO:
9417 * Findmatch() should be adapted for lisp, also to make showmatch
9418 * work correctly: now (v5.3) it seems all C/C++ oriented:
9419 * - it does not recognize the #\( and #\) notations as character literals
9420 * - it doesn't know about comments starting with a semicolon
9421 * - it incorrectly interprets '(' as a character literal
9422 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009423 * Update from Sergey Khorev:
9424 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 */
9426 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009427get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009429 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 int amount;
9431 char_u *that;
9432 colnr_T col;
9433 colnr_T firsttry;
9434 int parencount, quotecount;
9435 int vi_lisp;
9436
9437 /* Set vi_lisp to use the vi-compatible method */
9438 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9439
9440 realpos = curwin->w_cursor;
9441 curwin->w_cursor.col = 0;
9442
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009443 if ((pos = findmatch(NULL, '(')) == NULL)
9444 pos = findmatch(NULL, '[');
9445 else
9446 {
9447 paren = *pos;
9448 pos = findmatch(NULL, '[');
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009449 if (pos == NULL || LT_POSP(pos, &paren))
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009450 pos = &paren;
9451 }
9452 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 {
9454 /* Extra trick: Take the indent of the first previous non-white
9455 * line that is at the same () level. */
9456 amount = -1;
9457 parencount = 0;
9458
9459 while (--curwin->w_cursor.lnum >= pos->lnum)
9460 {
9461 if (linewhite(curwin->w_cursor.lnum))
9462 continue;
9463 for (that = ml_get_curline(); *that != NUL; ++that)
9464 {
9465 if (*that == ';')
9466 {
9467 while (*(that + 1) != NUL)
9468 ++that;
9469 continue;
9470 }
9471 if (*that == '\\')
9472 {
9473 if (*(that + 1) != NUL)
9474 ++that;
9475 continue;
9476 }
9477 if (*that == '"' && *(that + 1) != NUL)
9478 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009479 while (*++that && *that != '"')
9480 {
9481 /* skipping escaped characters in the string */
9482 if (*that == '\\')
9483 {
9484 if (*++that == NUL)
9485 break;
9486 if (that[1] == NUL)
9487 {
9488 ++that;
9489 break;
9490 }
9491 }
9492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009494 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009496 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 --parencount;
9498 }
9499 if (parencount == 0)
9500 {
9501 amount = get_indent();
9502 break;
9503 }
9504 }
9505
9506 if (amount == -1)
9507 {
9508 curwin->w_cursor.lnum = pos->lnum;
9509 curwin->w_cursor.col = pos->col;
9510 col = pos->col;
9511
9512 that = ml_get_curline();
9513
9514 if (vi_lisp && get_indent() == 0)
9515 amount = 2;
9516 else
9517 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009518 char_u *line = that;
9519
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 amount = 0;
9521 while (*that && col)
9522 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009523 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 col--;
9525 }
9526
9527 /*
9528 * Some keywords require "body" indenting rules (the
9529 * non-standard-lisp ones are Scheme special forms):
9530 *
9531 * (let ((a 1)) instead (let ((a 1))
9532 * (...)) of (...))
9533 */
9534
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009535 if (!vi_lisp && (*that == '(' || *that == '[')
9536 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537 amount += 2;
9538 else
9539 {
9540 that++;
9541 amount++;
9542 firsttry = amount;
9543
Bram Moolenaar1c465442017-03-12 20:10:05 +01009544 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009546 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 ++that;
9548 }
9549
9550 if (*that && *that != ';') /* not a comment line */
9551 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009552 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009554 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 firsttry++;
9556
9557 parencount = 0;
9558 quotecount = 0;
9559
9560 if (vi_lisp
9561 || (*that != '"'
9562 && *that != '\''
9563 && *that != '#'
9564 && (*that < '0' || *that > '9')))
9565 {
9566 while (*that
Bram Moolenaar1c465442017-03-12 20:10:05 +01009567 && (!VIM_ISWHITE(*that)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568 || quotecount
9569 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009570 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 && !quotecount
9572 && !parencount
9573 && vi_lisp)))
9574 {
9575 if (*that == '"')
9576 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009577 if ((*that == '(' || *that == '[')
9578 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009580 if ((*that == ')' || *that == ']')
9581 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 --parencount;
9583 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009584 amount += lbr_chartabsize_adv(
9585 line, &that, (colnr_T)amount);
9586 amount += lbr_chartabsize_adv(
9587 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 }
9589 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01009590 while (VIM_ISWHITE(*that))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009592 amount += lbr_chartabsize(
9593 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 that++;
9595 }
9596 if (!*that || *that == ';')
9597 amount = firsttry;
9598 }
9599 }
9600 }
9601 }
9602 }
9603 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009604 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605
9606 curwin->w_cursor = realpos;
9607
9608 return amount;
9609}
9610#endif /* FEAT_LISP */
9611
9612 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009613prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009615#if defined(SIGHUP) && defined(SIG_IGN)
9616 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9617 * makes Vim exit and then handling SIGHUP causes various reentrance
9618 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009619 signal(SIGHUP, SIG_IGN);
9620#endif
9621
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622#ifdef FEAT_GUI
9623 if (gui.in_use)
9624 {
9625 gui.dying = TRUE;
9626 out_trash(); /* trash any pending output */
9627 }
9628 else
9629#endif
9630 {
9631 windgoto((int)Rows - 1, 0);
9632
9633 /*
9634 * Switch terminal mode back now, so messages end up on the "normal"
9635 * screen (if there are two screens).
9636 */
9637 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009638 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 out_flush();
9640 }
9641}
9642
9643/*
9644 * Preserve files and exit.
9645 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009646 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9647 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 */
9649 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009650preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651{
9652 buf_T *buf;
9653
9654 prepare_to_exit();
9655
Bram Moolenaar4770d092006-01-12 23:22:24 +00009656 /* Setting this will prevent free() calls. That avoids calling free()
9657 * recursively when free() was invoked with a bad pointer. */
9658 really_exiting = TRUE;
9659
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660 out_str(IObuff);
9661 screen_start(); /* don't know where cursor is now */
9662 out_flush();
9663
9664 ml_close_notmod(); /* close all not-modified buffers */
9665
Bram Moolenaar29323592016-07-24 22:04:11 +02009666 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 {
9668 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9669 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009670 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 screen_start(); /* don't know where cursor is now */
9672 out_flush();
9673 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9674 break;
9675 }
9676 }
9677
9678 ml_close_all(FALSE); /* close all memfiles, without deleting */
9679
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009680 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681
9682 getout(1);
9683}
9684
9685/*
9686 * return TRUE if "fname" exists.
9687 */
9688 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009689vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009691 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692
9693 if (mch_stat((char *)fname, &st))
9694 return FALSE;
9695 return TRUE;
9696}
9697
9698/*
9699 * Check for CTRL-C pressed, but only once in a while.
9700 * Should be used instead of ui_breakcheck() for functions that check for
9701 * each line in the file. Calling ui_breakcheck() each time takes too much
9702 * time, because it can be a system call.
9703 */
9704
9705#ifndef BREAKCHECK_SKIP
9706# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9707# define BREAKCHECK_SKIP 200
9708# else
9709# define BREAKCHECK_SKIP 32
9710# endif
9711#endif
9712
9713static int breakcheck_count = 0;
9714
9715 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009716line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717{
9718 if (++breakcheck_count >= BREAKCHECK_SKIP)
9719 {
9720 breakcheck_count = 0;
9721 ui_breakcheck();
9722 }
9723}
9724
9725/*
9726 * Like line_breakcheck() but check 10 times less often.
9727 */
9728 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009729fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730{
9731 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9732 {
9733 breakcheck_count = 0;
9734 ui_breakcheck();
9735 }
9736}
9737
9738/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009739 * Invoke expand_wildcards() for one pattern.
9740 * Expand items like "%:h" before the expansion.
9741 * Returns OK or FAIL.
9742 */
9743 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009744expand_wildcards_eval(
9745 char_u **pat, /* pointer to input pattern */
9746 int *num_file, /* resulting number of files */
9747 char_u ***file, /* array of resulting files */
9748 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009749{
9750 int ret = FAIL;
9751 char_u *eval_pat = NULL;
9752 char_u *exp_pat = *pat;
9753 char_u *ignored_msg;
9754 int usedlen;
9755
9756 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9757 {
9758 ++emsg_off;
9759 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9760 NULL, &ignored_msg, NULL);
9761 --emsg_off;
9762 if (eval_pat != NULL)
9763 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9764 }
9765
9766 if (exp_pat != NULL)
9767 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9768
9769 if (eval_pat != NULL)
9770 {
9771 vim_free(exp_pat);
9772 vim_free(eval_pat);
9773 }
9774
9775 return ret;
9776}
9777
9778/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9780 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009781 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 */
9783 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009784expand_wildcards(
9785 int num_pat, /* number of input patterns */
9786 char_u **pat, /* array of input patterns */
9787 int *num_files, /* resulting number of files */
9788 char_u ***files, /* array of resulting files */
9789 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790{
9791 int retval;
9792 int i, j;
9793 char_u *p;
9794 int non_suf_match; /* number without matching suffix */
9795
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009796 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797
9798 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009799 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800 return retval;
9801
9802#ifdef FEAT_WILDIGN
9803 /*
9804 * Remove names that match 'wildignore'.
9805 */
9806 if (*p_wig)
9807 {
9808 char_u *ffname;
9809
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009810 /* check all files in (*files)[] */
9811 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009813 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814 if (ffname == NULL) /* out of memory */
9815 break;
9816# ifdef VMS
9817 vms_remove_version(ffname);
9818# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009819 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +01009821 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009822 vim_free((*files)[i]);
9823 for (j = i; j + 1 < *num_files; ++j)
9824 (*files)[j] = (*files)[j + 1];
9825 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 --i;
9827 }
9828 vim_free(ffname);
9829 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009830
9831 /* If the number of matches is now zero, we fail. */
9832 if (*num_files == 0)
9833 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01009834 VIM_CLEAR(*files);
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009835 return FAIL;
9836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837 }
9838#endif
9839
9840 /*
9841 * Move the names where 'suffixes' match to the end.
9842 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009843 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 {
9845 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009846 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009848 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 {
9850 /*
9851 * Move the name without matching suffix to the front
9852 * of the list.
9853 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009854 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009856 (*files)[j] = (*files)[j - 1];
9857 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 }
9859 }
9860 }
9861
9862 return retval;
9863}
9864
9865/*
9866 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9867 */
9868 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009869match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870{
9871 int fnamelen, setsuflen;
9872 char_u *setsuf;
9873#define MAXSUFLEN 30 /* maximum length of a file suffix */
9874 char_u suf_buf[MAXSUFLEN];
9875
9876 fnamelen = (int)STRLEN(fname);
9877 setsuflen = 0;
9878 for (setsuf = p_su; *setsuf; )
9879 {
9880 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009881 if (setsuflen == 0)
9882 {
9883 char_u *tail = gettail(fname);
9884
9885 /* empty entry: match name without a '.' */
9886 if (vim_strchr(tail, '.') == NULL)
9887 {
9888 setsuflen = 1;
9889 break;
9890 }
9891 }
9892 else
9893 {
9894 if (fnamelen >= setsuflen
9895 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9896 (size_t)setsuflen) == 0)
9897 break;
9898 setsuflen = 0;
9899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900 }
9901 return (setsuflen != 0);
9902}
9903
9904#if !defined(NO_EXPANDPATH) || defined(PROTO)
9905
9906# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009907static int vim_backtick(char_u *p);
9908static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909# endif
9910
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009911# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912/*
9913 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9914 * it's shared between these systems.
9915 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009916# if defined(PROTO)
9917# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918# else
9919# ifdef __BORLANDC__
9920# define _cdecl _RTLENTRYF
9921# endif
9922# endif
9923
9924/*
9925 * comparison function for qsort in dos_expandpath()
9926 */
9927 static int _cdecl
9928pstrcmp(const void *a, const void *b)
9929{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009930 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931}
9932
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009934 * Recursively expand one path component into all matching files and/or
9935 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 * Return the number of matches found.
9937 * "path" has backslashes before chars that are not to be expanded, starting
9938 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009939 * Return the number of matches found.
9940 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 */
9942 static int
9943dos_expandpath(
9944 garray_T *gap,
9945 char_u *path,
9946 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009947 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009948 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009950 char_u *buf;
9951 char_u *path_end;
9952 char_u *p, *s, *e;
9953 int start_len = gap->ga_len;
9954 char_u *pat;
9955 regmatch_T regmatch;
9956 int starts_with_dot;
9957 int matches;
9958 int len;
9959 int starstar = FALSE;
9960 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009961 WIN32_FIND_DATA fb;
9962 HANDLE hFind = (HANDLE)0;
9963# ifdef FEAT_MBYTE
9964 WIN32_FIND_DATAW wfb;
9965 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9966# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009968 int ok;
9969
9970 /* Expanding "**" may take a long time, check for CTRL-C. */
9971 if (stardepth > 0)
9972 {
9973 ui_breakcheck();
9974 if (got_int)
9975 return 0;
9976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977
Bram Moolenaar7314efd2015-10-31 15:32:52 +01009978 /* Make room for file name. When doing encoding conversion the actual
9979 * length may be quite a bit longer, thus use the maximum possible length. */
9980 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981 if (buf == NULL)
9982 return 0;
9983
9984 /*
9985 * Find the first part in the path name that contains a wildcard or a ~1.
9986 * Copy it into buf, including the preceding characters.
9987 */
9988 p = buf;
9989 s = buf;
9990 e = NULL;
9991 path_end = path;
9992 while (*path_end != NUL)
9993 {
9994 /* May ignore a wildcard that has a backslash before it; it will
9995 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9996 if (path_end >= path + wildoff && rem_backslash(path_end))
9997 *p++ = *path_end++;
9998 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9999 {
10000 if (e != NULL)
10001 break;
10002 s = p + 1;
10003 }
10004 else if (path_end >= path + wildoff
10005 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
10006 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +010010007# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 if (has_mbyte)
10009 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010010 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011 STRNCPY(p, path_end, len);
10012 p += len;
10013 path_end += len;
10014 }
10015 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +010010016# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 *p++ = *path_end++;
10018 }
10019 e = p;
10020 *e = NUL;
10021
10022 /* now we have one wildcard component between s and e */
10023 /* Remove backslashes between "wildoff" and the start of the wildcard
10024 * component. */
10025 for (p = buf + wildoff; p < s; ++p)
10026 if (rem_backslash(p))
10027 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010028 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 --e;
10030 --s;
10031 }
10032
Bram Moolenaar231334e2005-07-25 20:46:57 +000010033 /* Check for "**" between "s" and "e". */
10034 for (p = s; p < e; ++p)
10035 if (p[0] == '*' && p[1] == '*')
10036 starstar = TRUE;
10037
Bram Moolenaard82103e2016-01-17 17:04:05 +010010038 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10040 if (pat == NULL)
10041 {
10042 vim_free(buf);
10043 return 0;
10044 }
10045
10046 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010047 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010048 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 regmatch.rm_ic = TRUE; /* Always ignore case */
10050 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010051 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010052 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 vim_free(pat);
10054
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010055 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056 {
10057 vim_free(buf);
10058 return 0;
10059 }
10060
10061 /* remember the pattern or file name being looked for */
10062 matchname = vim_strsave(s);
10063
Bram Moolenaar231334e2005-07-25 20:46:57 +000010064 /* If "**" is by itself, this is the first time we encounter it and more
10065 * is following then find matches without any directory. */
10066 if (!didstar && stardepth < 100 && starstar && e - s == 2
10067 && *path_end == '/')
10068 {
10069 STRCPY(s, path_end + 1);
10070 ++stardepth;
10071 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10072 --stardepth;
10073 }
10074
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075 /* Scan all files in the directory with "dir/ *.*" */
10076 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077# ifdef FEAT_MBYTE
10078 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
10079 {
10080 /* The active codepage differs from 'encoding'. Attempt using the
10081 * wide function. If it fails because it is not implemented fall back
10082 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010083 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 if (wn != NULL)
10085 {
10086 hFind = FindFirstFileW(wn, &wfb);
10087 if (hFind == INVALID_HANDLE_VALUE
10088 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
Bram Moolenaard23a8232018-02-10 18:45:26 +010010089 VIM_CLEAR(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 }
10091 }
10092
10093 if (wn == NULL)
10094# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010095 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097
10098 while (ok)
10099 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100# ifdef FEAT_MBYTE
10101 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010102 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 else
10104# endif
10105 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 /* Ignore entries starting with a dot, unless when asked for. Accept
10107 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010108 if ((p[0] != '.' || starts_with_dot
10109 || ((flags & EW_DODOT)
10110 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010112 || (regmatch.regprog != NULL
10113 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010114 || ((flags & EW_NOTWILD)
10115 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010119
10120 if (starstar && stardepth < 100)
10121 {
10122 /* For "**" in the pattern first go deeper in the tree to
10123 * find matches. */
10124 STRCPY(buf + len, "/**");
10125 STRCPY(buf + len + 3, path_end);
10126 ++stardepth;
10127 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
10128 --stardepth;
10129 }
10130
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131 STRCPY(buf + len, path_end);
10132 if (mch_has_exp_wildcard(path_end))
10133 {
10134 /* need to expand another component of the path */
10135 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010136 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137 }
10138 else
10139 {
10140 /* no more wildcards, check if there is a match */
10141 /* remove backslashes for the remaining components only */
10142 if (*path_end != 0)
10143 backslash_halve(buf + len + 1);
10144 if (mch_getperm(buf) >= 0) /* add existing file */
10145 addfile(gap, buf, flags);
10146 }
10147 }
10148
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149# ifdef FEAT_MBYTE
10150 if (wn != NULL)
10151 {
10152 vim_free(p);
10153 ok = FindNextFileW(hFind, &wfb);
10154 }
10155 else
10156# endif
10157 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158
10159 /* If no more matches and no match was used, try expanding the name
10160 * itself. Finds the long name of a short filename. */
10161 if (!ok && matchname != NULL && gap->ga_len == start_len)
10162 {
10163 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164 FindClose(hFind);
10165# ifdef FEAT_MBYTE
10166 if (wn != NULL)
10167 {
10168 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010169 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 if (wn != NULL)
10171 hFind = FindFirstFileW(wn, &wfb);
10172 }
10173 if (wn == NULL)
10174# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010175 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaard23a8232018-02-10 18:45:26 +010010177 VIM_CLEAR(matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178 }
10179 }
10180
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 FindClose(hFind);
10182# ifdef FEAT_MBYTE
10183 vim_free(wn);
10184# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010186 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187 vim_free(matchname);
10188
10189 matches = gap->ga_len - start_len;
10190 if (matches > 0)
10191 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10192 sizeof(char_u *), pstrcmp);
10193 return matches;
10194}
10195
10196 int
10197mch_expandpath(
10198 garray_T *gap,
10199 char_u *path,
10200 int flags) /* EW_* flags */
10201{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010202 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010204# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205
Bram Moolenaar231334e2005-07-25 20:46:57 +000010206#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10207 || defined(PROTO)
10208/*
10209 * Unix style wildcard expansion code.
10210 * It's here because it's used both for Unix and Mac.
10211 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010212static int pstrcmp(const void *, const void *);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010213
10214 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010215pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010216{
10217 return (pathcmp(*(char **)a, *(char **)b, -1));
10218}
10219
10220/*
10221 * Recursively expand one path component into all matching files and/or
10222 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10223 * "path" has backslashes before chars that are not to be expanded, starting
10224 * at "path + wildoff".
10225 * Return the number of matches found.
10226 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10227 */
10228 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010229unix_expandpath(
10230 garray_T *gap,
10231 char_u *path,
10232 int wildoff,
10233 int flags, /* EW_* flags */
10234 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010235{
10236 char_u *buf;
10237 char_u *path_end;
10238 char_u *p, *s, *e;
10239 int start_len = gap->ga_len;
10240 char_u *pat;
10241 regmatch_T regmatch;
10242 int starts_with_dot;
10243 int matches;
10244 int len;
10245 int starstar = FALSE;
10246 static int stardepth = 0; /* depth for "**" expansion */
10247
10248 DIR *dirp;
10249 struct dirent *dp;
10250
10251 /* Expanding "**" may take a long time, check for CTRL-C. */
10252 if (stardepth > 0)
10253 {
10254 ui_breakcheck();
10255 if (got_int)
10256 return 0;
10257 }
10258
10259 /* make room for file name */
10260 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10261 if (buf == NULL)
10262 return 0;
10263
10264 /*
10265 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010266 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010267 * Copy it into "buf", including the preceding characters.
10268 */
10269 p = buf;
10270 s = buf;
10271 e = NULL;
10272 path_end = path;
10273 while (*path_end != NUL)
10274 {
10275 /* May ignore a wildcard that has a backslash before it; it will
10276 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10277 if (path_end >= path + wildoff && rem_backslash(path_end))
10278 *p++ = *path_end++;
10279 else if (*path_end == '/')
10280 {
10281 if (e != NULL)
10282 break;
10283 s = p + 1;
10284 }
10285 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010286 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010287 || (!p_fic && (flags & EW_ICASE)
10288 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010289 e = p;
10290#ifdef FEAT_MBYTE
10291 if (has_mbyte)
10292 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010293 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010294 STRNCPY(p, path_end, len);
10295 p += len;
10296 path_end += len;
10297 }
10298 else
10299#endif
10300 *p++ = *path_end++;
10301 }
10302 e = p;
10303 *e = NUL;
10304
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010305 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010306 /* Remove backslashes between "wildoff" and the start of the wildcard
10307 * component. */
10308 for (p = buf + wildoff; p < s; ++p)
10309 if (rem_backslash(p))
10310 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010311 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010312 --e;
10313 --s;
10314 }
10315
10316 /* Check for "**" between "s" and "e". */
10317 for (p = s; p < e; ++p)
10318 if (p[0] == '*' && p[1] == '*')
10319 starstar = TRUE;
10320
10321 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010322 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010323 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10324 if (pat == NULL)
10325 {
10326 vim_free(buf);
10327 return 0;
10328 }
10329
10330 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010331 if (flags & EW_ICASE)
10332 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10333 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010334 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010335 if (flags & (EW_NOERROR | EW_NOTWILD))
10336 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010337 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010338 if (flags & (EW_NOERROR | EW_NOTWILD))
10339 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010340 vim_free(pat);
10341
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010342 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010343 {
10344 vim_free(buf);
10345 return 0;
10346 }
10347
10348 /* If "**" is by itself, this is the first time we encounter it and more
10349 * is following then find matches without any directory. */
10350 if (!didstar && stardepth < 100 && starstar && e - s == 2
10351 && *path_end == '/')
10352 {
10353 STRCPY(s, path_end + 1);
10354 ++stardepth;
10355 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10356 --stardepth;
10357 }
10358
10359 /* open the directory for scanning */
10360 *s = NUL;
10361 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10362
10363 /* Find all matching entries */
10364 if (dirp != NULL)
10365 {
10366 for (;;)
10367 {
10368 dp = readdir(dirp);
10369 if (dp == NULL)
10370 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010371 if ((dp->d_name[0] != '.' || starts_with_dot
10372 || ((flags & EW_DODOT)
10373 && dp->d_name[1] != NUL
10374 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010375 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10376 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010377 || ((flags & EW_NOTWILD)
10378 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010379 {
10380 STRCPY(s, dp->d_name);
10381 len = STRLEN(buf);
10382
10383 if (starstar && stardepth < 100)
10384 {
10385 /* For "**" in the pattern first go deeper in the tree to
10386 * find matches. */
10387 STRCPY(buf + len, "/**");
10388 STRCPY(buf + len + 3, path_end);
10389 ++stardepth;
10390 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10391 --stardepth;
10392 }
10393
10394 STRCPY(buf + len, path_end);
10395 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10396 {
10397 /* need to expand another component of the path */
10398 /* remove backslashes for the remaining components only */
10399 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10400 }
10401 else
10402 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010403 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010404
Bram Moolenaar231334e2005-07-25 20:46:57 +000010405 /* no more wildcards, check if there is a match */
10406 /* remove backslashes for the remaining components only */
10407 if (*path_end != NUL)
10408 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010409 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010410 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010411 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010412 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010413#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010414 size_t precomp_len = STRLEN(buf)+1;
10415 char_u *precomp_buf =
10416 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010417
Bram Moolenaar231334e2005-07-25 20:46:57 +000010418 if (precomp_buf)
10419 {
10420 mch_memmove(buf, precomp_buf, precomp_len);
10421 vim_free(precomp_buf);
10422 }
10423#endif
10424 addfile(gap, buf, flags);
10425 }
10426 }
10427 }
10428 }
10429
10430 closedir(dirp);
10431 }
10432
10433 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010434 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010435
10436 matches = gap->ga_len - start_len;
10437 if (matches > 0)
10438 qsort(((char_u **)gap->ga_data) + start_len, matches,
10439 sizeof(char_u *), pstrcmp);
10440 return matches;
10441}
10442#endif
10443
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010444#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010445static int find_previous_pathsep(char_u *path, char_u **psep);
10446static int is_unique(char_u *maybe_unique, garray_T *gap, int i);
10447static void expand_path_option(char_u *curdir, garray_T *gap);
10448static char_u *get_path_cutoff(char_u *fname, garray_T *gap);
10449static void uniquefy_paths(garray_T *gap, char_u *pattern);
10450static int expand_in_path(garray_T *gap, char_u *pattern, int flags);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010451
10452/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010453 * Moves "*psep" back to the previous path separator in "path".
10454 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010455 */
10456 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010457find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010458{
10459 /* skip the current separator */
10460 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010461 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010462
10463 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010464 while (*psep > path)
10465 {
10466 if (vim_ispathsep(**psep))
10467 return OK;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010468 MB_PTR_BACK(path, *psep);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010469 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010470
10471 return FAIL;
10472}
10473
10474/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010475 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10476 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010477 */
10478 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010479is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010480{
10481 int j;
10482 int candidate_len;
10483 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010484 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010485 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010486
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010487 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010488 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010489 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010490 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010491
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010492 candidate_len = (int)STRLEN(maybe_unique);
10493 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010494 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010495 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010496
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010497 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010498 if (fnamecmp(maybe_unique, rival) == 0
10499 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010500 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010501 }
10502
Bram Moolenaar162bd912010-07-28 22:29:10 +020010503 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010504}
10505
10506/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010507 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010508 * paths are expanded to their equivalent fullpath. This includes the "."
10509 * (relative to current buffer directory) and empty path (relative to current
10510 * directory) notations.
10511 *
10512 * TODO: handle upward search (;) and path limiter (**N) notations by
10513 * expanding each into their equivalent path(s).
10514 */
10515 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010516expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010517{
10518 char_u *path_option = *curbuf->b_p_path == NUL
10519 ? p_path : curbuf->b_p_path;
10520 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010521 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010522 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010523
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010524 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010525 return;
10526
10527 while (*path_option != NUL)
10528 {
10529 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10530
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010531 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010532 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010533 /* Relative to current buffer:
10534 * "/path/file" + "." -> "/path/"
10535 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010536 if (curbuf->b_ffname == NULL)
10537 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010538 p = gettail(curbuf->b_ffname);
10539 len = (int)(p - curbuf->b_ffname);
10540 if (len + (int)STRLEN(buf) >= MAXPATHL)
10541 continue;
10542 if (buf[1] == NUL)
10543 buf[len] = NUL;
10544 else
10545 STRMOVE(buf + len, buf + 2);
10546 mch_memmove(buf, curbuf->b_ffname, len);
10547 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010548 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010549 else if (buf[0] == NUL)
10550 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010551 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010552 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010553 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010554 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010555 else if (!mch_isFullName(buf))
10556 {
10557 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010558 len = (int)STRLEN(curdir);
10559 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010560 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010561 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010562 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010563 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010564 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010565 }
10566
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010567 if (ga_grow(gap, 1) == FAIL)
10568 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010569
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010570# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010571 /* Avoid the path ending in a backslash, it fails when a comma is
10572 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010573 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010574 if (buf[len - 1] == '\\')
10575 buf[len - 1] = '/';
10576# endif
10577
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010578 p = vim_strsave(buf);
10579 if (p == NULL)
10580 break;
10581 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010582 }
10583
10584 vim_free(buf);
10585}
10586
10587/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010588 * Returns a pointer to the file or directory name in "fname" that matches the
10589 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010590 *
10591 * path: /foo/bar/baz
10592 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010593 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010594 */
10595 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010596get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010597{
10598 int i;
10599 int maxlen = 0;
10600 char_u **path_part = (char_u **)gap->ga_data;
10601 char_u *cutoff = NULL;
10602
10603 for (i = 0; i < gap->ga_len; i++)
10604 {
10605 int j = 0;
10606
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010607 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010608# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010609 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10610#endif
10611 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010612 j++;
10613 if (j > maxlen)
10614 {
10615 maxlen = j;
10616 cutoff = &fname[j];
10617 }
10618 }
10619
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010620 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010621 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010622 while (vim_ispathsep(*cutoff))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010623 MB_PTR_ADV(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010624
10625 return cutoff;
10626}
10627
10628/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010629 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10630 * that they are unique with respect to each other while conserving the part
10631 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010632 */
10633 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010634uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010635{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010636 int i;
10637 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010638 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010639 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010640 char_u *pat;
10641 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010642 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010643 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010644 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010645 char_u **in_curdir = NULL;
10646 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010647
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010648 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010649 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010650
10651 /*
10652 * We need to prepend a '*' at the beginning of file_pattern so that the
10653 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010654 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010655 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010656 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010657 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010658 if (file_pattern == NULL)
10659 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010660 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010661 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010662 STRCAT(file_pattern, pattern);
10663 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10664 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010665 if (pat == NULL)
10666 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010667
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010668 regmatch.rm_ic = TRUE; /* always ignore case */
10669 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10670 vim_free(pat);
10671 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010672 return;
10673
Bram Moolenaar162bd912010-07-28 22:29:10 +020010674 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010675 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010676 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010677 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010678
10679 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010680 if (in_curdir == NULL)
10681 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010682
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010683 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010684 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010685 char_u *path = fnames[i];
10686 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010687 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010688 char_u *pathsep_p;
10689 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010690
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010691 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010692 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010693 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010694 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010695 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010696
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010697 /* Shorten the filename while maintaining its uniqueness */
10698 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010699
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010700 /* Don't assume all files can be reached without path when search
10701 * pattern starts with star star slash, so only remove path_cutoff
10702 * when possible. */
10703 if (pattern[0] == '*' && pattern[1] == '*'
10704 && vim_ispathsep_nocolon(pattern[2])
10705 && path_cutoff != NULL
10706 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10707 && is_unique(path_cutoff, gap, i))
10708 {
10709 sort_again = TRUE;
10710 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10711 }
10712 else
10713 {
10714 /* Here all files can be reached without path, so get shortest
10715 * unique path. We start at the end of the path. */
10716 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010717
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010718 while (find_previous_pathsep(path, &pathsep_p))
10719 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10720 && is_unique(pathsep_p + 1, gap, i)
10721 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10722 {
10723 sort_again = TRUE;
10724 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10725 break;
10726 }
10727 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010728
10729 if (mch_isFullName(path))
10730 {
10731 /*
10732 * Last resort: shorten relative to curdir if possible.
10733 * 'possible' means:
10734 * 1. It is under the current directory.
10735 * 2. The result is actually shorter than the original.
10736 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010737 * Before curdir After
10738 * /foo/bar/file.txt /foo/bar ./file.txt
10739 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10740 * /file.txt / /file.txt
10741 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010742 */
10743 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010744 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010745#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010746 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010747 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010748 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010749 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010750 && !vim_ispathsep(*short_name)
10751#endif
10752 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010753 {
10754 STRCPY(path, ".");
10755 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010756 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010757 }
10758 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010759 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010760 }
10761
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010762 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010763 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010764 {
10765 char_u *rel_path;
10766 char_u *path = in_curdir[i];
10767
10768 if (path == NULL)
10769 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010770
10771 /* If the {filename} is not unique, change it to ./{filename}.
10772 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010773 short_name = shorten_fname(path, curdir);
10774 if (short_name == NULL)
10775 short_name = path;
10776 if (is_unique(short_name, gap, i))
10777 {
10778 STRCPY(fnames[i], short_name);
10779 continue;
10780 }
10781
10782 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10783 if (rel_path == NULL)
10784 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010785 STRCPY(rel_path, ".");
10786 add_pathsep(rel_path);
10787 STRCAT(rel_path, short_name);
10788
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010789 vim_free(fnames[i]);
10790 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010791 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010792 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010793 }
10794
Bram Moolenaar162bd912010-07-28 22:29:10 +020010795theend:
10796 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010797 if (in_curdir != NULL)
10798 {
10799 for (i = 0; i < gap->ga_len; i++)
10800 vim_free(in_curdir[i]);
10801 vim_free(in_curdir);
10802 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010803 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010804 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010805
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010806 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010807 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010808}
10809
10810/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010811 * Calls globpath() with 'path' values for the given pattern and stores the
10812 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010813 * Returns the total number of matches.
10814 */
10815 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010816expand_in_path(
10817 garray_T *gap,
10818 char_u *pattern,
10819 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010820{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010821 char_u *curdir;
10822 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010823 char_u *paths = NULL;
Bram Moolenaar8a37b032018-02-03 20:43:08 +010010824 int glob_flags = 0;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010825
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010826 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010827 return 0;
10828 mch_dirname(curdir, MAXPATHL);
10829
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010830 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010831 expand_path_option(curdir, &path_ga);
10832 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010833 if (path_ga.ga_len == 0)
10834 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010835
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010836 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010837 ga_clear_strings(&path_ga);
10838 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010839 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010840
Bram Moolenaar8a37b032018-02-03 20:43:08 +010010841 if (flags & EW_ICASE)
10842 glob_flags |= WILD_ICASE;
10843 if (flags & EW_ADDSLASH)
10844 glob_flags |= WILD_ADD_SLASH;
10845 globpath(paths, pattern, gap, glob_flags);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010846 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010847
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010848 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010849}
10850#endif
10851
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010852#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10853/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010854 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10855 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010856 */
10857 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010858remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010859{
10860 int i;
10861 int j;
10862 char_u **fnames = (char_u **)gap->ga_data;
10863
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010864 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010865 for (i = gap->ga_len - 1; i > 0; --i)
10866 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10867 {
10868 vim_free(fnames[i]);
10869 for (j = i + 1; j < gap->ga_len; ++j)
10870 fnames[j - 1] = fnames[j];
10871 --gap->ga_len;
10872 }
10873}
10874#endif
10875
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010876static int has_env_var(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010877
10878/*
10879 * Return TRUE if "p" contains what looks like an environment variable.
10880 * Allowing for escaping.
10881 */
10882 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010883has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010884{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010885 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010886 {
10887 if (*p == '\\' && p[1] != NUL)
10888 ++p;
10889 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010890#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010891 "$%"
10892#else
10893 "$"
10894#endif
10895 , *p) != NULL)
10896 return TRUE;
10897 }
10898 return FALSE;
10899}
10900
10901#ifdef SPECIAL_WILDCHAR
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010902static int has_special_wildchar(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010903
10904/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010905 * Return TRUE if "p" contains a special wildcard character, one that Vim
10906 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010907 */
10908 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010909has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010910{
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010911 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010912 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010913 /* Allow for escaping. */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010914 if (*p == '\\' && p[1] != NUL)
10915 ++p;
10916 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10917 return TRUE;
10918 }
10919 return FALSE;
10920}
10921#endif
10922
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923/*
10924 * Generic wildcard expansion code.
10925 *
10926 * Characters in "pat" that should not be expanded must be preceded with a
10927 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10928 *
10929 * Return FAIL when no single file was found. In this case "num_file" is not
10930 * set, and "file" may contain an error message.
10931 * Return OK when some files found. "num_file" is set to the number of
10932 * matches, "file" to the array of matches. Call FreeWild() later.
10933 */
10934 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010935gen_expand_wildcards(
10936 int num_pat, /* number of input patterns */
10937 char_u **pat, /* array of input patterns */
10938 int *num_file, /* resulting number of files */
10939 char_u ***file, /* array of resulting files */
10940 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941{
10942 int i;
10943 garray_T ga;
10944 char_u *p;
10945 static int recursive = FALSE;
10946 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020010947 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010948#if defined(FEAT_SEARCHPATH)
10949 int did_expand_in_path = FALSE;
10950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951
10952 /*
10953 * expand_env() is called to expand things like "~user". If this fails,
10954 * it calls ExpandOne(), which brings us back here. In this case, always
10955 * call the machine specific expansion function, if possible. Otherwise,
10956 * return FAIL.
10957 */
10958 if (recursive)
10959#ifdef SPECIAL_WILDCHAR
10960 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10961#else
10962 return FAIL;
10963#endif
10964
10965#ifdef SPECIAL_WILDCHAR
10966 /*
10967 * If there are any special wildcard characters which we cannot handle
10968 * here, call machine specific function for all the expansion. This
10969 * avoids starting the shell for each argument separately.
10970 * For `=expr` do use the internal function.
10971 */
10972 for (i = 0; i < num_pat; i++)
10973 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010974 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975# ifdef VIM_BACKTICK
10976 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10977# endif
10978 )
10979 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10980 }
10981#endif
10982
10983 recursive = TRUE;
10984
10985 /*
10986 * The matching file names are stored in a growarray. Init it empty.
10987 */
10988 ga_init2(&ga, (int)sizeof(char_u *), 30);
10989
10990 for (i = 0; i < num_pat; ++i)
10991 {
10992 add_pat = -1;
10993 p = pat[i];
10994
10995#ifdef VIM_BACKTICK
10996 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020010997 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020010999 if (add_pat == -1)
11000 retval = FAIL;
11001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002 else
11003#endif
11004 {
11005 /*
11006 * First expand environment variables, "~/" and "~user/".
11007 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011008 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000011010 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011 if (p == NULL)
11012 p = pat[i];
11013#ifdef UNIX
11014 /*
11015 * On Unix, if expand_env() can't expand an environment
11016 * variable, use the shell to do that. Discard previously
11017 * found file names and start all over again.
11018 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011019 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020 {
11021 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000011022 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020011024 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025 recursive = FALSE;
11026 return i;
11027 }
11028#endif
11029 }
11030
11031 /*
11032 * If there are wildcards: Expand file names and add each match to
11033 * the list. If there is no match, and EW_NOTFOUND is given, add
11034 * the pattern.
11035 * If there are no wildcards: Add the file name if it exists or
11036 * when EW_NOTFOUND is given.
11037 */
11038 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011039 {
11040#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011041 if ((flags & EW_PATH)
11042 && !mch_isFullName(p)
11043 && !(p[0] == '.'
11044 && (vim_ispathsep(p[1])
11045 || (p[1] == '.' && vim_ispathsep(p[2]))))
11046 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011047 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011048 /* :find completion where 'path' is used.
11049 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011050 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011051 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011052 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011053 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011054 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011055 else
11056#endif
11057 add_pat = mch_expandpath(&ga, p, flags);
11058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059 }
11060
11061 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
11062 {
11063 char_u *t = backslash_halve_save(p);
11064
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065 /* When EW_NOTFOUND is used, always add files and dirs. Makes
11066 * "vim c:/" work. */
11067 if (flags & EW_NOTFOUND)
11068 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020011069 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070 addfile(&ga, t, flags);
11071 vim_free(t);
11072 }
11073
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011074#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011075 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011076 uniquefy_paths(&ga, p);
11077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078 if (p != pat[i])
11079 vim_free(p);
11080 }
11081
11082 *num_file = ga.ga_len;
11083 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
11084
11085 recursive = FALSE;
11086
Bram Moolenaar336bd622016-01-17 18:23:58 +010011087 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088}
11089
11090# ifdef VIM_BACKTICK
11091
11092/*
11093 * Return TRUE if we can expand this backtick thing here.
11094 */
11095 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011096vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097{
11098 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
11099}
11100
11101/*
11102 * Expand an item in `backticks` by executing it as a command.
11103 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020011104 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105 */
11106 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011107expand_backtick(
11108 garray_T *gap,
11109 char_u *pat,
11110 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111{
11112 char_u *p;
11113 char_u *cmd;
11114 char_u *buffer;
11115 int cnt = 0;
11116 int i;
11117
11118 /* Create the command: lop off the backticks. */
11119 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
11120 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011121 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122
11123#ifdef FEAT_EVAL
11124 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011125 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011126 else
11127#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011128 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011129 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130 vim_free(cmd);
11131 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011132 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133
11134 cmd = buffer;
11135 while (*cmd != NUL)
11136 {
11137 cmd = skipwhite(cmd); /* skip over white space */
11138 p = cmd;
11139 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
11140 ++p;
11141 /* add an entry if it is not empty */
11142 if (p > cmd)
11143 {
11144 i = *p;
11145 *p = NUL;
11146 addfile(gap, cmd, flags);
11147 *p = i;
11148 ++cnt;
11149 }
11150 cmd = p;
11151 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
11152 ++cmd;
11153 }
11154
11155 vim_free(buffer);
11156 return cnt;
11157}
11158# endif /* VIM_BACKTICK */
11159
11160/*
11161 * Add a file to a file list. Accepted flags:
11162 * EW_DIR add directories
11163 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011164 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165 * EW_NOTFOUND add even when it doesn't exist
11166 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011167 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168 */
11169 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011170addfile(
11171 garray_T *gap,
11172 char_u *f, /* filename */
11173 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174{
11175 char_u *p;
11176 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011177 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011179 /* if the file/dir/link doesn't exist, may not add it */
11180 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011181 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011182 return;
11183
11184#ifdef FNAME_ILLEGAL
11185 /* if the file/dir contains illegal characters, don't add it */
11186 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11187 return;
11188#endif
11189
11190 isdir = mch_isdir(f);
11191 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11192 return;
11193
Bram Moolenaarb5971142015-03-21 17:32:19 +010011194 /* If the file isn't executable, may not add it. Do accept directories.
11195 * When invoked from expand_shellcmd() do not use $PATH. */
11196 if (!isdir && (flags & EW_EXEC)
11197 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011198 return;
11199
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200 /* Make room for another item in the file list. */
11201 if (ga_grow(gap, 1) == FAIL)
11202 return;
11203
11204 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11205 if (p == NULL)
11206 return;
11207
11208 STRCPY(p, f);
11209#ifdef BACKSLASH_IN_FILENAME
11210 slash_adjust(p);
11211#endif
11212 /*
11213 * Append a slash or backslash after directory names if none is present.
11214 */
11215#ifndef DONT_ADD_PATHSEP_TO_DIR
11216 if (isdir && (flags & EW_ADDSLASH))
11217 add_pathsep(p);
11218#endif
11219 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220}
11221#endif /* !NO_EXPANDPATH */
11222
11223#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11224
11225#ifndef SEEK_SET
11226# define SEEK_SET 0
11227#endif
11228#ifndef SEEK_END
11229# define SEEK_END 2
11230#endif
11231
11232/*
11233 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011234 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11235 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011236 * Returns an allocated string, or NULL for error.
11237 */
11238 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011239get_cmd_output(
11240 char_u *cmd,
11241 char_u *infile, /* optional input file name */
11242 int flags, /* can be SHELL_SILENT */
11243 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244{
11245 char_u *tempname;
11246 char_u *command;
11247 char_u *buffer = NULL;
11248 int len;
11249 int i = 0;
11250 FILE *fd;
11251
11252 if (check_restricted() || check_secure())
11253 return NULL;
11254
11255 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011256 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 {
11258 EMSG(_(e_notmp));
11259 return NULL;
11260 }
11261
11262 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011263 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011264 if (command == NULL)
11265 goto done;
11266
11267 /*
11268 * Call the shell to execute the command (errors are ignored).
11269 * Don't check timestamps here.
11270 */
11271 ++no_check_timestamps;
11272 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11273 --no_check_timestamps;
11274
11275 vim_free(command);
11276
11277 /*
11278 * read the names from the file into memory
11279 */
11280# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011281 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282 fd = mch_fopen((char *)tempname, "r");
11283# else
11284 fd = mch_fopen((char *)tempname, READBIN);
11285# endif
11286
11287 if (fd == NULL)
11288 {
11289 EMSG2(_(e_notopen), tempname);
11290 goto done;
11291 }
11292
11293 fseek(fd, 0L, SEEK_END);
11294 len = ftell(fd); /* get size of temp file */
11295 fseek(fd, 0L, SEEK_SET);
11296
11297 buffer = alloc(len + 1);
11298 if (buffer != NULL)
11299 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11300 fclose(fd);
11301 mch_remove(tempname);
11302 if (buffer == NULL)
11303 goto done;
11304#ifdef VMS
11305 len = i; /* VMS doesn't give us what we asked for... */
11306#endif
11307 if (i != len)
11308 {
11309 EMSG2(_(e_notread), tempname);
Bram Moolenaard23a8232018-02-10 18:45:26 +010011310 VIM_CLEAR(buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011312 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011313 {
11314 /* Change NUL into SOH, otherwise the string is truncated. */
11315 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011316 if (buffer[i] == NUL)
11317 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011318
Bram Moolenaar162bd912010-07-28 22:29:10 +020011319 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011320 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011321 else
11322 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011323
11324done:
11325 vim_free(tempname);
11326 return buffer;
11327}
11328#endif
11329
11330/*
11331 * Free the list of files returned by expand_wildcards() or other expansion
11332 * functions.
11333 */
11334 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011335FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011337 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339 while (count--)
11340 vim_free(files[count]);
11341 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342}
11343
11344/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011345 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346 * Don't do this when still processing a command or a mapping.
11347 * Don't do this when inside a ":normal" command.
11348 */
11349 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011350goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011351{
11352 return (p_im && stuff_empty() && typebuf_typed());
11353}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011354
11355/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011356 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011357 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11358 * - Remove any argument. E.g., "csh -f" -> "csh".
11359 * But don't allow a space in the path, so that this works:
11360 * "/usr/bin/csh --rcfile ~/.cshrc"
11361 * But don't do that for Windows, it's common to have a space in the path.
11362 */
11363 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011364get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011365{
11366 char_u *p;
11367
11368#ifdef WIN3264
11369 p = gettail(p_sh);
11370 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11371#else
11372 p = skiptowhite(p_sh);
11373 if (*p == NUL)
11374 {
11375 /* No white space, use the tail. */
11376 p = vim_strsave(gettail(p_sh));
11377 }
11378 else
11379 {
11380 char_u *p1, *p2;
11381
11382 /* Find the last path separator before the space. */
11383 p1 = p_sh;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010011384 for (p2 = p_sh; p2 < p; MB_PTR_ADV(p2))
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011385 if (vim_ispathsep(*p2))
11386 p1 = p2 + 1;
11387 p = vim_strnsave(p1, (int)(p - p1));
11388 }
11389#endif
11390 return p;
11391}