blob: 17779ba0b602989a08ee1381799120dc2648c216 [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 Moolenaar92b8b2d2016-01-29 22:36:45 +010017static char_u *vim_version_dir(char_u *vimdir);
18static char_u *remove_tail(char_u *p, char_u *pend, char_u *name);
Bram Moolenaar06ae70d2013-06-17 19:26:36 +020019#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010020static void init_users(void);
Bram Moolenaar06ae70d2013-06-17 19:26:36 +020021#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static int copy_indent(int size, char_u *src);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023
Bram Moolenaar24305862012-08-15 14:05:05 +020024/* All user names (for ~user completion as done by shell). */
25#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
26static garray_T ga_users;
27#endif
28
Bram Moolenaar071d4272004-06-13 20:20:40 +000029/*
30 * Count the size (in window cells) of the indent in the current line.
31 */
32 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010033get_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000034{
Bram Moolenaar597a4222014-06-25 14:39:50 +020035 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000036}
37
38/*
39 * Count the size (in window cells) of the indent in line "lnum".
40 */
41 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010042get_indent_lnum(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000043{
Bram Moolenaar597a4222014-06-25 14:39:50 +020044 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000045}
46
47#if defined(FEAT_FOLDING) || defined(PROTO)
48/*
49 * Count the size (in window cells) of the indent in line "lnum" of buffer
50 * "buf".
51 */
52 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010053get_indent_buf(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000054{
Bram Moolenaar597a4222014-06-25 14:39:50 +020055 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000056}
57#endif
58
59/*
60 * count the size (in window cells) of the indent in line "ptr", with
61 * 'tabstop' at "ts"
62 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +000063 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010064get_indent_str(
65 char_u *ptr,
66 int ts,
67 int list) /* if TRUE, count only screen size for tabs */
Bram Moolenaar071d4272004-06-13 20:20:40 +000068{
69 int count = 0;
70
71 for ( ; *ptr; ++ptr)
72 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020073 if (*ptr == TAB)
74 {
75 if (!list || lcs_tab1) /* count a tab for what it is worth */
76 count += ts - (count % ts);
77 else
Bram Moolenaare4df1642014-08-29 12:58:44 +020078 /* In list mode, when tab is not set, count screen char width
79 * for Tab, displays: ^I */
Bram Moolenaar597a4222014-06-25 14:39:50 +020080 count += ptr2cells(ptr);
81 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000082 else if (*ptr == ' ')
83 ++count; /* count a space for one */
84 else
85 break;
86 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +000087 return count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000088}
89
90/*
91 * Set the indent of the current line.
92 * Leaves the cursor on the first non-blank in the line.
93 * Caller must take care of undo.
94 * "flags":
95 * SIN_CHANGED: call changed_bytes() if the line was changed.
96 * SIN_INSERT: insert the indent in front of the line.
97 * SIN_UNDO: save line for undo before changing it.
98 * Returns TRUE if the line was changed.
99 */
100 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100101set_indent(
102 int size, /* measured in spaces */
103 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104{
105 char_u *p;
106 char_u *newline;
107 char_u *oldline;
108 char_u *s;
109 int todo;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000110 int ind_len; /* measured in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111 int line_len;
112 int doit = FALSE;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000113 int ind_done = 0; /* measured in spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 int tab_pad;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000115 int retval = FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000116 int orig_char_len = -1; /* number of initial whitespace chars when
Bram Moolenaar5002c292007-07-24 13:26:15 +0000117 'et' and 'pi' are both set */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119 /*
120 * First check if there is anything to do and compute the number of
121 * characters needed for the indent.
122 */
123 todo = size;
124 ind_len = 0;
125 p = oldline = ml_get_curline();
126
127 /* Calculate the buffer size for the new indent, and check to see if it
128 * isn't already set */
129
Bram Moolenaar5002c292007-07-24 13:26:15 +0000130 /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and
131 * 'preserveindent' are set count the number of characters at the
132 * beginning of the line to be copied */
133 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 {
135 /* If 'preserveindent' is set then reuse as much as possible of
136 * the existing indent structure for the new indent */
137 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
138 {
139 ind_done = 0;
140
141 /* count as many characters as we can use */
142 while (todo > 0 && vim_iswhite(*p))
143 {
144 if (*p == TAB)
145 {
146 tab_pad = (int)curbuf->b_p_ts
147 - (ind_done % (int)curbuf->b_p_ts);
148 /* stop if this tab will overshoot the target */
149 if (todo < tab_pad)
150 break;
151 todo -= tab_pad;
152 ++ind_len;
153 ind_done += tab_pad;
154 }
155 else
156 {
157 --todo;
158 ++ind_len;
159 ++ind_done;
160 }
161 ++p;
162 }
163
Bram Moolenaar5002c292007-07-24 13:26:15 +0000164 /* Set initial number of whitespace chars to copy if we are
165 * preserving indent but expandtab is set */
166 if (curbuf->b_p_et)
167 orig_char_len = ind_len;
168
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 /* Fill to next tabstop with a tab, if possible */
170 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000171 if (todo >= tab_pad && orig_char_len == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 {
173 doit = TRUE;
174 todo -= tab_pad;
175 ++ind_len;
176 /* ind_done += tab_pad; */
177 }
178 }
179
180 /* count tabs required for indent */
181 while (todo >= (int)curbuf->b_p_ts)
182 {
183 if (*p != TAB)
184 doit = TRUE;
185 else
186 ++p;
187 todo -= (int)curbuf->b_p_ts;
188 ++ind_len;
189 /* ind_done += (int)curbuf->b_p_ts; */
190 }
191 }
192 /* count spaces required for indent */
193 while (todo > 0)
194 {
195 if (*p != ' ')
196 doit = TRUE;
197 else
198 ++p;
199 --todo;
200 ++ind_len;
201 /* ++ind_done; */
202 }
203
204 /* Return if the indent is OK already. */
205 if (!doit && !vim_iswhite(*p) && !(flags & SIN_INSERT))
206 return FALSE;
207
208 /* Allocate memory for the new line. */
209 if (flags & SIN_INSERT)
210 p = oldline;
211 else
212 p = skipwhite(p);
213 line_len = (int)STRLEN(p) + 1;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000214
215 /* If 'preserveindent' and 'expandtab' are both set keep the original
216 * characters and allocate accordingly. We will fill the rest with spaces
217 * after the if (!curbuf->b_p_et) below. */
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000218 if (orig_char_len != -1)
Bram Moolenaar5002c292007-07-24 13:26:15 +0000219 {
220 newline = alloc(orig_char_len + size - ind_done + line_len);
221 if (newline == NULL)
222 return FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000223 todo = size - ind_done;
224 ind_len = orig_char_len + todo; /* Set total length of indent in
225 * characters, which may have been
226 * undercounted until now */
Bram Moolenaar5002c292007-07-24 13:26:15 +0000227 p = oldline;
228 s = newline;
229 while (orig_char_len > 0)
230 {
231 *s++ = *p++;
232 orig_char_len--;
233 }
Bram Moolenaar913626c2008-01-03 11:43:42 +0000234
Bram Moolenaar5002c292007-07-24 13:26:15 +0000235 /* Skip over any additional white space (useful when newindent is less
236 * than old) */
237 while (vim_iswhite(*p))
Bram Moolenaar913626c2008-01-03 11:43:42 +0000238 ++p;
Bram Moolenaarcc00b952007-08-11 12:32:57 +0000239
Bram Moolenaar5002c292007-07-24 13:26:15 +0000240 }
241 else
242 {
243 todo = size;
244 newline = alloc(ind_len + line_len);
245 if (newline == NULL)
246 return FALSE;
247 s = newline;
248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249
250 /* Put the characters in the new line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 /* if 'expandtab' isn't set: use TABs */
252 if (!curbuf->b_p_et)
253 {
254 /* If 'preserveindent' is set then reuse as much as possible of
255 * the existing indent structure for the new indent */
256 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
257 {
258 p = oldline;
259 ind_done = 0;
260
261 while (todo > 0 && vim_iswhite(*p))
262 {
263 if (*p == TAB)
264 {
265 tab_pad = (int)curbuf->b_p_ts
266 - (ind_done % (int)curbuf->b_p_ts);
267 /* stop if this tab will overshoot the target */
268 if (todo < tab_pad)
269 break;
270 todo -= tab_pad;
271 ind_done += tab_pad;
272 }
273 else
274 {
275 --todo;
276 ++ind_done;
277 }
278 *s++ = *p++;
279 }
280
281 /* Fill to next tabstop with a tab, if possible */
282 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
283 if (todo >= tab_pad)
284 {
285 *s++ = TAB;
286 todo -= tab_pad;
287 }
288
289 p = skipwhite(p);
290 }
291
292 while (todo >= (int)curbuf->b_p_ts)
293 {
294 *s++ = TAB;
295 todo -= (int)curbuf->b_p_ts;
296 }
297 }
298 while (todo > 0)
299 {
300 *s++ = ' ';
301 --todo;
302 }
303 mch_memmove(s, p, (size_t)line_len);
304
305 /* Replace the line (unless undo fails). */
306 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
307 {
308 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
309 if (flags & SIN_CHANGED)
310 changed_bytes(curwin->w_cursor.lnum, 0);
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200311 /* Correct saved cursor position if it is in this line. */
312 if (saved_cursor.lnum == curwin->w_cursor.lnum)
313 {
314 if (saved_cursor.col >= (colnr_T)(p - oldline))
315 /* cursor was after the indent, adjust for the number of
316 * bytes added/removed */
317 saved_cursor.col += ind_len - (colnr_T)(p - oldline);
318 else if (saved_cursor.col >= (colnr_T)(s - newline))
319 /* cursor was in the indent, and is now after it, put it back
320 * at the start of the indent (replacing spaces with TAB) */
321 saved_cursor.col = (colnr_T)(s - newline);
322 }
Bram Moolenaar5409c052005-03-18 20:27:04 +0000323 retval = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 }
325 else
326 vim_free(newline);
327
328 curwin->w_cursor.col = ind_len;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000329 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330}
331
332/*
333 * Copy the indent from ptr to the current line (and fill to size)
334 * Leaves the cursor on the first non-blank in the line.
335 * Returns TRUE if the line was changed.
336 */
337 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100338copy_indent(int size, char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339{
340 char_u *p = NULL;
341 char_u *line = NULL;
342 char_u *s;
343 int todo;
344 int ind_len;
345 int line_len = 0;
346 int tab_pad;
347 int ind_done;
348 int round;
349
350 /* Round 1: compute the number of characters needed for the indent
351 * Round 2: copy the characters. */
352 for (round = 1; round <= 2; ++round)
353 {
354 todo = size;
355 ind_len = 0;
356 ind_done = 0;
357 s = src;
358
359 /* Count/copy the usable portion of the source line */
360 while (todo > 0 && vim_iswhite(*s))
361 {
362 if (*s == TAB)
363 {
364 tab_pad = (int)curbuf->b_p_ts
365 - (ind_done % (int)curbuf->b_p_ts);
366 /* Stop if this tab will overshoot the target */
367 if (todo < tab_pad)
368 break;
369 todo -= tab_pad;
370 ind_done += tab_pad;
371 }
372 else
373 {
374 --todo;
375 ++ind_done;
376 }
377 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000378 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 *p++ = *s;
380 ++s;
381 }
382
383 /* Fill to next tabstop with a tab, if possible */
384 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200385 if (todo >= tab_pad && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 {
387 todo -= tab_pad;
388 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000389 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 *p++ = TAB;
391 }
392
393 /* Add tabs required for indent */
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200394 while (todo >= (int)curbuf->b_p_ts && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 {
396 todo -= (int)curbuf->b_p_ts;
397 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000398 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 *p++ = TAB;
400 }
401
402 /* Count/add spaces required for indent */
403 while (todo > 0)
404 {
405 --todo;
406 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000407 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 *p++ = ' ';
409 }
410
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000411 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 {
413 /* Allocate memory for the result: the copied indent, new indent
414 * and the rest of the line. */
415 line_len = (int)STRLEN(ml_get_curline()) + 1;
416 line = alloc(ind_len + line_len);
417 if (line == NULL)
418 return FALSE;
419 p = line;
420 }
421 }
422
423 /* Append the original line */
424 mch_memmove(p, ml_get_curline(), (size_t)line_len);
425
426 /* Replace the line */
427 ml_replace(curwin->w_cursor.lnum, line, FALSE);
428
429 /* Put the cursor after the indent. */
430 curwin->w_cursor.col = ind_len;
431 return TRUE;
432}
433
434/*
435 * Return the indent of the current line after a number. Return -1 if no
436 * number was found. Used for 'n' in 'formatoptions': numbered list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000437 * Since a pattern is used it can actually handle more than numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 */
439 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100440get_number_indent(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 colnr_T col;
443 pos_T pos;
444
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200445 regmatch_T regmatch;
446 int lead_len = 0; /* length of comment leader */
447
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 if (lnum > curbuf->b_ml.ml_line_count)
449 return -1;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000450 pos.lnum = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200451
452#ifdef FEAT_COMMENTS
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200453 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
454 if ((State & INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200455 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000456#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200457 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
458 if (regmatch.regprog != NULL)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200459 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200460 regmatch.rm_ic = FALSE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200461
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200462 /* vim_regexec() expects a pointer to a line. This lets us
463 * start matching for the flp beyond any comment leader... */
464 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200465 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200466 pos.lnum = lnum;
467 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200468#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200469 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200470#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200471 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200472 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200473 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000474
475 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 getvcol(curwin, &pos, &col, NULL, NULL);
478 return (int)col;
479}
480
Bram Moolenaar597a4222014-06-25 14:39:50 +0200481#if defined(FEAT_LINEBREAK) || defined(PROTO)
482/*
483 * Return appropriate space number for breakindent, taking influencing
484 * parameters into account. Window must be specified, since it is not
485 * necessarily always the current one.
486 */
487 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100488get_breakindent_win(
489 win_T *wp,
490 char_u *line) /* start of the line */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200491{
492 static int prev_indent = 0; /* cached indent value */
493 static long prev_ts = 0L; /* cached tabstop value */
494 static char_u *prev_line = NULL; /* cached pointer to line */
Bram Moolenaar79518e22017-02-17 16:31:35 +0100495 static varnumber_T prev_tick = 0; /* changedtick of cached value */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200496 int bri = 0;
497 /* window width minus window margin space, i.e. what rests for text */
498 const int eff_wwidth = W_WIDTH(wp)
499 - ((wp->w_p_nu || wp->w_p_rnu)
500 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
501 ? number_width(wp) + 1 : 0);
502
503 /* used cached indent, unless pointer or 'tabstop' changed */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200504 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
Bram Moolenaar79518e22017-02-17 16:31:35 +0100505 || prev_tick != *wp->w_buffer->b_changedtick)
Bram Moolenaar597a4222014-06-25 14:39:50 +0200506 {
507 prev_line = line;
508 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100509 prev_tick = *wp->w_buffer->b_changedtick;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200510 prev_indent = get_indent_str(line,
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200511 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
Bram Moolenaar597a4222014-06-25 14:39:50 +0200512 }
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200513 bri = prev_indent + wp->w_p_brishift;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200514
515 /* indent minus the length of the showbreak string */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200516 if (wp->w_p_brisbr)
517 bri -= vim_strsize(p_sbr);
518
519 /* Add offset for number column, if 'n' is in 'cpoptions' */
520 bri += win_col_off2(wp);
521
522 /* never indent past left window margin */
523 if (bri < 0)
524 bri = 0;
525 /* always leave at least bri_min characters on the left,
526 * if text width is sufficient */
527 else if (bri > eff_wwidth - wp->w_p_brimin)
528 bri = (eff_wwidth - wp->w_p_brimin < 0)
529 ? 0 : eff_wwidth - wp->w_p_brimin;
530
531 return bri;
532}
533#endif
534
535
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
537
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100538static int cin_is_cinword(char_u *line);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539
540/*
541 * Return TRUE if the string "line" starts with a word from 'cinwords'.
542 */
543 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100544cin_is_cinword(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545{
546 char_u *cinw;
547 char_u *cinw_buf;
548 int cinw_len;
549 int retval = FALSE;
550 int len;
551
552 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1;
553 cinw_buf = alloc((unsigned)cinw_len);
554 if (cinw_buf != NULL)
555 {
556 line = skipwhite(line);
557 for (cinw = curbuf->b_p_cinw; *cinw; )
558 {
559 len = copy_option_part(&cinw, cinw_buf, cinw_len, ",");
560 if (STRNCMP(line, cinw_buf, len) == 0
561 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1])))
562 {
563 retval = TRUE;
564 break;
565 }
566 }
567 vim_free(cinw_buf);
568 }
569 return retval;
570}
571#endif
572
573/*
574 * open_line: Add a new line below or above the current line.
575 *
576 * For VREPLACE mode, we only add a new line when we get to the end of the
577 * file, otherwise we just start replacing the next line.
578 *
579 * Caller must take care of undo. Since VREPLACE may affect any number of
580 * lines however, it may call u_save_cursor() again when starting to change a
581 * new line.
582 * "flags": OPENLINE_DELSPACES delete spaces after cursor
583 * OPENLINE_DO_COM format comments
584 * OPENLINE_KEEPTRAIL keep trailing spaces
585 * OPENLINE_MARKFIX adjust mark positions after the line break
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200586 * OPENLINE_COM_LIST format comments with list or 2nd line indent
587 *
588 * "second_line_indent": indent for after ^^D in Insert mode or if flag
589 * OPENLINE_COM_LIST
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 *
591 * Return TRUE for success, FALSE for failure
592 */
593 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100594open_line(
595 int dir, /* FORWARD or BACKWARD */
596 int flags,
597 int second_line_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598{
599 char_u *saved_line; /* copy of the original line */
600 char_u *next_line = NULL; /* copy of the next line */
601 char_u *p_extra = NULL; /* what goes to next line */
602 int less_cols = 0; /* less columns for mark in new line */
603 int less_cols_off = 0; /* columns to skip for mark adjust */
604 pos_T old_cursor; /* old cursor position */
605 int newcol = 0; /* new cursor column */
606 int newindent = 0; /* auto-indent of the new line */
607 int n;
608 int trunc_line = FALSE; /* truncate current line afterwards */
609 int retval = FALSE; /* return value, default is FAIL */
610#ifdef FEAT_COMMENTS
611 int extra_len = 0; /* length of p_extra string */
612 int lead_len; /* length of comment leader */
613 char_u *lead_flags; /* position in 'comments' for comment leader */
614 char_u *leader = NULL; /* copy of comment leader */
615#endif
616 char_u *allocated = NULL; /* allocated memory */
617#if defined(FEAT_SMARTINDENT) || defined(FEAT_VREPLACE) || defined(FEAT_LISP) \
618 || defined(FEAT_CINDENT) || defined(FEAT_COMMENTS)
619 char_u *p;
620#endif
621 int saved_char = NUL; /* init for GCC */
622#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
623 pos_T *pos;
624#endif
625#ifdef FEAT_SMARTINDENT
626 int do_si = (!p_paste && curbuf->b_p_si
627# ifdef FEAT_CINDENT
628 && !curbuf->b_p_cin
629# endif
630 );
631 int no_si = FALSE; /* reset did_si afterwards */
632 int first_char = NUL; /* init for GCC */
633#endif
634#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
635 int vreplace_mode;
636#endif
637 int did_append; /* appended a new line */
638 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */
639
640 /*
641 * make a copy of the current line so we can mess with it
642 */
643 saved_line = vim_strsave(ml_get_curline());
644 if (saved_line == NULL) /* out of memory! */
645 return FALSE;
646
647#ifdef FEAT_VREPLACE
648 if (State & VREPLACE_FLAG)
649 {
650 /*
651 * With VREPLACE we make a copy of the next line, which we will be
652 * starting to replace. First make the new line empty and let vim play
653 * with the indenting and comment leader to its heart's content. Then
654 * we grab what it ended up putting on the new line, put back the
655 * original line, and call ins_char() to put each new character onto
656 * the line, replacing what was there before and pushing the right
657 * stuff onto the replace stack. -- webb.
658 */
659 if (curwin->w_cursor.lnum < orig_line_count)
660 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
661 else
662 next_line = vim_strsave((char_u *)"");
663 if (next_line == NULL) /* out of memory! */
664 goto theend;
665
666 /*
667 * In VREPLACE mode, a NL replaces the rest of the line, and starts
668 * replacing the next line, so push all of the characters left on the
669 * line onto the replace stack. We'll push any other characters that
670 * might be replaced at the start of the next line (due to autoindent
671 * etc) a bit later.
672 */
673 replace_push(NUL); /* Call twice because BS over NL expects it */
674 replace_push(NUL);
675 p = saved_line + curwin->w_cursor.col;
676 while (*p != NUL)
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000677 {
678#ifdef FEAT_MBYTE
679 if (has_mbyte)
680 p += replace_push_mb(p);
681 else
682#endif
683 replace_push(*p++);
684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 saved_line[curwin->w_cursor.col] = NUL;
686 }
687#endif
688
689 if ((State & INSERT)
690#ifdef FEAT_VREPLACE
691 && !(State & VREPLACE_FLAG)
692#endif
693 )
694 {
695 p_extra = saved_line + curwin->w_cursor.col;
696#ifdef FEAT_SMARTINDENT
697 if (do_si) /* need first char after new line break */
698 {
699 p = skipwhite(p_extra);
700 first_char = *p;
701 }
702#endif
703#ifdef FEAT_COMMENTS
704 extra_len = (int)STRLEN(p_extra);
705#endif
706 saved_char = *p_extra;
707 *p_extra = NUL;
708 }
709
710 u_clearline(); /* cannot do "U" command when adding lines */
711#ifdef FEAT_SMARTINDENT
712 did_si = FALSE;
713#endif
714 ai_col = 0;
715
716 /*
717 * If we just did an auto-indent, then we didn't type anything on
718 * the prior line, and it should be truncated. Do this even if 'ai' is not
719 * set because automatically inserting a comment leader also sets did_ai.
720 */
721 if (dir == FORWARD && did_ai)
722 trunc_line = TRUE;
723
724 /*
725 * If 'autoindent' and/or 'smartindent' is set, try to figure out what
726 * indent to use for the new line.
727 */
728 if (curbuf->b_p_ai
729#ifdef FEAT_SMARTINDENT
730 || do_si
731#endif
732 )
733 {
734 /*
735 * count white space on current line
736 */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200737 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200738 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
739 newindent = second_line_indent; /* for ^^D command in insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740
741#ifdef FEAT_SMARTINDENT
742 /*
743 * Do smart indenting.
744 * In insert/replace mode (only when dir == FORWARD)
745 * we may move some text to the next line. If it starts with '{'
746 * don't add an indent. Fixes inserting a NL before '{' in line
747 * "if (condition) {"
748 */
749 if (!trunc_line && do_si && *saved_line != NUL
750 && (p_extra == NULL || first_char != '{'))
751 {
752 char_u *ptr;
753 char_u last_char;
754
755 old_cursor = curwin->w_cursor;
756 ptr = saved_line;
757# ifdef FEAT_COMMENTS
758 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200759 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 else
761 lead_len = 0;
762# endif
763 if (dir == FORWARD)
764 {
765 /*
766 * Skip preprocessor directives, unless they are
767 * recognised as comments.
768 */
769 if (
770# ifdef FEAT_COMMENTS
771 lead_len == 0 &&
772# endif
773 ptr[0] == '#')
774 {
775 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
776 ptr = ml_get(--curwin->w_cursor.lnum);
777 newindent = get_indent();
778 }
779# ifdef FEAT_COMMENTS
780 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200781 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 else
783 lead_len = 0;
784 if (lead_len > 0)
785 {
786 /*
787 * This case gets the following right:
788 * \*
789 * * A comment (read '\' as '/').
790 * *\
791 * #define IN_THE_WAY
792 * This should line up here;
793 */
794 p = skipwhite(ptr);
795 if (p[0] == '/' && p[1] == '*')
796 p++;
797 if (p[0] == '*')
798 {
799 for (p++; *p; p++)
800 {
801 if (p[0] == '/' && p[-1] == '*')
802 {
803 /*
804 * End of C comment, indent should line up
805 * with the line containing the start of
806 * the comment
807 */
808 curwin->w_cursor.col = (colnr_T)(p - ptr);
809 if ((pos = findmatch(NULL, NUL)) != NULL)
810 {
811 curwin->w_cursor.lnum = pos->lnum;
812 newindent = get_indent();
813 }
814 }
815 }
816 }
817 }
818 else /* Not a comment line */
819# endif
820 {
821 /* Find last non-blank in line */
822 p = ptr + STRLEN(ptr) - 1;
823 while (p > ptr && vim_iswhite(*p))
824 --p;
825 last_char = *p;
826
827 /*
828 * find the character just before the '{' or ';'
829 */
830 if (last_char == '{' || last_char == ';')
831 {
832 if (p > ptr)
833 --p;
834 while (p > ptr && vim_iswhite(*p))
835 --p;
836 }
837 /*
838 * Try to catch lines that are split over multiple
839 * lines. eg:
840 * if (condition &&
841 * condition) {
842 * Should line up here!
843 * }
844 */
845 if (*p == ')')
846 {
847 curwin->w_cursor.col = (colnr_T)(p - ptr);
848 if ((pos = findmatch(NULL, '(')) != NULL)
849 {
850 curwin->w_cursor.lnum = pos->lnum;
851 newindent = get_indent();
852 ptr = ml_get_curline();
853 }
854 }
855 /*
856 * If last character is '{' do indent, without
857 * checking for "if" and the like.
858 */
859 if (last_char == '{')
860 {
861 did_si = TRUE; /* do indent */
862 no_si = TRUE; /* don't delete it when '{' typed */
863 }
864 /*
865 * Look for "if" and the like, use 'cinwords'.
866 * Don't do this if the previous line ended in ';' or
867 * '}'.
868 */
869 else if (last_char != ';' && last_char != '}'
870 && cin_is_cinword(ptr))
871 did_si = TRUE;
872 }
873 }
874 else /* dir == BACKWARD */
875 {
876 /*
877 * Skip preprocessor directives, unless they are
878 * recognised as comments.
879 */
880 if (
881# ifdef FEAT_COMMENTS
882 lead_len == 0 &&
883# endif
884 ptr[0] == '#')
885 {
886 int was_backslashed = FALSE;
887
888 while ((ptr[0] == '#' || was_backslashed) &&
889 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
890 {
891 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
892 was_backslashed = TRUE;
893 else
894 was_backslashed = FALSE;
895 ptr = ml_get(++curwin->w_cursor.lnum);
896 }
897 if (was_backslashed)
898 newindent = 0; /* Got to end of file */
899 else
900 newindent = get_indent();
901 }
902 p = skipwhite(ptr);
903 if (*p == '}') /* if line starts with '}': do indent */
904 did_si = TRUE;
905 else /* can delete indent when '{' typed */
906 can_si_back = TRUE;
907 }
908 curwin->w_cursor = old_cursor;
909 }
910 if (do_si)
911 can_si = TRUE;
912#endif /* FEAT_SMARTINDENT */
913
914 did_ai = TRUE;
915 }
916
917#ifdef FEAT_COMMENTS
918 /*
919 * Find out if the current line starts with a comment leader.
920 * This may then be inserted in front of the new line.
921 */
922 end_comment_pending = NUL;
923 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200924 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 else
926 lead_len = 0;
927 if (lead_len > 0)
928 {
929 char_u *lead_repl = NULL; /* replaces comment leader */
930 int lead_repl_len = 0; /* length of *lead_repl */
931 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
932 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
933 char_u *comment_end = NULL; /* where lead_end has been found */
934 int extra_space = FALSE; /* append extra space */
935 int current_flag;
936 int require_blank = FALSE; /* requires blank after middle */
937 char_u *p2;
938
939 /*
940 * If the comment leader has the start, middle or end flag, it may not
941 * be used or may be replaced with the middle leader.
942 */
943 for (p = lead_flags; *p && *p != ':'; ++p)
944 {
945 if (*p == COM_BLANK)
946 {
947 require_blank = TRUE;
948 continue;
949 }
950 if (*p == COM_START || *p == COM_MIDDLE)
951 {
952 current_flag = *p;
953 if (*p == COM_START)
954 {
955 /*
956 * Doing "O" on a start of comment does not insert leader.
957 */
958 if (dir == BACKWARD)
959 {
960 lead_len = 0;
961 break;
962 }
963
964 /* find start of middle part */
965 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
966 require_blank = FALSE;
967 }
968
969 /*
970 * Isolate the strings of the middle and end leader.
971 */
972 while (*p && p[-1] != ':') /* find end of middle flags */
973 {
974 if (*p == COM_BLANK)
975 require_blank = TRUE;
976 ++p;
977 }
978 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
979
980 while (*p && p[-1] != ':') /* find end of end flags */
981 {
982 /* Check whether we allow automatic ending of comments */
983 if (*p == COM_AUTO_END)
984 end_comment_pending = -1; /* means we want to set it */
985 ++p;
986 }
987 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
988
989 if (end_comment_pending == -1) /* we can set it now */
990 end_comment_pending = lead_end[n - 1];
991
992 /*
993 * If the end of the comment is in the same line, don't use
994 * the comment leader.
995 */
996 if (dir == FORWARD)
997 {
998 for (p = saved_line + lead_len; *p; ++p)
999 if (STRNCMP(p, lead_end, n) == 0)
1000 {
1001 comment_end = p;
1002 lead_len = 0;
1003 break;
1004 }
1005 }
1006
1007 /*
1008 * Doing "o" on a start of comment inserts the middle leader.
1009 */
1010 if (lead_len > 0)
1011 {
1012 if (current_flag == COM_START)
1013 {
1014 lead_repl = lead_middle;
1015 lead_repl_len = (int)STRLEN(lead_middle);
1016 }
1017
1018 /*
1019 * If we have hit RETURN immediately after the start
1020 * comment leader, then put a space after the middle
1021 * comment leader on the next line.
1022 */
1023 if (!vim_iswhite(saved_line[lead_len - 1])
1024 && ((p_extra != NULL
1025 && (int)curwin->w_cursor.col == lead_len)
1026 || (p_extra == NULL
1027 && saved_line[lead_len] == NUL)
1028 || require_blank))
1029 extra_space = TRUE;
1030 }
1031 break;
1032 }
1033 if (*p == COM_END)
1034 {
1035 /*
1036 * Doing "o" on the end of a comment does not insert leader.
1037 * Remember where the end is, might want to use it to find the
1038 * start (for C-comments).
1039 */
1040 if (dir == FORWARD)
1041 {
1042 comment_end = skipwhite(saved_line);
1043 lead_len = 0;
1044 break;
1045 }
1046
1047 /*
1048 * Doing "O" on the end of a comment inserts the middle leader.
1049 * Find the string for the middle leader, searching backwards.
1050 */
1051 while (p > curbuf->b_p_com && *p != ',')
1052 --p;
1053 for (lead_repl = p; lead_repl > curbuf->b_p_com
1054 && lead_repl[-1] != ':'; --lead_repl)
1055 ;
1056 lead_repl_len = (int)(p - lead_repl);
1057
1058 /* We can probably always add an extra space when doing "O" on
1059 * the comment-end */
1060 extra_space = TRUE;
1061
1062 /* Check whether we allow automatic ending of comments */
1063 for (p2 = p; *p2 && *p2 != ':'; p2++)
1064 {
1065 if (*p2 == COM_AUTO_END)
1066 end_comment_pending = -1; /* means we want to set it */
1067 }
1068 if (end_comment_pending == -1)
1069 {
1070 /* Find last character in end-comment string */
1071 while (*p2 && *p2 != ',')
1072 p2++;
1073 end_comment_pending = p2[-1];
1074 }
1075 break;
1076 }
1077 if (*p == COM_FIRST)
1078 {
1079 /*
1080 * Comment leader for first line only: Don't repeat leader
1081 * when using "O", blank out leader when using "o".
1082 */
1083 if (dir == BACKWARD)
1084 lead_len = 0;
1085 else
1086 {
1087 lead_repl = (char_u *)"";
1088 lead_repl_len = 0;
1089 }
1090 break;
1091 }
1092 }
1093 if (lead_len)
1094 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001095 /* allocate buffer (may concatenate p_extra later) */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001096 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001097 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 allocated = leader; /* remember to free it later */
1099
1100 if (leader == NULL)
1101 lead_len = 0;
1102 else
1103 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001104 vim_strncpy(leader, saved_line, lead_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105
1106 /*
1107 * Replace leader with lead_repl, right or left adjusted
1108 */
1109 if (lead_repl != NULL)
1110 {
1111 int c = 0;
1112 int off = 0;
1113
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001114 for (p = lead_flags; *p != NUL && *p != ':'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 {
1116 if (*p == COM_RIGHT || *p == COM_LEFT)
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001117 c = *p++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 else if (VIM_ISDIGIT(*p) || *p == '-')
1119 off = getdigits(&p);
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001120 else
1121 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 }
1123 if (c == COM_RIGHT) /* right adjusted leader */
1124 {
1125 /* find last non-white in the leader to line up with */
1126 for (p = leader + lead_len - 1; p > leader
1127 && vim_iswhite(*p); --p)
1128 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 ++p;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001130
1131#ifdef FEAT_MBYTE
1132 /* Compute the length of the replaced characters in
1133 * screen characters, not bytes. */
1134 {
1135 int repl_size = vim_strnsize(lead_repl,
1136 lead_repl_len);
1137 int old_size = 0;
1138 char_u *endp = p;
1139 int l;
1140
1141 while (old_size < repl_size && p > leader)
1142 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001143 mb_ptr_back(leader, p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001144 old_size += ptr2cells(p);
1145 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001146 l = lead_repl_len - (int)(endp - p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001147 if (l != 0)
1148 mch_memmove(endp + l, endp,
1149 (size_t)((leader + lead_len) - endp));
1150 lead_len += l;
1151 }
1152#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 if (p < leader + lead_repl_len)
1154 p = leader;
1155 else
1156 p -= lead_repl_len;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1159 if (p + lead_repl_len > leader + lead_len)
1160 p[lead_repl_len] = NUL;
1161
1162 /* blank-out any other chars from the old leader. */
1163 while (--p >= leader)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001164 {
1165#ifdef FEAT_MBYTE
1166 int l = mb_head_off(leader, p);
1167
1168 if (l > 1)
1169 {
1170 p -= l;
1171 if (ptr2cells(p) > 1)
1172 {
1173 p[1] = ' ';
1174 --l;
1175 }
1176 mch_memmove(p + 1, p + l + 1,
1177 (size_t)((leader + lead_len) - (p + l + 1)));
1178 lead_len -= l;
1179 *p = ' ';
1180 }
1181 else
1182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 if (!vim_iswhite(*p))
1184 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 }
1187 else /* left adjusted leader */
1188 {
1189 p = skipwhite(leader);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001190#ifdef FEAT_MBYTE
1191 /* Compute the length of the replaced characters in
1192 * screen characters, not bytes. Move the part that is
1193 * not to be overwritten. */
1194 {
1195 int repl_size = vim_strnsize(lead_repl,
1196 lead_repl_len);
1197 int i;
1198 int l;
1199
Bram Moolenaardc633cf2016-04-23 14:33:19 +02001200 for (i = 0; i < lead_len && p[i] != NUL; i += l)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001201 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001202 l = (*mb_ptr2len)(p + i);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001203 if (vim_strnsize(p, i + l) > repl_size)
1204 break;
1205 }
1206 if (i != lead_repl_len)
1207 {
1208 mch_memmove(p + lead_repl_len, p + i,
Bram Moolenaar2d7ff052009-11-17 15:08:26 +00001209 (size_t)(lead_len - i - (p - leader)));
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001210 lead_len += lead_repl_len - i;
1211 }
1212 }
1213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1215
1216 /* Replace any remaining non-white chars in the old
1217 * leader by spaces. Keep Tabs, the indent must
1218 * remain the same. */
1219 for (p += lead_repl_len; p < leader + lead_len; ++p)
1220 if (!vim_iswhite(*p))
1221 {
1222 /* Don't put a space before a TAB. */
1223 if (p + 1 < leader + lead_len && p[1] == TAB)
1224 {
1225 --lead_len;
1226 mch_memmove(p, p + 1,
1227 (leader + lead_len) - p);
1228 }
1229 else
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001230 {
1231#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001232 int l = (*mb_ptr2len)(p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001233
1234 if (l > 1)
1235 {
1236 if (ptr2cells(p) > 1)
1237 {
1238 /* Replace a double-wide char with
1239 * two spaces */
1240 --l;
1241 *p++ = ' ';
1242 }
1243 mch_memmove(p + 1, p + l,
1244 (leader + lead_len) - p);
1245 lead_len -= l - 1;
1246 }
1247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 }
1251 *p = NUL;
1252 }
1253
1254 /* Recompute the indent, it may have changed. */
1255 if (curbuf->b_p_ai
1256#ifdef FEAT_SMARTINDENT
1257 || do_si
1258#endif
1259 )
Bram Moolenaar597a4222014-06-25 14:39:50 +02001260 newindent = get_indent_str(leader, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261
1262 /* Add the indent offset */
1263 if (newindent + off < 0)
1264 {
1265 off = -newindent;
1266 newindent = 0;
1267 }
1268 else
1269 newindent += off;
1270
1271 /* Correct trailing spaces for the shift, so that
1272 * alignment remains equal. */
1273 while (off > 0 && lead_len > 0
1274 && leader[lead_len - 1] == ' ')
1275 {
1276 /* Don't do it when there is a tab before the space */
1277 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1278 break;
1279 --lead_len;
1280 --off;
1281 }
1282
1283 /* If the leader ends in white space, don't add an
1284 * extra space */
1285 if (lead_len > 0 && vim_iswhite(leader[lead_len - 1]))
1286 extra_space = FALSE;
1287 leader[lead_len] = NUL;
1288 }
1289
1290 if (extra_space)
1291 {
1292 leader[lead_len++] = ' ';
1293 leader[lead_len] = NUL;
1294 }
1295
1296 newcol = lead_len;
1297
1298 /*
1299 * if a new indent will be set below, remove the indent that
1300 * is in the comment leader
1301 */
1302 if (newindent
1303#ifdef FEAT_SMARTINDENT
1304 || did_si
1305#endif
1306 )
1307 {
1308 while (lead_len && vim_iswhite(*leader))
1309 {
1310 --lead_len;
1311 --newcol;
1312 ++leader;
1313 }
1314 }
1315
1316 }
1317#ifdef FEAT_SMARTINDENT
1318 did_si = can_si = FALSE;
1319#endif
1320 }
1321 else if (comment_end != NULL)
1322 {
1323 /*
1324 * We have finished a comment, so we don't use the leader.
1325 * If this was a C-comment and 'ai' or 'si' is set do a normal
1326 * indent to align with the line containing the start of the
1327 * comment.
1328 */
1329 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1330 (curbuf->b_p_ai
1331#ifdef FEAT_SMARTINDENT
1332 || do_si
1333#endif
1334 ))
1335 {
1336 old_cursor = curwin->w_cursor;
1337 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1338 if ((pos = findmatch(NULL, NUL)) != NULL)
1339 {
1340 curwin->w_cursor.lnum = pos->lnum;
1341 newindent = get_indent();
1342 }
1343 curwin->w_cursor = old_cursor;
1344 }
1345 }
1346 }
1347#endif
1348
1349 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */
1350 if (p_extra != NULL)
1351 {
1352 *p_extra = saved_char; /* restore char that NUL replaced */
1353
1354 /*
1355 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
1356 * non-blank.
1357 *
1358 * When in REPLACE mode, put the deleted blanks on the replace stack,
1359 * preceded by a NUL, so they can be put back when a BS is entered.
1360 */
1361 if (REPLACE_NORMAL(State))
1362 replace_push(NUL); /* end of extra blanks */
1363 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
1364 {
1365 while ((*p_extra == ' ' || *p_extra == '\t')
1366#ifdef FEAT_MBYTE
1367 && (!enc_utf8
1368 || !utf_iscomposing(utf_ptr2char(p_extra + 1)))
1369#endif
1370 )
1371 {
1372 if (REPLACE_NORMAL(State))
1373 replace_push(*p_extra);
1374 ++p_extra;
1375 ++less_cols_off;
1376 }
1377 }
1378 if (*p_extra != NUL)
1379 did_ai = FALSE; /* append some text, don't truncate now */
1380
1381 /* columns for marks adjusted for removed columns */
1382 less_cols = (int)(p_extra - saved_line);
1383 }
1384
1385 if (p_extra == NULL)
1386 p_extra = (char_u *)""; /* append empty line */
1387
1388#ifdef FEAT_COMMENTS
1389 /* concatenate leader and p_extra, if there is a leader */
1390 if (lead_len)
1391 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001392 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
1393 {
1394 int i;
Bram Moolenaar36105782012-06-14 20:59:25 +02001395 int padding = second_line_indent
1396 - (newindent + (int)STRLEN(leader));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001397
1398 /* Here whitespace is inserted after the comment char.
1399 * Below, set_indent(newindent, SIN_INSERT) will insert the
1400 * whitespace needed before the comment char. */
1401 for (i = 0; i < padding; i++)
1402 {
1403 STRCAT(leader, " ");
Bram Moolenaar5fb9ec52012-07-25 16:10:03 +02001404 less_cols--;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001405 newcol++;
1406 }
1407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 STRCAT(leader, p_extra);
1409 p_extra = leader;
1410 did_ai = TRUE; /* So truncating blanks works with comments */
1411 less_cols -= lead_len;
1412 }
1413 else
1414 end_comment_pending = NUL; /* turns out there was no leader */
1415#endif
1416
1417 old_cursor = curwin->w_cursor;
1418 if (dir == BACKWARD)
1419 --curwin->w_cursor.lnum;
1420#ifdef FEAT_VREPLACE
1421 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
1422#endif
1423 {
1424 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
1425 == FAIL)
1426 goto theend;
1427 /* Postpone calling changed_lines(), because it would mess up folding
Bram Moolenaar82faa252016-06-04 20:14:07 +02001428 * with markers.
1429 * Skip mark_adjust when adding a line after the last one, there can't
1430 * be marks there. */
1431 if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count)
1432 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 did_append = TRUE;
1434 }
1435#ifdef FEAT_VREPLACE
1436 else
1437 {
1438 /*
1439 * In VREPLACE mode we are starting to replace the next line.
1440 */
1441 curwin->w_cursor.lnum++;
1442 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1443 {
1444 /* In case we NL to a new line, BS to the previous one, and NL
1445 * again, we don't want to save the new line for undo twice.
1446 */
1447 (void)u_save_cursor(); /* errors are ignored! */
1448 vr_lines_changed++;
1449 }
1450 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1451 changed_bytes(curwin->w_cursor.lnum, 0);
1452 curwin->w_cursor.lnum--;
1453 did_append = FALSE;
1454 }
1455#endif
1456
1457 if (newindent
1458#ifdef FEAT_SMARTINDENT
1459 || did_si
1460#endif
1461 )
1462 {
1463 ++curwin->w_cursor.lnum;
1464#ifdef FEAT_SMARTINDENT
1465 if (did_si)
1466 {
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001467 int sw = (int)get_sw_value(curbuf);
Bram Moolenaar14f24742012-08-08 18:01:05 +02001468
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001470 newindent -= newindent % sw;
1471 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 }
1473#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001474 /* Copy the indent */
1475 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 {
1477 (void)copy_indent(newindent, saved_line);
1478
1479 /*
1480 * Set the 'preserveindent' option so that any further screwing
1481 * with the line doesn't entirely destroy our efforts to preserve
1482 * it. It gets restored at the function end.
1483 */
1484 curbuf->b_p_pi = TRUE;
1485 }
1486 else
1487 (void)set_indent(newindent, SIN_INSERT);
1488 less_cols -= curwin->w_cursor.col;
1489
1490 ai_col = curwin->w_cursor.col;
1491
1492 /*
1493 * In REPLACE mode, for each character in the new indent, there must
1494 * be a NUL on the replace stack, for when it is deleted with BS
1495 */
1496 if (REPLACE_NORMAL(State))
1497 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1498 replace_push(NUL);
1499 newcol += curwin->w_cursor.col;
1500#ifdef FEAT_SMARTINDENT
1501 if (no_si)
1502 did_si = FALSE;
1503#endif
1504 }
1505
1506#ifdef FEAT_COMMENTS
1507 /*
1508 * In REPLACE mode, for each character in the extra leader, there must be
1509 * a NUL on the replace stack, for when it is deleted with BS.
1510 */
1511 if (REPLACE_NORMAL(State))
1512 while (lead_len-- > 0)
1513 replace_push(NUL);
1514#endif
1515
1516 curwin->w_cursor = old_cursor;
1517
1518 if (dir == FORWARD)
1519 {
1520 if (trunc_line || (State & INSERT))
1521 {
1522 /* truncate current line at cursor */
1523 saved_line[curwin->w_cursor.col] = NUL;
1524 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1525 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1526 truncate_spaces(saved_line);
1527 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1528 saved_line = NULL;
1529 if (did_append)
1530 {
1531 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1532 curwin->w_cursor.lnum + 1, 1L);
1533 did_append = FALSE;
1534
1535 /* Move marks after the line break to the new line. */
1536 if (flags & OPENLINE_MARKFIX)
1537 mark_col_adjust(curwin->w_cursor.lnum,
1538 curwin->w_cursor.col + less_cols_off,
1539 1L, (long)-less_cols);
1540 }
1541 else
1542 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1543 }
1544
1545 /*
1546 * Put the cursor on the new line. Careful: the scrollup() above may
1547 * have moved w_cursor, we must use old_cursor.
1548 */
1549 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1550 }
1551 if (did_append)
1552 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1553
1554 curwin->w_cursor.col = newcol;
1555#ifdef FEAT_VIRTUALEDIT
1556 curwin->w_cursor.coladd = 0;
1557#endif
1558
1559#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1560 /*
1561 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1562 * fixthisline() from doing it (via change_indent()) by telling it we're in
1563 * normal INSERT mode.
1564 */
1565 if (State & VREPLACE_FLAG)
1566 {
1567 vreplace_mode = State; /* So we know to put things right later */
1568 State = INSERT;
1569 }
1570 else
1571 vreplace_mode = 0;
1572#endif
1573#ifdef FEAT_LISP
1574 /*
1575 * May do lisp indenting.
1576 */
1577 if (!p_paste
1578# ifdef FEAT_COMMENTS
1579 && leader == NULL
1580# endif
1581 && curbuf->b_p_lisp
1582 && curbuf->b_p_ai)
1583 {
1584 fixthisline(get_lisp_indent);
1585 p = ml_get_curline();
1586 ai_col = (colnr_T)(skipwhite(p) - p);
1587 }
1588#endif
1589#ifdef FEAT_CINDENT
1590 /*
1591 * May do indenting after opening a new line.
1592 */
1593 if (!p_paste
1594 && (curbuf->b_p_cin
1595# ifdef FEAT_EVAL
1596 || *curbuf->b_p_inde != NUL
1597# endif
1598 )
1599 && in_cinkeys(dir == FORWARD
1600 ? KEY_OPEN_FORW
1601 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1602 {
1603 do_c_expr_indent();
1604 p = ml_get_curline();
1605 ai_col = (colnr_T)(skipwhite(p) - p);
1606 }
1607#endif
1608#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1609 if (vreplace_mode != 0)
1610 State = vreplace_mode;
1611#endif
1612
1613#ifdef FEAT_VREPLACE
1614 /*
1615 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1616 * original line, and inserts the new stuff char by char, pushing old stuff
1617 * onto the replace stack (via ins_char()).
1618 */
1619 if (State & VREPLACE_FLAG)
1620 {
1621 /* Put new line in p_extra */
1622 p_extra = vim_strsave(ml_get_curline());
1623 if (p_extra == NULL)
1624 goto theend;
1625
1626 /* Put back original line */
1627 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1628
1629 /* Insert new stuff into line again */
1630 curwin->w_cursor.col = 0;
1631#ifdef FEAT_VIRTUALEDIT
1632 curwin->w_cursor.coladd = 0;
1633#endif
1634 ins_bytes(p_extra); /* will call changed_bytes() */
1635 vim_free(p_extra);
1636 next_line = NULL;
1637 }
1638#endif
1639
1640 retval = TRUE; /* success! */
1641theend:
1642 curbuf->b_p_pi = saved_pi;
1643 vim_free(saved_line);
1644 vim_free(next_line);
1645 vim_free(allocated);
1646 return retval;
1647}
1648
1649#if defined(FEAT_COMMENTS) || defined(PROTO)
1650/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +02001651 * get_leader_len() returns the length in bytes of the prefix of the given
1652 * string which introduces a comment. If this string is not a comment then
1653 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 * When "flags" is not NULL, it is set to point to the flags of the recognized
1655 * comment leader.
1656 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001657 * If "include_space" is set, include trailing whitespace while calculating the
1658 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 */
1660 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001661get_leader_len(
1662 char_u *line,
1663 char_u **flags,
1664 int backward,
1665 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666{
1667 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001668 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 int got_com = FALSE;
1670 int found_one;
1671 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1672 char_u *string; /* pointer to comment string */
1673 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001674 int middle_match_len = 0;
1675 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001676 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
Bram Moolenaar81340392012-06-06 16:12:59 +02001678 result = i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 while (vim_iswhite(line[i])) /* leading white space is ignored */
1680 ++i;
1681
1682 /*
1683 * Repeat to match several nested comment strings.
1684 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001685 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 {
1687 /*
1688 * scan through the 'comments' option for a match
1689 */
1690 found_one = FALSE;
1691 for (list = curbuf->b_p_com; *list; )
1692 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001693 /* Get one option part into part_buf[]. Advance "list" to next
1694 * one. Put "string" at start of string. */
1695 if (!got_com && flags != NULL)
1696 *flags = list; /* remember where flags started */
1697 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1699 string = vim_strchr(part_buf, ':');
1700 if (string == NULL) /* missing ':', ignore this part */
1701 continue;
1702 *string++ = NUL; /* isolate flags from string */
1703
Bram Moolenaara4271d52011-05-10 13:38:27 +02001704 /* If we found a middle match previously, use that match when this
1705 * is not a middle or end. */
1706 if (middle_match_len != 0
1707 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1708 && vim_strchr(part_buf, COM_END) == NULL)
1709 break;
1710
1711 /* When we already found a nested comment, only accept further
1712 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1714 continue;
1715
Bram Moolenaara4271d52011-05-10 13:38:27 +02001716 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1718 continue;
1719
Bram Moolenaara4271d52011-05-10 13:38:27 +02001720 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 * When string starts with white space, must have some white space
1722 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001723 * TABs and spaces). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 if (vim_iswhite(string[0]))
1725 {
1726 if (i == 0 || !vim_iswhite(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001727 continue; /* missing white space */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 while (vim_iswhite(string[0]))
1729 ++string;
1730 }
1731 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1732 ;
1733 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001734 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735
Bram Moolenaara4271d52011-05-10 13:38:27 +02001736 /* When 'b' flag used, there must be white space or an
1737 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 if (vim_strchr(part_buf, COM_BLANK) != NULL
1739 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1740 continue;
1741
Bram Moolenaara4271d52011-05-10 13:38:27 +02001742 /* We have found a match, stop searching unless this is a middle
1743 * comment. The middle comment can be a substring of the end
1744 * comment in which case it's better to return the length of the
1745 * end comment and its flags. Thus we keep searching with middle
1746 * and end matches and use an end match if it matches better. */
1747 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1748 {
1749 if (middle_match_len == 0)
1750 {
1751 middle_match_len = j;
1752 saved_flags = prev_list;
1753 }
1754 continue;
1755 }
1756 if (middle_match_len != 0 && j > middle_match_len)
1757 /* Use this match instead of the middle match, since it's a
1758 * longer thus better match. */
1759 middle_match_len = 0;
1760
1761 if (middle_match_len == 0)
1762 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 found_one = TRUE;
1764 break;
1765 }
1766
Bram Moolenaara4271d52011-05-10 13:38:27 +02001767 if (middle_match_len != 0)
1768 {
1769 /* Use the previously found middle match after failing to find a
1770 * match with an end. */
1771 if (!got_com && flags != NULL)
1772 *flags = saved_flags;
1773 i += middle_match_len;
1774 found_one = TRUE;
1775 }
1776
1777 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 if (!found_one)
1779 break;
1780
Bram Moolenaar81340392012-06-06 16:12:59 +02001781 result = i;
1782
Bram Moolenaara4271d52011-05-10 13:38:27 +02001783 /* Include any trailing white space. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 while (vim_iswhite(line[i]))
1785 ++i;
1786
Bram Moolenaar81340392012-06-06 16:12:59 +02001787 if (include_space)
1788 result = i;
1789
Bram Moolenaara4271d52011-05-10 13:38:27 +02001790 /* If this comment doesn't nest, stop here. */
1791 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 if (vim_strchr(part_buf, COM_NEST) == NULL)
1793 break;
1794 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001795 return result;
1796}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001797
Bram Moolenaar81340392012-06-06 16:12:59 +02001798/*
1799 * Return the offset at which the last comment in line starts. If there is no
1800 * comment in the whole line, -1 is returned.
1801 *
1802 * When "flags" is not null, it is set to point to the flags describing the
1803 * recognized comment leader.
1804 */
1805 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001806get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +02001807{
1808 int result = -1;
1809 int i, j;
1810 int lower_check_bound = 0;
1811 char_u *string;
1812 char_u *com_leader;
1813 char_u *com_flags;
1814 char_u *list;
1815 int found_one;
1816 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1817
1818 /*
1819 * Repeat to match several nested comment strings.
1820 */
1821 i = (int)STRLEN(line);
1822 while (--i >= lower_check_bound)
1823 {
1824 /*
1825 * scan through the 'comments' option for a match
1826 */
1827 found_one = FALSE;
1828 for (list = curbuf->b_p_com; *list; )
1829 {
1830 char_u *flags_save = list;
1831
1832 /*
1833 * Get one option part into part_buf[]. Advance list to next one.
1834 * put string at start of string.
1835 */
1836 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1837 string = vim_strchr(part_buf, ':');
1838 if (string == NULL) /* If everything is fine, this cannot actually
1839 * happen. */
1840 {
1841 continue;
1842 }
1843 *string++ = NUL; /* Isolate flags from string. */
1844 com_leader = string;
1845
1846 /*
1847 * Line contents and string must match.
1848 * When string starts with white space, must have some white space
1849 * (but the amount does not need to match, there might be a mix of
1850 * TABs and spaces).
1851 */
1852 if (vim_iswhite(string[0]))
1853 {
1854 if (i == 0 || !vim_iswhite(line[i - 1]))
1855 continue;
1856 while (vim_iswhite(string[0]))
1857 ++string;
1858 }
1859 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1860 /* do nothing */;
1861 if (string[j] != NUL)
1862 continue;
1863
1864 /*
1865 * When 'b' flag used, there must be white space or an
1866 * end-of-line after the string in the line.
1867 */
1868 if (vim_strchr(part_buf, COM_BLANK) != NULL
1869 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1870 {
1871 continue;
1872 }
1873
1874 /*
1875 * We have found a match, stop searching.
1876 */
1877 found_one = TRUE;
1878
1879 if (flags)
1880 *flags = flags_save;
1881 com_flags = flags_save;
1882
1883 break;
1884 }
1885
1886 if (found_one)
1887 {
1888 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
1889 int len1, len2, off;
1890
1891 result = i;
1892 /*
1893 * If this comment nests, continue searching.
1894 */
1895 if (vim_strchr(part_buf, COM_NEST) != NULL)
1896 continue;
1897
1898 lower_check_bound = i;
1899
1900 /* Let's verify whether the comment leader found is a substring
1901 * of other comment leaders. If it is, let's adjust the
1902 * lower_check_bound so that we make sure that we have determined
1903 * the comment leader correctly.
1904 */
1905
1906 while (vim_iswhite(*com_leader))
1907 ++com_leader;
1908 len1 = (int)STRLEN(com_leader);
1909
1910 for (list = curbuf->b_p_com; *list; )
1911 {
1912 char_u *flags_save = list;
1913
1914 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
1915 if (flags_save == com_flags)
1916 continue;
1917 string = vim_strchr(part_buf2, ':');
1918 ++string;
1919 while (vim_iswhite(*string))
1920 ++string;
1921 len2 = (int)STRLEN(string);
1922 if (len2 == 0)
1923 continue;
1924
1925 /* Now we have to verify whether string ends with a substring
1926 * beginning the com_leader. */
1927 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
1928 {
1929 --off;
1930 if (!STRNCMP(string + off, com_leader, len2 - off))
1931 {
1932 if (i - off < lower_check_bound)
1933 lower_check_bound = i - off;
1934 }
1935 }
1936 }
1937 }
1938 }
1939 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940}
1941#endif
1942
1943/*
1944 * Return the number of window lines occupied by buffer line "lnum".
1945 */
1946 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001947plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948{
1949 return plines_win(curwin, lnum, TRUE);
1950}
1951
1952 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001953plines_win(
1954 win_T *wp,
1955 linenr_T lnum,
1956 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957{
1958#if defined(FEAT_DIFF) || defined(PROTO)
1959 /* Check for filler lines above this buffer line. When folded the result
1960 * is one line anyway. */
1961 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
1962}
1963
1964 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001965plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966{
1967 return plines_win_nofill(curwin, lnum, TRUE);
1968}
1969
1970 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001971plines_win_nofill(
1972 win_T *wp,
1973 linenr_T lnum,
1974 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975{
1976#endif
1977 int lines;
1978
1979 if (!wp->w_p_wrap)
1980 return 1;
1981
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001982#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 if (wp->w_width == 0)
1984 return 1;
1985#endif
1986
1987#ifdef FEAT_FOLDING
1988 /* A folded lines is handled just like an empty line. */
1989 /* NOTE: Caller must handle lines that are MAYBE folded. */
1990 if (lineFolded(wp, lnum) == TRUE)
1991 return 1;
1992#endif
1993
1994 lines = plines_win_nofold(wp, lnum);
1995 if (winheight > 0 && lines > wp->w_height)
1996 return (int)wp->w_height;
1997 return lines;
1998}
1999
2000/*
2001 * Return number of window lines physical line "lnum" will occupy in window
2002 * "wp". Does not care about folding, 'wrap' or 'diff'.
2003 */
2004 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002005plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006{
2007 char_u *s;
2008 long col;
2009 int width;
2010
2011 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2012 if (*s == NUL) /* empty line */
2013 return 1;
2014 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
2015
2016 /*
2017 * If list mode is on, then the '$' at the end of the line may take up one
2018 * extra column.
2019 */
2020 if (wp->w_p_list && lcs_eol != NUL)
2021 col += 1;
2022
2023 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002024 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 */
2026 width = W_WIDTH(wp) - win_col_off(wp);
2027 if (width <= 0)
2028 return 32000;
2029 if (col <= width)
2030 return 1;
2031 col -= width;
2032 width += win_col_off2(wp);
2033 return (col + (width - 1)) / width + 1;
2034}
2035
2036/*
2037 * Like plines_win(), but only reports the number of physical screen lines
2038 * used from the start of the line to the given column number.
2039 */
2040 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002041plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042{
2043 long col;
2044 char_u *s;
2045 int lines = 0;
2046 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002047 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048
2049#ifdef FEAT_DIFF
2050 /* Check for filler lines above this buffer line. When folded the result
2051 * is one line anyway. */
2052 lines = diff_check_fill(wp, lnum);
2053#endif
2054
2055 if (!wp->w_p_wrap)
2056 return lines + 1;
2057
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002058#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 if (wp->w_width == 0)
2060 return lines + 1;
2061#endif
2062
Bram Moolenaar597a4222014-06-25 14:39:50 +02002063 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064
2065 col = 0;
2066 while (*s != NUL && --column >= 0)
2067 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02002068 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002069 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 }
2071
2072 /*
2073 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2074 * INSERT mode, then col must be adjusted so that it represents the last
2075 * screen position of the TAB. This only fixes an error when the TAB wraps
2076 * from one screen line to the next (when 'columns' is not a multiple of
2077 * 'ts') -- webb.
2078 */
2079 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02002080 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081
2082 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002083 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 */
2085 width = W_WIDTH(wp) - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002086 if (width <= 0)
2087 return 9999;
2088
2089 lines += 1;
2090 if (col > width)
2091 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2092 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093}
2094
2095 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002096plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097{
2098 int count = 0;
2099
2100 while (first <= last)
2101 {
2102#ifdef FEAT_FOLDING
2103 int x;
2104
2105 /* Check if there are any really folded lines, but also included lines
2106 * that are maybe folded. */
2107 x = foldedCount(wp, first, NULL);
2108 if (x > 0)
2109 {
2110 ++count; /* count 1 for "+-- folded" line */
2111 first += x;
2112 }
2113 else
2114#endif
2115 {
2116#ifdef FEAT_DIFF
2117 if (first == wp->w_topline)
2118 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2119 else
2120#endif
2121 count += plines_win(wp, first, TRUE);
2122 ++first;
2123 }
2124 }
2125 return (count);
2126}
2127
2128#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO)
2129/*
2130 * Insert string "p" at the cursor position. Stops at a NUL byte.
2131 * Handles Replace mode and multi-byte characters.
2132 */
2133 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002134ins_bytes(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135{
2136 ins_bytes_len(p, (int)STRLEN(p));
2137}
2138#endif
2139
2140#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2141 || defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO)
2142/*
2143 * Insert string "p" with length "len" at the cursor position.
2144 * Handles Replace mode and multi-byte characters.
2145 */
2146 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002147ins_bytes_len(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148{
2149 int i;
2150# ifdef FEAT_MBYTE
2151 int n;
2152
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002153 if (has_mbyte)
2154 for (i = 0; i < len; i += n)
2155 {
2156 if (enc_utf8)
2157 /* avoid reading past p[len] */
2158 n = utfc_ptr2len_len(p + i, len - i);
2159 else
2160 n = (*mb_ptr2len)(p + i);
2161 ins_char_bytes(p + i, n);
2162 }
2163 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164# endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002165 for (i = 0; i < len; ++i)
2166 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167}
2168#endif
2169
2170/*
2171 * Insert or replace a single character at the cursor position.
2172 * When in REPLACE or VREPLACE mode, replace any existing character.
2173 * Caller must have prepared for undo.
2174 * For multi-byte characters we get the whole character, the caller must
2175 * convert bytes to a character.
2176 */
2177 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002178ins_char(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002180 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar560379d2017-01-21 22:50:00 +01002181 int n = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182
Bram Moolenaar6a8ede92017-01-22 19:49:12 +01002183#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 n = (*mb_char2bytes)(c, buf);
2185
2186 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2187 * Happens for CTRL-Vu9900. */
2188 if (buf[0] == 0)
2189 buf[0] = '\n';
Bram Moolenaar560379d2017-01-21 22:50:00 +01002190#else
2191 buf[0] = c;
2192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193
2194 ins_char_bytes(buf, n);
2195}
2196
2197 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002198ins_char_bytes(char_u *buf, int charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199{
2200 int c = buf[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 int newlen; /* nr of bytes inserted */
2202 int oldlen; /* nr of bytes deleted (0 when not replacing) */
2203 char_u *p;
2204 char_u *newp;
2205 char_u *oldp;
2206 int linelen; /* length of old line including NUL */
2207 colnr_T col;
2208 linenr_T lnum = curwin->w_cursor.lnum;
2209 int i;
2210
2211#ifdef FEAT_VIRTUALEDIT
2212 /* Break tabs if needed. */
2213 if (virtual_active() && curwin->w_cursor.coladd > 0)
2214 coladvance_force(getviscol());
2215#endif
2216
2217 col = curwin->w_cursor.col;
2218 oldp = ml_get(lnum);
2219 linelen = (int)STRLEN(oldp) + 1;
2220
2221 /* The lengths default to the values for when not replacing. */
2222 oldlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 newlen = charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224
2225 if (State & REPLACE_FLAG)
2226 {
2227#ifdef FEAT_VREPLACE
2228 if (State & VREPLACE_FLAG)
2229 {
2230 colnr_T new_vcol = 0; /* init for GCC */
2231 colnr_T vcol;
2232 int old_list;
2233#ifndef FEAT_MBYTE
2234 char_u buf[2];
2235#endif
2236
2237 /*
2238 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2239 * Returns the old value of list, so when finished,
2240 * curwin->w_p_list should be set back to this.
2241 */
2242 old_list = curwin->w_p_list;
2243 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2244 curwin->w_p_list = FALSE;
2245
2246 /*
2247 * In virtual replace mode each character may replace one or more
2248 * characters (zero if it's a TAB). Count the number of bytes to
2249 * be deleted to make room for the new character, counting screen
2250 * cells. May result in adding spaces to fill a gap.
2251 */
2252 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2253#ifndef FEAT_MBYTE
2254 buf[0] = c;
2255 buf[1] = NUL;
2256#endif
2257 new_vcol = vcol + chartabsize(buf, vcol);
2258 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2259 {
2260 vcol += chartabsize(oldp + col + oldlen, vcol);
2261 /* Don't need to remove a TAB that takes us to the right
2262 * position. */
2263 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2264 break;
2265#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002266 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267#else
2268 ++oldlen;
2269#endif
2270 /* Deleted a bit too much, insert spaces. */
2271 if (vcol > new_vcol)
2272 newlen += vcol - new_vcol;
2273 }
2274 curwin->w_p_list = old_list;
2275 }
2276 else
2277#endif
2278 if (oldp[col] != NUL)
2279 {
2280 /* normal replace */
2281#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002282 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283#else
2284 oldlen = 1;
2285#endif
2286 }
2287
2288
2289 /* Push the replaced bytes onto the replace stack, so that they can be
2290 * put back when BS is used. The bytes of a multi-byte character are
2291 * done the other way around, so that the first byte is popped off
2292 * first (it tells the byte length of the character). */
2293 replace_push(NUL);
2294 for (i = 0; i < oldlen; ++i)
2295 {
2296#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002297 if (has_mbyte)
2298 i += replace_push_mb(oldp + col + i) - 1;
2299 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002301 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 }
2303 }
2304
2305 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2306 if (newp == NULL)
2307 return;
2308
2309 /* Copy bytes before the cursor. */
2310 if (col > 0)
2311 mch_memmove(newp, oldp, (size_t)col);
2312
2313 /* Copy bytes after the changed character(s). */
2314 p = newp + col;
2315 mch_memmove(p + newlen, oldp + col + oldlen,
2316 (size_t)(linelen - col - oldlen));
2317
2318 /* Insert or overwrite the new character. */
2319#ifdef FEAT_MBYTE
2320 mch_memmove(p, buf, charlen);
2321 i = charlen;
2322#else
2323 *p = c;
2324 i = 1;
2325#endif
2326
2327 /* Fill with spaces when necessary. */
2328 while (i < newlen)
2329 p[i++] = ' ';
2330
2331 /* Replace the line in the buffer. */
2332 ml_replace(lnum, newp, FALSE);
2333
2334 /* mark the buffer as changed and prepare for displaying */
2335 changed_bytes(lnum, col);
2336
2337 /*
2338 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2339 * show the match for right parens and braces.
2340 */
2341 if (p_sm && (State & INSERT)
2342 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002343#ifdef FEAT_INS_EXPAND
2344 && !ins_compl_active()
2345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002347 {
2348#ifdef FEAT_MBYTE
2349 if (has_mbyte)
2350 showmatch(mb_ptr2char(buf));
2351 else
2352#endif
2353 showmatch(c);
2354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355
2356#ifdef FEAT_RIGHTLEFT
2357 if (!p_ri || (State & REPLACE_FLAG))
2358#endif
2359 {
2360 /* Normal insert: move cursor right */
2361#ifdef FEAT_MBYTE
2362 curwin->w_cursor.col += charlen;
2363#else
2364 ++curwin->w_cursor.col;
2365#endif
2366 }
2367 /*
2368 * TODO: should try to update w_row here, to avoid recomputing it later.
2369 */
2370}
2371
2372/*
2373 * Insert a string at the cursor position.
2374 * Note: Does NOT handle Replace mode.
2375 * Caller must have prepared for undo.
2376 */
2377 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002378ins_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379{
2380 char_u *oldp, *newp;
2381 int newlen = (int)STRLEN(s);
2382 int oldlen;
2383 colnr_T col;
2384 linenr_T lnum = curwin->w_cursor.lnum;
2385
2386#ifdef FEAT_VIRTUALEDIT
2387 if (virtual_active() && curwin->w_cursor.coladd > 0)
2388 coladvance_force(getviscol());
2389#endif
2390
2391 col = curwin->w_cursor.col;
2392 oldp = ml_get(lnum);
2393 oldlen = (int)STRLEN(oldp);
2394
2395 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2396 if (newp == NULL)
2397 return;
2398 if (col > 0)
2399 mch_memmove(newp, oldp, (size_t)col);
2400 mch_memmove(newp + col, s, (size_t)newlen);
2401 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2402 ml_replace(lnum, newp, FALSE);
2403 changed_bytes(lnum, col);
2404 curwin->w_cursor.col += newlen;
2405}
2406
2407/*
2408 * Delete one character under the cursor.
2409 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2410 * Caller must have prepared for undo.
2411 *
2412 * return FAIL for failure, OK otherwise
2413 */
2414 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002415del_char(int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416{
2417#ifdef FEAT_MBYTE
2418 if (has_mbyte)
2419 {
2420 /* Make sure the cursor is at the start of a character. */
2421 mb_adjust_cursor();
2422 if (*ml_get_cursor() == NUL)
2423 return FAIL;
2424 return del_chars(1L, fixpos);
2425 }
2426#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002427 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428}
2429
2430#if defined(FEAT_MBYTE) || defined(PROTO)
2431/*
2432 * Like del_bytes(), but delete characters instead of bytes.
2433 */
2434 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002435del_chars(long count, int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436{
2437 long bytes = 0;
2438 long i;
2439 char_u *p;
2440 int l;
2441
2442 p = ml_get_cursor();
2443 for (i = 0; i < count && *p != NUL; ++i)
2444 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002445 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 bytes += l;
2447 p += l;
2448 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002449 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450}
2451#endif
2452
2453/*
2454 * Delete "count" bytes under the cursor.
2455 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2456 * Caller must have prepared for undo.
2457 *
2458 * return FAIL for failure, OK otherwise
2459 */
2460 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002461del_bytes(
2462 long count,
2463 int fixpos_arg,
2464 int use_delcombine UNUSED) /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465{
2466 char_u *oldp, *newp;
2467 colnr_T oldlen;
2468 linenr_T lnum = curwin->w_cursor.lnum;
2469 colnr_T col = curwin->w_cursor.col;
2470 int was_alloced;
2471 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002472 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473
2474 oldp = ml_get(lnum);
2475 oldlen = (int)STRLEN(oldp);
2476
2477 /*
2478 * Can't do anything when the cursor is on the NUL after the line.
2479 */
2480 if (col >= oldlen)
2481 return FAIL;
2482
2483#ifdef FEAT_MBYTE
2484 /* If 'delcombine' is set and deleting (less than) one character, only
2485 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002486 if (p_deco && use_delcombine && enc_utf8
2487 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002489 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 int n;
2491
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002492 (void)utfc_ptr2char(oldp + col, cc);
2493 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 {
2495 /* Find the last composing char, there can be several. */
2496 n = col;
2497 do
2498 {
2499 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002500 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 n += count;
2502 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2503 fixpos = 0;
2504 }
2505 }
2506#endif
2507
2508 /*
2509 * When count is too big, reduce it.
2510 */
2511 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2512 if (movelen <= 1)
2513 {
2514 /*
2515 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002516 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2517 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002519 if (col > 0 && fixpos && restart_edit == 0
2520#ifdef FEAT_VIRTUALEDIT
2521 && (ve_flags & VE_ONEMORE) == 0
2522#endif
2523 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 {
2525 --curwin->w_cursor.col;
2526#ifdef FEAT_VIRTUALEDIT
2527 curwin->w_cursor.coladd = 0;
2528#endif
2529#ifdef FEAT_MBYTE
2530 if (has_mbyte)
2531 curwin->w_cursor.col -=
2532 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2533#endif
2534 }
2535 count = oldlen - col;
2536 movelen = 1;
2537 }
2538
2539 /*
2540 * If the old line has been allocated the deletion can be done in the
2541 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002542 * Can't do this when using Netbeans, because we would need to invoke
2543 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002544 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002547 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002548 was_alloced = FALSE;
2549 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002551 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 if (was_alloced)
2553 newp = oldp; /* use same allocated memory */
2554 else
2555 { /* need to allocate a new line */
2556 newp = alloc((unsigned)(oldlen + 1 - count));
2557 if (newp == NULL)
2558 return FAIL;
2559 mch_memmove(newp, oldp, (size_t)col);
2560 }
2561 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2562 if (!was_alloced)
2563 ml_replace(lnum, newp, FALSE);
2564
2565 /* mark the buffer as changed and prepare for displaying */
2566 changed_bytes(lnum, curwin->w_cursor.col);
2567
2568 return OK;
2569}
2570
2571/*
2572 * Delete from cursor to end of line.
2573 * Caller must have prepared for undo.
2574 *
2575 * return FAIL for failure, OK otherwise
2576 */
2577 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002578truncate_line(
2579 int fixpos) /* if TRUE fix the cursor position when done */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580{
2581 char_u *newp;
2582 linenr_T lnum = curwin->w_cursor.lnum;
2583 colnr_T col = curwin->w_cursor.col;
2584
2585 if (col == 0)
2586 newp = vim_strsave((char_u *)"");
2587 else
2588 newp = vim_strnsave(ml_get(lnum), col);
2589
2590 if (newp == NULL)
2591 return FAIL;
2592
2593 ml_replace(lnum, newp, FALSE);
2594
2595 /* mark the buffer as changed and prepare for displaying */
2596 changed_bytes(lnum, curwin->w_cursor.col);
2597
2598 /*
2599 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2600 */
2601 if (fixpos && curwin->w_cursor.col > 0)
2602 --curwin->w_cursor.col;
2603
2604 return OK;
2605}
2606
2607/*
2608 * Delete "nlines" lines at the cursor.
2609 * Saves the lines for undo first if "undo" is TRUE.
2610 */
2611 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002612del_lines(
2613 long nlines, /* number of lines to delete */
2614 int undo) /* if TRUE, prepare for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615{
2616 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002617 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618
2619 if (nlines <= 0)
2620 return;
2621
2622 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002623 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 return;
2625
2626 for (n = 0; n < nlines; )
2627 {
2628 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2629 break;
2630
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002631 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 ++n;
2633
2634 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002635 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 break;
2637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002639 /* Correct the cursor position before calling deleted_lines_mark(), it may
2640 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 curwin->w_cursor.col = 0;
2642 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002643
2644 /* adjust marks, mark the buffer as changed and prepare for displaying */
2645 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646}
2647
2648 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002649gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650{
2651 char_u *ptr = ml_get_pos(pos);
2652
2653#ifdef FEAT_MBYTE
2654 if (has_mbyte)
2655 return (*mb_ptr2char)(ptr);
2656#endif
2657 return (int)*ptr;
2658}
2659
2660 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002661gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662{
2663#ifdef FEAT_MBYTE
2664 if (has_mbyte)
2665 return (*mb_ptr2char)(ml_get_cursor());
2666#endif
2667 return (int)*ml_get_cursor();
2668}
2669
2670/*
2671 * Write a character at the current cursor position.
2672 * It is directly written into the block.
2673 */
2674 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002675pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676{
2677 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2678 + curwin->w_cursor.col) = c;
2679}
2680
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681/*
2682 * When extra == 0: Return TRUE if the cursor is before or on the first
2683 * non-blank in the line.
2684 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2685 * the line.
2686 */
2687 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002688inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689{
2690 char_u *ptr;
2691 colnr_T col;
2692
2693 for (col = 0, ptr = ml_get_curline(); vim_iswhite(*ptr); ++col)
2694 ++ptr;
2695 if (col >= curwin->w_cursor.col + extra)
2696 return TRUE;
2697 else
2698 return FALSE;
2699}
2700
2701/*
2702 * Skip to next part of an option argument: Skip space and comma.
2703 */
2704 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002705skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706{
2707 if (*p == ',')
2708 ++p;
2709 while (*p == ' ')
2710 ++p;
2711 return p;
2712}
2713
2714/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002715 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 *
2717 * Most often called through changed_bytes() and changed_lines(), which also
2718 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002719 *
2720 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 */
2722 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002723changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724{
2725#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2726 /* The text of the preediting area is inserted, but this doesn't
2727 * mean a change of the buffer yet. That is delayed until the
2728 * text is committed. (this means preedit becomes empty) */
2729 if (im_is_preediting() && !xim_changed_while_preediting)
2730 return;
2731 xim_changed_while_preediting = FALSE;
2732#endif
2733
2734 if (!curbuf->b_changed)
2735 {
2736 int save_msg_scroll = msg_scroll;
2737
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002738 /* Give a warning about changing a read-only file. This may also
2739 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002741
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 /* Create a swap file if that is wanted.
2743 * Don't do this for "nofile" and "nowrite" buffer types. */
2744 if (curbuf->b_may_swap
2745#ifdef FEAT_QUICKFIX
2746 && !bt_dontwrite(curbuf)
2747#endif
2748 )
2749 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002750 int save_need_wait_return = need_wait_return;
2751
2752 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 ml_open_file(curbuf);
2754
2755 /* The ml_open_file() can cause an ATTENTION message.
2756 * Wait two seconds, to make sure the user reads this unexpected
2757 * message. Since we could be anywhere, call wait_return() now,
2758 * and don't let the emsg() set msg_scroll. */
2759 if (need_wait_return && emsg_silent == 0)
2760 {
2761 out_flush();
2762 ui_delay(2000L, TRUE);
2763 wait_return(TRUE);
2764 msg_scroll = save_msg_scroll;
2765 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002766 else
2767 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002769 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002771 ++*curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772}
2773
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002774/*
2775 * Internal part of changed(), no user interaction.
2776 */
2777 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002778changed_int(void)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002779{
2780 curbuf->b_changed = TRUE;
2781 ml_setflags(curbuf);
2782#ifdef FEAT_WINDOWS
2783 check_status(curbuf);
2784 redraw_tabline = TRUE;
2785#endif
2786#ifdef FEAT_TITLE
2787 need_maketitle = TRUE; /* set window title later */
2788#endif
2789}
2790
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002791static void changedOneline(buf_T *buf, linenr_T lnum);
2792static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
2793static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794
2795/*
2796 * Changed bytes within a single line for the current buffer.
2797 * - marks the windows on this buffer to be redisplayed
2798 * - marks the buffer changed by calling changed()
2799 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002800 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 */
2802 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002803changed_bytes(linenr_T lnum, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002805 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002807
2808#ifdef FEAT_DIFF
2809 /* Diff highlighting in other diff windows may need to be updated too. */
2810 if (curwin->w_p_diff)
2811 {
2812 win_T *wp;
2813 linenr_T wlnum;
2814
Bram Moolenaar29323592016-07-24 22:04:11 +02002815 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002816 if (wp->w_p_diff && wp != curwin)
2817 {
2818 redraw_win_later(wp, VALID);
2819 wlnum = diff_lnum_win(lnum, wp);
2820 if (wlnum > 0)
2821 changedOneline(wp->w_buffer, wlnum);
2822 }
2823 }
2824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825}
2826
2827 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002828changedOneline(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002830 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 {
2832 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002833 if (lnum < buf->b_mod_top)
2834 buf->b_mod_top = lnum;
2835 else if (lnum >= buf->b_mod_bot)
2836 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 }
2838 else
2839 {
2840 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002841 buf->b_mod_set = TRUE;
2842 buf->b_mod_top = lnum;
2843 buf->b_mod_bot = lnum + 1;
2844 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 }
2846}
2847
2848/*
2849 * Appended "count" lines below line "lnum" in the current buffer.
2850 * Must be called AFTER the change and after mark_adjust().
2851 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2852 */
2853 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002854appended_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855{
2856 changed_lines(lnum + 1, 0, lnum + 1, count);
2857}
2858
2859/*
2860 * Like appended_lines(), but adjust marks first.
2861 */
2862 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002863appended_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864{
Bram Moolenaar82faa252016-06-04 20:14:07 +02002865 /* Skip mark_adjust when adding a line after the last one, there can't
2866 * be marks there. */
2867 if (lnum + count < curbuf->b_ml.ml_line_count)
2868 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 changed_lines(lnum + 1, 0, lnum + 1, count);
2870}
2871
2872/*
2873 * Deleted "count" lines at line "lnum" in the current buffer.
2874 * Must be called AFTER the change and after mark_adjust().
2875 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2876 */
2877 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002878deleted_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879{
2880 changed_lines(lnum, 0, lnum + count, -count);
2881}
2882
2883/*
2884 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002885 * Make sure the cursor is on a valid line before calling, a GUI callback may
2886 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 */
2888 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002889deleted_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890{
2891 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
2892 changed_lines(lnum, 0, lnum + count, -count);
2893}
2894
2895/*
2896 * Changed lines for the current buffer.
2897 * Must be called AFTER the change and after mark_adjust().
2898 * - mark the buffer changed by calling changed()
2899 * - mark the windows on this buffer to be redisplayed
2900 * - invalidate cached values
2901 * "lnum" is the first line that needs displaying, "lnume" the first line
2902 * below the changed lines (BEFORE the change).
2903 * When only inserting lines, "lnum" and "lnume" are equal.
2904 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002905 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 */
2907 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002908changed_lines(
2909 linenr_T lnum, /* first line with change */
2910 colnr_T col, /* column in first line with change */
2911 linenr_T lnume, /* line below last changed line */
2912 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002914 changed_lines_buf(curbuf, lnum, lnume, xtra);
2915
2916#ifdef FEAT_DIFF
2917 if (xtra == 0 && curwin->w_p_diff)
2918 {
2919 /* When the number of lines doesn't change then mark_adjust() isn't
2920 * called and other diff buffers still need to be marked for
2921 * displaying. */
2922 win_T *wp;
2923 linenr_T wlnum;
2924
Bram Moolenaar29323592016-07-24 22:04:11 +02002925 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002926 if (wp->w_p_diff && wp != curwin)
2927 {
2928 redraw_win_later(wp, VALID);
2929 wlnum = diff_lnum_win(lnum, wp);
2930 if (wlnum > 0)
2931 changed_lines_buf(wp->w_buffer, wlnum,
2932 lnume - lnum + wlnum, 0L);
2933 }
2934 }
2935#endif
2936
2937 changed_common(lnum, col, lnume, xtra);
2938}
2939
2940 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002941changed_lines_buf(
2942 buf_T *buf,
2943 linenr_T lnum, /* first line with change */
2944 linenr_T lnume, /* line below last changed line */
2945 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002946{
2947 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 {
2949 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002950 if (lnum < buf->b_mod_top)
2951 buf->b_mod_top = lnum;
2952 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 {
2954 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002955 buf->b_mod_bot += xtra;
2956 if (buf->b_mod_bot < lnum)
2957 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00002959 if (lnume + xtra > buf->b_mod_bot)
2960 buf->b_mod_bot = lnume + xtra;
2961 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 }
2963 else
2964 {
2965 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002966 buf->b_mod_set = TRUE;
2967 buf->b_mod_top = lnum;
2968 buf->b_mod_bot = lnume + xtra;
2969 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971}
2972
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002973/*
2974 * Common code for when a change is was made.
2975 * See changed_lines() for the arguments.
2976 * Careful: may trigger autocommands that reload the buffer.
2977 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002979changed_common(
2980 linenr_T lnum,
2981 colnr_T col,
2982 linenr_T lnume,
2983 long xtra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984{
2985 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00002986#ifdef FEAT_WINDOWS
2987 tabpage_T *tp;
2988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 int i;
2990#ifdef FEAT_JUMPLIST
2991 int cols;
2992 pos_T *p;
2993 int add;
2994#endif
2995
2996 /* mark the buffer as modified */
2997 changed();
2998
2999 /* set the '. mark */
3000 if (!cmdmod.keepjumps)
3001 {
3002 curbuf->b_last_change.lnum = lnum;
3003 curbuf->b_last_change.col = col;
3004
3005#ifdef FEAT_JUMPLIST
3006 /* Create a new entry if a new undo-able change was started or we
3007 * don't have an entry yet. */
3008 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3009 {
3010 if (curbuf->b_changelistlen == 0)
3011 add = TRUE;
3012 else
3013 {
3014 /* Don't create a new entry when the line number is the same
3015 * as the last one and the column is not too far away. Avoids
3016 * creating many entries for typing "xxxxx". */
3017 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3018 if (p->lnum != lnum)
3019 add = TRUE;
3020 else
3021 {
3022 cols = comp_textwidth(FALSE);
3023 if (cols == 0)
3024 cols = 79;
3025 add = (p->col + cols < col || col + cols < p->col);
3026 }
3027 }
3028 if (add)
3029 {
3030 /* This is the first of a new sequence of undo-able changes
3031 * and it's at some distance of the last change. Use a new
3032 * position in the changelist. */
3033 curbuf->b_new_change = FALSE;
3034
3035 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3036 {
3037 /* changelist is full: remove oldest entry */
3038 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3039 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3040 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003041 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 {
3043 /* Correct position in changelist for other windows on
3044 * this buffer. */
3045 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3046 --wp->w_changelistidx;
3047 }
3048 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003049 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 {
3051 /* For other windows, if the position in the changelist is
3052 * at the end it stays at the end. */
3053 if (wp->w_buffer == curbuf
3054 && wp->w_changelistidx == curbuf->b_changelistlen)
3055 ++wp->w_changelistidx;
3056 }
3057 ++curbuf->b_changelistlen;
3058 }
3059 }
3060 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3061 curbuf->b_last_change;
3062 /* The current window is always after the last change, so that "g,"
3063 * takes you back to it. */
3064 curwin->w_changelistidx = curbuf->b_changelistlen;
3065#endif
3066 }
3067
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003068 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 {
3070 if (wp->w_buffer == curbuf)
3071 {
3072 /* Mark this window to be redrawn later. */
3073 if (wp->w_redr_type < VALID)
3074 wp->w_redr_type = VALID;
3075
3076 /* Check if a change in the buffer has invalidated the cached
3077 * values for the cursor. */
3078#ifdef FEAT_FOLDING
3079 /*
3080 * Update the folds for this window. Can't postpone this, because
3081 * a following operator might work on the whole fold: ">>dd".
3082 */
3083 foldUpdate(wp, lnum, lnume + xtra - 1);
3084
3085 /* The change may cause lines above or below the change to become
3086 * included in a fold. Set lnum/lnume to the first/last line that
3087 * might be displayed differently.
3088 * Set w_cline_folded here as an efficient way to update it when
3089 * inserting lines just above a closed fold. */
3090 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3091 if (wp->w_cursor.lnum == lnum)
3092 wp->w_cline_folded = i;
3093 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3094 if (wp->w_cursor.lnum == lnume)
3095 wp->w_cline_folded = i;
3096
3097 /* If the changed line is in a range of previously folded lines,
3098 * compare with the first line in that range. */
3099 if (wp->w_cursor.lnum <= lnum)
3100 {
3101 i = find_wl_entry(wp, lnum);
3102 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3103 changed_line_abv_curs_win(wp);
3104 }
3105#endif
3106
3107 if (wp->w_cursor.lnum > lnum)
3108 changed_line_abv_curs_win(wp);
3109 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3110 changed_cline_bef_curs_win(wp);
3111 if (wp->w_botline >= lnum)
3112 {
3113 /* Assume that botline doesn't change (inserted lines make
3114 * other lines scroll down below botline). */
3115 approximate_botline_win(wp);
3116 }
3117
3118 /* Check if any w_lines[] entries have become invalid.
3119 * For entries below the change: Correct the lnums for
3120 * inserted/deleted lines. Makes it possible to stop displaying
3121 * after the change. */
3122 for (i = 0; i < wp->w_lines_valid; ++i)
3123 if (wp->w_lines[i].wl_valid)
3124 {
3125 if (wp->w_lines[i].wl_lnum >= lnum)
3126 {
3127 if (wp->w_lines[i].wl_lnum < lnume)
3128 {
3129 /* line included in change */
3130 wp->w_lines[i].wl_valid = FALSE;
3131 }
3132 else if (xtra != 0)
3133 {
3134 /* line below change */
3135 wp->w_lines[i].wl_lnum += xtra;
3136#ifdef FEAT_FOLDING
3137 wp->w_lines[i].wl_lastlnum += xtra;
3138#endif
3139 }
3140 }
3141#ifdef FEAT_FOLDING
3142 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3143 {
3144 /* change somewhere inside this range of folded lines,
3145 * may need to be redrawn */
3146 wp->w_lines[i].wl_valid = FALSE;
3147 }
3148#endif
3149 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003150
3151#ifdef FEAT_FOLDING
3152 /* Take care of side effects for setting w_topline when folds have
3153 * changed. Esp. when the buffer was changed in another window. */
3154 if (hasAnyFolding(wp))
3155 set_topline(wp, wp->w_topline);
3156#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003157 /* relative numbering may require updating more */
3158 if (wp->w_p_rnu)
3159 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 }
3161 }
3162
3163 /* Call update_screen() later, which checks out what needs to be redrawn,
3164 * since it notices b_mod_set and then uses b_mod_*. */
3165 if (must_redraw < VALID)
3166 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003167
3168#ifdef FEAT_AUTOCMD
3169 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003170 if (lnum <= curwin->w_cursor.lnum
3171 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003172 last_cursormoved.lnum = 0;
3173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174}
3175
3176/*
3177 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3178 */
3179 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003180unchanged(
3181 buf_T *buf,
3182 int ff) /* also reset 'fileformat' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003184 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 {
3186 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003187 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 if (ff)
3189 save_file_ff(buf);
3190#ifdef FEAT_WINDOWS
3191 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003192 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193#endif
3194#ifdef FEAT_TITLE
3195 need_maketitle = TRUE; /* set window title later */
3196#endif
3197 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01003198 ++*buf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199#ifdef FEAT_NETBEANS_INTG
3200 netbeans_unmodified(buf);
3201#endif
3202}
3203
3204#if defined(FEAT_WINDOWS) || defined(PROTO)
3205/*
3206 * check_status: called when the status bars for the buffer 'buf'
3207 * need to be updated
3208 */
3209 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003210check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211{
3212 win_T *wp;
3213
Bram Moolenaar29323592016-07-24 22:04:11 +02003214 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 if (wp->w_buffer == buf && wp->w_status_height)
3216 {
3217 wp->w_redr_status = TRUE;
3218 if (must_redraw < VALID)
3219 must_redraw = VALID;
3220 }
3221}
3222#endif
3223
3224/*
3225 * If the file is readonly, give a warning message with the first change.
3226 * Don't do this for autocommands.
3227 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003228 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003230 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 */
3232 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003233change_warning(
3234 int col) /* column for message; non-zero when in insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 mode and 'showmode' is on */
3236{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003237 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3238
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 if (curbuf->b_did_warn == FALSE
3240 && curbufIsChanged() == 0
3241#ifdef FEAT_AUTOCMD
3242 && !autocmd_busy
3243#endif
3244 && curbuf->b_p_ro)
3245 {
3246#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003247 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003249 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 if (!curbuf->b_p_ro)
3251 return;
3252#endif
3253 /*
3254 * Do what msg() does, but with a column offset if the warning should
3255 * be after the mode message.
3256 */
3257 msg_start();
3258 if (msg_row == Rows - 1)
3259 msg_col = col;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003260 msg_source(hl_attr(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003261 MSG_PUTS_ATTR(_(w_readonly), hl_attr(HLF_W) | MSG_HIST);
3262#ifdef FEAT_EVAL
3263 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 msg_clr_eos();
3266 (void)msg_end();
Bram Moolenaare5f2a072017-02-01 22:31:49 +01003267 if (msg_silent == 0 && !silent_mode
3268#ifdef FEAT_EVAL
3269 && time_for_testing != 1
3270#endif
3271 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 {
3273 out_flush();
3274 ui_delay(1000L, TRUE); /* give the user time to think about it */
3275 }
3276 curbuf->b_did_warn = TRUE;
3277 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3278 if (msg_row < Rows - 1)
3279 showmode();
3280 }
3281}
3282
3283/*
3284 * Ask for a reply from the user, a 'y' or a 'n'.
3285 * No other characters are accepted, the message is repeated until a valid
3286 * reply is entered or CTRL-C is hit.
3287 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3288 * from any buffers but directly from the user.
3289 *
3290 * return the 'y' or 'n'
3291 */
3292 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003293ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294{
3295 int r = ' ';
3296 int save_State = State;
3297
3298 if (exiting) /* put terminal in raw mode for this question */
3299 settmode(TMODE_RAW);
3300 ++no_wait_return;
3301#ifdef USE_ON_FLY_SCROLL
3302 dont_scroll = TRUE; /* disallow scrolling here */
3303#endif
3304 State = CONFIRM; /* mouse behaves like with :confirm */
3305#ifdef FEAT_MOUSE
3306 setmouse(); /* disables mouse for xterm */
3307#endif
3308 ++no_mapping;
3309 ++allow_keys; /* no mapping here, but recognize keys */
3310
3311 while (r != 'y' && r != 'n')
3312 {
3313 /* same highlighting as for wait_return */
3314 smsg_attr(hl_attr(HLF_R), (char_u *)"%s (y/n)?", str);
3315 if (direct)
3316 r = get_keystroke();
3317 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003318 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 if (r == Ctrl_C || r == ESC)
3320 r = 'n';
3321 msg_putchar(r); /* show what you typed */
3322 out_flush();
3323 }
3324 --no_wait_return;
3325 State = save_State;
3326#ifdef FEAT_MOUSE
3327 setmouse();
3328#endif
3329 --no_mapping;
3330 --allow_keys;
3331
3332 return r;
3333}
3334
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003335#if defined(FEAT_MOUSE) || defined(PROTO)
3336/*
3337 * Return TRUE if "c" is a mouse key.
3338 */
3339 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003340is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003341{
3342 return c == K_LEFTMOUSE
3343 || c == K_LEFTMOUSE_NM
3344 || c == K_LEFTDRAG
3345 || c == K_LEFTRELEASE
3346 || c == K_LEFTRELEASE_NM
3347 || c == K_MIDDLEMOUSE
3348 || c == K_MIDDLEDRAG
3349 || c == K_MIDDLERELEASE
3350 || c == K_RIGHTMOUSE
3351 || c == K_RIGHTDRAG
3352 || c == K_RIGHTRELEASE
3353 || c == K_MOUSEDOWN
3354 || c == K_MOUSEUP
3355 || c == K_MOUSELEFT
3356 || c == K_MOUSERIGHT
3357 || c == K_X1MOUSE
3358 || c == K_X1DRAG
3359 || c == K_X1RELEASE
3360 || c == K_X2MOUSE
3361 || c == K_X2DRAG
3362 || c == K_X2RELEASE;
3363}
3364#endif
3365
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366/*
3367 * Get a key stroke directly from the user.
3368 * Ignores mouse clicks and scrollbar events, except a click for the left
3369 * button (used at the more prompt).
3370 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3371 * Disadvantage: typeahead is ignored.
3372 * Translates the interrupt character for unix to ESC.
3373 */
3374 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003375get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003377 char_u *buf = NULL;
3378 int buflen = 150;
3379 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 int len = 0;
3381 int n;
3382 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003383 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384
3385 mapped_ctrl_c = FALSE; /* mappings are not used here */
3386 for (;;)
3387 {
3388 cursor_on();
3389 out_flush();
3390
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003391 /* Leave some room for check_termcode() to insert a key code into (max
3392 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3393 * bytes. */
3394 maxlen = (buflen - 6 - len) / 3;
3395 if (buf == NULL)
3396 buf = alloc(buflen);
3397 else if (maxlen < 10)
3398 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003399 char_u *t_buf = buf;
3400
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003401 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003402 * escape sequence. */
3403 buflen += 100;
3404 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003405 if (buf == NULL)
3406 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003407 maxlen = (buflen - 6 - len) / 3;
3408 }
3409 if (buf == NULL)
3410 {
3411 do_outofmem_msg((long_u)buflen);
3412 return ESC; /* panic! */
3413 }
3414
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003416 * terminal code to complete. */
3417 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 if (n > 0)
3419 {
3420 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003421 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003423 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003425 else if (len > 0)
3426 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427
Bram Moolenaar4395a712006-09-05 18:57:57 +00003428 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003429 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003430 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003432
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003433 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003434 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003435 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003436 {
3437 /* Redrawing was postponed, do it now. */
3438 update_screen(0);
3439 setcursor(); /* put cursor back where it belongs */
3440 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003441 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003442 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003443 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003445 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 continue;
3447
3448 /* Handle modifier and/or special key code. */
3449 n = buf[0];
3450 if (n == K_SPECIAL)
3451 {
3452 n = TO_SPECIAL(buf[1], buf[2]);
3453 if (buf[1] == KS_MODIFIER
3454 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003455#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003456 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003457#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003458#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 || n == K_VER_SCROLLBAR
3460 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461#endif
3462 )
3463 {
3464 if (buf[1] == KS_MODIFIER)
3465 mod_mask = buf[2];
3466 len -= 3;
3467 if (len > 0)
3468 mch_memmove(buf, buf + 3, (size_t)len);
3469 continue;
3470 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003471 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 }
3473#ifdef FEAT_MBYTE
3474 if (has_mbyte)
3475 {
3476 if (MB_BYTE2LEN(n) > len)
3477 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003478 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 n = (*mb_ptr2char)(buf);
3480 }
3481#endif
3482#ifdef UNIX
3483 if (n == intr_char)
3484 n = ESC;
3485#endif
3486 break;
3487 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003488 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
3490 mapped_ctrl_c = save_mapped_ctrl_c;
3491 return n;
3492}
3493
3494/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003495 * Get a number from the user.
3496 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 */
3498 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003499get_number(
3500 int colon, /* allow colon to abort */
3501 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502{
3503 int n = 0;
3504 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003505 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003507 if (mouse_used != NULL)
3508 *mouse_used = FALSE;
3509
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 /* When not printing messages, the user won't know what to type, return a
3511 * zero (as if CR was hit). */
3512 if (msg_silent != 0)
3513 return 0;
3514
3515#ifdef USE_ON_FLY_SCROLL
3516 dont_scroll = TRUE; /* disallow scrolling here */
3517#endif
3518 ++no_mapping;
3519 ++allow_keys; /* no mapping here, but recognize keys */
3520 for (;;)
3521 {
3522 windgoto(msg_row, msg_col);
3523 c = safe_vgetc();
3524 if (VIM_ISDIGIT(c))
3525 {
3526 n = n * 10 + c - '0';
3527 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003528 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 }
3530 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3531 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003532 if (typed > 0)
3533 {
3534 MSG_PUTS("\b \b");
3535 --typed;
3536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003539#ifdef FEAT_MOUSE
3540 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3541 {
3542 *mouse_used = TRUE;
3543 n = mouse_row + 1;
3544 break;
3545 }
3546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 else if (n == 0 && c == ':' && colon)
3548 {
3549 stuffcharReadbuff(':');
3550 if (!exmode_active)
3551 cmdline_row = msg_row;
3552 skip_redraw = TRUE; /* skip redraw once */
3553 do_redraw = FALSE;
3554 break;
3555 }
3556 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3557 break;
3558 }
3559 --no_mapping;
3560 --allow_keys;
3561 return n;
3562}
3563
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003564/*
3565 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003566 * When "mouse_used" is not NULL allow using the mouse and in that case return
3567 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003568 */
3569 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003570prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003571{
3572 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003573 int save_cmdline_row;
3574 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003575
3576 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003577 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003578 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003579 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003580 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003581
Bram Moolenaar203335e2006-09-03 14:35:42 +00003582 /* Set the state such that text can be selected/copied/pasted and we still
3583 * get mouse events. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003584 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003585 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003586 save_State = State;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003587 State = CMDLINE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003588
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003589 i = get_number(TRUE, mouse_used);
3590 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003591 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003592 /* don't call wait_return() now */
3593 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003594 cmdline_row = msg_row - 1;
3595 need_wait_return = FALSE;
3596 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003597 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003598 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003599 else
3600 cmdline_row = save_cmdline_row;
3601 State = save_State;
3602
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003603 return i;
3604}
3605
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003607msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608{
3609 long pn;
3610
3611 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3613 return;
3614
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003615 /* We don't want to overwrite another important message, but do overwrite
3616 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3617 * then "put" reports the last action. */
3618 if (keep_msg != NULL && !keep_msg_more)
3619 return;
3620
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 if (n > 0)
3622 pn = n;
3623 else
3624 pn = -n;
3625
3626 if (pn > p_report)
3627 {
3628 if (pn == 1)
3629 {
3630 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003631 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3632 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003634 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3635 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 }
3637 else
3638 {
3639 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003640 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3641 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003643 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3644 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 }
3646 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003647 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 if (msg(msg_buf))
3649 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003650 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003651 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 }
3653 }
3654}
3655
3656/*
3657 * flush map and typeahead buffers and give a warning for an error
3658 */
3659 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003660beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661{
3662 if (emsg_silent == 0)
3663 {
3664 flush_buffers(FALSE);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003665 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 }
3667}
3668
3669/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003670 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 */
3672 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003673vim_beep(
3674 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675{
3676 if (emsg_silent == 0)
3677 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003678 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3679 {
3680 if (p_vb
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681#ifdef FEAT_GUI
Bram Moolenaar165bc692015-07-21 17:53:25 +02003682 /* While the GUI is starting up the termcap is set for the
3683 * GUI but the output still goes to a terminal. */
3684 && !(gui.in_use && gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685#endif
Bram Moolenaar165bc692015-07-21 17:53:25 +02003686 )
Bram Moolenaar165bc692015-07-21 17:53:25 +02003687 out_str(T_VB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02003689 out_char(BELL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003691
3692 /* When 'verbose' is set and we are sourcing a script or executing a
3693 * function give the user a hint where the beep comes from. */
3694 if (vim_strchr(p_debug, 'e') != NULL)
3695 {
3696 msg_source(hl_attr(HLF_W));
3697 msg_attr((char_u *)_("Beep!"), hl_attr(HLF_W));
3698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 }
3700}
3701
3702/*
3703 * To get the "real" home directory:
3704 * - get value of $HOME
3705 * For Unix:
3706 * - go to that directory
3707 * - do mch_dirname() to get the real name of that directory.
3708 * This also works with mounts and links.
3709 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3710 */
3711static char_u *homedir = NULL;
3712
3713 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003714init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715{
3716 char_u *var;
3717
Bram Moolenaar05159a02005-02-26 23:04:13 +00003718 /* In case we are called a second time (when 'encoding' changes). */
3719 vim_free(homedir);
3720 homedir = NULL;
3721
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722#ifdef VMS
3723 var = mch_getenv((char_u *)"SYS$LOGIN");
3724#else
3725 var = mch_getenv((char_u *)"HOME");
3726#endif
3727
3728 if (var != NULL && *var == NUL) /* empty is same as not set */
3729 var = NULL;
3730
3731#ifdef WIN3264
3732 /*
3733 * Weird but true: $HOME may contain an indirect reference to another
3734 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3735 * when $HOME is being set.
3736 */
3737 if (var != NULL && *var == '%')
3738 {
3739 char_u *p;
3740 char_u *exp;
3741
3742 p = vim_strchr(var + 1, '%');
3743 if (p != NULL)
3744 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003745 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 exp = mch_getenv(NameBuff);
3747 if (exp != NULL && *exp != NUL
3748 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3749 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003750 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 var = NameBuff;
3752 /* Also set $HOME, it's needed for _viminfo. */
3753 vim_setenv((char_u *)"HOME", NameBuff);
3754 }
3755 }
3756 }
3757
3758 /*
3759 * Typically, $HOME is not defined on Windows, unless the user has
3760 * specifically defined it for Vim's sake. However, on Windows NT
3761 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3762 * each user. Try constructing $HOME from these.
3763 */
3764 if (var == NULL)
3765 {
3766 char_u *homedrive, *homepath;
3767
3768 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3769 homepath = mch_getenv((char_u *)"HOMEPATH");
Bram Moolenaar6f977012010-01-06 17:53:38 +01003770 if (homepath == NULL || *homepath == NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003771 homepath = (char_u *)"\\";
Bram Moolenaar6f977012010-01-06 17:53:38 +01003772 if (homedrive != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3774 {
3775 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3776 if (NameBuff[0] != NUL)
3777 {
3778 var = NameBuff;
3779 /* Also set $HOME, it's needed for _viminfo. */
3780 vim_setenv((char_u *)"HOME", NameBuff);
3781 }
3782 }
3783 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003784
3785# if defined(FEAT_MBYTE)
3786 if (enc_utf8 && var != NULL)
3787 {
3788 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003789 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003790
3791 /* Convert from active codepage to UTF-8. Other conversions are
3792 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003793 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003794 if (pp != NULL)
3795 {
3796 homedir = pp;
3797 return;
3798 }
3799 }
3800# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801#endif
3802
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003803#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 /*
3805 * Default home dir is C:/
3806 * Best assumption we can make in such a situation.
3807 */
3808 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003809 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810#endif
3811 if (var != NULL)
3812 {
3813#ifdef UNIX
3814 /*
3815 * Change to the directory and get the actual path. This resolves
3816 * links. Don't do it when we can't return.
3817 */
3818 if (mch_dirname(NameBuff, MAXPATHL) == OK
3819 && mch_chdir((char *)NameBuff) == 0)
3820 {
3821 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3822 var = IObuff;
3823 if (mch_chdir((char *)NameBuff) != 0)
3824 EMSG(_(e_prev_dir));
3825 }
3826#endif
3827 homedir = vim_strsave(var);
3828 }
3829}
3830
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003831#if defined(EXITFREE) || defined(PROTO)
3832 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003833free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003834{
3835 vim_free(homedir);
3836}
Bram Moolenaar24305862012-08-15 14:05:05 +02003837
3838# ifdef FEAT_CMDL_COMPL
3839 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003840free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02003841{
3842 ga_clear_strings(&ga_users);
3843}
3844# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003845#endif
3846
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003848 * Call expand_env() and store the result in an allocated string.
3849 * This is not very memory efficient, this expects the result to be freed
3850 * again soon.
3851 */
3852 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003853expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003854{
3855 return expand_env_save_opt(src, FALSE);
3856}
3857
3858/*
3859 * Idem, but when "one" is TRUE handle the string as one file name, only
3860 * expand "~" at the start.
3861 */
3862 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003863expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003864{
3865 char_u *p;
3866
3867 p = alloc(MAXPATHL);
3868 if (p != NULL)
3869 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3870 return p;
3871}
3872
3873/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 * Expand environment variable with path name.
3875 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003876 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 * If anything fails no expansion is done and dst equals src.
3878 */
3879 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003880expand_env(
3881 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
3882 char_u *dst, /* where to put the result */
3883 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003885 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886}
3887
3888 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003889expand_env_esc(
3890 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
3891 char_u *dst, /* where to put the result */
3892 int dstlen, /* maximum length of the result */
3893 int esc, /* escape spaces in expanded variables */
3894 int one, /* "srcp" is one file name */
3895 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003897 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 char_u *tail;
3899 int c;
3900 char_u *var;
3901 int copy_char;
3902 int mustfree; /* var was allocated, need to free it later */
3903 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003904 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003906 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003907 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003908
3909 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 --dstlen; /* leave one char space for "\," */
3911 while (*src && dstlen > 0)
3912 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003913#ifdef FEAT_EVAL
3914 /* Skip over `=expr`. */
3915 if (src[0] == '`' && src[1] == '=')
3916 {
3917 size_t len;
3918
3919 var = src;
3920 src += 2;
3921 (void)skip_expr(&src);
3922 if (*src == '`')
3923 ++src;
3924 len = src - var;
3925 if (len > (size_t)dstlen)
3926 len = dstlen;
3927 vim_strncpy(dst, var, len);
3928 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02003929 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003930 continue;
3931 }
3932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003934 if ((*src == '$'
3935#ifdef VMS
3936 && at_start
3937#endif
3938 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003939#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 || *src == '%'
3941#endif
3942 || (*src == '~' && at_start))
3943 {
3944 mustfree = FALSE;
3945
3946 /*
3947 * The variable name is copied into dst temporarily, because it may
3948 * be a string in read-only memory and a NUL needs to be appended.
3949 */
3950 if (*src != '~') /* environment var */
3951 {
3952 tail = src + 1;
3953 var = dst;
3954 c = dstlen - 1;
3955
3956#ifdef UNIX
3957 /* Unix has ${var-name} type environment vars */
3958 if (*tail == '{' && !vim_isIDc('{'))
3959 {
3960 tail++; /* ignore '{' */
3961 while (c-- > 0 && *tail && *tail != '}')
3962 *var++ = *tail++;
3963 }
3964 else
3965#endif
3966 {
3967 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003968#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 || (*src == '%' && *tail != '%')
3970#endif
3971 ))
3972 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 }
3975 }
3976
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003977#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978# ifdef UNIX
3979 if (src[1] == '{' && *tail != '}')
3980# else
3981 if (*src == '%' && *tail != '%')
3982# endif
3983 var = NULL;
3984 else
3985 {
3986# ifdef UNIX
3987 if (src[1] == '{')
3988# else
3989 if (*src == '%')
3990#endif
3991 ++tail;
3992#endif
3993 *var = NUL;
3994 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003995#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 }
3997#endif
3998 }
3999 /* home directory */
4000 else if ( src[1] == NUL
4001 || vim_ispathsep(src[1])
4002 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4003 {
4004 var = homedir;
4005 tail = src + 1;
4006 }
4007 else /* user directory */
4008 {
4009#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4010 /*
4011 * Copy ~user to dst[], so we can put a NUL after it.
4012 */
4013 tail = src;
4014 var = dst;
4015 c = dstlen - 1;
4016 while ( c-- > 0
4017 && *tail
4018 && vim_isfilec(*tail)
4019 && !vim_ispathsep(*tail))
4020 *var++ = *tail++;
4021 *var = NUL;
4022# ifdef UNIX
4023 /*
4024 * If the system supports getpwnam(), use it.
4025 * Otherwise, or if getpwnam() fails, the shell is used to
4026 * expand ~user. This is slower and may fail if the shell
4027 * does not support ~user (old versions of /bin/sh).
4028 */
4029# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4030 {
4031 struct passwd *pw;
4032
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004033 /* Note: memory allocated by getpwnam() is never freed.
4034 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 pw = getpwnam((char *)dst + 1);
4036 if (pw != NULL)
4037 var = (char_u *)pw->pw_dir;
4038 else
4039 var = NULL;
4040 }
4041 if (var == NULL)
4042# endif
4043 {
4044 expand_T xpc;
4045
4046 ExpandInit(&xpc);
4047 xpc.xp_context = EXPAND_FILES;
4048 var = ExpandOne(&xpc, dst, NULL,
4049 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 mustfree = TRUE;
4051 }
4052
4053# else /* !UNIX, thus VMS */
4054 /*
4055 * USER_HOME is a comma-separated list of
4056 * directories to search for the user account in.
4057 */
4058 {
4059 char_u test[MAXPATHL], paths[MAXPATHL];
4060 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004061 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062
4063 STRCPY(paths, USER_HOME);
4064 next_path = paths;
4065 while (*next_path)
4066 {
4067 for (path = next_path; *next_path && *next_path != ',';
4068 next_path++);
4069 if (*next_path)
4070 *next_path++ = NUL;
4071 STRCPY(test, path);
4072 STRCAT(test, "/");
4073 STRCAT(test, dst + 1);
4074 if (mch_stat(test, &st) == 0)
4075 {
4076 var = alloc(STRLEN(test) + 1);
4077 STRCPY(var, test);
4078 mustfree = TRUE;
4079 break;
4080 }
4081 }
4082 }
4083# endif /* UNIX */
4084#else
4085 /* cannot expand user's home directory, so don't try */
4086 var = NULL;
4087 tail = (char_u *)""; /* for gcc */
4088#endif /* UNIX || VMS */
4089 }
4090
4091#ifdef BACKSLASH_IN_FILENAME
4092 /* If 'shellslash' is set change backslashes to forward slashes.
4093 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4094 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4095 {
4096 char_u *p = vim_strsave(var);
4097
4098 if (p != NULL)
4099 {
4100 if (mustfree)
4101 vim_free(var);
4102 var = p;
4103 mustfree = TRUE;
4104 forward_slash(var);
4105 }
4106 }
4107#endif
4108
4109 /* If "var" contains white space, escape it with a backslash.
4110 * Required for ":e ~/tt" when $HOME includes a space. */
4111 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4112 {
4113 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4114
4115 if (p != NULL)
4116 {
4117 if (mustfree)
4118 vim_free(var);
4119 var = p;
4120 mustfree = TRUE;
4121 }
4122 }
4123
4124 if (var != NULL && *var != NUL
4125 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4126 {
4127 STRCPY(dst, var);
4128 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004129 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 /* if var[] ends in a path separator and tail[] starts
4131 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004132 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4134 && dst[-1] != ':'
4135#endif
4136 && vim_ispathsep(*tail))
4137 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004138 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 src = tail;
4140 copy_char = FALSE;
4141 }
4142 if (mustfree)
4143 vim_free(var);
4144 }
4145
4146 if (copy_char) /* copy at least one char */
4147 {
4148 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004149 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004150 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4151 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 */
4153 at_start = FALSE;
4154 if (src[0] == '\\' && src[1] != NUL)
4155 {
4156 *dst++ = *src++;
4157 --dstlen;
4158 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004159 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 at_start = TRUE;
4161 *dst++ = *src++;
4162 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004163
4164 if (startstr != NULL && src - startstr_len >= srcp
4165 && STRNCMP(src - startstr_len, startstr, startstr_len) == 0)
4166 at_start = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 }
4168 }
4169 *dst = NUL;
4170}
4171
4172/*
4173 * Vim's version of getenv().
4174 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004175 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004176 * "mustfree" is set to TRUE when returned is allocated, it must be
4177 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 */
4179 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004180vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181{
4182 char_u *p;
4183 char_u *pend;
4184 int vimruntime;
4185
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004186#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 /* use "C:/" when $HOME is not set */
4188 if (STRCMP(name, "HOME") == 0)
4189 return homedir;
4190#endif
4191
4192 p = mch_getenv(name);
4193 if (p != NULL && *p == NUL) /* empty is the same as not set */
4194 p = NULL;
4195
4196 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004197 {
4198#if defined(FEAT_MBYTE) && defined(WIN3264)
4199 if (enc_utf8)
4200 {
4201 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004202 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004203
4204 /* Convert from active codepage to UTF-8. Other conversions are
4205 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004206 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004207 if (pp != NULL)
4208 {
4209 p = pp;
4210 *mustfree = TRUE;
4211 }
4212 }
4213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216
4217 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4218 if (!vimruntime && STRCMP(name, "VIM") != 0)
4219 return NULL;
4220
4221 /*
4222 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4223 * Don't do this when default_vimruntime_dir is non-empty.
4224 */
4225 if (vimruntime
4226#ifdef HAVE_PATHDEF
4227 && *default_vimruntime_dir == NUL
4228#endif
4229 )
4230 {
4231 p = mch_getenv((char_u *)"VIM");
4232 if (p != NULL && *p == NUL) /* empty is the same as not set */
4233 p = NULL;
4234 if (p != NULL)
4235 {
4236 p = vim_version_dir(p);
4237 if (p != NULL)
4238 *mustfree = TRUE;
4239 else
4240 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004241
4242#if defined(FEAT_MBYTE) && defined(WIN3264)
4243 if (enc_utf8)
4244 {
4245 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004246 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004247
4248 /* Convert from active codepage to UTF-8. Other conversions
4249 * are not done, because they would fail for non-ASCII
4250 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004251 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004252 if (pp != NULL)
4253 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004254 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004255 vim_free(p);
4256 p = pp;
4257 *mustfree = TRUE;
4258 }
4259 }
4260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 }
4262 }
4263
4264 /*
4265 * When expanding $VIM or $VIMRUNTIME fails, try using:
4266 * - the directory name from 'helpfile' (unless it contains '$')
4267 * - the executable name from argv[0]
4268 */
4269 if (p == NULL)
4270 {
4271 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4272 p = p_hf;
4273#ifdef USE_EXE_NAME
4274 /*
4275 * Use the name of the executable, obtained from argv[0].
4276 */
4277 else
4278 p = exe_name;
4279#endif
4280 if (p != NULL)
4281 {
4282 /* remove the file name */
4283 pend = gettail(p);
4284
4285 /* remove "doc/" from 'helpfile', if present */
4286 if (p == p_hf)
4287 pend = remove_tail(p, pend, (char_u *)"doc");
4288
4289#ifdef USE_EXE_NAME
4290# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004291 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 if (p == exe_name)
4293 {
4294 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004295 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004297 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4298 if (pend1 != pend)
4299 {
4300 pnew = alloc((unsigned)(pend1 - p) + 15);
4301 if (pnew != NULL)
4302 {
4303 STRNCPY(pnew, p, (pend1 - p));
4304 STRCPY(pnew + (pend1 - p), "Resources/vim");
4305 p = pnew;
4306 pend = p + STRLEN(p);
4307 }
4308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 }
4310# endif
4311 /* remove "src/" from exe_name, if present */
4312 if (p == exe_name)
4313 pend = remove_tail(p, pend, (char_u *)"src");
4314#endif
4315
4316 /* for $VIM, remove "runtime/" or "vim54/", if present */
4317 if (!vimruntime)
4318 {
4319 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4320 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4321 }
4322
4323 /* remove trailing path separator */
4324#ifndef MACOS_CLASSIC
4325 /* With MacOS path (with colons) the final colon is required */
Bram Moolenaare21877a2008-02-13 09:58:14 +00004326 /* to avoid confusion between absolute and relative path */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004327 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 --pend;
4329#endif
4330
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004331#ifdef MACOS_X
4332 if (p == exe_name || p == p_hf)
4333#endif
4334 /* check that the result is a directory name */
4335 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336
4337 if (p != NULL && !mch_isdir(p))
4338 {
4339 vim_free(p);
4340 p = NULL;
4341 }
4342 else
4343 {
4344#ifdef USE_EXE_NAME
4345 /* may add "/vim54" or "/runtime" if it exists */
4346 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4347 {
4348 vim_free(p);
4349 p = pend;
4350 }
4351#endif
4352 *mustfree = TRUE;
4353 }
4354 }
4355 }
4356
4357#ifdef HAVE_PATHDEF
4358 /* When there is a pathdef.c file we can use default_vim_dir and
4359 * default_vimruntime_dir */
4360 if (p == NULL)
4361 {
4362 /* Only use default_vimruntime_dir when it is not empty */
4363 if (vimruntime && *default_vimruntime_dir != NUL)
4364 {
4365 p = default_vimruntime_dir;
4366 *mustfree = FALSE;
4367 }
4368 else if (*default_vim_dir != NUL)
4369 {
4370 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4371 *mustfree = TRUE;
4372 else
4373 {
4374 p = default_vim_dir;
4375 *mustfree = FALSE;
4376 }
4377 }
4378 }
4379#endif
4380
4381 /*
4382 * Set the environment variable, so that the new value can be found fast
4383 * next time, and others can also use it (e.g. Perl).
4384 */
4385 if (p != NULL)
4386 {
4387 if (vimruntime)
4388 {
4389 vim_setenv((char_u *)"VIMRUNTIME", p);
4390 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 }
4392 else
4393 {
4394 vim_setenv((char_u *)"VIM", p);
4395 didset_vim = TRUE;
4396 }
4397 }
4398 return p;
4399}
4400
4401/*
4402 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4403 * Return NULL if not, return its name in allocated memory otherwise.
4404 */
4405 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004406vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407{
4408 char_u *p;
4409
4410 if (vimdir == NULL || *vimdir == NUL)
4411 return NULL;
4412 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4413 if (p != NULL && mch_isdir(p))
4414 return p;
4415 vim_free(p);
4416 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4417 if (p != NULL && mch_isdir(p))
4418 return p;
4419 vim_free(p);
4420 return NULL;
4421}
4422
4423/*
4424 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4425 * the length of "name/". Otherwise return "pend".
4426 */
4427 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004428remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429{
4430 int len = (int)STRLEN(name) + 1;
4431 char_u *newend = pend - len;
4432
4433 if (newend >= p
4434 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004435 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 return newend;
4437 return pend;
4438}
4439
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 * Our portable version of setenv.
4442 */
4443 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004444vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445{
4446#ifdef HAVE_SETENV
4447 mch_setenv((char *)name, (char *)val, 1);
4448#else
4449 char_u *envbuf;
4450
4451 /*
4452 * Putenv does not copy the string, it has to remain
4453 * valid. The allocated memory will never be freed.
4454 */
4455 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4456 if (envbuf != NULL)
4457 {
4458 sprintf((char *)envbuf, "%s=%s", name, val);
4459 putenv((char *)envbuf);
4460 }
4461#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004462#ifdef FEAT_GETTEXT
4463 /*
4464 * When setting $VIMRUNTIME adjust the directory to find message
4465 * translations to $VIMRUNTIME/lang.
4466 */
4467 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4468 {
4469 char_u *buf = concat_str(val, (char_u *)"/lang");
4470
4471 if (buf != NULL)
4472 {
4473 bindtextdomain(VIMPACKAGE, (char *)buf);
4474 vim_free(buf);
4475 }
4476 }
4477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478}
4479
4480#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4481/*
4482 * Function given to ExpandGeneric() to obtain an environment variable name.
4483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004485get_env_name(
4486 expand_T *xp UNUSED,
4487 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488{
4489# if defined(AMIGA) || defined(__MRC__) || defined(__SC__)
4490 /*
4491 * No environ[] on the Amiga and on the Mac (using MPW).
4492 */
4493 return NULL;
4494# else
4495# ifndef __WIN32__
4496 /* Borland C++ 5.2 has this in a header file. */
4497 extern char **environ;
4498# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004499# define ENVNAMELEN 100
4500 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 char_u *str;
4502 int n;
4503
4504 str = (char_u *)environ[idx];
4505 if (str == NULL)
4506 return NULL;
4507
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004508 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 {
4510 if (str[n] == '=' || str[n] == NUL)
4511 break;
4512 name[n] = str[n];
4513 }
4514 name[n] = NUL;
4515 return name;
4516# endif
4517}
Bram Moolenaar24305862012-08-15 14:05:05 +02004518
4519/*
4520 * Find all user names for user completion.
4521 * Done only once and then cached.
4522 */
4523 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004524init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004525{
Bram Moolenaar24305862012-08-15 14:05:05 +02004526 static int lazy_init_done = FALSE;
4527
4528 if (lazy_init_done)
4529 return;
4530
4531 lazy_init_done = TRUE;
4532 ga_init2(&ga_users, sizeof(char_u *), 20);
4533
4534# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4535 {
4536 char_u* user;
4537 struct passwd* pw;
4538
4539 setpwent();
4540 while ((pw = getpwent()) != NULL)
4541 /* pw->pw_name shouldn't be NULL but just in case... */
4542 if (pw->pw_name != NULL)
4543 {
4544 if (ga_grow(&ga_users, 1) == FAIL)
4545 break;
4546 user = vim_strsave((char_u*)pw->pw_name);
4547 if (user == NULL)
4548 break;
4549 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4550 }
4551 endpwent();
4552 }
4553# endif
4554}
4555
4556/*
4557 * Function given to ExpandGeneric() to obtain an user names.
4558 */
4559 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004560get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004561{
4562 init_users();
4563 if (idx < ga_users.ga_len)
4564 return ((char_u **)ga_users.ga_data)[idx];
4565 return NULL;
4566}
4567
4568/*
4569 * Check whether name matches a user name. Return:
4570 * 0 if name does not match any user name.
4571 * 1 if name partially matches the beginning of a user name.
4572 * 2 is name fully matches a user name.
4573 */
Bram Moolenaar9b578142016-01-30 19:39:49 +01004574int match_user(char_u* name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004575{
4576 int i;
4577 int n = (int)STRLEN(name);
4578 int result = 0;
4579
4580 init_users();
4581 for (i = 0; i < ga_users.ga_len; i++)
4582 {
4583 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4584 return 2; /* full match */
4585 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4586 result = 1; /* partial match */
4587 }
4588 return result;
4589}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590#endif
4591
4592/*
4593 * Replace home directory by "~" in each space or comma separated file name in
4594 * 'src'.
4595 * If anything fails (except when out of space) dst equals src.
4596 */
4597 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004598home_replace(
4599 buf_T *buf, /* when not NULL, check for help files */
4600 char_u *src, /* input file name */
4601 char_u *dst, /* where to put the result */
4602 int dstlen, /* maximum length of the result */
4603 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 spaces and commas in the file name. */
4605{
4606 size_t dirlen = 0, envlen = 0;
4607 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004608 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 char_u *p;
4610
4611 if (src == NULL)
4612 {
4613 *dst = NUL;
4614 return;
4615 }
4616
4617 /*
4618 * If the file is a help file, remove the path completely.
4619 */
4620 if (buf != NULL && buf->b_help)
4621 {
4622 STRCPY(dst, gettail(src));
4623 return;
4624 }
4625
4626 /*
4627 * We check both the value of the $HOME environment variable and the
4628 * "real" home directory.
4629 */
4630 if (homedir != NULL)
4631 dirlen = STRLEN(homedir);
4632
4633#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004634 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004636 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4637#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004638 /* Empty is the same as not set. */
4639 if (homedir_env != NULL && *homedir_env == NUL)
4640 homedir_env = NULL;
4641
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004642#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004643 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004644 {
4645 int usedlen = 0;
4646 int flen;
4647 char_u *fbuf = NULL;
4648
4649 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004650 (void)modify_fname((char_u *)":p", &usedlen,
4651 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004652 flen = (int)STRLEN(homedir_env);
4653 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4654 /* Remove the trailing / that is added to a directory. */
4655 homedir_env[flen - 1] = NUL;
4656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657#endif
4658
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 if (homedir_env != NULL)
4660 envlen = STRLEN(homedir_env);
4661
4662 if (!one)
4663 src = skipwhite(src);
4664 while (*src && dstlen > 0)
4665 {
4666 /*
4667 * Here we are at the beginning of a file name.
4668 * First, check to see if the beginning of the file name matches
4669 * $HOME or the "real" home directory. Check that there is a '/'
4670 * after the match (so that if e.g. the file is "/home/pieter/bla",
4671 * and the home directory is "/home/piet", the file does not end up
4672 * as "~er/bla" (which would seem to indicate the file "bla" in user
4673 * er's home directory)).
4674 */
4675 p = homedir;
4676 len = dirlen;
4677 for (;;)
4678 {
4679 if ( len
4680 && fnamencmp(src, p, len) == 0
4681 && (vim_ispathsep(src[len])
4682 || (!one && (src[len] == ',' || src[len] == ' '))
4683 || src[len] == NUL))
4684 {
4685 src += len;
4686 if (--dstlen > 0)
4687 *dst++ = '~';
4688
4689 /*
4690 * If it's just the home directory, add "/".
4691 */
4692 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4693 *dst++ = '/';
4694 break;
4695 }
4696 if (p == homedir_env)
4697 break;
4698 p = homedir_env;
4699 len = envlen;
4700 }
4701
4702 /* if (!one) skip to separator: space or comma */
4703 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4704 *dst++ = *src++;
4705 /* skip separator */
4706 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4707 *dst++ = *src++;
4708 }
4709 /* if (dstlen == 0) out of space, what to do??? */
4710
4711 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004712
4713 if (homedir_env != homedir_env_orig)
4714 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715}
4716
4717/*
4718 * Like home_replace, store the replaced string in allocated memory.
4719 * When something fails, NULL is returned.
4720 */
4721 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004722home_replace_save(
4723 buf_T *buf, /* when not NULL, check for help files */
4724 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725{
4726 char_u *dst;
4727 unsigned len;
4728
4729 len = 3; /* space for "~/" and trailing NUL */
4730 if (src != NULL) /* just in case */
4731 len += (unsigned)STRLEN(src);
4732 dst = alloc(len);
4733 if (dst != NULL)
4734 home_replace(buf, src, dst, len, TRUE);
4735 return dst;
4736}
4737
4738/*
4739 * Compare two file names and return:
4740 * FPC_SAME if they both exist and are the same file.
4741 * FPC_SAMEX if they both don't exist and have the same file name.
4742 * FPC_DIFF if they both exist and are different files.
4743 * FPC_NOTX if they both don't exist.
4744 * FPC_DIFFX if one of them doesn't exist.
4745 * For the first name environment variables are expanded
4746 */
4747 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004748fullpathcmp(
4749 char_u *s1,
4750 char_u *s2,
4751 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752{
4753#ifdef UNIX
4754 char_u exp1[MAXPATHL];
4755 char_u full1[MAXPATHL];
4756 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02004757 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 int r1, r2;
4759
4760 expand_env(s1, exp1, MAXPATHL);
4761 r1 = mch_stat((char *)exp1, &st1);
4762 r2 = mch_stat((char *)s2, &st2);
4763 if (r1 != 0 && r2 != 0)
4764 {
4765 /* if mch_stat() doesn't work, may compare the names */
4766 if (checkname)
4767 {
4768 if (fnamecmp(exp1, s2) == 0)
4769 return FPC_SAMEX;
4770 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4771 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4772 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4773 return FPC_SAMEX;
4774 }
4775 return FPC_NOTX;
4776 }
4777 if (r1 != 0 || r2 != 0)
4778 return FPC_DIFFX;
4779 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4780 return FPC_SAME;
4781 return FPC_DIFF;
4782#else
4783 char_u *exp1; /* expanded s1 */
4784 char_u *full1; /* full path of s1 */
4785 char_u *full2; /* full path of s2 */
4786 int retval = FPC_DIFF;
4787 int r1, r2;
4788
4789 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4790 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4791 {
4792 full1 = exp1 + MAXPATHL;
4793 full2 = full1 + MAXPATHL;
4794
4795 expand_env(s1, exp1, MAXPATHL);
4796 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4797 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4798
4799 /* If vim_FullName() fails, the file probably doesn't exist. */
4800 if (r1 != OK && r2 != OK)
4801 {
4802 if (checkname && fnamecmp(exp1, s2) == 0)
4803 retval = FPC_SAMEX;
4804 else
4805 retval = FPC_NOTX;
4806 }
4807 else if (r1 != OK || r2 != OK)
4808 retval = FPC_DIFFX;
4809 else if (fnamecmp(full1, full2))
4810 retval = FPC_DIFF;
4811 else
4812 retval = FPC_SAME;
4813 vim_free(exp1);
4814 }
4815 return retval;
4816#endif
4817}
4818
4819/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004820 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004821 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004822 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 */
4824 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004825gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826{
4827 char_u *p1, *p2;
4828
4829 if (fname == NULL)
4830 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01004831 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01004833 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 p1 = p2 + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004835 mb_ptr_adv(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 }
4837 return p1;
4838}
4839
Bram Moolenaar31710262010-08-13 13:36:15 +02004840#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004841static char_u *gettail_dir(char_u *fname);
Bram Moolenaar31710262010-08-13 13:36:15 +02004842
4843/*
4844 * Return the end of the directory name, on the first path
4845 * separator:
4846 * "/path/file", "/path/dir/", "/path//dir", "/file"
4847 * ^ ^ ^ ^
4848 */
4849 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004850gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02004851{
4852 char_u *dir_end = fname;
4853 char_u *next_dir_end = fname;
4854 int look_for_sep = TRUE;
4855 char_u *p;
4856
4857 for (p = fname; *p != NUL; )
4858 {
4859 if (vim_ispathsep(*p))
4860 {
4861 if (look_for_sep)
4862 {
4863 next_dir_end = p;
4864 look_for_sep = FALSE;
4865 }
4866 }
4867 else
4868 {
4869 if (!look_for_sep)
4870 dir_end = next_dir_end;
4871 look_for_sep = TRUE;
4872 }
4873 mb_ptr_adv(p);
4874 }
4875 return dir_end;
4876}
4877#endif
4878
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004880 * Get pointer to tail of "fname", including path separators. Putting a NUL
4881 * here leaves the directory name. Takes care of "c:/" and "//".
4882 * Always returns a valid pointer.
4883 */
4884 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004885gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004886{
4887 char_u *p;
4888 char_u *t;
4889
4890 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4891 t = gettail(fname);
4892 while (t > p && after_pathsep(fname, t))
4893 --t;
4894#ifdef VMS
4895 /* path separator is part of the path */
4896 ++t;
4897#endif
4898 return t;
4899}
4900
4901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 * get the next path component (just after the next path separator).
4903 */
4904 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004905getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906{
4907 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004908 mb_ptr_adv(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 if (*fname)
4910 ++fname;
4911 return fname;
4912}
4913
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914/*
4915 * Get a pointer to one character past the head of a path name.
4916 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4917 * If there is no head, path is returned.
4918 */
4919 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004920get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921{
4922 char_u *retval;
4923
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004924#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 /* may skip "c:" */
4926 if (isalpha(path[0]) && path[1] == ':')
4927 retval = path + 2;
4928 else
4929 retval = path;
4930#else
4931# if defined(AMIGA)
4932 /* may skip "label:" */
4933 retval = vim_strchr(path, ':');
4934 if (retval == NULL)
4935 retval = path;
4936# else /* Unix */
4937 retval = path;
4938# endif
4939#endif
4940
4941 while (vim_ispathsep(*retval))
4942 ++retval;
4943
4944 return retval;
4945}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946
4947/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01004948 * Return TRUE if 'c' is a path separator.
4949 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 */
4951 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004952vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953{
Bram Moolenaare60acc12011-05-10 16:41:25 +02004954#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02004956#else
4957# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004959# else
4960# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
4962 return (c == ':' || c == '[' || c == ']' || c == '/'
4963 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02004964# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004966# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02004968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969}
4970
Bram Moolenaar69c35002013-11-04 02:54:12 +01004971/*
4972 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
4973 */
4974 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004975vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01004976{
4977 return vim_ispathsep(c)
4978#ifdef BACKSLASH_IN_FILENAME
4979 && c != ':'
4980#endif
4981 ;
4982}
4983
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984#if defined(FEAT_SEARCHPATH) || defined(PROTO)
4985/*
4986 * return TRUE if 'c' is a path list separator.
4987 */
4988 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004989vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990{
4991#ifdef UNIX
4992 return (c == ':');
4993#else
Bram Moolenaar25394022007-05-10 19:06:20 +00004994 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995#endif
4996}
4997#endif
4998
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004999#if defined(FEAT_GUI_TABLINE) || defined(FEAT_WINDOWS) \
5000 || defined(FEAT_EVAL) || defined(PROTO)
5001/*
5002 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5003 * It's done in-place.
5004 */
5005 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005006shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005007{
5008 char_u *tail, *s, *d;
5009 int skip = FALSE;
5010
5011 tail = gettail(str);
5012 d = str;
5013 for (s = str; ; ++s)
5014 {
5015 if (s >= tail) /* copy the whole tail */
5016 {
5017 *d++ = *s;
5018 if (*s == NUL)
5019 break;
5020 }
5021 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5022 {
5023 *d++ = *s;
5024 skip = FALSE;
5025 }
5026 else if (!skip)
5027 {
5028 *d++ = *s; /* copy next char */
5029 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5030 skip = TRUE;
5031# ifdef FEAT_MBYTE
5032 if (has_mbyte)
5033 {
5034 int l = mb_ptr2len(s);
5035
5036 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005037 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005038 }
5039# endif
5040 }
5041 }
5042}
5043#endif
5044
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005045/*
5046 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5047 * Also returns TRUE if there is no directory name.
5048 * "fname" must be writable!.
5049 */
5050 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005051dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005052{
5053 char_u *p;
5054 int c;
5055 int retval;
5056
5057 p = gettail_sep(fname);
5058 if (p == fname)
5059 return TRUE;
5060 c = *p;
5061 *p = NUL;
5062 retval = mch_isdir(fname);
5063 *p = c;
5064 return retval;
5065}
5066
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005068 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5069 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 */
5071 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005072vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005074#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005076#else
5077 if (p_fic)
5078 return MB_STRICMP(x, y);
5079 return STRCMP(x, y);
5080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081}
5082
5083 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005084vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005086#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005087 char_u *px = x;
5088 char_u *py = y;
5089 int cx = NUL;
5090 int cy = NUL;
5091
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005092 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005094 cx = PTR2CHAR(px);
5095 cy = PTR2CHAR(py);
5096 if (cx == NUL || cy == NUL
5097 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5098 && !(cx == '/' && cy == '\\')
5099 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005101 len -= MB_PTR2LEN(px);
5102 px += MB_PTR2LEN(px);
5103 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 }
5105 if (len == 0)
5106 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005107 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005108#else
5109 if (p_fic)
5110 return MB_STRNICMP(x, y, len);
5111 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005113}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114
5115/*
5116 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005117 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 */
5119 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005120concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121{
5122 char_u *dest;
5123
5124 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5125 if (dest != NULL)
5126 {
5127 STRCPY(dest, fname1);
5128 if (sep)
5129 add_pathsep(dest);
5130 STRCAT(dest, fname2);
5131 }
5132 return dest;
5133}
5134
Bram Moolenaard6754642005-01-17 22:18:45 +00005135/*
5136 * Concatenate two strings and return the result in allocated memory.
5137 * Returns NULL when out of memory.
5138 */
5139 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005140concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005141{
5142 char_u *dest;
5143 size_t l = STRLEN(str1);
5144
5145 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5146 if (dest != NULL)
5147 {
5148 STRCPY(dest, str1);
5149 STRCPY(dest + l, str2);
5150 }
5151 return dest;
5152}
Bram Moolenaard6754642005-01-17 22:18:45 +00005153
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154/*
5155 * Add a path separator to a file name, unless it already ends in a path
5156 * separator.
5157 */
5158 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005159add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005161 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 STRCAT(p, PATHSEPSTR);
5163}
5164
5165/*
5166 * FullName_save - Make an allocated copy of a full file name.
5167 * Returns NULL when out of memory.
5168 */
5169 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005170FullName_save(
5171 char_u *fname,
5172 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005173 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174{
5175 char_u *buf;
5176 char_u *new_fname = NULL;
5177
5178 if (fname == NULL)
5179 return NULL;
5180
5181 buf = alloc((unsigned)MAXPATHL);
5182 if (buf != NULL)
5183 {
5184 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5185 new_fname = vim_strsave(buf);
5186 else
5187 new_fname = vim_strsave(fname);
5188 vim_free(buf);
5189 }
5190 return new_fname;
5191}
5192
5193#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5194
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005195static char_u *skip_string(char_u *p);
5196static pos_T *ind_find_start_comment(void);
5197static pos_T *ind_find_start_CORS(void);
5198static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199
5200/*
5201 * Find the start of a comment, not knowing if we are in a comment right now.
5202 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005203 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005205 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005206ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005207{
5208 return find_start_comment(curbuf->b_ind_maxcomment);
5209}
5210
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005212find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213{
5214 pos_T *pos;
5215 char_u *line;
5216 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005217 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005219 for (;;)
5220 {
5221 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5222 if (pos == NULL)
5223 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005225 /*
5226 * Check if the comment start we found is inside a string.
5227 * If it is then restrict the search to below this line and try again.
5228 */
5229 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005230 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005231 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005232 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005233 break;
5234 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5235 if (cur_maxcomment <= 0)
5236 {
5237 pos = NULL;
5238 break;
5239 }
5240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 return pos;
5242}
5243
5244/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005245 * Find the start of a comment or raw string, not knowing if we are in a
5246 * comment or raw string right now.
5247 * Search starts at w_cursor.lnum and goes backwards.
5248 * Return NULL when not inside a comment or raw string.
5249 * "CORS" -> Comment Or Raw String
5250 */
5251 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005252ind_find_start_CORS(void) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005253{
Bram Moolenaar089af182015-10-07 11:41:49 +02005254 static pos_T comment_pos_copy;
5255 pos_T *comment_pos;
5256 pos_T *rs_pos;
5257
5258 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5259 if (comment_pos != NULL)
5260 {
5261 /* Need to make a copy of the static pos in findmatchlimit(),
5262 * calling find_start_rawstring() may change it. */
5263 comment_pos_copy = *comment_pos;
5264 comment_pos = &comment_pos_copy;
5265 }
5266 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005267
5268 /* If comment_pos is before rs_pos the raw string is inside the comment.
5269 * If rs_pos is before comment_pos the comment is inside the raw string. */
5270 if (comment_pos == NULL || (rs_pos != NULL && lt(*rs_pos, *comment_pos)))
5271 return rs_pos;
5272 return comment_pos;
5273}
5274
5275/*
5276 * Find the start of a raw string, not knowing if we are in one right now.
5277 * Search starts at w_cursor.lnum and goes backwards.
5278 * Return NULL when not inside a raw string.
5279 */
5280 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005281find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005282{
5283 pos_T *pos;
5284 char_u *line;
5285 char_u *p;
5286 int cur_maxcomment = ind_maxcomment;
5287
5288 for (;;)
5289 {
5290 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5291 if (pos == NULL)
5292 break;
5293
5294 /*
5295 * Check if the raw string start we found is inside a string.
5296 * If it is then restrict the search to below this line and try again.
5297 */
5298 line = ml_get(pos->lnum);
5299 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5300 p = skip_string(p);
5301 if ((colnr_T)(p - line) <= pos->col)
5302 break;
5303 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5304 if (cur_maxcomment <= 0)
5305 {
5306 pos = NULL;
5307 break;
5308 }
5309 }
5310 return pos;
5311}
5312
5313/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 * Skip to the end of a "string" and a 'c' character.
5315 * If there is no string or character, return argument unmodified.
5316 */
5317 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005318skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319{
5320 int i;
5321
5322 /*
5323 * We loop, because strings may be concatenated: "date""time".
5324 */
5325 for ( ; ; ++p)
5326 {
5327 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5328 {
5329 if (!p[1]) /* ' at end of line */
5330 break;
5331 i = 2;
5332 if (p[1] == '\\') /* '\n' or '\000' */
5333 {
5334 ++i;
5335 while (vim_isdigit(p[i - 1])) /* '\000' */
5336 ++i;
5337 }
5338 if (p[i] == '\'') /* check for trailing ' */
5339 {
5340 p += i;
5341 continue;
5342 }
5343 }
5344 else if (p[0] == '"') /* start of string */
5345 {
5346 for (++p; p[0]; ++p)
5347 {
5348 if (p[0] == '\\' && p[1] != NUL)
5349 ++p;
5350 else if (p[0] == '"') /* end of string */
5351 break;
5352 }
5353 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005354 continue; /* continue for another string */
5355 }
5356 else if (p[0] == 'R' && p[1] == '"')
5357 {
5358 /* Raw string: R"[delim](...)[delim]" */
5359 char_u *delim = p + 2;
5360 char_u *paren = vim_strchr(delim, '(');
5361
5362 if (paren != NULL)
5363 {
5364 size_t delim_len = paren - delim;
5365
5366 for (p += 3; *p; ++p)
5367 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5368 && p[delim_len + 1] == '"')
5369 {
5370 p += delim_len + 1;
5371 break;
5372 }
5373 if (p[0] == '"')
5374 continue; /* continue for another string */
5375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 }
5377 break; /* no string found */
5378 }
5379 if (!*p)
5380 --p; /* backup from NUL */
5381 return p;
5382}
5383#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5384
5385#if defined(FEAT_CINDENT) || defined(PROTO)
5386
5387/*
5388 * Do C or expression indenting on the current line.
5389 */
5390 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005391do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392{
5393# ifdef FEAT_EVAL
5394 if (*curbuf->b_p_inde != NUL)
5395 fixthisline(get_expr_indent);
5396 else
5397# endif
5398 fixthisline(get_c_indent);
5399}
5400
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005401/* Find result cache for cpp_baseclass */
5402typedef struct {
5403 int found;
5404 lpos_T lpos;
5405} cpp_baseclass_cache_T;
5406
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407/*
5408 * Functions for C-indenting.
5409 * Most of this originally comes from Eric Fischer.
5410 */
5411/*
5412 * Below "XXX" means that this function may unlock the current line.
5413 */
5414
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005415static char_u *cin_skipcomment(char_u *);
5416static int cin_nocode(char_u *);
5417static pos_T *find_line_comment(void);
5418static int cin_has_js_key(char_u *text);
5419static int cin_islabel_skip(char_u **);
5420static int cin_isdefault(char_u *);
5421static char_u *after_label(char_u *l);
5422static int get_indent_nolabel(linenr_T lnum);
5423static int skip_label(linenr_T, char_u **pp);
5424static int cin_first_id_amount(void);
5425static int cin_get_equal_amount(linenr_T lnum);
5426static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005427static int cin_iscomment(char_u *);
5428static int cin_islinecomment(char_u *);
5429static int cin_isterminated(char_u *, int, int);
5430static int cin_isinit(void);
5431static int cin_isfuncdecl(char_u **, linenr_T, linenr_T);
5432static int cin_isif(char_u *);
5433static int cin_iselse(char_u *);
5434static int cin_isdo(char_u *);
5435static int cin_iswhileofdo(char_u *, linenr_T);
5436static int cin_is_if_for_while_before_offset(char_u *line, int *poffset);
5437static int cin_iswhileofdo_end(int terminated);
5438static int cin_isbreak(char_u *);
5439static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached);
5440static int get_baseclass_amount(int col);
5441static int cin_ends_in(char_u *, char_u *, char_u *);
5442static int cin_starts_with(char_u *s, char *word);
5443static int cin_skip2pos(pos_T *trypos);
5444static pos_T *find_start_brace(void);
5445static pos_T *find_match_paren(int);
5446static pos_T *find_match_char(int c, int ind_maxparen);
5447static int corr_ind_maxparen(pos_T *startpos);
5448static int find_last_paren(char_u *l, int start, int end);
5449static int find_match(int lookfor, linenr_T ourscope);
5450static int cin_is_cpp_namespace(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451
5452/*
5453 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005454 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 */
5456 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005457cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458{
5459 while (*s)
5460 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005461 char_u *prev_s = s;
5462
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005464
5465 /* Perl/shell # comment comment continues until eol. Require a space
5466 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005467 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005468 {
5469 s += STRLEN(s);
5470 break;
5471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 if (*s != '/')
5473 break;
5474 ++s;
5475 if (*s == '/') /* slash-slash comment continues till eol */
5476 {
5477 s += STRLEN(s);
5478 break;
5479 }
5480 if (*s != '*')
5481 break;
5482 for (++s; *s; ++s) /* skip slash-star comment */
5483 if (s[0] == '*' && s[1] == '/')
5484 {
5485 s += 2;
5486 break;
5487 }
5488 }
5489 return s;
5490}
5491
5492/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005493 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 * not considered code.
5495 */
5496 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005497cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498{
5499 return *cin_skipcomment(s) == NUL;
5500}
5501
5502/*
5503 * Check previous lines for a "//" line comment, skipping over blank lines.
5504 */
5505 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005506find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507{
5508 static pos_T pos;
5509 char_u *line;
5510 char_u *p;
5511
5512 pos = curwin->w_cursor;
5513 while (--pos.lnum > 0)
5514 {
5515 line = ml_get(pos.lnum);
5516 p = skipwhite(line);
5517 if (cin_islinecomment(p))
5518 {
5519 pos.col = (int)(p - line);
5520 return &pos;
5521 }
5522 if (*p != NUL)
5523 break;
5524 }
5525 return NULL;
5526}
5527
5528/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005529 * Return TRUE if "text" starts with "key:".
5530 */
5531 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005532cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005533{
5534 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005535 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005536
5537 if (*s == '\'' || *s == '"')
5538 {
5539 /* can be 'key': or "key": */
5540 quote = *s;
5541 ++s;
5542 }
5543 if (!vim_isIDc(*s)) /* need at least one ID character */
5544 return FALSE;
5545
5546 while (vim_isIDc(*s))
5547 ++s;
5548 if (*s == quote)
5549 ++s;
5550
5551 s = cin_skipcomment(s);
5552
5553 /* "::" is not a label, it's C++ */
5554 return (*s == ':' && s[1] != ':');
5555}
5556
5557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005559 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 */
5561 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005562cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563{
5564 if (!vim_isIDc(**s)) /* need at least one ID character */
5565 return FALSE;
5566
5567 while (vim_isIDc(**s))
5568 (*s)++;
5569
5570 *s = cin_skipcomment(*s);
5571
5572 /* "::" is not a label, it's C++ */
5573 return (**s == ':' && *++*s != ':');
5574}
5575
5576/*
5577 * Recognize a label: "label:".
5578 * Note: curwin->w_cursor must be where we are looking for the label.
5579 */
5580 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005581cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582{
5583 char_u *s;
5584
5585 s = cin_skipcomment(ml_get_curline());
5586
5587 /*
5588 * Exclude "default" from labels, since it should be indented
5589 * like a switch label. Same for C++ scope declarations.
5590 */
5591 if (cin_isdefault(s))
5592 return FALSE;
5593 if (cin_isscopedecl(s))
5594 return FALSE;
5595
5596 if (cin_islabel_skip(&s))
5597 {
5598 /*
5599 * Only accept a label if the previous line is terminated or is a case
5600 * label.
5601 */
5602 pos_T cursor_save;
5603 pos_T *trypos;
5604 char_u *line;
5605
5606 cursor_save = curwin->w_cursor;
5607 while (curwin->w_cursor.lnum > 1)
5608 {
5609 --curwin->w_cursor.lnum;
5610
5611 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005612 * If we're in a comment or raw string now, skip to the start of
5613 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 */
5615 curwin->w_cursor.col = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005616 if ((trypos = ind_find_start_CORS()) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 curwin->w_cursor = *trypos;
5618
5619 line = ml_get_curline();
5620 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5621 continue;
5622 if (*(line = cin_skipcomment(line)) == NUL)
5623 continue;
5624
5625 curwin->w_cursor = cursor_save;
5626 if (cin_isterminated(line, TRUE, FALSE)
5627 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005628 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 || (cin_islabel_skip(&line) && cin_nocode(line)))
5630 return TRUE;
5631 return FALSE;
5632 }
5633 curwin->w_cursor = cursor_save;
5634 return TRUE; /* label at start of file??? */
5635 }
5636 return FALSE;
5637}
5638
5639/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005640 * Recognize structure initialization and enumerations:
5641 * "[typedef] [static|public|protected|private] enum"
5642 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 */
5644 static int
5645cin_isinit(void)
5646{
5647 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005648 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649
5650 s = cin_skipcomment(ml_get_curline());
5651
Bram Moolenaar75342212013-03-07 13:13:52 +01005652 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 s = cin_skipcomment(s + 7);
5654
Bram Moolenaar75342212013-03-07 13:13:52 +01005655 for (;;)
5656 {
5657 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005658
Bram Moolenaar75342212013-03-07 13:13:52 +01005659 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5660 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005661 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005662 if (cin_starts_with(s, skip[i]))
5663 {
5664 s = cin_skipcomment(s + l);
5665 l = 0;
5666 break;
5667 }
5668 }
5669 if (l != 0)
5670 break;
5671 }
5672
5673 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 return TRUE;
5675
5676 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5677 return TRUE;
5678
5679 return FALSE;
5680}
5681
5682/*
5683 * Recognize a switch label: "case .*:" or "default:".
5684 */
5685 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005686cin_iscase(
5687 char_u *s,
5688 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689{
5690 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005691 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 {
5693 for (s += 4; *s; ++s)
5694 {
5695 s = cin_skipcomment(s);
5696 if (*s == ':')
5697 {
5698 if (s[1] == ':') /* skip over "::" for C++ */
5699 ++s;
5700 else
5701 return TRUE;
5702 }
5703 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005704 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5706 return FALSE; /* stop at comment */
5707 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005708 {
5709 /* JS etc. */
5710 if (strict)
5711 return FALSE; /* stop at string */
5712 else
5713 return TRUE;
5714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 }
5716 return FALSE;
5717 }
5718
5719 if (cin_isdefault(s))
5720 return TRUE;
5721 return FALSE;
5722}
5723
5724/*
5725 * Recognize a "default" switch label.
5726 */
5727 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005728cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729{
5730 return (STRNCMP(s, "default", 7) == 0
5731 && *(s = cin_skipcomment(s + 7)) == ':'
5732 && s[1] != ':');
5733}
5734
5735/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005736 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 */
5738 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005739cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740{
5741 int i;
5742
5743 s = cin_skipcomment(s);
5744 if (STRNCMP(s, "public", 6) == 0)
5745 i = 6;
5746 else if (STRNCMP(s, "protected", 9) == 0)
5747 i = 9;
5748 else if (STRNCMP(s, "private", 7) == 0)
5749 i = 7;
5750 else
5751 return FALSE;
5752 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5753}
5754
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005755/* Maximum number of lines to search back for a "namespace" line. */
5756#define FIND_NAMESPACE_LIM 20
5757
5758/*
5759 * Recognize a "namespace" scope declaration.
5760 */
5761 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005762cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005763{
5764 char_u *p;
5765 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005766 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005767
5768 s = cin_skipcomment(s);
5769 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5770 {
5771 p = cin_skipcomment(skipwhite(s + 9));
5772 while (*p != NUL)
5773 {
5774 if (vim_iswhite(*p))
5775 {
5776 has_name = TRUE; /* found end of a name */
5777 p = cin_skipcomment(skipwhite(p));
5778 }
5779 else if (*p == '{')
5780 {
5781 break;
5782 }
5783 else if (vim_iswordc(*p))
5784 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005785 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005786 if (has_name)
5787 return FALSE; /* word character after skipping past name */
5788 ++p;
5789 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005790 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
5791 {
5792 if (!has_name_start || has_name)
5793 return FALSE;
5794 /* C++ 17 nested namespace */
5795 p += 3;
5796 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005797 else
5798 {
5799 return FALSE;
5800 }
5801 }
5802 return TRUE;
5803 }
5804 return FALSE;
5805}
5806
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807/*
5808 * Return a pointer to the first non-empty non-comment character after a ':'.
5809 * Return NULL if not found.
5810 * case 234: a = b;
5811 * ^
5812 */
5813 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005814after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815{
5816 for ( ; *l; ++l)
5817 {
5818 if (*l == ':')
5819 {
5820 if (l[1] == ':') /* skip over "::" for C++ */
5821 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005822 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 break;
5824 }
5825 else if (*l == '\'' && l[1] && l[2] == '\'')
5826 l += 2; /* skip over 'x' */
5827 }
5828 if (*l == NUL)
5829 return NULL;
5830 l = cin_skipcomment(l + 1);
5831 if (*l == NUL)
5832 return NULL;
5833 return l;
5834}
5835
5836/*
5837 * Get indent of line "lnum", skipping a label.
5838 * Return 0 if there is nothing after the label.
5839 */
5840 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005841get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842{
5843 char_u *l;
5844 pos_T fp;
5845 colnr_T col;
5846 char_u *p;
5847
5848 l = ml_get(lnum);
5849 p = after_label(l);
5850 if (p == NULL)
5851 return 0;
5852
5853 fp.col = (colnr_T)(p - l);
5854 fp.lnum = lnum;
5855 getvcol(curwin, &fp, &col, NULL, NULL);
5856 return (int)col;
5857}
5858
5859/*
5860 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005861 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 * label: if (asdf && asdfasdf)
5863 * ^
5864 */
5865 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005866skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867{
5868 char_u *l;
5869 int amount;
5870 pos_T cursor_save;
5871
5872 cursor_save = curwin->w_cursor;
5873 curwin->w_cursor.lnum = lnum;
5874 l = ml_get_curline();
5875 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005876 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 {
5878 amount = get_indent_nolabel(lnum);
5879 l = after_label(ml_get_curline());
5880 if (l == NULL) /* just in case */
5881 l = ml_get_curline();
5882 }
5883 else
5884 {
5885 amount = get_indent();
5886 l = ml_get_curline();
5887 }
5888 *pp = l;
5889
5890 curwin->w_cursor = cursor_save;
5891 return amount;
5892}
5893
5894/*
5895 * Return the indent of the first variable name after a type in a declaration.
5896 * int a, indent of "a"
5897 * static struct foo b, indent of "b"
5898 * enum bla c, indent of "c"
5899 * Returns zero when it doesn't look like a declaration.
5900 */
5901 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005902cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903{
5904 char_u *line, *p, *s;
5905 int len;
5906 pos_T fp;
5907 colnr_T col;
5908
5909 line = ml_get_curline();
5910 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005911 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 if (len == 6 && STRNCMP(p, "static", 6) == 0)
5913 {
5914 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005915 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 }
5917 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
5918 p = skipwhite(p + 6);
5919 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
5920 p = skipwhite(p + 4);
5921 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
5922 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
5923 {
5924 s = skipwhite(p + len);
5925 if ((STRNCMP(s, "int", 3) == 0 && vim_iswhite(s[3]))
5926 || (STRNCMP(s, "long", 4) == 0 && vim_iswhite(s[4]))
5927 || (STRNCMP(s, "short", 5) == 0 && vim_iswhite(s[5]))
5928 || (STRNCMP(s, "char", 4) == 0 && vim_iswhite(s[4])))
5929 p = s;
5930 }
5931 for (len = 0; vim_isIDc(p[len]); ++len)
5932 ;
5933 if (len == 0 || !vim_iswhite(p[len]) || cin_nocode(p))
5934 return 0;
5935
5936 p = skipwhite(p + len);
5937 fp.lnum = curwin->w_cursor.lnum;
5938 fp.col = (colnr_T)(p - line);
5939 getvcol(curwin, &fp, &col, NULL, NULL);
5940 return (int)col;
5941}
5942
5943/*
5944 * Return the indent of the first non-blank after an equal sign.
5945 * char *foo = "here";
5946 * Return zero if no (useful) equal sign found.
5947 * Return -1 if the line above "lnum" ends in a backslash.
5948 * foo = "asdf\
5949 * asdf\
5950 * here";
5951 */
5952 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005953cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954{
5955 char_u *line;
5956 char_u *s;
5957 colnr_T col;
5958 pos_T fp;
5959
5960 if (lnum > 1)
5961 {
5962 line = ml_get(lnum - 1);
5963 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
5964 return -1;
5965 }
5966
5967 line = s = ml_get(lnum);
5968 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
5969 {
5970 if (cin_iscomment(s)) /* ignore comments */
5971 s = cin_skipcomment(s);
5972 else
5973 ++s;
5974 }
5975 if (*s != '=')
5976 return 0;
5977
5978 s = skipwhite(s + 1);
5979 if (cin_nocode(s))
5980 return 0;
5981
5982 if (*s == '"') /* nice alignment for continued strings */
5983 ++s;
5984
5985 fp.lnum = lnum;
5986 fp.col = (colnr_T)(s - line);
5987 getvcol(curwin, &fp, &col, NULL, NULL);
5988 return (int)col;
5989}
5990
5991/*
5992 * Recognize a preprocessor statement: Any line that starts with '#'.
5993 */
5994 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005995cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02005997 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 return TRUE;
5999 return FALSE;
6000}
6001
6002/*
6003 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6004 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6005 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006006 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 */
6008 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006009cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010{
6011 char_u *line = *pp;
6012 linenr_T lnum = *lnump;
6013 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006014 int candidate_amount = *amount;
6015
6016 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6017 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006019 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 {
6021 if (cin_ispreproc(line))
6022 {
6023 retval = TRUE;
6024 *lnump = lnum;
6025 break;
6026 }
6027 if (lnum == 1)
6028 break;
6029 line = ml_get(--lnum);
6030 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6031 break;
6032 }
6033
6034 if (lnum != *lnump)
6035 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006036 if (retval)
6037 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 return retval;
6039}
6040
6041/*
6042 * Recognize the start of a C or C++ comment.
6043 */
6044 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006045cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046{
6047 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6048}
6049
6050/*
6051 * Recognize the start of a "//" comment.
6052 */
6053 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006054cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055{
6056 return (p[0] == '/' && p[1] == '/');
6057}
6058
6059/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006060 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6061 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006063 * If a line begins with an "else", only consider it terminated if no unmatched
6064 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 * Return the character terminating the line (ending char's have precedence if
6066 * both apply in order to determine initializations).
6067 */
6068 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006069cin_isterminated(
6070 char_u *s,
6071 int incl_open, /* include '{' at the end as terminator */
6072 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006074 char_u found_start = 0;
6075 unsigned n_open = 0;
6076 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077
6078 s = cin_skipcomment(s);
6079
6080 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6081 found_start = *s;
6082
Bram Moolenaar496f9512011-05-19 16:35:09 +02006083 if (!found_start)
6084 is_else = cin_iselse(s);
6085
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 while (*s)
6087 {
6088 /* skip over comments, "" strings and 'c'haracters */
6089 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006090 if (*s == '}' && n_open > 0)
6091 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006092 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006093 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 && cin_nocode(s + 1))
6095 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006096 else if (*s == '{')
6097 {
6098 if (incl_open && cin_nocode(s + 1))
6099 return *s;
6100 else
6101 ++n_open;
6102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103
6104 if (*s)
6105 s++;
6106 }
6107 return found_start;
6108}
6109
6110/*
6111 * Recognize the basic picture of a function declaration -- it needs to
6112 * have an open paren somewhere and a close paren at the end of the line and
6113 * no semicolons anywhere.
6114 * When a line ends in a comma we continue looking in the next line.
6115 * "sp" points to a string with the line. When looking at other lines it must
6116 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006117 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006118 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 */
6120 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006121cin_isfuncdecl(
6122 char_u **sp,
6123 linenr_T first_lnum,
6124 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125{
6126 char_u *s;
6127 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006128 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006130 pos_T *trypos;
6131 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132
6133 if (sp == NULL)
6134 s = ml_get(lnum);
6135 else
6136 s = *sp;
6137
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006138 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006139 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006140 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006141 {
6142 lnum = trypos->lnum;
6143 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006144 {
6145 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006146 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006147 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006148
6149 s = ml_get(lnum);
6150 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006151 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006152
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006153 /* Ignore line starting with #. */
6154 if (cin_ispreproc(s))
6155 return FALSE;
6156
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6158 {
6159 if (cin_iscomment(s)) /* ignore comments */
6160 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006161 else if (*s == ':')
6162 {
6163 if (*(s + 1) == ':')
6164 s += 2;
6165 else
6166 /* To avoid a mistake in the following situation:
6167 * A::A(int a, int b)
6168 * : a(0) // <--not a function decl
6169 * , b(0)
6170 * {...
6171 */
6172 return FALSE;
6173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 else
6175 ++s;
6176 }
6177 if (*s != '(')
6178 return FALSE; /* ';', ' or " before any () or no '(' */
6179
6180 while (*s && *s != ';' && *s != '\'' && *s != '"')
6181 {
6182 if (*s == ')' && cin_nocode(s + 1))
6183 {
6184 /* ')' at the end: may have found a match
6185 * Check for he previous line not to end in a backslash:
6186 * #if defined(x) && \
6187 * defined(y)
6188 */
6189 lnum = first_lnum - 1;
6190 s = ml_get(lnum);
6191 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6192 retval = TRUE;
6193 goto done;
6194 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006195 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006197 int comma = (*s == ',');
6198
6199 /* ',' at the end: continue looking in the next line.
6200 * At the end: check for ',' in the next line, for this style:
6201 * func(arg1
6202 * , arg2) */
6203 for (;;)
6204 {
6205 if (lnum >= curbuf->b_ml.ml_line_count)
6206 break;
6207 s = ml_get(++lnum);
6208 if (!cin_ispreproc(s))
6209 break;
6210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 if (lnum >= curbuf->b_ml.ml_line_count)
6212 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006213 /* Require a comma at end of the line or a comma or ')' at the
6214 * start of next line. */
6215 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006216 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006217 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006218 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 }
6220 else if (cin_iscomment(s)) /* ignore comments */
6221 s = cin_skipcomment(s);
6222 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006223 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006225 just_started = FALSE;
6226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 }
6228
6229done:
6230 if (lnum != first_lnum && sp != NULL)
6231 *sp = ml_get(first_lnum);
6232
6233 return retval;
6234}
6235
6236 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006237cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006239 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240}
6241
6242 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006243cin_iselse(
6244 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245{
6246 if (*p == '}') /* accept "} else" */
6247 p = cin_skipcomment(p + 1);
6248 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6249}
6250
6251 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006252cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253{
6254 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6255}
6256
6257/*
6258 * Check if this is a "while" that should have a matching "do".
6259 * We only accept a "while (condition) ;", with only white space between the
6260 * ')' and ';'. The condition may be spread over several lines.
6261 */
6262 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006263cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264{
6265 pos_T cursor_save;
6266 pos_T *trypos;
6267 int retval = FALSE;
6268
6269 p = cin_skipcomment(p);
6270 if (*p == '}') /* accept "} while (cond);" */
6271 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006272 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 {
6274 cursor_save = curwin->w_cursor;
6275 curwin->w_cursor.lnum = lnum;
6276 curwin->w_cursor.col = 0;
6277 p = ml_get_curline();
6278 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6279 {
6280 ++p;
6281 ++curwin->w_cursor.col;
6282 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006283 if ((trypos = findmatchlimit(NULL, 0, 0,
6284 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6286 retval = TRUE;
6287 curwin->w_cursor = cursor_save;
6288 }
6289 return retval;
6290}
6291
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006292/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006293 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006294 * Return 0 if there is none.
6295 * Otherwise return !0 and update "*poffset" to point to the place where the
6296 * string was found.
6297 */
6298 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006299cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006300{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006301 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006302
6303 if (offset-- < 2)
6304 return 0;
6305 while (offset > 2 && vim_iswhite(line[offset]))
6306 --offset;
6307
6308 offset -= 1;
6309 if (!STRNCMP(line + offset, "if", 2))
6310 goto probablyFound;
6311
6312 if (offset >= 1)
6313 {
6314 offset -= 1;
6315 if (!STRNCMP(line + offset, "for", 3))
6316 goto probablyFound;
6317
6318 if (offset >= 2)
6319 {
6320 offset -= 2;
6321 if (!STRNCMP(line + offset, "while", 5))
6322 goto probablyFound;
6323 }
6324 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006325 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006326
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006327probablyFound:
6328 if (!offset || !vim_isIDc(line[offset - 1]))
6329 {
6330 *poffset = offset;
6331 return 1;
6332 }
6333 return 0;
6334}
6335
6336/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006337 * Return TRUE if we are at the end of a do-while.
6338 * do
6339 * nothing;
6340 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006341 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006342 * Adjust the cursor to the line with "while".
6343 */
6344 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006345cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006346{
6347 char_u *line;
6348 char_u *p;
6349 char_u *s;
6350 pos_T *trypos;
6351 int i;
6352
6353 if (terminated != ';') /* there must be a ';' at the end */
6354 return FALSE;
6355
6356 p = line = ml_get_curline();
6357 while (*p != NUL)
6358 {
6359 p = cin_skipcomment(p);
6360 if (*p == ')')
6361 {
6362 s = skipwhite(p + 1);
6363 if (*s == ';' && cin_nocode(s + 1))
6364 {
6365 /* Found ");" at end of the line, now check there is "while"
6366 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006367 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006368 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006369 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006370 if (trypos != NULL)
6371 {
6372 s = cin_skipcomment(ml_get(trypos->lnum));
6373 if (*s == '}') /* accept "} while (cond);" */
6374 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006375 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006376 {
6377 curwin->w_cursor.lnum = trypos->lnum;
6378 return TRUE;
6379 }
6380 }
6381
6382 /* Searching may have made "line" invalid, get it again. */
6383 line = ml_get_curline();
6384 p = line + i;
6385 }
6386 }
6387 if (*p != NUL)
6388 ++p;
6389 }
6390 return FALSE;
6391}
6392
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006394cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395{
6396 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6397}
6398
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006399/*
6400 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 * constructor-initialization. eg:
6402 *
6403 * class MyClass :
6404 * baseClass <-- here
6405 * class MyClass : public baseClass,
6406 * anotherBaseClass <-- here (should probably lineup ??)
6407 * MyClass::MyClass(...) :
6408 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006409 *
6410 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 */
6412 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006413cin_is_cpp_baseclass(
6414 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006416 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 char_u *s;
6418 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006419 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006420 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006422 if (pos->lnum <= lnum)
6423 return cached->found; /* Use the cached result */
6424
6425 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006427 s = skipwhite(line);
6428 if (*s == '#') /* skip #define FOO x ? (x) : x */
6429 return FALSE;
6430 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 if (*s == NUL)
6432 return FALSE;
6433
6434 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6435
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006436 /* Search for a line starting with '#', empty, ending in ';' or containing
6437 * '{' or '}' and start below it. This handles the following situations:
6438 * a = cond ?
6439 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006440 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006441 * func::foo()
6442 * : something
6443 * {}
6444 * Foo::Foo (int one, int two)
6445 * : something(4),
6446 * somethingelse(3)
6447 * {}
6448 */
6449 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006451 line = ml_get(lnum - 1);
6452 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006453 if (*s == '#' || *s == NUL)
6454 break;
6455 while (*s != NUL)
6456 {
6457 s = cin_skipcomment(s);
6458 if (*s == '{' || *s == '}'
6459 || (*s == ';' && cin_nocode(s + 1)))
6460 break;
6461 if (*s != NUL)
6462 ++s;
6463 }
6464 if (*s != NUL)
6465 break;
6466 --lnum;
6467 }
6468
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006469 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006470 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006471 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006472 for (;;)
6473 {
6474 if (*s == NUL)
6475 {
6476 if (lnum == curwin->w_cursor.lnum)
6477 break;
6478 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006479 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006480 s = line;
6481 }
6482 if (s == line)
6483 {
6484 /* don't recognize "case (foo):" as a baseclass */
6485 if (cin_iscase(s, FALSE))
6486 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006487 s = cin_skipcomment(line);
6488 if (*s == NUL)
6489 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006490 }
6491
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006492 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006493 s = skip_string(s) + 1;
6494 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 {
6496 if (s[1] == ':')
6497 {
6498 /* skip double colon. It can't be a constructor
6499 * initialization any more */
6500 lookfor_ctor_init = FALSE;
6501 s = cin_skipcomment(s + 2);
6502 }
6503 else if (lookfor_ctor_init || class_or_struct)
6504 {
6505 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006506 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 cpp_base_class = TRUE;
6508 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006509 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 s = cin_skipcomment(s + 1);
6511 }
6512 else
6513 s = cin_skipcomment(s + 1);
6514 }
6515 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6516 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6517 {
6518 class_or_struct = TRUE;
6519 lookfor_ctor_init = FALSE;
6520
6521 if (*s == 'c')
6522 s = cin_skipcomment(s + 5);
6523 else
6524 s = cin_skipcomment(s + 6);
6525 }
6526 else
6527 {
6528 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6529 {
6530 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6531 }
6532 else if (s[0] == ')')
6533 {
6534 /* Constructor-initialization is assumed if we come across
6535 * something like "):" */
6536 class_or_struct = FALSE;
6537 lookfor_ctor_init = TRUE;
6538 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006539 else if (s[0] == '?')
6540 {
6541 /* Avoid seeing '() :' after '?' as constructor init. */
6542 return FALSE;
6543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 else if (!vim_isIDc(s[0]))
6545 {
6546 /* if it is not an identifier, we are wrong */
6547 class_or_struct = FALSE;
6548 lookfor_ctor_init = FALSE;
6549 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006550 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 {
6552 /* it can't be a constructor-initialization any more */
6553 lookfor_ctor_init = FALSE;
6554
6555 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006556 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006557 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 }
6559
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006560 /* When the line ends in a comma don't align with it. */
6561 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006562 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006563
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 s = cin_skipcomment(s + 1);
6565 }
6566 }
6567
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006568 cached->found = cpp_base_class;
6569 if (cpp_base_class)
6570 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 return cpp_base_class;
6572}
6573
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006574 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006575get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006576{
6577 int amount;
6578 colnr_T vcol;
6579 pos_T *trypos;
6580
6581 if (col == 0)
6582 {
6583 amount = get_indent();
6584 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006585 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006586 amount = get_indent_lnum(trypos->lnum); /* XXX */
6587 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006588 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006589 }
6590 else
6591 {
6592 curwin->w_cursor.col = col;
6593 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6594 amount = (int)vcol;
6595 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006596 if (amount < curbuf->b_ind_cpp_baseclass)
6597 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006598 return amount;
6599}
6600
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601/*
6602 * Return TRUE if string "s" ends with the string "find", possibly followed by
6603 * white space and comments. Skip strings and comments.
6604 * Ignore "ignore" after "find" if it's not NULL.
6605 */
6606 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006607cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608{
6609 char_u *p = s;
6610 char_u *r;
6611 int len = (int)STRLEN(find);
6612
6613 while (*p != NUL)
6614 {
6615 p = cin_skipcomment(p);
6616 if (STRNCMP(p, find, len) == 0)
6617 {
6618 r = skipwhite(p + len);
6619 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6620 r = skipwhite(r + STRLEN(ignore));
6621 if (cin_nocode(r))
6622 return TRUE;
6623 }
6624 if (*p != NUL)
6625 ++p;
6626 }
6627 return FALSE;
6628}
6629
6630/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006631 * Return TRUE when "s" starts with "word" and then a non-ID character.
6632 */
6633 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006634cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006635{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006636 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006637
6638 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6639}
6640
6641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 * Skip strings, chars and comments until at or past "trypos".
6643 * Return the column found.
6644 */
6645 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006646cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647{
6648 char_u *line;
6649 char_u *p;
6650
6651 p = line = ml_get(trypos->lnum);
6652 while (*p && (colnr_T)(p - line) < trypos->col)
6653 {
6654 if (cin_iscomment(p))
6655 p = cin_skipcomment(p);
6656 else
6657 {
6658 p = skip_string(p);
6659 ++p;
6660 }
6661 }
6662 return (int)(p - line);
6663}
6664
6665/*
6666 * Find the '{' at the start of the block we are in.
6667 * Return NULL if no match found.
6668 * Ignore a '{' that is in a comment, makes indenting the next three lines
6669 * work. */
6670/* foo() */
6671/* { */
6672/* } */
6673
6674 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006675find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676{
6677 pos_T cursor_save;
6678 pos_T *trypos;
6679 pos_T *pos;
6680 static pos_T pos_copy;
6681
6682 cursor_save = curwin->w_cursor;
6683 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6684 {
6685 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6686 trypos = &pos_copy;
6687 curwin->w_cursor = *trypos;
6688 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006689 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006691 && (pos = ind_find_start_CORS()) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 break;
6693 if (pos != NULL)
6694 curwin->w_cursor.lnum = pos->lnum;
6695 }
6696 curwin->w_cursor = cursor_save;
6697 return trypos;
6698}
6699
6700/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006701 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006702 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 */
6704 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006705find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006707 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006708}
6709
6710 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006711find_match_char (int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006712{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 pos_T cursor_save;
6714 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006715 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006716 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717
6718 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006719 ind_maxp_wk = ind_maxparen;
6720retry:
6721 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 {
6723 /* check if the ( is in a // comment */
6724 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01006725 {
6726 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
6727 if (ind_maxp_wk > 0)
6728 {
6729 curwin->w_cursor = *trypos;
6730 curwin->w_cursor.col = 0; /* XXX */
6731 goto retry;
6732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 else
6736 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01006737 pos_T *trypos_wk;
6738
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6740 trypos = &pos_copy;
6741 curwin->w_cursor = *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006742 if ((trypos_wk = ind_find_start_CORS()) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01006743 {
6744 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
6745 - trypos_wk->lnum);
6746 if (ind_maxp_wk > 0)
6747 {
6748 curwin->w_cursor = *trypos_wk;
6749 goto retry;
6750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 }
6754 }
6755 curwin->w_cursor = cursor_save;
6756 return trypos;
6757}
6758
6759/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006760 * Find the matching '(', ignoring it if it is in a comment or before an
6761 * unmatched {.
6762 * Return NULL if no match found.
6763 */
6764 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006765find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02006766{
6767 pos_T *trypos = find_match_paren(ind_maxparen);
6768
6769 if (trypos != NULL)
6770 {
6771 pos_T *tryposBrace = find_start_brace();
6772
6773 /* If both an unmatched '(' and '{' is found. Ignore the '('
6774 * position if the '{' is further down. */
6775 if (tryposBrace != NULL
6776 && (trypos->lnum != tryposBrace->lnum
6777 ? trypos->lnum < tryposBrace->lnum
6778 : trypos->col < tryposBrace->col))
6779 trypos = NULL;
6780 }
6781 return trypos;
6782}
6783
6784/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 * Return ind_maxparen corrected for the difference in line number between the
6786 * cursor position and "startpos". This makes sure that searching for a
6787 * matching paren above the cursor line doesn't find a match because of
6788 * looking a few lines further.
6789 */
6790 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006791corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792{
6793 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6794
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006795 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
6796 return curbuf->b_ind_maxparen - (int)n;
6797 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798}
6799
6800/*
6801 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006802 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 */
6804 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006805find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806{
6807 int i;
6808 int retval = FALSE;
6809 int open_count = 0;
6810
6811 curwin->w_cursor.col = 0; /* default is start of line */
6812
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006813 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 {
6815 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6816 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6817 if (l[i] == start)
6818 ++open_count;
6819 else if (l[i] == end)
6820 {
6821 if (open_count > 0)
6822 --open_count;
6823 else
6824 {
6825 curwin->w_cursor.col = i;
6826 retval = TRUE;
6827 }
6828 }
6829 }
6830 return retval;
6831}
6832
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006833/*
6834 * Parse 'cinoptions' and set the values in "curbuf".
6835 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
6836 */
6837 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006838parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006839{
6840 char_u *p;
6841 char_u *l;
6842 char_u *digits;
6843 int n;
6844 int divider;
6845 int fraction = 0;
6846 int sw = (int)get_sw_value(buf);
6847
6848 /*
6849 * Set the default values.
6850 */
6851 /* Spaces from a block's opening brace the prevailing indent for that
6852 * block should be. */
6853 buf->b_ind_level = sw;
6854
6855 /* Spaces from the edge of the line an open brace that's at the end of a
6856 * line is imagined to be. */
6857 buf->b_ind_open_imag = 0;
6858
6859 /* Spaces from the prevailing indent for a line that is not preceded by
6860 * an opening brace. */
6861 buf->b_ind_no_brace = 0;
6862
6863 /* Column where the first { of a function should be located }. */
6864 buf->b_ind_first_open = 0;
6865
6866 /* Spaces from the prevailing indent a leftmost open brace should be
6867 * located. */
6868 buf->b_ind_open_extra = 0;
6869
6870 /* Spaces from the matching open brace (real location for one at the left
6871 * edge; imaginary location from one that ends a line) the matching close
6872 * brace should be located. */
6873 buf->b_ind_close_extra = 0;
6874
6875 /* Spaces from the edge of the line an open brace sitting in the leftmost
6876 * column is imagined to be. */
6877 buf->b_ind_open_left_imag = 0;
6878
6879 /* Spaces jump labels should be shifted to the left if N is non-negative,
6880 * otherwise the jump label will be put to column 1. */
6881 buf->b_ind_jump_label = -1;
6882
6883 /* Spaces from the switch() indent a "case xx" label should be located. */
6884 buf->b_ind_case = sw;
6885
6886 /* Spaces from the "case xx:" code after a switch() should be located. */
6887 buf->b_ind_case_code = sw;
6888
6889 /* Lineup break at end of case in switch() with case label. */
6890 buf->b_ind_case_break = 0;
6891
6892 /* Spaces from the class declaration indent a scope declaration label
6893 * should be located. */
6894 buf->b_ind_scopedecl = sw;
6895
6896 /* Spaces from the scope declaration label code should be located. */
6897 buf->b_ind_scopedecl_code = sw;
6898
6899 /* Amount K&R-style parameters should be indented. */
6900 buf->b_ind_param = sw;
6901
6902 /* Amount a function type spec should be indented. */
6903 buf->b_ind_func_type = sw;
6904
6905 /* Amount a cpp base class declaration or constructor initialization
6906 * should be indented. */
6907 buf->b_ind_cpp_baseclass = sw;
6908
6909 /* additional spaces beyond the prevailing indent a continuation line
6910 * should be located. */
6911 buf->b_ind_continuation = sw;
6912
6913 /* Spaces from the indent of the line with an unclosed parentheses. */
6914 buf->b_ind_unclosed = sw * 2;
6915
6916 /* Spaces from the indent of the line with an unclosed parentheses, which
6917 * itself is also unclosed. */
6918 buf->b_ind_unclosed2 = sw;
6919
6920 /* Suppress ignoring spaces from the indent of a line starting with an
6921 * unclosed parentheses. */
6922 buf->b_ind_unclosed_noignore = 0;
6923
6924 /* If the opening paren is the last nonwhite character on the line, and
6925 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
6926 * context (for very long lines). */
6927 buf->b_ind_unclosed_wrapped = 0;
6928
6929 /* Suppress ignoring white space when lining up with the character after
6930 * an unclosed parentheses. */
6931 buf->b_ind_unclosed_whiteok = 0;
6932
6933 /* Indent a closing parentheses under the line start of the matching
6934 * opening parentheses. */
6935 buf->b_ind_matching_paren = 0;
6936
6937 /* Indent a closing parentheses under the previous line. */
6938 buf->b_ind_paren_prev = 0;
6939
6940 /* Extra indent for comments. */
6941 buf->b_ind_comment = 0;
6942
6943 /* Spaces from the comment opener when there is nothing after it. */
6944 buf->b_ind_in_comment = 3;
6945
6946 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
6947 * after the comment opener. */
6948 buf->b_ind_in_comment2 = 0;
6949
6950 /* Max lines to search for an open paren. */
6951 buf->b_ind_maxparen = 20;
6952
6953 /* Max lines to search for an open comment. */
6954 buf->b_ind_maxcomment = 70;
6955
6956 /* Handle braces for java code. */
6957 buf->b_ind_java = 0;
6958
6959 /* Not to confuse JS object properties with labels. */
6960 buf->b_ind_js = 0;
6961
6962 /* Handle blocked cases correctly. */
6963 buf->b_ind_keep_case_label = 0;
6964
6965 /* Handle C++ namespace. */
6966 buf->b_ind_cpp_namespace = 0;
6967
6968 /* Handle continuation lines containing conditions of if(), for() and
6969 * while(). */
6970 buf->b_ind_if_for_while = 0;
6971
6972 for (p = buf->b_p_cino; *p; )
6973 {
6974 l = p++;
6975 if (*p == '-')
6976 ++p;
6977 digits = p; /* remember where the digits start */
6978 n = getdigits(&p);
6979 divider = 0;
6980 if (*p == '.') /* ".5s" means a fraction */
6981 {
6982 fraction = atol((char *)++p);
6983 while (VIM_ISDIGIT(*p))
6984 {
6985 ++p;
6986 if (divider)
6987 divider *= 10;
6988 else
6989 divider = 10;
6990 }
6991 }
6992 if (*p == 's') /* "2s" means two times 'shiftwidth' */
6993 {
6994 if (p == digits)
6995 n = sw; /* just "s" is one 'shiftwidth' */
6996 else
6997 {
6998 n *= sw;
6999 if (divider)
7000 n += (sw * fraction + divider / 2) / divider;
7001 }
7002 ++p;
7003 }
7004 if (l[1] == '-')
7005 n = -n;
7006
7007 /* When adding an entry here, also update the default 'cinoptions' in
7008 * doc/indent.txt, and add explanation for it! */
7009 switch (*l)
7010 {
7011 case '>': buf->b_ind_level = n; break;
7012 case 'e': buf->b_ind_open_imag = n; break;
7013 case 'n': buf->b_ind_no_brace = n; break;
7014 case 'f': buf->b_ind_first_open = n; break;
7015 case '{': buf->b_ind_open_extra = n; break;
7016 case '}': buf->b_ind_close_extra = n; break;
7017 case '^': buf->b_ind_open_left_imag = n; break;
7018 case 'L': buf->b_ind_jump_label = n; break;
7019 case ':': buf->b_ind_case = n; break;
7020 case '=': buf->b_ind_case_code = n; break;
7021 case 'b': buf->b_ind_case_break = n; break;
7022 case 'p': buf->b_ind_param = n; break;
7023 case 't': buf->b_ind_func_type = n; break;
7024 case '/': buf->b_ind_comment = n; break;
7025 case 'c': buf->b_ind_in_comment = n; break;
7026 case 'C': buf->b_ind_in_comment2 = n; break;
7027 case 'i': buf->b_ind_cpp_baseclass = n; break;
7028 case '+': buf->b_ind_continuation = n; break;
7029 case '(': buf->b_ind_unclosed = n; break;
7030 case 'u': buf->b_ind_unclosed2 = n; break;
7031 case 'U': buf->b_ind_unclosed_noignore = n; break;
7032 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7033 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7034 case 'm': buf->b_ind_matching_paren = n; break;
7035 case 'M': buf->b_ind_paren_prev = n; break;
7036 case ')': buf->b_ind_maxparen = n; break;
7037 case '*': buf->b_ind_maxcomment = n; break;
7038 case 'g': buf->b_ind_scopedecl = n; break;
7039 case 'h': buf->b_ind_scopedecl_code = n; break;
7040 case 'j': buf->b_ind_java = n; break;
7041 case 'J': buf->b_ind_js = n; break;
7042 case 'l': buf->b_ind_keep_case_label = n; break;
7043 case '#': buf->b_ind_hash_comment = n; break;
7044 case 'N': buf->b_ind_cpp_namespace = n; break;
7045 case 'k': buf->b_ind_if_for_while = n; break;
7046 }
7047 if (*p == ',')
7048 ++p;
7049 }
7050}
7051
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007052/*
7053 * Return the desired indent for C code.
7054 * Return -1 if the indent should be left alone (inside a raw string).
7055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007057get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 pos_T cur_curpos;
7060 int amount;
7061 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007062 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 colnr_T col;
7064 char_u *theline;
7065 char_u *linecopy;
7066 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007067 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007069 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 pos_T our_paren_pos;
7071 char_u *start;
7072 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007073#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074#define BRACE_AT_START 2 /* '{' is at start of line */
7075#define BRACE_AT_END 3 /* '{' is at end of line */
7076 linenr_T ourscope;
7077 char_u *l;
7078 char_u *look;
7079 char_u terminated;
7080 int lookfor;
7081#define LOOKFOR_INITIAL 0
7082#define LOOKFOR_IF 1
7083#define LOOKFOR_DO 2
7084#define LOOKFOR_CASE 3
7085#define LOOKFOR_ANY 4
7086#define LOOKFOR_TERM 5
7087#define LOOKFOR_UNTERM 6
7088#define LOOKFOR_SCOPEDECL 7
7089#define LOOKFOR_NOBREAK 8
7090#define LOOKFOR_CPP_BASECLASS 9
7091#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007092#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007093#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094
7095 int whilelevel;
7096 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 int n;
7098 int iscase;
7099 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007100 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007102 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007103 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007104 int js_cur_has_key = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007105 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007107 /* make a copy, value is changed below */
7108 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109
7110 /* remember where the cursor was when we started */
7111 cur_curpos = curwin->w_cursor;
7112
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007113 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007114 if (cur_curpos.lnum == 1)
7115 return 0;
7116
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 /* Get a copy of the current contents of the line.
7118 * This is required, because only the most recent line obtained with
7119 * ml_get is valid! */
7120 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7121 if (linecopy == NULL)
7122 return 0;
7123
7124 /*
7125 * In insert mode and the cursor is on a ')' truncate the line at the
7126 * cursor position. We don't want to line up with the matching '(' when
7127 * inserting new stuff.
7128 * For unknown reasons the cursor might be past the end of the line, thus
7129 * check for that.
7130 */
7131 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007132 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 && linecopy[curwin->w_cursor.col] == ')')
7134 linecopy[curwin->w_cursor.col] = NUL;
7135
7136 theline = skipwhite(linecopy);
7137
7138 /* move the cursor to the start of the line */
7139
7140 curwin->w_cursor.col = 0;
7141
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007142 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007143
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007145 * If we are inside a raw string don't change the indent.
7146 * Ignore a raw string inside a comment.
7147 */
7148 comment_pos = ind_find_start_comment();
7149 if (comment_pos != NULL)
7150 {
7151 /* findmatchlimit() static pos is overwritten, make a copy */
7152 tryposCopy = *comment_pos;
7153 comment_pos = &tryposCopy;
7154 }
7155 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
7156 if (trypos != NULL && (comment_pos == NULL || lt(*trypos, *comment_pos)))
7157 {
7158 amount = -1;
7159 goto laterend;
7160 }
7161
7162 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 * #defines and so on always go at the left when included in 'cinkeys'.
7164 */
7165 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007166 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007167 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007168 goto theend;
7169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170
7171 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007172 * Is it a non-case label? Then that goes at the left margin too unless:
7173 * - JS flag is set.
7174 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007176 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007177 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 {
7179 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007180 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 }
7182
7183 /*
7184 * If we're inside a "//" comment and there is a "//" comment in a
7185 * previous line, lineup with that one.
7186 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007187 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 && (trypos = find_line_comment()) != NULL) /* XXX */
7189 {
7190 /* find how indented the line beginning the comment is */
7191 getvcol(curwin, trypos, &col, NULL, NULL);
7192 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007193 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 }
7195
7196 /*
7197 * If we're inside a comment and not looking at the start of the
7198 * comment, try using the 'comments' option.
7199 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007200 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 {
7202 int lead_start_len = 2;
7203 int lead_middle_len = 1;
7204 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7205 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7206 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7207 char_u *p;
7208 int start_align = 0;
7209 int start_off = 0;
7210 int done = FALSE;
7211
7212 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007213 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007215 *lead_start = NUL;
7216 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217
7218 p = curbuf->b_p_com;
7219 while (*p != NUL)
7220 {
7221 int align = 0;
7222 int off = 0;
7223 int what = 0;
7224
7225 while (*p != NUL && *p != ':')
7226 {
7227 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7228 what = *p++;
7229 else if (*p == COM_LEFT || *p == COM_RIGHT)
7230 align = *p++;
7231 else if (VIM_ISDIGIT(*p) || *p == '-')
7232 off = getdigits(&p);
7233 else
7234 ++p;
7235 }
7236
7237 if (*p == ':')
7238 ++p;
7239 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7240 if (what == COM_START)
7241 {
7242 STRCPY(lead_start, lead_end);
7243 lead_start_len = (int)STRLEN(lead_start);
7244 start_off = off;
7245 start_align = align;
7246 }
7247 else if (what == COM_MIDDLE)
7248 {
7249 STRCPY(lead_middle, lead_end);
7250 lead_middle_len = (int)STRLEN(lead_middle);
7251 }
7252 else if (what == COM_END)
7253 {
7254 /* If our line starts with the middle comment string, line it
7255 * up with the comment opener per the 'comments' option. */
7256 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7257 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7258 {
7259 done = TRUE;
7260 if (curwin->w_cursor.lnum > 1)
7261 {
7262 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007263 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 * the middle comment string matches in the previous
7265 * line, use the indent of that line. XXX */
7266 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7267 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7268 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7269 else if (STRNCMP(look, lead_middle,
7270 lead_middle_len) == 0)
7271 {
7272 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7273 break;
7274 }
7275 /* If the start comment string doesn't match with the
7276 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007277 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 lead_start, lead_start_len) != 0)
7279 continue;
7280 }
7281 if (start_off != 0)
7282 amount += start_off;
7283 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007284 amount += vim_strsize(lead_start)
7285 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 break;
7287 }
7288
7289 /* If our line starts with the end comment string, line it up
7290 * with the middle comment */
7291 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7292 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7293 {
7294 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7295 /* XXX */
7296 if (off != 0)
7297 amount += off;
7298 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007299 amount += vim_strsize(lead_start)
7300 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 done = TRUE;
7302 break;
7303 }
7304 }
7305 }
7306
7307 /* If our line starts with an asterisk, line up with the
7308 * asterisk in the comment opener; otherwise, line up
7309 * with the first character of the comment text.
7310 */
7311 if (done)
7312 ;
7313 else if (theline[0] == '*')
7314 amount += 1;
7315 else
7316 {
7317 /*
7318 * If we are more than one line away from the comment opener, take
7319 * the indent of the previous non-empty line. If 'cino' has "CO"
7320 * and we are just below the comment opener and there are any
7321 * white characters after it line up with the text after it;
7322 * otherwise, add the amount specified by "c" in 'cino'
7323 */
7324 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007325 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 {
7327 if (linewhite(lnum)) /* skip blank lines */
7328 continue;
7329 amount = get_indent_lnum(lnum); /* XXX */
7330 break;
7331 }
7332 if (amount == -1) /* use the comment opener */
7333 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007334 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007336 start = ml_get(comment_pos->lnum);
7337 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007339 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007341 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007343 if (curbuf->b_ind_in_comment2 || *look == NUL)
7344 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 }
7346 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007347 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 }
7349
7350 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007351 * Are we looking at a ']' that has a match?
7352 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007353 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007354 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7355 {
7356 /* align with the line containing the '['. */
7357 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007358 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007359 }
7360
7361 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 * Are we inside parentheses or braces?
7363 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007364 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007365 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007366 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 || trypos != NULL)
7368 {
7369 if (trypos != NULL && tryposBrace != NULL)
7370 {
7371 /* Both an unmatched '(' and '{' is found. Use the one which is
7372 * closer to the current cursor position, set the other to NULL. */
7373 if (trypos->lnum != tryposBrace->lnum
7374 ? trypos->lnum < tryposBrace->lnum
7375 : trypos->col < tryposBrace->col)
7376 trypos = NULL;
7377 else
7378 tryposBrace = NULL;
7379 }
7380
7381 if (trypos != NULL)
7382 {
7383 /*
7384 * If the matching paren is more than one line away, use the indent of
7385 * a previous non-empty line that matches the same paren.
7386 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007387 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007389 /* Line up with the start of the matching paren line. */
7390 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7391 }
7392 else
7393 {
7394 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007395 our_paren_pos = *trypos;
7396 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007398 l = skipwhite(ml_get(lnum));
7399 if (cin_nocode(l)) /* skip comment lines */
7400 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007401 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007402 continue; /* ignore #define, #if, etc. */
7403 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007405 /* Skip a comment or raw string. XXX */
7406 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007407 {
7408 lnum = trypos->lnum + 1;
7409 continue;
7410 }
7411
7412 /* XXX */
7413 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007414 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007415 && trypos->lnum == our_paren_pos.lnum
7416 && trypos->col == our_paren_pos.col)
7417 {
7418 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007420 if (theline[0] == ')')
7421 {
7422 if (our_paren_pos.lnum != lnum
7423 && cur_amount > amount)
7424 cur_amount = amount;
7425 amount = -1;
7426 }
7427 break;
7428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 }
7430 }
7431
7432 /*
7433 * Line up with line where the matching paren is. XXX
7434 * If the line starts with a '(' or the indent for unclosed
7435 * parentheses is zero, line up with the unclosed parentheses.
7436 */
7437 if (amount == -1)
7438 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007439 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007440 int is_if_for_while = 0;
7441
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007442 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007443 {
7444 /* Look for the outermost opening parenthesis on this line
7445 * and check whether it belongs to an "if", "for" or "while". */
7446
7447 pos_T cursor_save = curwin->w_cursor;
7448 pos_T outermost;
7449 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007450
7451 trypos = &our_paren_pos;
7452 do {
7453 outermost = *trypos;
7454 curwin->w_cursor.lnum = outermost.lnum;
7455 curwin->w_cursor.col = outermost.col;
7456
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007457 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007458 } while (trypos && trypos->lnum == outermost.lnum);
7459
7460 curwin->w_cursor = cursor_save;
7461
7462 line = ml_get(outermost.lnum);
7463
7464 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007465 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007466 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007467
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007468 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007469 look = skipwhite(look);
7470 if (*look == '(')
7471 {
7472 linenr_T save_lnum = curwin->w_cursor.lnum;
7473 char_u *line;
7474 int look_col;
7475
7476 /* Ignore a '(' in front of the line that has a match before
7477 * our matching '('. */
7478 curwin->w_cursor.lnum = our_paren_pos.lnum;
7479 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007480 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007481 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007482 if ((trypos = findmatchlimit(NULL, ')', 0,
7483 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007484 != NULL
7485 && trypos->lnum == our_paren_pos.lnum
7486 && trypos->col < our_paren_pos.col)
7487 ignore_paren_col = trypos->col + 1;
7488
7489 curwin->w_cursor.lnum = save_lnum;
7490 look = ml_get(our_paren_pos.lnum) + look_col;
7491 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007492 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7493 && is_if_for_while == 0)
7494 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007495 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 {
7497 /*
7498 * If we're looking at a close paren, line up right there;
7499 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007500 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 * the last nonwhite character of the line, use either the
7502 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007503 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 * lines).
7505 */
7506 if (theline[0] != ')')
7507 {
7508 cur_amount = MAXCOL;
7509 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007510 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 && cin_ends_in(l, (char_u *)"(", NULL))
7512 {
7513 /* look for opening unmatched paren, indent one level
7514 * for each additional level */
7515 n = 1;
7516 for (col = 0; col < our_paren_pos.col; ++col)
7517 {
7518 switch (l[col])
7519 {
7520 case '(':
7521 case '{': ++n;
7522 break;
7523
7524 case ')':
7525 case '}': if (n > 1)
7526 --n;
7527 break;
7528 }
7529 }
7530
7531 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007532 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007534 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 our_paren_pos.col++;
7536 else
7537 {
7538 col = our_paren_pos.col + 1;
7539 while (vim_iswhite(l[col]))
7540 col++;
7541 if (l[col] != NUL) /* In case of trailing space */
7542 our_paren_pos.col = col;
7543 else
7544 our_paren_pos.col++;
7545 }
7546 }
7547
7548 /*
7549 * Find how indented the paren is, or the character after it
7550 * if we did the above "if".
7551 */
7552 if (our_paren_pos.col > 0)
7553 {
7554 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7555 if (cur_amount > (int)col)
7556 cur_amount = col;
7557 }
7558 }
7559
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007560 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 {
7562 /* Line up with the start of the matching paren line. */
7563 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007564 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7565 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007566 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 {
7568 if (cur_amount != MAXCOL)
7569 amount = cur_amount;
7570 }
7571 else
7572 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007573 /* Add b_ind_unclosed2 for each '(' before our matching one,
7574 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007576 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 {
7578 --our_paren_pos.col;
7579 switch (*ml_get_pos(&our_paren_pos))
7580 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007581 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 col = our_paren_pos.col;
7583 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007584 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 col = MAXCOL;
7586 break;
7587 }
7588 }
7589
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007590 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 * braces */
7592 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007593 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 else
7595 {
7596 curwin->w_cursor.lnum = our_paren_pos.lnum;
7597 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007598 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7599 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007600 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007602 {
7603 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007604 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007605 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007606 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 }
7609 /*
7610 * For a line starting with ')' use the minimum of the two
7611 * positions, to avoid giving it more indent than the previous
7612 * lines:
7613 * func_long_name( if (x
7614 * arg && yy
7615 * ) ^ not here ) ^ not here
7616 */
7617 if (cur_amount < amount)
7618 amount = cur_amount;
7619 }
7620 }
7621
7622 /* add extra indent for a comment */
7623 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007624 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 else
7627 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007628 /*
7629 * We are inside braces, there is a { before this line at the position
7630 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007631 * Make a copy of tryposBrace, it may point to pos_copy inside
7632 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007633 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007634 tryposCopy = *tryposBrace;
7635 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 ourscope = trypos->lnum;
7638 start = ml_get(ourscope);
7639
7640 /*
7641 * Now figure out how indented the line is in general.
7642 * If the brace was at the start of the line, we use that;
7643 * otherwise, check out the indentation of the line as
7644 * a whole and then add the "imaginary indent" to that.
7645 */
7646 look = skipwhite(start);
7647 if (*look == '{')
7648 {
7649 getvcol(curwin, trypos, &col, NULL, NULL);
7650 amount = col;
7651 if (*start == '{')
7652 start_brace = BRACE_IN_COL0;
7653 else
7654 start_brace = BRACE_AT_START;
7655 }
7656 else
7657 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007658 /* That opening brace might have been on a continuation
7659 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 curwin->w_cursor.lnum = ourscope;
7661
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007662 /* Position the cursor over the rightmost paren, so that
7663 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 lnum = ourscope;
7665 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007666 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7667 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 lnum = trypos->lnum;
7669
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007670 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 * case 1: if (asdf &&
7672 * ldfd) {
7673 * }
7674 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007675 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7676 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007678 else if (curbuf->b_ind_js)
7679 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007681 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682
7683 start_brace = BRACE_AT_END;
7684 }
7685
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007686 /* For Javascript check if the line starts with "key:". */
7687 if (curbuf->b_ind_js)
7688 js_cur_has_key = cin_has_js_key(theline);
7689
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007691 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 * we want to be. otherwise, add the amount of room
7693 * that an indent is supposed to be.
7694 */
7695 if (theline[0] == '}')
7696 {
7697 /*
7698 * they may want closing braces to line up with something
7699 * other than the open brace. indulge them, if so.
7700 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007701 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 }
7703 else
7704 {
7705 /*
7706 * If we're looking at an "else", try to find an "if"
7707 * to match it with.
7708 * If we're looking at a "while", try to find a "do"
7709 * to match it with.
7710 */
7711 lookfor = LOOKFOR_INITIAL;
7712 if (cin_iselse(theline))
7713 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007714 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 lookfor = LOOKFOR_DO;
7716 if (lookfor != LOOKFOR_INITIAL)
7717 {
7718 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007719 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 {
7721 amount = get_indent(); /* XXX */
7722 goto theend;
7723 }
7724 }
7725
7726 /*
7727 * We get here if we are not on an "while-of-do" or "else" (or
7728 * failed to find a matching "if").
7729 * Search backwards for something to line up with.
7730 * First set amount for when we don't find anything.
7731 */
7732
7733 /*
7734 * if the '{' is _really_ at the left margin, use the imaginary
7735 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007736 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 */
7738
7739 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7740 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007741 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007742 lookfor_cpp_namespace = TRUE;
7743 }
7744 else if (start_brace == BRACE_AT_START &&
7745 lookfor_cpp_namespace) /* '{' is at start */
7746 {
7747
7748 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 }
7750 else
7751 {
7752 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007753 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007754 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007755
7756 l = skipwhite(ml_get_curline());
7757 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007758 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 else
7761 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007762 /* Compensate for adding b_ind_open_extra later. */
7763 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 if (amount < 0)
7765 amount = 0;
7766 }
7767 }
7768
7769 lookfor_break = FALSE;
7770
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007771 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 {
7773 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007774 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 }
7776 else if (cin_isscopedecl(theline)) /* private:, ... */
7777 {
7778 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007779 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 }
7781 else
7782 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007783 if (curbuf->b_ind_case_break && cin_isbreak(theline))
7784 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 lookfor_break = TRUE;
7786
7787 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007788 /* b_ind_level from start of block */
7789 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 }
7791 scope_amount = amount;
7792 whilelevel = 0;
7793
7794 /*
7795 * Search backwards. If we find something we recognize, line up
7796 * with that.
7797 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007798 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 * the usual amount relative to the conditional
7800 * that opens the block.
7801 */
7802 curwin->w_cursor = cur_curpos;
7803 for (;;)
7804 {
7805 curwin->w_cursor.lnum--;
7806 curwin->w_cursor.col = 0;
7807
7808 /*
7809 * If we went all the way back to the start of our scope, line
7810 * up with it.
7811 */
7812 if (curwin->w_cursor.lnum <= ourscope)
7813 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007814 /* We reached end of scope:
7815 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007817 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 * don't add ind_continuation, otherwise it is a variable
7819 * declaration:
7820 * int x,
7821 * here; <-- add ind_continuation
7822 */
7823 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7824 {
7825 if (curwin->w_cursor.lnum == 0
7826 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007827 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007829 /* nothing found (abuse curbuf->b_ind_maxparen as
7830 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 * initialization) */
7832 if (cont_amount > 0)
7833 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007834 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 amount += ind_continuation;
7836 break;
7837 }
7838
7839 l = ml_get_curline();
7840
7841 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007842 * If we're in a comment or raw string now, skip to
7843 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007845 trypos = ind_find_start_CORS();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 if (trypos != NULL)
7847 {
7848 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007849 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 continue;
7851 }
7852
7853 /*
7854 * Skip preprocessor directives and blank lines.
7855 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007856 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
7857 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 continue;
7859
7860 if (cin_nocode(l))
7861 continue;
7862
7863 terminated = cin_isterminated(l, FALSE, TRUE);
7864
7865 /*
7866 * If we are at top level and the line looks like a
7867 * function declaration, we are done
7868 * (it's a variable declaration).
7869 */
7870 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007871 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 {
7873 /* if the line is terminated with another ','
7874 * it is a continued variable initialization.
7875 * don't add extra indent.
7876 * TODO: does not work, if a function
7877 * declaration is split over multiple lines:
7878 * cin_isfuncdecl returns FALSE then.
7879 */
7880 if (terminated == ',')
7881 break;
7882
7883 /* if it es a enum declaration or an assignment,
7884 * we are done.
7885 */
7886 if (terminated != ';' && cin_isinit())
7887 break;
7888
7889 /* nothing useful found */
7890 if (terminated == 0 || terminated == '{')
7891 continue;
7892 }
7893
7894 if (terminated != ';')
7895 {
7896 /* Skip parens and braces. Position the cursor
7897 * over the rightmost paren, so that matching it
7898 * will take us back to the start of the line.
7899 */ /* XXX */
7900 trypos = NULL;
7901 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007902 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007903 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904
7905 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007906 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907
7908 if (trypos != NULL)
7909 {
7910 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007911 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 continue;
7913 }
7914 }
7915
7916 /* it's a variable declaration, add indentation
7917 * like in
7918 * int a,
7919 * b;
7920 */
7921 if (cont_amount > 0)
7922 amount = cont_amount;
7923 else
7924 amount += ind_continuation;
7925 }
7926 else if (lookfor == LOOKFOR_UNTERM)
7927 {
7928 if (cont_amount > 0)
7929 amount = cont_amount;
7930 else
7931 amount += ind_continuation;
7932 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02007933 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007934 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02007935 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01007936 && lookfor != LOOKFOR_CPP_BASECLASS
7937 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02007938 {
7939 amount = scope_amount;
7940 if (theline[0] == '{')
7941 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007942 amount += curbuf->b_ind_open_extra;
7943 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007944 }
7945 }
7946
7947 if (lookfor_cpp_namespace)
7948 {
7949 /*
7950 * Looking for C++ namespace, need to look further
7951 * back.
7952 */
7953 if (curwin->w_cursor.lnum == ourscope)
7954 continue;
7955
7956 if (curwin->w_cursor.lnum == 0
7957 || curwin->w_cursor.lnum
7958 < ourscope - FIND_NAMESPACE_LIM)
7959 break;
7960
7961 l = ml_get_curline();
7962
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007963 /* If we're in a comment or raw string now, skip
7964 * to the start of it. */
7965 trypos = ind_find_start_CORS();
Bram Moolenaare79d1532011-10-04 18:03:47 +02007966 if (trypos != NULL)
7967 {
7968 curwin->w_cursor.lnum = trypos->lnum + 1;
7969 curwin->w_cursor.col = 0;
7970 continue;
7971 }
7972
7973 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007974 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
7975 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02007976 continue;
7977
7978 /* Finally the actual check for "namespace". */
7979 if (cin_is_cpp_namespace(l))
7980 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007981 amount += curbuf->b_ind_cpp_namespace
7982 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007983 break;
7984 }
7985
7986 if (cin_nocode(l))
7987 continue;
7988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 }
7990 break;
7991 }
7992
7993 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007994 * If we're in a comment or raw string now, skip to the start
7995 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007997 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 {
7999 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008000 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 continue;
8002 }
8003
8004 l = ml_get_curline();
8005
8006 /*
8007 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008008 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008010 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 if (iscase || cin_isscopedecl(l))
8012 {
8013 /* we are only looking for cpp base class
8014 * declaration/initialization any longer */
8015 if (lookfor == LOOKFOR_CPP_BASECLASS)
8016 break;
8017
8018 /* When looking for a "do" we are not interested in
8019 * labels. */
8020 if (whilelevel > 0)
8021 continue;
8022
8023 /*
8024 * case xx:
8025 * c = 99 + <- this indent plus continuation
8026 *-> here;
8027 */
8028 if (lookfor == LOOKFOR_UNTERM
8029 || lookfor == LOOKFOR_ENUM_OR_INIT)
8030 {
8031 if (cont_amount > 0)
8032 amount = cont_amount;
8033 else
8034 amount += ind_continuation;
8035 break;
8036 }
8037
8038 /*
8039 * case xx: <- line up with this case
8040 * x = 333;
8041 * case yy:
8042 */
8043 if ( (iscase && lookfor == LOOKFOR_CASE)
8044 || (iscase && lookfor_break)
8045 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8046 {
8047 /*
8048 * Check that this case label is not for another
8049 * switch()
8050 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008051 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008052 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 {
8054 amount = get_indent(); /* XXX */
8055 break;
8056 }
8057 continue;
8058 }
8059
8060 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8061
8062 /*
8063 * case xx: if (cond) <- line up with this if
8064 * y = y + 1;
8065 * -> s = 99;
8066 *
8067 * case xx:
8068 * if (cond) <- line up with this line
8069 * y = y + 1;
8070 * -> s = 99;
8071 */
8072 if (lookfor == LOOKFOR_TERM)
8073 {
8074 if (n)
8075 amount = n;
8076
8077 if (!lookfor_break)
8078 break;
8079 }
8080
8081 /*
8082 * case xx: x = x + 1; <- line up with this x
8083 * -> y = y + 1;
8084 *
8085 * case xx: if (cond) <- line up with this if
8086 * -> y = y + 1;
8087 */
8088 if (n)
8089 {
8090 amount = n;
8091 l = after_label(ml_get_curline());
8092 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008093 {
8094 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008095 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008096 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008097 amount += curbuf->b_ind_level
8098 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 break;
8101 }
8102
8103 /*
8104 * Try to get the indent of a statement before the switch
8105 * label. If nothing is found, line up relative to the
8106 * switch label.
8107 * break; <- may line up with this line
8108 * case xx:
8109 * -> y = 1;
8110 */
8111 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008112 ? curbuf->b_ind_case_code
8113 : curbuf->b_ind_scopedecl_code);
8114 lookfor = curbuf->b_ind_case_break
8115 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 continue;
8117 }
8118
8119 /*
8120 * Looking for a switch() label or C++ scope declaration,
8121 * ignore other lines, skip {}-blocks.
8122 */
8123 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8124 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008125 if (find_last_paren(l, '{', '}')
8126 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008127 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008129 curwin->w_cursor.col = 0;
8130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 continue;
8132 }
8133
8134 /*
8135 * Ignore jump labels with nothing after them.
8136 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008137 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 {
8139 l = after_label(ml_get_curline());
8140 if (l == NULL || cin_nocode(l))
8141 continue;
8142 }
8143
8144 /*
8145 * Ignore #defines, #if, etc.
8146 * Ignore comment and empty lines.
8147 * (need to get the line again, cin_islabel() may have
8148 * unlocked it)
8149 */
8150 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008151 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 || cin_nocode(l))
8153 continue;
8154
8155 /*
8156 * Are we at the start of a cpp base class declaration or
8157 * constructor initialization?
8158 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008159 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008160 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008161 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008162 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008163 l = ml_get_curline();
8164 }
8165 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 {
8167 if (lookfor == LOOKFOR_UNTERM)
8168 {
8169 if (cont_amount > 0)
8170 amount = cont_amount;
8171 else
8172 amount += ind_continuation;
8173 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008174 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008176 /* Need to find start of the declaration. */
8177 lookfor = LOOKFOR_UNTERM;
8178 ind_continuation = 0;
8179 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 }
8181 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008182 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008183 amount = get_baseclass_amount(
8184 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 break;
8186 }
8187 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8188 {
8189 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008190 * declaration or initialization before the opening brace.
8191 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 if (cin_isterminated(l, TRUE, FALSE))
8193 break;
8194 else
8195 continue;
8196 }
8197
8198 /*
8199 * What happens next depends on the line being terminated.
8200 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008201 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 * 123,
8203 * sizeof
8204 * here
8205 * Otherwise check whether it is a enumeration or structure
8206 * initialisation (not indented) or a variable declaration
8207 * (indented).
8208 */
8209 terminated = cin_isterminated(l, FALSE, TRUE);
8210
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008211 if (js_cur_has_key)
8212 {
8213 js_cur_has_key = 0; /* only check the first line */
8214 if (curbuf->b_ind_js && terminated == ',')
8215 {
8216 /* For Javascript we might be inside an object:
8217 * key: something, <- align with this
8218 * key: something
8219 * or:
8220 * key: something + <- align with this
8221 * something,
8222 * key: something
8223 */
8224 lookfor = LOOKFOR_JS_KEY;
8225 }
8226 }
8227 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8228 {
8229 amount = get_indent();
8230 break;
8231 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008232 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008233 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008234 if (tryposBrace != NULL && tryposBrace->lnum
8235 >= curwin->w_cursor.lnum)
8236 break;
8237 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008238 /* line below current line is the one that starts a
8239 * (possibly broken) line ending in a comma. */
8240 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008241 else
8242 {
8243 amount = get_indent();
8244 if (curwin->w_cursor.lnum - 1 == ourscope)
8245 /* line above is start of the scope, thus current
8246 * line is the one that stars a (possibly broken)
8247 * line ending in a comma. */
8248 break;
8249 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008250 }
8251
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8253 && terminated == ','))
8254 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008255 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8256 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008257 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 /*
8259 * if we're in the middle of a paren thing,
8260 * go back to the line that starts it so
8261 * we can get the right prevailing indent
8262 * if ( foo &&
8263 * bar )
8264 */
8265 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008266 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008268 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 */
8270 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008271 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008272 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8273 || (trypos->lnum == tryposBrace->lnum
8274 && trypos->col < tryposBrace->col)))
8275 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276
8277 /*
8278 * If we are looking for ',', we also look for matching
8279 * braces.
8280 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008281 if (trypos == NULL && terminated == ','
8282 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008283 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284
8285 if (trypos != NULL)
8286 {
8287 /*
8288 * Check if we are on a case label now. This is
8289 * handled above.
8290 * case xx: if ( asdf &&
8291 * asdf)
8292 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008293 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008295 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 {
8297 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008298 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 continue;
8300 }
8301 }
8302
8303 /*
8304 * Skip over continuation lines to find the one to get the
8305 * indent from
8306 * char *usethis = "bla\
8307 * bla",
8308 * here;
8309 */
8310 if (terminated == ',')
8311 {
8312 while (curwin->w_cursor.lnum > 1)
8313 {
8314 l = ml_get(curwin->w_cursor.lnum - 1);
8315 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8316 break;
8317 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008318 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 }
8320 }
8321
8322 /*
8323 * Get indent and pointer to text for current line,
8324 * ignoring any jump label. XXX
8325 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008326 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008327 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008328 else
8329 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 /*
8331 * If this is just above the line we are indenting, and it
8332 * starts with a '{', line it up with this line.
8333 * while (not)
8334 * -> {
8335 * }
8336 */
8337 if (terminated != ',' && lookfor != LOOKFOR_TERM
8338 && theline[0] == '{')
8339 {
8340 amount = cur_amount;
8341 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008342 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 * doesn't start with a '{', which must have a match
8344 * in the same line (scope is the same). Probably:
8345 * { 1, 2 },
8346 * -> { 3, 4 }
8347 */
8348 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008349 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008351 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 {
8353 /* have to look back, whether it is a cpp base
8354 * class declaration or initialization */
8355 lookfor = LOOKFOR_CPP_BASECLASS;
8356 continue;
8357 }
8358 break;
8359 }
8360
8361 /*
8362 * Check if we are after an "if", "while", etc.
8363 * Also allow " } else".
8364 */
8365 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8366 {
8367 /*
8368 * Found an unterminated line after an if (), line up
8369 * with the last one.
8370 * if (cond)
8371 * 100 +
8372 * -> here;
8373 */
8374 if (lookfor == LOOKFOR_UNTERM
8375 || lookfor == LOOKFOR_ENUM_OR_INIT)
8376 {
8377 if (cont_amount > 0)
8378 amount = cont_amount;
8379 else
8380 amount += ind_continuation;
8381 break;
8382 }
8383
8384 /*
8385 * If this is just above the line we are indenting, we
8386 * are finished.
8387 * while (not)
8388 * -> here;
8389 * Otherwise this indent can be used when the line
8390 * before this is terminated.
8391 * yyy;
8392 * if (stat)
8393 * while (not)
8394 * xxx;
8395 * -> here;
8396 */
8397 amount = cur_amount;
8398 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008399 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 if (lookfor != LOOKFOR_TERM)
8401 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008402 amount += curbuf->b_ind_level
8403 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 break;
8405 }
8406
8407 /*
8408 * Special trick: when expecting the while () after a
8409 * do, line up with the while()
8410 * do
8411 * x = 1;
8412 * -> here
8413 */
8414 l = skipwhite(ml_get_curline());
8415 if (cin_isdo(l))
8416 {
8417 if (whilelevel == 0)
8418 break;
8419 --whilelevel;
8420 }
8421
8422 /*
8423 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008424 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 * Need to use the scope of this "else". XXX
8426 * If whilelevel != 0 continue looking for a "do {".
8427 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008428 if (cin_iselse(l) && whilelevel == 0)
8429 {
8430 /* If we're looking at "} else", let's make sure we
8431 * find the opening brace of the enclosing scope,
8432 * not the one from "if () {". */
8433 if (*l == '}')
8434 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008435 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008436
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008437 if ((trypos = find_start_brace()) == NULL
8438 || find_match(LOOKFOR_IF, trypos->lnum)
8439 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008440 break;
8441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 }
8443
8444 /*
8445 * If we're below an unterminated line that is not an
8446 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008447 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 * the line before this one.
8449 */
8450 else
8451 {
8452 /*
8453 * Found two unterminated lines on a row, line up with
8454 * the last one.
8455 * c = 99 +
8456 * 100 +
8457 * -> here;
8458 */
8459 if (lookfor == LOOKFOR_UNTERM)
8460 {
8461 /* When line ends in a comma add extra indent */
8462 if (terminated == ',')
8463 amount += ind_continuation;
8464 break;
8465 }
8466
8467 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8468 {
8469 /* Found two lines ending in ',', lineup with the
8470 * lowest one, but check for cpp base class
8471 * declaration/initialization, if it is an
8472 * opening brace or we are looking just for
8473 * enumerations/initializations. */
8474 if (terminated == ',')
8475 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008476 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 break;
8478
8479 lookfor = LOOKFOR_CPP_BASECLASS;
8480 continue;
8481 }
8482
8483 /* Ignore unterminated lines in between, but
8484 * reduce indent. */
8485 if (amount > cur_amount)
8486 amount = cur_amount;
8487 }
8488 else
8489 {
8490 /*
8491 * Found first unterminated line on a row, may
8492 * line up with this line, remember its indent
8493 * 100 +
8494 * -> here;
8495 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008496 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008498
8499 n = (int)STRLEN(l);
8500 if (terminated == ',' && (*skipwhite(l) == ']'
8501 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008502 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503
8504 /*
8505 * If previous line ends in ',', check whether we
8506 * are in an initialization or enum
8507 * struct xxx =
8508 * {
8509 * sizeof a,
8510 * 124 };
8511 * or a normal possible continuation line.
8512 * but only, of no other statement has been found
8513 * yet.
8514 */
8515 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8516 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008517 if (curbuf->b_ind_js)
8518 {
8519 /* Search for a line ending in a comma
8520 * and line up with the line below it
8521 * (could be the current line).
8522 * some = [
8523 * 1, <- line up here
8524 * 2,
8525 * some = [
8526 * 3 + <- line up here
8527 * 4 *
8528 * 5,
8529 * 6,
8530 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008531 if (cin_iscomment(skipwhite(l)))
8532 break;
8533 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008534 trypos = find_match_char('[',
8535 curbuf->b_ind_maxparen);
8536 if (trypos != NULL)
8537 {
8538 if (trypos->lnum
8539 == curwin->w_cursor.lnum - 1)
8540 {
8541 /* Current line is first inside
8542 * [], line up with it. */
8543 break;
8544 }
8545 ourscope = trypos->lnum;
8546 }
8547 }
8548 else
8549 {
8550 lookfor = LOOKFOR_ENUM_OR_INIT;
8551 cont_amount = cin_first_id_amount();
8552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 }
8554 else
8555 {
8556 if (lookfor == LOOKFOR_INITIAL
8557 && *l != NUL
8558 && l[STRLEN(l) - 1] == '\\')
8559 /* XXX */
8560 cont_amount = cin_get_equal_amount(
8561 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008562 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008563 && lookfor != LOOKFOR_JS_KEY
8564 && lookfor != LOOKFOR_COMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 lookfor = LOOKFOR_UNTERM;
8566 }
8567 }
8568 }
8569 }
8570
8571 /*
8572 * Check if we are after a while (cond);
8573 * If so: Ignore until the matching "do".
8574 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008575 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 {
8577 /*
8578 * Found an unterminated line after a while ();, line up
8579 * with the last one.
8580 * while (cond);
8581 * 100 + <- line up with this one
8582 * -> here;
8583 */
8584 if (lookfor == LOOKFOR_UNTERM
8585 || lookfor == LOOKFOR_ENUM_OR_INIT)
8586 {
8587 if (cont_amount > 0)
8588 amount = cont_amount;
8589 else
8590 amount += ind_continuation;
8591 break;
8592 }
8593
8594 if (whilelevel == 0)
8595 {
8596 lookfor = LOOKFOR_TERM;
8597 amount = get_indent(); /* XXX */
8598 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008599 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 }
8601 ++whilelevel;
8602 }
8603
8604 /*
8605 * We are after a "normal" statement.
8606 * If we had another statement we can stop now and use the
8607 * indent of that other statement.
8608 * Otherwise the indent of the current statement may be used,
8609 * search backwards for the next "normal" statement.
8610 */
8611 else
8612 {
8613 /*
8614 * Skip single break line, if before a switch label. It
8615 * may be lined up with the case label.
8616 */
8617 if (lookfor == LOOKFOR_NOBREAK
8618 && cin_isbreak(skipwhite(ml_get_curline())))
8619 {
8620 lookfor = LOOKFOR_ANY;
8621 continue;
8622 }
8623
8624 /*
8625 * Handle "do {" line.
8626 */
8627 if (whilelevel > 0)
8628 {
8629 l = cin_skipcomment(ml_get_curline());
8630 if (cin_isdo(l))
8631 {
8632 amount = get_indent(); /* XXX */
8633 --whilelevel;
8634 continue;
8635 }
8636 }
8637
8638 /*
8639 * Found a terminated line above an unterminated line. Add
8640 * the amount for a continuation line.
8641 * x = 1;
8642 * y = foo +
8643 * -> here;
8644 * or
8645 * int x = 1;
8646 * int foo,
8647 * -> here;
8648 */
8649 if (lookfor == LOOKFOR_UNTERM
8650 || lookfor == LOOKFOR_ENUM_OR_INIT)
8651 {
8652 if (cont_amount > 0)
8653 amount = cont_amount;
8654 else
8655 amount += ind_continuation;
8656 break;
8657 }
8658
8659 /*
8660 * Found a terminated line above a terminated line or "if"
8661 * etc. line. Use the amount of the line below us.
8662 * x = 1; x = 1;
8663 * if (asdf) y = 2;
8664 * while (asdf) ->here;
8665 * here;
8666 * ->foo;
8667 */
8668 if (lookfor == LOOKFOR_TERM)
8669 {
8670 if (!lookfor_break && whilelevel == 0)
8671 break;
8672 }
8673
8674 /*
8675 * First line above the one we're indenting is terminated.
8676 * To know what needs to be done look further backward for
8677 * a terminated line.
8678 */
8679 else
8680 {
8681 /*
8682 * position the cursor over the rightmost paren, so
8683 * that matching it will take us back to the start of
8684 * the line. Helps for:
8685 * func(asdr,
8686 * asdfasdf);
8687 * here;
8688 */
8689term_again:
8690 l = ml_get_curline();
8691 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008692 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008693 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 {
8695 /*
8696 * Check if we are on a case label now. This is
8697 * handled above.
8698 * case xx: if ( asdf &&
8699 * asdf)
8700 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008701 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008703 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 {
8705 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008706 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 continue;
8708 }
8709 }
8710
8711 /* When aligning with the case statement, don't align
8712 * with a statement after it.
8713 * case 1: { <-- don't use this { position
8714 * stat;
8715 * }
8716 * case 2:
8717 * stat;
8718 * }
8719 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008720 iscase = (curbuf->b_ind_keep_case_label
8721 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722
8723 /*
8724 * Get indent and pointer to text for current line,
8725 * ignoring any jump label.
8726 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008727 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728
8729 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008730 amount += curbuf->b_ind_open_extra;
8731 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008732 l = skipwhite(l);
8733 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008734 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8736
8737 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008738 * When a terminated line starts with "else" skip to
8739 * the matching "if":
8740 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008741 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008742 * Need to use the scope of this "else". XXX
8743 * If whilelevel != 0 continue looking for a "do {".
8744 */
8745 if (lookfor == LOOKFOR_TERM
8746 && *l != '}'
8747 && cin_iselse(l)
8748 && whilelevel == 0)
8749 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008750 if ((trypos = find_start_brace()) == NULL
8751 || find_match(LOOKFOR_IF, trypos->lnum)
8752 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008753 break;
8754 continue;
8755 }
8756
8757 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 * If we're at the end of a block, skip to the start of
8759 * that block.
8760 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008761 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008762 if (find_last_paren(l, '{', '}') /* XXX */
8763 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008765 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 /* if not "else {" check for terminated again */
8767 /* but skip block for "} else {" */
8768 l = cin_skipcomment(ml_get_curline());
8769 if (*l == '}' || !cin_iselse(l))
8770 goto term_again;
8771 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008772 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 }
8774 }
8775 }
8776 }
8777 }
8778 }
8779
8780 /* add extra indent for a comment */
8781 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008782 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008783
8784 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008785 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
8786 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008787
8788 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008790
8791 /*
8792 * ok -- we're not inside any sort of structure at all!
8793 *
8794 * This means we're at the top level, and everything should
8795 * basically just match where the previous line is, except
8796 * for the lines immediately following a function declaration,
8797 * which are K&R-style parameters and need to be indented.
8798 *
8799 * if our line starts with an open brace, forget about any
8800 * prevailing indent and make sure it looks like the start
8801 * of a function
8802 */
8803
8804 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008806 amount = curbuf->b_ind_first_open;
8807 goto theend;
8808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008810 /*
8811 * If the NEXT line is a function declaration, the current
8812 * line needs to be indented as a function type spec.
8813 * Don't do this if the current line looks like a comment or if the
8814 * current line is terminated, ie. ends in ';', or if the current line
8815 * contains { or }: "void f() {\n if (1)"
8816 */
8817 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8818 && !cin_nocode(theline)
8819 && vim_strchr(theline, '{') == NULL
8820 && vim_strchr(theline, '}') == NULL
8821 && !cin_ends_in(theline, (char_u *)":", NULL)
8822 && !cin_ends_in(theline, (char_u *)",", NULL)
8823 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
8824 cur_curpos.lnum + 1)
8825 && !cin_isterminated(theline, FALSE, TRUE))
8826 {
8827 amount = curbuf->b_ind_func_type;
8828 goto theend;
8829 }
8830
8831 /* search backwards until we find something we recognize */
8832 amount = 0;
8833 curwin->w_cursor = cur_curpos;
8834 while (curwin->w_cursor.lnum > 1)
8835 {
8836 curwin->w_cursor.lnum--;
8837 curwin->w_cursor.col = 0;
8838
8839 l = ml_get_curline();
8840
8841 /*
8842 * If we're in a comment or raw string now, skip to the start
8843 * of it.
8844 */ /* XXX */
8845 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008847 curwin->w_cursor.lnum = trypos->lnum + 1;
8848 curwin->w_cursor.col = 0;
8849 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 }
8851
8852 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008853 * Are we at the start of a cpp base class declaration or
8854 * constructor initialization?
8855 */ /* XXX */
8856 n = FALSE;
8857 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008859 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
8860 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008862 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008864 /* XXX */
8865 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
8866 break;
8867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008869 /*
8870 * Skip preprocessor directives and blank lines.
8871 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008872 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008873 continue;
8874
8875 if (cin_nocode(l))
8876 continue;
8877
8878 /*
8879 * If the previous line ends in ',', use one level of
8880 * indentation:
8881 * int foo,
8882 * bar;
8883 * do this before checking for '}' in case of eg.
8884 * enum foobar
8885 * {
8886 * ...
8887 * } foo,
8888 * bar;
8889 */
8890 n = 0;
8891 if (cin_ends_in(l, (char_u *)",", NULL)
8892 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
8893 {
8894 /* take us back to opening paren */
8895 if (find_last_paren(l, '(', ')')
8896 && (trypos = find_match_paren(
8897 curbuf->b_ind_maxparen)) != NULL)
8898 curwin->w_cursor = *trypos;
8899
8900 /* For a line ending in ',' that is a continuation line go
8901 * back to the first line with a backslash:
8902 * char *foo = "bla\
8903 * bla",
8904 * here;
8905 */
8906 while (n == 0 && curwin->w_cursor.lnum > 1)
8907 {
8908 l = ml_get(curwin->w_cursor.lnum - 1);
8909 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8910 break;
8911 --curwin->w_cursor.lnum;
8912 curwin->w_cursor.col = 0;
8913 }
8914
8915 amount = get_indent(); /* XXX */
8916
8917 if (amount == 0)
8918 amount = cin_first_id_amount();
8919 if (amount == 0)
8920 amount = ind_continuation;
8921 break;
8922 }
8923
8924 /*
8925 * If the line looks like a function declaration, and we're
8926 * not in a comment, put it the left margin.
8927 */
8928 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
8929 break;
8930 l = ml_get_curline();
8931
8932 /*
8933 * Finding the closing '}' of a previous function. Put
8934 * current line at the left margin. For when 'cino' has "fs".
8935 */
8936 if (*skipwhite(l) == '}')
8937 break;
8938
8939 /* (matching {)
8940 * If the previous line ends on '};' (maybe followed by
8941 * comments) align at column 0. For example:
8942 * char *string_array[] = { "foo",
8943 * / * x * / "b};ar" }; / * foobar * /
8944 */
8945 if (cin_ends_in(l, (char_u *)"};", NULL))
8946 break;
8947
8948 /*
8949 * If the previous line ends on '[' we are probably in an
8950 * array constant:
8951 * something = [
8952 * 234, <- extra indent
8953 */
8954 if (cin_ends_in(l, (char_u *)"[", NULL))
8955 {
8956 amount = get_indent() + ind_continuation;
8957 break;
8958 }
8959
8960 /*
8961 * Find a line only has a semicolon that belongs to a previous
8962 * line ending in '}', e.g. before an #endif. Don't increase
8963 * indent then.
8964 */
8965 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
8966 {
8967 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968
8969 while (curwin->w_cursor.lnum > 1)
8970 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008971 look = ml_get(--curwin->w_cursor.lnum);
8972 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008973 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008975 }
8976 if (curwin->w_cursor.lnum > 0
8977 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008980 curwin->w_cursor = curpos_save;
8981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008983 /*
8984 * If the PREVIOUS line is a function declaration, the current
8985 * line (and the ones that follow) needs to be indented as
8986 * parameters.
8987 */
8988 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
8989 {
8990 amount = curbuf->b_ind_param;
8991 break;
8992 }
8993
8994 /*
8995 * If the previous line ends in ';' and the line before the
8996 * previous line ends in ',' or '\', ident to column zero:
8997 * int foo,
8998 * bar;
8999 * indent_to_0 here;
9000 */
9001 if (cin_ends_in(l, (char_u *)";", NULL))
9002 {
9003 l = ml_get(curwin->w_cursor.lnum - 1);
9004 if (cin_ends_in(l, (char_u *)",", NULL)
9005 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9006 break;
9007 l = ml_get_curline();
9008 }
9009
9010 /*
9011 * Doesn't look like anything interesting -- so just
9012 * use the indent of this line.
9013 *
9014 * Position the cursor over the rightmost paren, so that
9015 * matching it will take us back to the start of the line.
9016 */
9017 find_last_paren(l, '(', ')');
9018
9019 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9020 curwin->w_cursor = *trypos;
9021 amount = get_indent(); /* XXX */
9022 break;
9023 }
9024
9025 /* add extra indent for a comment */
9026 if (cin_iscomment(theline))
9027 amount += curbuf->b_ind_comment;
9028
9029 /* add extra indent if the previous line ended in a backslash:
9030 * "asdfasdf\
9031 * here";
9032 * char *foo = "asdf\
9033 * here";
9034 */
9035 if (cur_curpos.lnum > 1)
9036 {
9037 l = ml_get(cur_curpos.lnum - 1);
9038 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9039 {
9040 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9041 if (cur_amount > 0)
9042 amount = cur_amount;
9043 else if (cur_amount == 0)
9044 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 }
9046 }
9047
9048theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009049 if (amount < 0)
9050 amount = 0;
9051
9052laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 /* put the cursor back where it belongs */
9054 curwin->w_cursor = cur_curpos;
9055
9056 vim_free(linecopy);
9057
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 return amount;
9059}
9060
9061 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009062find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063{
9064 char_u *look;
9065 pos_T *theirscope;
9066 char_u *mightbeif;
9067 int elselevel;
9068 int whilelevel;
9069
9070 if (lookfor == LOOKFOR_IF)
9071 {
9072 elselevel = 1;
9073 whilelevel = 0;
9074 }
9075 else
9076 {
9077 elselevel = 0;
9078 whilelevel = 1;
9079 }
9080
9081 curwin->w_cursor.col = 0;
9082
9083 while (curwin->w_cursor.lnum > ourscope + 1)
9084 {
9085 curwin->w_cursor.lnum--;
9086 curwin->w_cursor.col = 0;
9087
9088 look = cin_skipcomment(ml_get_curline());
9089 if (cin_iselse(look)
9090 || cin_isif(look)
9091 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009092 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 {
9094 /*
9095 * if we've gone outside the braces entirely,
9096 * we must be out of scope...
9097 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009098 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 if (theirscope == NULL)
9100 break;
9101
9102 /*
9103 * and if the brace enclosing this is further
9104 * back than the one enclosing the else, we're
9105 * out of luck too.
9106 */
9107 if (theirscope->lnum < ourscope)
9108 break;
9109
9110 /*
9111 * and if they're enclosed in a *deeper* brace,
9112 * then we can ignore it because it's in a
9113 * different scope...
9114 */
9115 if (theirscope->lnum > ourscope)
9116 continue;
9117
9118 /*
9119 * if it was an "else" (that's not an "else if")
9120 * then we need to go back to another if, so
9121 * increment elselevel
9122 */
9123 look = cin_skipcomment(ml_get_curline());
9124 if (cin_iselse(look))
9125 {
9126 mightbeif = cin_skipcomment(look + 4);
9127 if (!cin_isif(mightbeif))
9128 ++elselevel;
9129 continue;
9130 }
9131
9132 /*
9133 * if it was a "while" then we need to go back to
9134 * another "do", so increment whilelevel. XXX
9135 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009136 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 {
9138 ++whilelevel;
9139 continue;
9140 }
9141
9142 /* If it's an "if" decrement elselevel */
9143 look = cin_skipcomment(ml_get_curline());
9144 if (cin_isif(look))
9145 {
9146 elselevel--;
9147 /*
9148 * When looking for an "if" ignore "while"s that
9149 * get in the way.
9150 */
9151 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9152 whilelevel = 0;
9153 }
9154
9155 /* If it's a "do" decrement whilelevel */
9156 if (cin_isdo(look))
9157 whilelevel--;
9158
9159 /*
9160 * if we've used up all the elses, then
9161 * this must be the if that we want!
9162 * match the indent level of that if.
9163 */
9164 if (elselevel <= 0 && whilelevel <= 0)
9165 {
9166 return OK;
9167 }
9168 }
9169 }
9170 return FAIL;
9171}
9172
9173# if defined(FEAT_EVAL) || defined(PROTO)
9174/*
9175 * Get indent level from 'indentexpr'.
9176 */
9177 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009178get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179{
9180 int indent;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009181 pos_T save_pos;
9182 colnr_T save_curswant;
9183 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009185 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9186 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009188 /* Save and restore cursor position and curswant, in case it was changed
9189 * via :normal commands */
9190 save_pos = curwin->w_cursor;
9191 save_curswant = curwin->w_curswant;
9192 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009194 if (use_sandbox)
9195 ++sandbox;
9196 ++textlock;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02009197 indent = (int)eval_to_number(curbuf->b_p_inde);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009198 if (use_sandbox)
9199 --sandbox;
9200 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201
9202 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9203 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9204 * command. */
9205 save_State = State;
9206 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009207 curwin->w_cursor = save_pos;
9208 curwin->w_curswant = save_curswant;
9209 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210 check_cursor();
9211 State = save_State;
9212
9213 /* If there is an error, just keep the current indent. */
9214 if (indent < 0)
9215 indent = get_indent();
9216
9217 return indent;
9218}
9219# endif
9220
9221#endif /* FEAT_CINDENT */
9222
9223#if defined(FEAT_LISP) || defined(PROTO)
9224
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009225static int lisp_match(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226
9227 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009228lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229{
9230 char_u buf[LSIZE];
9231 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009232 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233
9234 while (*word != NUL)
9235 {
9236 (void)copy_option_part(&word, buf, LSIZE, ",");
9237 len = (int)STRLEN(buf);
9238 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9239 return TRUE;
9240 }
9241 return FALSE;
9242}
9243
9244/*
9245 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9246 * The incompatible newer method is quite a bit better at indenting
9247 * code in lisp-like languages than the traditional one; it's still
9248 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9249 *
9250 * TODO:
9251 * Findmatch() should be adapted for lisp, also to make showmatch
9252 * work correctly: now (v5.3) it seems all C/C++ oriented:
9253 * - it does not recognize the #\( and #\) notations as character literals
9254 * - it doesn't know about comments starting with a semicolon
9255 * - it incorrectly interprets '(' as a character literal
9256 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009257 * Update from Sergey Khorev:
9258 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259 */
9260 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009261get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009263 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 int amount;
9265 char_u *that;
9266 colnr_T col;
9267 colnr_T firsttry;
9268 int parencount, quotecount;
9269 int vi_lisp;
9270
9271 /* Set vi_lisp to use the vi-compatible method */
9272 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9273
9274 realpos = curwin->w_cursor;
9275 curwin->w_cursor.col = 0;
9276
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009277 if ((pos = findmatch(NULL, '(')) == NULL)
9278 pos = findmatch(NULL, '[');
9279 else
9280 {
9281 paren = *pos;
9282 pos = findmatch(NULL, '[');
9283 if (pos == NULL || ltp(pos, &paren))
9284 pos = &paren;
9285 }
9286 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287 {
9288 /* Extra trick: Take the indent of the first previous non-white
9289 * line that is at the same () level. */
9290 amount = -1;
9291 parencount = 0;
9292
9293 while (--curwin->w_cursor.lnum >= pos->lnum)
9294 {
9295 if (linewhite(curwin->w_cursor.lnum))
9296 continue;
9297 for (that = ml_get_curline(); *that != NUL; ++that)
9298 {
9299 if (*that == ';')
9300 {
9301 while (*(that + 1) != NUL)
9302 ++that;
9303 continue;
9304 }
9305 if (*that == '\\')
9306 {
9307 if (*(that + 1) != NUL)
9308 ++that;
9309 continue;
9310 }
9311 if (*that == '"' && *(that + 1) != NUL)
9312 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009313 while (*++that && *that != '"')
9314 {
9315 /* skipping escaped characters in the string */
9316 if (*that == '\\')
9317 {
9318 if (*++that == NUL)
9319 break;
9320 if (that[1] == NUL)
9321 {
9322 ++that;
9323 break;
9324 }
9325 }
9326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009328 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009330 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331 --parencount;
9332 }
9333 if (parencount == 0)
9334 {
9335 amount = get_indent();
9336 break;
9337 }
9338 }
9339
9340 if (amount == -1)
9341 {
9342 curwin->w_cursor.lnum = pos->lnum;
9343 curwin->w_cursor.col = pos->col;
9344 col = pos->col;
9345
9346 that = ml_get_curline();
9347
9348 if (vi_lisp && get_indent() == 0)
9349 amount = 2;
9350 else
9351 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009352 char_u *line = that;
9353
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 amount = 0;
9355 while (*that && col)
9356 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009357 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 col--;
9359 }
9360
9361 /*
9362 * Some keywords require "body" indenting rules (the
9363 * non-standard-lisp ones are Scheme special forms):
9364 *
9365 * (let ((a 1)) instead (let ((a 1))
9366 * (...)) of (...))
9367 */
9368
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009369 if (!vi_lisp && (*that == '(' || *that == '[')
9370 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 amount += 2;
9372 else
9373 {
9374 that++;
9375 amount++;
9376 firsttry = amount;
9377
9378 while (vim_iswhite(*that))
9379 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009380 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 ++that;
9382 }
9383
9384 if (*that && *that != ';') /* not a comment line */
9385 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009386 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009388 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 firsttry++;
9390
9391 parencount = 0;
9392 quotecount = 0;
9393
9394 if (vi_lisp
9395 || (*that != '"'
9396 && *that != '\''
9397 && *that != '#'
9398 && (*that < '0' || *that > '9')))
9399 {
9400 while (*that
9401 && (!vim_iswhite(*that)
9402 || quotecount
9403 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009404 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 && !quotecount
9406 && !parencount
9407 && vi_lisp)))
9408 {
9409 if (*that == '"')
9410 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009411 if ((*that == '(' || *that == '[')
9412 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009414 if ((*that == ')' || *that == ']')
9415 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 --parencount;
9417 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009418 amount += lbr_chartabsize_adv(
9419 line, &that, (colnr_T)amount);
9420 amount += lbr_chartabsize_adv(
9421 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422 }
9423 }
9424 while (vim_iswhite(*that))
9425 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009426 amount += lbr_chartabsize(
9427 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 that++;
9429 }
9430 if (!*that || *that == ';')
9431 amount = firsttry;
9432 }
9433 }
9434 }
9435 }
9436 }
9437 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009438 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439
9440 curwin->w_cursor = realpos;
9441
9442 return amount;
9443}
9444#endif /* FEAT_LISP */
9445
9446 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009447prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009449#if defined(SIGHUP) && defined(SIG_IGN)
9450 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9451 * makes Vim exit and then handling SIGHUP causes various reentrance
9452 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009453 signal(SIGHUP, SIG_IGN);
9454#endif
9455
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456#ifdef FEAT_GUI
9457 if (gui.in_use)
9458 {
9459 gui.dying = TRUE;
9460 out_trash(); /* trash any pending output */
9461 }
9462 else
9463#endif
9464 {
9465 windgoto((int)Rows - 1, 0);
9466
9467 /*
9468 * Switch terminal mode back now, so messages end up on the "normal"
9469 * screen (if there are two screens).
9470 */
9471 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009472 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 out_flush();
9474 }
9475}
9476
9477/*
9478 * Preserve files and exit.
9479 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009480 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9481 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 */
9483 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009484preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485{
9486 buf_T *buf;
9487
9488 prepare_to_exit();
9489
Bram Moolenaar4770d092006-01-12 23:22:24 +00009490 /* Setting this will prevent free() calls. That avoids calling free()
9491 * recursively when free() was invoked with a bad pointer. */
9492 really_exiting = TRUE;
9493
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 out_str(IObuff);
9495 screen_start(); /* don't know where cursor is now */
9496 out_flush();
9497
9498 ml_close_notmod(); /* close all not-modified buffers */
9499
Bram Moolenaar29323592016-07-24 22:04:11 +02009500 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 {
9502 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9503 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009504 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 screen_start(); /* don't know where cursor is now */
9506 out_flush();
9507 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9508 break;
9509 }
9510 }
9511
9512 ml_close_all(FALSE); /* close all memfiles, without deleting */
9513
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009514 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515
9516 getout(1);
9517}
9518
9519/*
9520 * return TRUE if "fname" exists.
9521 */
9522 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009523vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009525 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526
9527 if (mch_stat((char *)fname, &st))
9528 return FALSE;
9529 return TRUE;
9530}
9531
9532/*
9533 * Check for CTRL-C pressed, but only once in a while.
9534 * Should be used instead of ui_breakcheck() for functions that check for
9535 * each line in the file. Calling ui_breakcheck() each time takes too much
9536 * time, because it can be a system call.
9537 */
9538
9539#ifndef BREAKCHECK_SKIP
9540# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9541# define BREAKCHECK_SKIP 200
9542# else
9543# define BREAKCHECK_SKIP 32
9544# endif
9545#endif
9546
9547static int breakcheck_count = 0;
9548
9549 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009550line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551{
9552 if (++breakcheck_count >= BREAKCHECK_SKIP)
9553 {
9554 breakcheck_count = 0;
9555 ui_breakcheck();
9556 }
9557}
9558
9559/*
9560 * Like line_breakcheck() but check 10 times less often.
9561 */
9562 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009563fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564{
9565 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9566 {
9567 breakcheck_count = 0;
9568 ui_breakcheck();
9569 }
9570}
9571
9572/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009573 * Invoke expand_wildcards() for one pattern.
9574 * Expand items like "%:h" before the expansion.
9575 * Returns OK or FAIL.
9576 */
9577 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009578expand_wildcards_eval(
9579 char_u **pat, /* pointer to input pattern */
9580 int *num_file, /* resulting number of files */
9581 char_u ***file, /* array of resulting files */
9582 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009583{
9584 int ret = FAIL;
9585 char_u *eval_pat = NULL;
9586 char_u *exp_pat = *pat;
9587 char_u *ignored_msg;
9588 int usedlen;
9589
9590 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9591 {
9592 ++emsg_off;
9593 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9594 NULL, &ignored_msg, NULL);
9595 --emsg_off;
9596 if (eval_pat != NULL)
9597 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9598 }
9599
9600 if (exp_pat != NULL)
9601 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9602
9603 if (eval_pat != NULL)
9604 {
9605 vim_free(exp_pat);
9606 vim_free(eval_pat);
9607 }
9608
9609 return ret;
9610}
9611
9612/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9614 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009615 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 */
9617 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009618expand_wildcards(
9619 int num_pat, /* number of input patterns */
9620 char_u **pat, /* array of input patterns */
9621 int *num_files, /* resulting number of files */
9622 char_u ***files, /* array of resulting files */
9623 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624{
9625 int retval;
9626 int i, j;
9627 char_u *p;
9628 int non_suf_match; /* number without matching suffix */
9629
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009630 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631
9632 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009633 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634 return retval;
9635
9636#ifdef FEAT_WILDIGN
9637 /*
9638 * Remove names that match 'wildignore'.
9639 */
9640 if (*p_wig)
9641 {
9642 char_u *ffname;
9643
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009644 /* check all files in (*files)[] */
9645 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009647 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 if (ffname == NULL) /* out of memory */
9649 break;
9650# ifdef VMS
9651 vms_remove_version(ffname);
9652# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009653 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009655 /* remove this matching files from the list */
9656 vim_free((*files)[i]);
9657 for (j = i; j + 1 < *num_files; ++j)
9658 (*files)[j] = (*files)[j + 1];
9659 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660 --i;
9661 }
9662 vim_free(ffname);
9663 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009664
9665 /* If the number of matches is now zero, we fail. */
9666 if (*num_files == 0)
9667 {
9668 vim_free(*files);
9669 *files = NULL;
9670 return FAIL;
9671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 }
9673#endif
9674
9675 /*
9676 * Move the names where 'suffixes' match to the end.
9677 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009678 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 {
9680 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009681 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009683 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 {
9685 /*
9686 * Move the name without matching suffix to the front
9687 * of the list.
9688 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009689 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009691 (*files)[j] = (*files)[j - 1];
9692 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 }
9694 }
9695 }
9696
9697 return retval;
9698}
9699
9700/*
9701 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9702 */
9703 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009704match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705{
9706 int fnamelen, setsuflen;
9707 char_u *setsuf;
9708#define MAXSUFLEN 30 /* maximum length of a file suffix */
9709 char_u suf_buf[MAXSUFLEN];
9710
9711 fnamelen = (int)STRLEN(fname);
9712 setsuflen = 0;
9713 for (setsuf = p_su; *setsuf; )
9714 {
9715 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009716 if (setsuflen == 0)
9717 {
9718 char_u *tail = gettail(fname);
9719
9720 /* empty entry: match name without a '.' */
9721 if (vim_strchr(tail, '.') == NULL)
9722 {
9723 setsuflen = 1;
9724 break;
9725 }
9726 }
9727 else
9728 {
9729 if (fnamelen >= setsuflen
9730 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9731 (size_t)setsuflen) == 0)
9732 break;
9733 setsuflen = 0;
9734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 }
9736 return (setsuflen != 0);
9737}
9738
9739#if !defined(NO_EXPANDPATH) || defined(PROTO)
9740
9741# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009742static int vim_backtick(char_u *p);
9743static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744# endif
9745
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009746# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747/*
9748 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9749 * it's shared between these systems.
9750 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009751# if defined(PROTO)
9752# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753# else
9754# ifdef __BORLANDC__
9755# define _cdecl _RTLENTRYF
9756# endif
9757# endif
9758
9759/*
9760 * comparison function for qsort in dos_expandpath()
9761 */
9762 static int _cdecl
9763pstrcmp(const void *a, const void *b)
9764{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009765 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766}
9767
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009769 * Recursively expand one path component into all matching files and/or
9770 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 * Return the number of matches found.
9772 * "path" has backslashes before chars that are not to be expanded, starting
9773 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009774 * Return the number of matches found.
9775 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 */
9777 static int
9778dos_expandpath(
9779 garray_T *gap,
9780 char_u *path,
9781 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009782 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009783 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009785 char_u *buf;
9786 char_u *path_end;
9787 char_u *p, *s, *e;
9788 int start_len = gap->ga_len;
9789 char_u *pat;
9790 regmatch_T regmatch;
9791 int starts_with_dot;
9792 int matches;
9793 int len;
9794 int starstar = FALSE;
9795 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 WIN32_FIND_DATA fb;
9797 HANDLE hFind = (HANDLE)0;
9798# ifdef FEAT_MBYTE
9799 WIN32_FIND_DATAW wfb;
9800 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9801# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009803 int ok;
9804
9805 /* Expanding "**" may take a long time, check for CTRL-C. */
9806 if (stardepth > 0)
9807 {
9808 ui_breakcheck();
9809 if (got_int)
9810 return 0;
9811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812
Bram Moolenaar7314efd2015-10-31 15:32:52 +01009813 /* Make room for file name. When doing encoding conversion the actual
9814 * length may be quite a bit longer, thus use the maximum possible length. */
9815 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 if (buf == NULL)
9817 return 0;
9818
9819 /*
9820 * Find the first part in the path name that contains a wildcard or a ~1.
9821 * Copy it into buf, including the preceding characters.
9822 */
9823 p = buf;
9824 s = buf;
9825 e = NULL;
9826 path_end = path;
9827 while (*path_end != NUL)
9828 {
9829 /* May ignore a wildcard that has a backslash before it; it will
9830 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9831 if (path_end >= path + wildoff && rem_backslash(path_end))
9832 *p++ = *path_end++;
9833 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9834 {
9835 if (e != NULL)
9836 break;
9837 s = p + 1;
9838 }
9839 else if (path_end >= path + wildoff
9840 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9841 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009842# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 if (has_mbyte)
9844 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009845 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 STRNCPY(p, path_end, len);
9847 p += len;
9848 path_end += len;
9849 }
9850 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009851# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 *p++ = *path_end++;
9853 }
9854 e = p;
9855 *e = NUL;
9856
9857 /* now we have one wildcard component between s and e */
9858 /* Remove backslashes between "wildoff" and the start of the wildcard
9859 * component. */
9860 for (p = buf + wildoff; p < s; ++p)
9861 if (rem_backslash(p))
9862 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009863 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 --e;
9865 --s;
9866 }
9867
Bram Moolenaar231334e2005-07-25 20:46:57 +00009868 /* Check for "**" between "s" and "e". */
9869 for (p = s; p < e; ++p)
9870 if (p[0] == '*' && p[1] == '*')
9871 starstar = TRUE;
9872
Bram Moolenaard82103e2016-01-17 17:04:05 +01009873 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9875 if (pat == NULL)
9876 {
9877 vim_free(buf);
9878 return 0;
9879 }
9880
9881 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009882 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009883 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 regmatch.rm_ic = TRUE; /* Always ignore case */
9885 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009886 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009887 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 vim_free(pat);
9889
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009890 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891 {
9892 vim_free(buf);
9893 return 0;
9894 }
9895
9896 /* remember the pattern or file name being looked for */
9897 matchname = vim_strsave(s);
9898
Bram Moolenaar231334e2005-07-25 20:46:57 +00009899 /* If "**" is by itself, this is the first time we encounter it and more
9900 * is following then find matches without any directory. */
9901 if (!didstar && stardepth < 100 && starstar && e - s == 2
9902 && *path_end == '/')
9903 {
9904 STRCPY(s, path_end + 1);
9905 ++stardepth;
9906 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9907 --stardepth;
9908 }
9909
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910 /* Scan all files in the directory with "dir/ *.*" */
9911 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912# ifdef FEAT_MBYTE
9913 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
9914 {
9915 /* The active codepage differs from 'encoding'. Attempt using the
9916 * wide function. If it fails because it is not implemented fall back
9917 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009918 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 if (wn != NULL)
9920 {
9921 hFind = FindFirstFileW(wn, &wfb);
9922 if (hFind == INVALID_HANDLE_VALUE
9923 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
9924 {
9925 vim_free(wn);
9926 wn = NULL;
9927 }
9928 }
9929 }
9930
9931 if (wn == NULL)
9932# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009933 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935
9936 while (ok)
9937 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938# ifdef FEAT_MBYTE
9939 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009940 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 else
9942# endif
9943 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 /* Ignore entries starting with a dot, unless when asked for. Accept
9945 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +01009946 if ((p[0] != '.' || starts_with_dot
9947 || ((flags & EW_DODOT)
9948 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009950 || (regmatch.regprog != NULL
9951 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009952 || ((flags & EW_NOTWILD)
9953 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009957
9958 if (starstar && stardepth < 100)
9959 {
9960 /* For "**" in the pattern first go deeper in the tree to
9961 * find matches. */
9962 STRCPY(buf + len, "/**");
9963 STRCPY(buf + len + 3, path_end);
9964 ++stardepth;
9965 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
9966 --stardepth;
9967 }
9968
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 STRCPY(buf + len, path_end);
9970 if (mch_has_exp_wildcard(path_end))
9971 {
9972 /* need to expand another component of the path */
9973 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009974 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 }
9976 else
9977 {
9978 /* no more wildcards, check if there is a match */
9979 /* remove backslashes for the remaining components only */
9980 if (*path_end != 0)
9981 backslash_halve(buf + len + 1);
9982 if (mch_getperm(buf) >= 0) /* add existing file */
9983 addfile(gap, buf, flags);
9984 }
9985 }
9986
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987# ifdef FEAT_MBYTE
9988 if (wn != NULL)
9989 {
9990 vim_free(p);
9991 ok = FindNextFileW(hFind, &wfb);
9992 }
9993 else
9994# endif
9995 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996
9997 /* If no more matches and no match was used, try expanding the name
9998 * itself. Finds the long name of a short filename. */
9999 if (!ok && matchname != NULL && gap->ga_len == start_len)
10000 {
10001 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 FindClose(hFind);
10003# ifdef FEAT_MBYTE
10004 if (wn != NULL)
10005 {
10006 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010007 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 if (wn != NULL)
10009 hFind = FindFirstFileW(wn, &wfb);
10010 }
10011 if (wn == NULL)
10012# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010013 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 vim_free(matchname);
10016 matchname = NULL;
10017 }
10018 }
10019
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 FindClose(hFind);
10021# ifdef FEAT_MBYTE
10022 vim_free(wn);
10023# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010025 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 vim_free(matchname);
10027
10028 matches = gap->ga_len - start_len;
10029 if (matches > 0)
10030 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10031 sizeof(char_u *), pstrcmp);
10032 return matches;
10033}
10034
10035 int
10036mch_expandpath(
10037 garray_T *gap,
10038 char_u *path,
10039 int flags) /* EW_* flags */
10040{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010041 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010043# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044
Bram Moolenaar231334e2005-07-25 20:46:57 +000010045#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10046 || defined(PROTO)
10047/*
10048 * Unix style wildcard expansion code.
10049 * It's here because it's used both for Unix and Mac.
10050 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010051static int pstrcmp(const void *, const void *);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010052
10053 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010054pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010055{
10056 return (pathcmp(*(char **)a, *(char **)b, -1));
10057}
10058
10059/*
10060 * Recursively expand one path component into all matching files and/or
10061 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10062 * "path" has backslashes before chars that are not to be expanded, starting
10063 * at "path + wildoff".
10064 * Return the number of matches found.
10065 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10066 */
10067 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010068unix_expandpath(
10069 garray_T *gap,
10070 char_u *path,
10071 int wildoff,
10072 int flags, /* EW_* flags */
10073 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010074{
10075 char_u *buf;
10076 char_u *path_end;
10077 char_u *p, *s, *e;
10078 int start_len = gap->ga_len;
10079 char_u *pat;
10080 regmatch_T regmatch;
10081 int starts_with_dot;
10082 int matches;
10083 int len;
10084 int starstar = FALSE;
10085 static int stardepth = 0; /* depth for "**" expansion */
10086
10087 DIR *dirp;
10088 struct dirent *dp;
10089
10090 /* Expanding "**" may take a long time, check for CTRL-C. */
10091 if (stardepth > 0)
10092 {
10093 ui_breakcheck();
10094 if (got_int)
10095 return 0;
10096 }
10097
10098 /* make room for file name */
10099 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10100 if (buf == NULL)
10101 return 0;
10102
10103 /*
10104 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010105 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010106 * Copy it into "buf", including the preceding characters.
10107 */
10108 p = buf;
10109 s = buf;
10110 e = NULL;
10111 path_end = path;
10112 while (*path_end != NUL)
10113 {
10114 /* May ignore a wildcard that has a backslash before it; it will
10115 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10116 if (path_end >= path + wildoff && rem_backslash(path_end))
10117 *p++ = *path_end++;
10118 else if (*path_end == '/')
10119 {
10120 if (e != NULL)
10121 break;
10122 s = p + 1;
10123 }
10124 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010125 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010126 || (!p_fic && (flags & EW_ICASE)
10127 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010128 e = p;
10129#ifdef FEAT_MBYTE
10130 if (has_mbyte)
10131 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010132 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010133 STRNCPY(p, path_end, len);
10134 p += len;
10135 path_end += len;
10136 }
10137 else
10138#endif
10139 *p++ = *path_end++;
10140 }
10141 e = p;
10142 *e = NUL;
10143
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010144 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010145 /* Remove backslashes between "wildoff" and the start of the wildcard
10146 * component. */
10147 for (p = buf + wildoff; p < s; ++p)
10148 if (rem_backslash(p))
10149 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010150 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010151 --e;
10152 --s;
10153 }
10154
10155 /* Check for "**" between "s" and "e". */
10156 for (p = s; p < e; ++p)
10157 if (p[0] == '*' && p[1] == '*')
10158 starstar = TRUE;
10159
10160 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010161 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010162 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10163 if (pat == NULL)
10164 {
10165 vim_free(buf);
10166 return 0;
10167 }
10168
10169 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010170 if (flags & EW_ICASE)
10171 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10172 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010173 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010174 if (flags & (EW_NOERROR | EW_NOTWILD))
10175 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010176 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010177 if (flags & (EW_NOERROR | EW_NOTWILD))
10178 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010179 vim_free(pat);
10180
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010181 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010182 {
10183 vim_free(buf);
10184 return 0;
10185 }
10186
10187 /* If "**" is by itself, this is the first time we encounter it and more
10188 * is following then find matches without any directory. */
10189 if (!didstar && stardepth < 100 && starstar && e - s == 2
10190 && *path_end == '/')
10191 {
10192 STRCPY(s, path_end + 1);
10193 ++stardepth;
10194 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10195 --stardepth;
10196 }
10197
10198 /* open the directory for scanning */
10199 *s = NUL;
10200 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10201
10202 /* Find all matching entries */
10203 if (dirp != NULL)
10204 {
10205 for (;;)
10206 {
10207 dp = readdir(dirp);
10208 if (dp == NULL)
10209 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010210 if ((dp->d_name[0] != '.' || starts_with_dot
10211 || ((flags & EW_DODOT)
10212 && dp->d_name[1] != NUL
10213 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010214 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10215 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010216 || ((flags & EW_NOTWILD)
10217 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010218 {
10219 STRCPY(s, dp->d_name);
10220 len = STRLEN(buf);
10221
10222 if (starstar && stardepth < 100)
10223 {
10224 /* For "**" in the pattern first go deeper in the tree to
10225 * find matches. */
10226 STRCPY(buf + len, "/**");
10227 STRCPY(buf + len + 3, path_end);
10228 ++stardepth;
10229 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10230 --stardepth;
10231 }
10232
10233 STRCPY(buf + len, path_end);
10234 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10235 {
10236 /* need to expand another component of the path */
10237 /* remove backslashes for the remaining components only */
10238 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10239 }
10240 else
10241 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010242 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010243
Bram Moolenaar231334e2005-07-25 20:46:57 +000010244 /* no more wildcards, check if there is a match */
10245 /* remove backslashes for the remaining components only */
10246 if (*path_end != NUL)
10247 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010248 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010249 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010250 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010251 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010252#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010253 size_t precomp_len = STRLEN(buf)+1;
10254 char_u *precomp_buf =
10255 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010256
Bram Moolenaar231334e2005-07-25 20:46:57 +000010257 if (precomp_buf)
10258 {
10259 mch_memmove(buf, precomp_buf, precomp_len);
10260 vim_free(precomp_buf);
10261 }
10262#endif
10263 addfile(gap, buf, flags);
10264 }
10265 }
10266 }
10267 }
10268
10269 closedir(dirp);
10270 }
10271
10272 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010273 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010274
10275 matches = gap->ga_len - start_len;
10276 if (matches > 0)
10277 qsort(((char_u **)gap->ga_data) + start_len, matches,
10278 sizeof(char_u *), pstrcmp);
10279 return matches;
10280}
10281#endif
10282
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010283#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010284static int find_previous_pathsep(char_u *path, char_u **psep);
10285static int is_unique(char_u *maybe_unique, garray_T *gap, int i);
10286static void expand_path_option(char_u *curdir, garray_T *gap);
10287static char_u *get_path_cutoff(char_u *fname, garray_T *gap);
10288static void uniquefy_paths(garray_T *gap, char_u *pattern);
10289static int expand_in_path(garray_T *gap, char_u *pattern, int flags);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010290
10291/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010292 * Moves "*psep" back to the previous path separator in "path".
10293 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010294 */
10295 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010296find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010297{
10298 /* skip the current separator */
10299 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010300 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010301
10302 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010303 while (*psep > path)
10304 {
10305 if (vim_ispathsep(**psep))
10306 return OK;
10307 mb_ptr_back(path, *psep);
10308 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010309
10310 return FAIL;
10311}
10312
10313/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010314 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10315 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010316 */
10317 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010318is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010319{
10320 int j;
10321 int candidate_len;
10322 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010323 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010324 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010325
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010326 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010327 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010328 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010329 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010330
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010331 candidate_len = (int)STRLEN(maybe_unique);
10332 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010333 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010334 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010335
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010336 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010337 if (fnamecmp(maybe_unique, rival) == 0
10338 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010339 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010340 }
10341
Bram Moolenaar162bd912010-07-28 22:29:10 +020010342 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010343}
10344
10345/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010346 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010347 * paths are expanded to their equivalent fullpath. This includes the "."
10348 * (relative to current buffer directory) and empty path (relative to current
10349 * directory) notations.
10350 *
10351 * TODO: handle upward search (;) and path limiter (**N) notations by
10352 * expanding each into their equivalent path(s).
10353 */
10354 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010355expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010356{
10357 char_u *path_option = *curbuf->b_p_path == NUL
10358 ? p_path : curbuf->b_p_path;
10359 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010360 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010361 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010362
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010363 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010364 return;
10365
10366 while (*path_option != NUL)
10367 {
10368 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10369
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010370 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010371 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010372 /* Relative to current buffer:
10373 * "/path/file" + "." -> "/path/"
10374 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010375 if (curbuf->b_ffname == NULL)
10376 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010377 p = gettail(curbuf->b_ffname);
10378 len = (int)(p - curbuf->b_ffname);
10379 if (len + (int)STRLEN(buf) >= MAXPATHL)
10380 continue;
10381 if (buf[1] == NUL)
10382 buf[len] = NUL;
10383 else
10384 STRMOVE(buf + len, buf + 2);
10385 mch_memmove(buf, curbuf->b_ffname, len);
10386 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010387 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010388 else if (buf[0] == NUL)
10389 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010390 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010391 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010392 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010393 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010394 else if (!mch_isFullName(buf))
10395 {
10396 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010397 len = (int)STRLEN(curdir);
10398 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010399 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010400 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010401 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010402 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010403 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010404 }
10405
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010406 if (ga_grow(gap, 1) == FAIL)
10407 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010408
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010409# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010410 /* Avoid the path ending in a backslash, it fails when a comma is
10411 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010412 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010413 if (buf[len - 1] == '\\')
10414 buf[len - 1] = '/';
10415# endif
10416
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010417 p = vim_strsave(buf);
10418 if (p == NULL)
10419 break;
10420 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010421 }
10422
10423 vim_free(buf);
10424}
10425
10426/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010427 * Returns a pointer to the file or directory name in "fname" that matches the
10428 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010429 *
10430 * path: /foo/bar/baz
10431 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010432 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010433 */
10434 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010435get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010436{
10437 int i;
10438 int maxlen = 0;
10439 char_u **path_part = (char_u **)gap->ga_data;
10440 char_u *cutoff = NULL;
10441
10442 for (i = 0; i < gap->ga_len; i++)
10443 {
10444 int j = 0;
10445
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010446 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010447# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010448 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10449#endif
10450 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010451 j++;
10452 if (j > maxlen)
10453 {
10454 maxlen = j;
10455 cutoff = &fname[j];
10456 }
10457 }
10458
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010459 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010460 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010461 while (vim_ispathsep(*cutoff))
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010462 mb_ptr_adv(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010463
10464 return cutoff;
10465}
10466
10467/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010468 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10469 * that they are unique with respect to each other while conserving the part
10470 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010471 */
10472 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010473uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010474{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010475 int i;
10476 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010477 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010478 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010479 char_u *pat;
10480 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010481 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010482 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010483 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010484 char_u **in_curdir = NULL;
10485 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010486
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010487 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010488 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010489
10490 /*
10491 * We need to prepend a '*' at the beginning of file_pattern so that the
10492 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010493 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010494 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010495 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010496 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010497 if (file_pattern == NULL)
10498 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010499 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010500 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010501 STRCAT(file_pattern, pattern);
10502 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10503 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010504 if (pat == NULL)
10505 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010506
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010507 regmatch.rm_ic = TRUE; /* always ignore case */
10508 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10509 vim_free(pat);
10510 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010511 return;
10512
Bram Moolenaar162bd912010-07-28 22:29:10 +020010513 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010514 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010515 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010516 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010517
10518 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010519 if (in_curdir == NULL)
10520 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010521
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010522 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010523 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010524 char_u *path = fnames[i];
10525 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010526 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010527 char_u *pathsep_p;
10528 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010529
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010530 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010531 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010532 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010533 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010534 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010535
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010536 /* Shorten the filename while maintaining its uniqueness */
10537 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010538
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010539 /* Don't assume all files can be reached without path when search
10540 * pattern starts with star star slash, so only remove path_cutoff
10541 * when possible. */
10542 if (pattern[0] == '*' && pattern[1] == '*'
10543 && vim_ispathsep_nocolon(pattern[2])
10544 && path_cutoff != NULL
10545 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10546 && is_unique(path_cutoff, gap, i))
10547 {
10548 sort_again = TRUE;
10549 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10550 }
10551 else
10552 {
10553 /* Here all files can be reached without path, so get shortest
10554 * unique path. We start at the end of the path. */
10555 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010556
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010557 while (find_previous_pathsep(path, &pathsep_p))
10558 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10559 && is_unique(pathsep_p + 1, gap, i)
10560 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10561 {
10562 sort_again = TRUE;
10563 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10564 break;
10565 }
10566 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010567
10568 if (mch_isFullName(path))
10569 {
10570 /*
10571 * Last resort: shorten relative to curdir if possible.
10572 * 'possible' means:
10573 * 1. It is under the current directory.
10574 * 2. The result is actually shorter than the original.
10575 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010576 * Before curdir After
10577 * /foo/bar/file.txt /foo/bar ./file.txt
10578 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10579 * /file.txt / /file.txt
10580 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010581 */
10582 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010583 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010584#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010585 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010586 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010587 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010588 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010589 && !vim_ispathsep(*short_name)
10590#endif
10591 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010592 {
10593 STRCPY(path, ".");
10594 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010595 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010596 }
10597 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010598 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010599 }
10600
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010601 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010602 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010603 {
10604 char_u *rel_path;
10605 char_u *path = in_curdir[i];
10606
10607 if (path == NULL)
10608 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010609
10610 /* If the {filename} is not unique, change it to ./{filename}.
10611 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010612 short_name = shorten_fname(path, curdir);
10613 if (short_name == NULL)
10614 short_name = path;
10615 if (is_unique(short_name, gap, i))
10616 {
10617 STRCPY(fnames[i], short_name);
10618 continue;
10619 }
10620
10621 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10622 if (rel_path == NULL)
10623 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010624 STRCPY(rel_path, ".");
10625 add_pathsep(rel_path);
10626 STRCAT(rel_path, short_name);
10627
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010628 vim_free(fnames[i]);
10629 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010630 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010631 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010632 }
10633
Bram Moolenaar162bd912010-07-28 22:29:10 +020010634theend:
10635 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010636 if (in_curdir != NULL)
10637 {
10638 for (i = 0; i < gap->ga_len; i++)
10639 vim_free(in_curdir[i]);
10640 vim_free(in_curdir);
10641 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010642 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010643 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010644
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010645 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010646 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010647}
10648
10649/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010650 * Calls globpath() with 'path' values for the given pattern and stores the
10651 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010652 * Returns the total number of matches.
10653 */
10654 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010655expand_in_path(
10656 garray_T *gap,
10657 char_u *pattern,
10658 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010659{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010660 char_u *curdir;
10661 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010662 char_u *paths = NULL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010663
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010664 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010665 return 0;
10666 mch_dirname(curdir, MAXPATHL);
10667
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010668 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010669 expand_path_option(curdir, &path_ga);
10670 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010671 if (path_ga.ga_len == 0)
10672 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010673
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010674 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010675 ga_clear_strings(&path_ga);
10676 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010677 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010678
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010679 globpath(paths, pattern, gap, (flags & EW_ICASE) ? WILD_ICASE : 0);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010680 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010681
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010682 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010683}
10684#endif
10685
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010686#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10687/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010688 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10689 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010690 */
10691 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010692remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010693{
10694 int i;
10695 int j;
10696 char_u **fnames = (char_u **)gap->ga_data;
10697
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010698 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010699 for (i = gap->ga_len - 1; i > 0; --i)
10700 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10701 {
10702 vim_free(fnames[i]);
10703 for (j = i + 1; j < gap->ga_len; ++j)
10704 fnames[j - 1] = fnames[j];
10705 --gap->ga_len;
10706 }
10707}
10708#endif
10709
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010710static int has_env_var(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010711
10712/*
10713 * Return TRUE if "p" contains what looks like an environment variable.
10714 * Allowing for escaping.
10715 */
10716 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010717has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010718{
10719 for ( ; *p; mb_ptr_adv(p))
10720 {
10721 if (*p == '\\' && p[1] != NUL)
10722 ++p;
10723 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010724#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010725 "$%"
10726#else
10727 "$"
10728#endif
10729 , *p) != NULL)
10730 return TRUE;
10731 }
10732 return FALSE;
10733}
10734
10735#ifdef SPECIAL_WILDCHAR
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010736static int has_special_wildchar(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010737
10738/*
10739 * Return TRUE if "p" contains a special wildcard character.
10740 * Allowing for escaping.
10741 */
10742 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010743has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010744{
10745 for ( ; *p; mb_ptr_adv(p))
10746 {
10747 if (*p == '\\' && p[1] != NUL)
10748 ++p;
10749 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10750 return TRUE;
10751 }
10752 return FALSE;
10753}
10754#endif
10755
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756/*
10757 * Generic wildcard expansion code.
10758 *
10759 * Characters in "pat" that should not be expanded must be preceded with a
10760 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10761 *
10762 * Return FAIL when no single file was found. In this case "num_file" is not
10763 * set, and "file" may contain an error message.
10764 * Return OK when some files found. "num_file" is set to the number of
10765 * matches, "file" to the array of matches. Call FreeWild() later.
10766 */
10767 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010768gen_expand_wildcards(
10769 int num_pat, /* number of input patterns */
10770 char_u **pat, /* array of input patterns */
10771 int *num_file, /* resulting number of files */
10772 char_u ***file, /* array of resulting files */
10773 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774{
10775 int i;
10776 garray_T ga;
10777 char_u *p;
10778 static int recursive = FALSE;
10779 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020010780 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010781#if defined(FEAT_SEARCHPATH)
10782 int did_expand_in_path = FALSE;
10783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010784
10785 /*
10786 * expand_env() is called to expand things like "~user". If this fails,
10787 * it calls ExpandOne(), which brings us back here. In this case, always
10788 * call the machine specific expansion function, if possible. Otherwise,
10789 * return FAIL.
10790 */
10791 if (recursive)
10792#ifdef SPECIAL_WILDCHAR
10793 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10794#else
10795 return FAIL;
10796#endif
10797
10798#ifdef SPECIAL_WILDCHAR
10799 /*
10800 * If there are any special wildcard characters which we cannot handle
10801 * here, call machine specific function for all the expansion. This
10802 * avoids starting the shell for each argument separately.
10803 * For `=expr` do use the internal function.
10804 */
10805 for (i = 0; i < num_pat; i++)
10806 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010807 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808# ifdef VIM_BACKTICK
10809 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10810# endif
10811 )
10812 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10813 }
10814#endif
10815
10816 recursive = TRUE;
10817
10818 /*
10819 * The matching file names are stored in a growarray. Init it empty.
10820 */
10821 ga_init2(&ga, (int)sizeof(char_u *), 30);
10822
10823 for (i = 0; i < num_pat; ++i)
10824 {
10825 add_pat = -1;
10826 p = pat[i];
10827
10828#ifdef VIM_BACKTICK
10829 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020010830 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020010832 if (add_pat == -1)
10833 retval = FAIL;
10834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835 else
10836#endif
10837 {
10838 /*
10839 * First expand environment variables, "~/" and "~user/".
10840 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010841 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010843 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844 if (p == NULL)
10845 p = pat[i];
10846#ifdef UNIX
10847 /*
10848 * On Unix, if expand_env() can't expand an environment
10849 * variable, use the shell to do that. Discard previously
10850 * found file names and start all over again.
10851 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010852 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853 {
10854 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010855 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020010857 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858 recursive = FALSE;
10859 return i;
10860 }
10861#endif
10862 }
10863
10864 /*
10865 * If there are wildcards: Expand file names and add each match to
10866 * the list. If there is no match, and EW_NOTFOUND is given, add
10867 * the pattern.
10868 * If there are no wildcards: Add the file name if it exists or
10869 * when EW_NOTFOUND is given.
10870 */
10871 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010872 {
10873#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010874 if ((flags & EW_PATH)
10875 && !mch_isFullName(p)
10876 && !(p[0] == '.'
10877 && (vim_ispathsep(p[1])
10878 || (p[1] == '.' && vim_ispathsep(p[2]))))
10879 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010880 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010881 /* :find completion where 'path' is used.
10882 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010883 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010884 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010885 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010886 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010887 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010888 else
10889#endif
10890 add_pat = mch_expandpath(&ga, p, flags);
10891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892 }
10893
10894 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
10895 {
10896 char_u *t = backslash_halve_save(p);
10897
10898#if defined(MACOS_CLASSIC)
10899 slash_to_colon(t);
10900#endif
10901 /* When EW_NOTFOUND is used, always add files and dirs. Makes
10902 * "vim c:/" work. */
10903 if (flags & EW_NOTFOUND)
10904 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020010905 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906 addfile(&ga, t, flags);
10907 vim_free(t);
10908 }
10909
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010910#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010911 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010912 uniquefy_paths(&ga, p);
10913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 if (p != pat[i])
10915 vim_free(p);
10916 }
10917
10918 *num_file = ga.ga_len;
10919 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
10920
10921 recursive = FALSE;
10922
Bram Moolenaar336bd622016-01-17 18:23:58 +010010923 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924}
10925
10926# ifdef VIM_BACKTICK
10927
10928/*
10929 * Return TRUE if we can expand this backtick thing here.
10930 */
10931 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010932vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933{
10934 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
10935}
10936
10937/*
10938 * Expand an item in `backticks` by executing it as a command.
10939 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020010940 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941 */
10942 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010943expand_backtick(
10944 garray_T *gap,
10945 char_u *pat,
10946 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947{
10948 char_u *p;
10949 char_u *cmd;
10950 char_u *buffer;
10951 int cnt = 0;
10952 int i;
10953
10954 /* Create the command: lop off the backticks. */
10955 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
10956 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020010957 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958
10959#ifdef FEAT_EVAL
10960 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000010961 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 else
10963#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010964 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010965 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966 vim_free(cmd);
10967 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020010968 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969
10970 cmd = buffer;
10971 while (*cmd != NUL)
10972 {
10973 cmd = skipwhite(cmd); /* skip over white space */
10974 p = cmd;
10975 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
10976 ++p;
10977 /* add an entry if it is not empty */
10978 if (p > cmd)
10979 {
10980 i = *p;
10981 *p = NUL;
10982 addfile(gap, cmd, flags);
10983 *p = i;
10984 ++cnt;
10985 }
10986 cmd = p;
10987 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
10988 ++cmd;
10989 }
10990
10991 vim_free(buffer);
10992 return cnt;
10993}
10994# endif /* VIM_BACKTICK */
10995
10996/*
10997 * Add a file to a file list. Accepted flags:
10998 * EW_DIR add directories
10999 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011000 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001 * EW_NOTFOUND add even when it doesn't exist
11002 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011003 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011004 */
11005 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011006addfile(
11007 garray_T *gap,
11008 char_u *f, /* filename */
11009 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010{
11011 char_u *p;
11012 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011013 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011015 /* if the file/dir/link doesn't exist, may not add it */
11016 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011017 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 return;
11019
11020#ifdef FNAME_ILLEGAL
11021 /* if the file/dir contains illegal characters, don't add it */
11022 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11023 return;
11024#endif
11025
11026 isdir = mch_isdir(f);
11027 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11028 return;
11029
Bram Moolenaarb5971142015-03-21 17:32:19 +010011030 /* If the file isn't executable, may not add it. Do accept directories.
11031 * When invoked from expand_shellcmd() do not use $PATH. */
11032 if (!isdir && (flags & EW_EXEC)
11033 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011034 return;
11035
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036 /* Make room for another item in the file list. */
11037 if (ga_grow(gap, 1) == FAIL)
11038 return;
11039
11040 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11041 if (p == NULL)
11042 return;
11043
11044 STRCPY(p, f);
11045#ifdef BACKSLASH_IN_FILENAME
11046 slash_adjust(p);
11047#endif
11048 /*
11049 * Append a slash or backslash after directory names if none is present.
11050 */
11051#ifndef DONT_ADD_PATHSEP_TO_DIR
11052 if (isdir && (flags & EW_ADDSLASH))
11053 add_pathsep(p);
11054#endif
11055 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056}
11057#endif /* !NO_EXPANDPATH */
11058
11059#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11060
11061#ifndef SEEK_SET
11062# define SEEK_SET 0
11063#endif
11064#ifndef SEEK_END
11065# define SEEK_END 2
11066#endif
11067
11068/*
11069 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011070 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11071 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072 * Returns an allocated string, or NULL for error.
11073 */
11074 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011075get_cmd_output(
11076 char_u *cmd,
11077 char_u *infile, /* optional input file name */
11078 int flags, /* can be SHELL_SILENT */
11079 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080{
11081 char_u *tempname;
11082 char_u *command;
11083 char_u *buffer = NULL;
11084 int len;
11085 int i = 0;
11086 FILE *fd;
11087
11088 if (check_restricted() || check_secure())
11089 return NULL;
11090
11091 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011092 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011093 {
11094 EMSG(_(e_notmp));
11095 return NULL;
11096 }
11097
11098 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011099 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100 if (command == NULL)
11101 goto done;
11102
11103 /*
11104 * Call the shell to execute the command (errors are ignored).
11105 * Don't check timestamps here.
11106 */
11107 ++no_check_timestamps;
11108 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11109 --no_check_timestamps;
11110
11111 vim_free(command);
11112
11113 /*
11114 * read the names from the file into memory
11115 */
11116# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011117 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118 fd = mch_fopen((char *)tempname, "r");
11119# else
11120 fd = mch_fopen((char *)tempname, READBIN);
11121# endif
11122
11123 if (fd == NULL)
11124 {
11125 EMSG2(_(e_notopen), tempname);
11126 goto done;
11127 }
11128
11129 fseek(fd, 0L, SEEK_END);
11130 len = ftell(fd); /* get size of temp file */
11131 fseek(fd, 0L, SEEK_SET);
11132
11133 buffer = alloc(len + 1);
11134 if (buffer != NULL)
11135 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11136 fclose(fd);
11137 mch_remove(tempname);
11138 if (buffer == NULL)
11139 goto done;
11140#ifdef VMS
11141 len = i; /* VMS doesn't give us what we asked for... */
11142#endif
11143 if (i != len)
11144 {
11145 EMSG2(_(e_notread), tempname);
11146 vim_free(buffer);
11147 buffer = NULL;
11148 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011149 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011150 {
11151 /* Change NUL into SOH, otherwise the string is truncated. */
11152 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011153 if (buffer[i] == NUL)
11154 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011155
Bram Moolenaar162bd912010-07-28 22:29:10 +020011156 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011157 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011158 else
11159 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160
11161done:
11162 vim_free(tempname);
11163 return buffer;
11164}
11165#endif
11166
11167/*
11168 * Free the list of files returned by expand_wildcards() or other expansion
11169 * functions.
11170 */
11171 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011172FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011174 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176 while (count--)
11177 vim_free(files[count]);
11178 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179}
11180
11181/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011182 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011183 * Don't do this when still processing a command or a mapping.
11184 * Don't do this when inside a ":normal" command.
11185 */
11186 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011187goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188{
11189 return (p_im && stuff_empty() && typebuf_typed());
11190}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011191
11192/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011193 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011194 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11195 * - Remove any argument. E.g., "csh -f" -> "csh".
11196 * But don't allow a space in the path, so that this works:
11197 * "/usr/bin/csh --rcfile ~/.cshrc"
11198 * But don't do that for Windows, it's common to have a space in the path.
11199 */
11200 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011201get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011202{
11203 char_u *p;
11204
11205#ifdef WIN3264
11206 p = gettail(p_sh);
11207 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11208#else
11209 p = skiptowhite(p_sh);
11210 if (*p == NUL)
11211 {
11212 /* No white space, use the tail. */
11213 p = vim_strsave(gettail(p_sh));
11214 }
11215 else
11216 {
11217 char_u *p1, *p2;
11218
11219 /* Find the last path separator before the space. */
11220 p1 = p_sh;
11221 for (p2 = p_sh; p2 < p; mb_ptr_adv(p2))
11222 if (vim_ispathsep(*p2))
11223 p1 = p2 + 1;
11224 p = vim_strnsave(p1, (int)(p - p1));
11225 }
11226#endif
11227 return p;
11228}