blob: 046e2f067163ebbb14efcb1398d6beefd3bc9d39 [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 Moolenaara40aa762014-06-25 22:55:38 +0200495 static int 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
505 || 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 Moolenaara40aa762014-06-25 22:55:38 +0200509 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 Moolenaar560379d2017-01-21 22:50:00 +01002183#if defined(FEAT_MBYTE) || defined(PROTO)
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 }
2771 ++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 }
3198 ++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();
3267 if (msg_silent == 0 && !silent_mode)
3268 {
3269 out_flush();
3270 ui_delay(1000L, TRUE); /* give the user time to think about it */
3271 }
3272 curbuf->b_did_warn = TRUE;
3273 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3274 if (msg_row < Rows - 1)
3275 showmode();
3276 }
3277}
3278
3279/*
3280 * Ask for a reply from the user, a 'y' or a 'n'.
3281 * No other characters are accepted, the message is repeated until a valid
3282 * reply is entered or CTRL-C is hit.
3283 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3284 * from any buffers but directly from the user.
3285 *
3286 * return the 'y' or 'n'
3287 */
3288 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003289ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290{
3291 int r = ' ';
3292 int save_State = State;
3293
3294 if (exiting) /* put terminal in raw mode for this question */
3295 settmode(TMODE_RAW);
3296 ++no_wait_return;
3297#ifdef USE_ON_FLY_SCROLL
3298 dont_scroll = TRUE; /* disallow scrolling here */
3299#endif
3300 State = CONFIRM; /* mouse behaves like with :confirm */
3301#ifdef FEAT_MOUSE
3302 setmouse(); /* disables mouse for xterm */
3303#endif
3304 ++no_mapping;
3305 ++allow_keys; /* no mapping here, but recognize keys */
3306
3307 while (r != 'y' && r != 'n')
3308 {
3309 /* same highlighting as for wait_return */
3310 smsg_attr(hl_attr(HLF_R), (char_u *)"%s (y/n)?", str);
3311 if (direct)
3312 r = get_keystroke();
3313 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003314 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 if (r == Ctrl_C || r == ESC)
3316 r = 'n';
3317 msg_putchar(r); /* show what you typed */
3318 out_flush();
3319 }
3320 --no_wait_return;
3321 State = save_State;
3322#ifdef FEAT_MOUSE
3323 setmouse();
3324#endif
3325 --no_mapping;
3326 --allow_keys;
3327
3328 return r;
3329}
3330
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003331#if defined(FEAT_MOUSE) || defined(PROTO)
3332/*
3333 * Return TRUE if "c" is a mouse key.
3334 */
3335 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003336is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003337{
3338 return c == K_LEFTMOUSE
3339 || c == K_LEFTMOUSE_NM
3340 || c == K_LEFTDRAG
3341 || c == K_LEFTRELEASE
3342 || c == K_LEFTRELEASE_NM
3343 || c == K_MIDDLEMOUSE
3344 || c == K_MIDDLEDRAG
3345 || c == K_MIDDLERELEASE
3346 || c == K_RIGHTMOUSE
3347 || c == K_RIGHTDRAG
3348 || c == K_RIGHTRELEASE
3349 || c == K_MOUSEDOWN
3350 || c == K_MOUSEUP
3351 || c == K_MOUSELEFT
3352 || c == K_MOUSERIGHT
3353 || c == K_X1MOUSE
3354 || c == K_X1DRAG
3355 || c == K_X1RELEASE
3356 || c == K_X2MOUSE
3357 || c == K_X2DRAG
3358 || c == K_X2RELEASE;
3359}
3360#endif
3361
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362/*
3363 * Get a key stroke directly from the user.
3364 * Ignores mouse clicks and scrollbar events, except a click for the left
3365 * button (used at the more prompt).
3366 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3367 * Disadvantage: typeahead is ignored.
3368 * Translates the interrupt character for unix to ESC.
3369 */
3370 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003371get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003373 char_u *buf = NULL;
3374 int buflen = 150;
3375 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 int len = 0;
3377 int n;
3378 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003379 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380
3381 mapped_ctrl_c = FALSE; /* mappings are not used here */
3382 for (;;)
3383 {
3384 cursor_on();
3385 out_flush();
3386
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003387 /* Leave some room for check_termcode() to insert a key code into (max
3388 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3389 * bytes. */
3390 maxlen = (buflen - 6 - len) / 3;
3391 if (buf == NULL)
3392 buf = alloc(buflen);
3393 else if (maxlen < 10)
3394 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003395 char_u *t_buf = buf;
3396
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003397 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003398 * escape sequence. */
3399 buflen += 100;
3400 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003401 if (buf == NULL)
3402 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003403 maxlen = (buflen - 6 - len) / 3;
3404 }
3405 if (buf == NULL)
3406 {
3407 do_outofmem_msg((long_u)buflen);
3408 return ESC; /* panic! */
3409 }
3410
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003412 * terminal code to complete. */
3413 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 if (n > 0)
3415 {
3416 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003417 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003419 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003421 else if (len > 0)
3422 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423
Bram Moolenaar4395a712006-09-05 18:57:57 +00003424 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003425 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003426 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003428
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003429 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003430 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003431 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003432 {
3433 /* Redrawing was postponed, do it now. */
3434 update_screen(0);
3435 setcursor(); /* put cursor back where it belongs */
3436 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003437 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003438 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003439 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003441 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 continue;
3443
3444 /* Handle modifier and/or special key code. */
3445 n = buf[0];
3446 if (n == K_SPECIAL)
3447 {
3448 n = TO_SPECIAL(buf[1], buf[2]);
3449 if (buf[1] == KS_MODIFIER
3450 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003451#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003452 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003453#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003454#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 || n == K_VER_SCROLLBAR
3456 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457#endif
3458 )
3459 {
3460 if (buf[1] == KS_MODIFIER)
3461 mod_mask = buf[2];
3462 len -= 3;
3463 if (len > 0)
3464 mch_memmove(buf, buf + 3, (size_t)len);
3465 continue;
3466 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003467 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 }
3469#ifdef FEAT_MBYTE
3470 if (has_mbyte)
3471 {
3472 if (MB_BYTE2LEN(n) > len)
3473 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003474 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 n = (*mb_ptr2char)(buf);
3476 }
3477#endif
3478#ifdef UNIX
3479 if (n == intr_char)
3480 n = ESC;
3481#endif
3482 break;
3483 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003484 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485
3486 mapped_ctrl_c = save_mapped_ctrl_c;
3487 return n;
3488}
3489
3490/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003491 * Get a number from the user.
3492 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 */
3494 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003495get_number(
3496 int colon, /* allow colon to abort */
3497 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498{
3499 int n = 0;
3500 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003501 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003503 if (mouse_used != NULL)
3504 *mouse_used = FALSE;
3505
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 /* When not printing messages, the user won't know what to type, return a
3507 * zero (as if CR was hit). */
3508 if (msg_silent != 0)
3509 return 0;
3510
3511#ifdef USE_ON_FLY_SCROLL
3512 dont_scroll = TRUE; /* disallow scrolling here */
3513#endif
3514 ++no_mapping;
3515 ++allow_keys; /* no mapping here, but recognize keys */
3516 for (;;)
3517 {
3518 windgoto(msg_row, msg_col);
3519 c = safe_vgetc();
3520 if (VIM_ISDIGIT(c))
3521 {
3522 n = n * 10 + c - '0';
3523 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003524 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 }
3526 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3527 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003528 if (typed > 0)
3529 {
3530 MSG_PUTS("\b \b");
3531 --typed;
3532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003535#ifdef FEAT_MOUSE
3536 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3537 {
3538 *mouse_used = TRUE;
3539 n = mouse_row + 1;
3540 break;
3541 }
3542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 else if (n == 0 && c == ':' && colon)
3544 {
3545 stuffcharReadbuff(':');
3546 if (!exmode_active)
3547 cmdline_row = msg_row;
3548 skip_redraw = TRUE; /* skip redraw once */
3549 do_redraw = FALSE;
3550 break;
3551 }
3552 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3553 break;
3554 }
3555 --no_mapping;
3556 --allow_keys;
3557 return n;
3558}
3559
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003560/*
3561 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003562 * When "mouse_used" is not NULL allow using the mouse and in that case return
3563 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003564 */
3565 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003566prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003567{
3568 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003569 int save_cmdline_row;
3570 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003571
3572 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003573 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003574 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003575 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003576 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003577
Bram Moolenaar203335e2006-09-03 14:35:42 +00003578 /* Set the state such that text can be selected/copied/pasted and we still
3579 * get mouse events. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003580 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003581 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003582 save_State = State;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003583 State = CMDLINE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003584
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003585 i = get_number(TRUE, mouse_used);
3586 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003587 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003588 /* don't call wait_return() now */
3589 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003590 cmdline_row = msg_row - 1;
3591 need_wait_return = FALSE;
3592 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003593 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003594 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003595 else
3596 cmdline_row = save_cmdline_row;
3597 State = save_State;
3598
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003599 return i;
3600}
3601
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003603msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604{
3605 long pn;
3606
3607 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3609 return;
3610
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003611 /* We don't want to overwrite another important message, but do overwrite
3612 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3613 * then "put" reports the last action. */
3614 if (keep_msg != NULL && !keep_msg_more)
3615 return;
3616
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 if (n > 0)
3618 pn = n;
3619 else
3620 pn = -n;
3621
3622 if (pn > p_report)
3623 {
3624 if (pn == 1)
3625 {
3626 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003627 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3628 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003630 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3631 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 }
3633 else
3634 {
3635 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003636 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3637 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003639 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3640 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 }
3642 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003643 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 if (msg(msg_buf))
3645 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003646 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003647 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 }
3649 }
3650}
3651
3652/*
3653 * flush map and typeahead buffers and give a warning for an error
3654 */
3655 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003656beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657{
3658 if (emsg_silent == 0)
3659 {
3660 flush_buffers(FALSE);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003661 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 }
3663}
3664
3665/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003666 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 */
3668 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003669vim_beep(
3670 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671{
3672 if (emsg_silent == 0)
3673 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003674 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3675 {
3676 if (p_vb
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677#ifdef FEAT_GUI
Bram Moolenaar165bc692015-07-21 17:53:25 +02003678 /* While the GUI is starting up the termcap is set for the
3679 * GUI but the output still goes to a terminal. */
3680 && !(gui.in_use && gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681#endif
Bram Moolenaar165bc692015-07-21 17:53:25 +02003682 )
Bram Moolenaar165bc692015-07-21 17:53:25 +02003683 out_str(T_VB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02003685 out_char(BELL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003687
3688 /* When 'verbose' is set and we are sourcing a script or executing a
3689 * function give the user a hint where the beep comes from. */
3690 if (vim_strchr(p_debug, 'e') != NULL)
3691 {
3692 msg_source(hl_attr(HLF_W));
3693 msg_attr((char_u *)_("Beep!"), hl_attr(HLF_W));
3694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 }
3696}
3697
3698/*
3699 * To get the "real" home directory:
3700 * - get value of $HOME
3701 * For Unix:
3702 * - go to that directory
3703 * - do mch_dirname() to get the real name of that directory.
3704 * This also works with mounts and links.
3705 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3706 */
3707static char_u *homedir = NULL;
3708
3709 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003710init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711{
3712 char_u *var;
3713
Bram Moolenaar05159a02005-02-26 23:04:13 +00003714 /* In case we are called a second time (when 'encoding' changes). */
3715 vim_free(homedir);
3716 homedir = NULL;
3717
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718#ifdef VMS
3719 var = mch_getenv((char_u *)"SYS$LOGIN");
3720#else
3721 var = mch_getenv((char_u *)"HOME");
3722#endif
3723
3724 if (var != NULL && *var == NUL) /* empty is same as not set */
3725 var = NULL;
3726
3727#ifdef WIN3264
3728 /*
3729 * Weird but true: $HOME may contain an indirect reference to another
3730 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3731 * when $HOME is being set.
3732 */
3733 if (var != NULL && *var == '%')
3734 {
3735 char_u *p;
3736 char_u *exp;
3737
3738 p = vim_strchr(var + 1, '%');
3739 if (p != NULL)
3740 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003741 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 exp = mch_getenv(NameBuff);
3743 if (exp != NULL && *exp != NUL
3744 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3745 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003746 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 var = NameBuff;
3748 /* Also set $HOME, it's needed for _viminfo. */
3749 vim_setenv((char_u *)"HOME", NameBuff);
3750 }
3751 }
3752 }
3753
3754 /*
3755 * Typically, $HOME is not defined on Windows, unless the user has
3756 * specifically defined it for Vim's sake. However, on Windows NT
3757 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3758 * each user. Try constructing $HOME from these.
3759 */
3760 if (var == NULL)
3761 {
3762 char_u *homedrive, *homepath;
3763
3764 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3765 homepath = mch_getenv((char_u *)"HOMEPATH");
Bram Moolenaar6f977012010-01-06 17:53:38 +01003766 if (homepath == NULL || *homepath == NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003767 homepath = (char_u *)"\\";
Bram Moolenaar6f977012010-01-06 17:53:38 +01003768 if (homedrive != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3770 {
3771 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3772 if (NameBuff[0] != NUL)
3773 {
3774 var = NameBuff;
3775 /* Also set $HOME, it's needed for _viminfo. */
3776 vim_setenv((char_u *)"HOME", NameBuff);
3777 }
3778 }
3779 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003780
3781# if defined(FEAT_MBYTE)
3782 if (enc_utf8 && var != NULL)
3783 {
3784 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003785 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003786
3787 /* Convert from active codepage to UTF-8. Other conversions are
3788 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003789 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003790 if (pp != NULL)
3791 {
3792 homedir = pp;
3793 return;
3794 }
3795 }
3796# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797#endif
3798
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003799#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 /*
3801 * Default home dir is C:/
3802 * Best assumption we can make in such a situation.
3803 */
3804 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003805 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806#endif
3807 if (var != NULL)
3808 {
3809#ifdef UNIX
3810 /*
3811 * Change to the directory and get the actual path. This resolves
3812 * links. Don't do it when we can't return.
3813 */
3814 if (mch_dirname(NameBuff, MAXPATHL) == OK
3815 && mch_chdir((char *)NameBuff) == 0)
3816 {
3817 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3818 var = IObuff;
3819 if (mch_chdir((char *)NameBuff) != 0)
3820 EMSG(_(e_prev_dir));
3821 }
3822#endif
3823 homedir = vim_strsave(var);
3824 }
3825}
3826
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003827#if defined(EXITFREE) || defined(PROTO)
3828 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003829free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003830{
3831 vim_free(homedir);
3832}
Bram Moolenaar24305862012-08-15 14:05:05 +02003833
3834# ifdef FEAT_CMDL_COMPL
3835 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003836free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02003837{
3838 ga_clear_strings(&ga_users);
3839}
3840# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003841#endif
3842
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003844 * Call expand_env() and store the result in an allocated string.
3845 * This is not very memory efficient, this expects the result to be freed
3846 * again soon.
3847 */
3848 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003849expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003850{
3851 return expand_env_save_opt(src, FALSE);
3852}
3853
3854/*
3855 * Idem, but when "one" is TRUE handle the string as one file name, only
3856 * expand "~" at the start.
3857 */
3858 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003859expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003860{
3861 char_u *p;
3862
3863 p = alloc(MAXPATHL);
3864 if (p != NULL)
3865 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3866 return p;
3867}
3868
3869/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 * Expand environment variable with path name.
3871 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003872 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 * If anything fails no expansion is done and dst equals src.
3874 */
3875 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003876expand_env(
3877 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
3878 char_u *dst, /* where to put the result */
3879 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003881 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882}
3883
3884 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003885expand_env_esc(
3886 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
3887 char_u *dst, /* where to put the result */
3888 int dstlen, /* maximum length of the result */
3889 int esc, /* escape spaces in expanded variables */
3890 int one, /* "srcp" is one file name */
3891 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003893 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 char_u *tail;
3895 int c;
3896 char_u *var;
3897 int copy_char;
3898 int mustfree; /* var was allocated, need to free it later */
3899 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003900 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003902 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003903 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003904
3905 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 --dstlen; /* leave one char space for "\," */
3907 while (*src && dstlen > 0)
3908 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003909#ifdef FEAT_EVAL
3910 /* Skip over `=expr`. */
3911 if (src[0] == '`' && src[1] == '=')
3912 {
3913 size_t len;
3914
3915 var = src;
3916 src += 2;
3917 (void)skip_expr(&src);
3918 if (*src == '`')
3919 ++src;
3920 len = src - var;
3921 if (len > (size_t)dstlen)
3922 len = dstlen;
3923 vim_strncpy(dst, var, len);
3924 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02003925 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003926 continue;
3927 }
3928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003930 if ((*src == '$'
3931#ifdef VMS
3932 && at_start
3933#endif
3934 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003935#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 || *src == '%'
3937#endif
3938 || (*src == '~' && at_start))
3939 {
3940 mustfree = FALSE;
3941
3942 /*
3943 * The variable name is copied into dst temporarily, because it may
3944 * be a string in read-only memory and a NUL needs to be appended.
3945 */
3946 if (*src != '~') /* environment var */
3947 {
3948 tail = src + 1;
3949 var = dst;
3950 c = dstlen - 1;
3951
3952#ifdef UNIX
3953 /* Unix has ${var-name} type environment vars */
3954 if (*tail == '{' && !vim_isIDc('{'))
3955 {
3956 tail++; /* ignore '{' */
3957 while (c-- > 0 && *tail && *tail != '}')
3958 *var++ = *tail++;
3959 }
3960 else
3961#endif
3962 {
3963 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003964#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 || (*src == '%' && *tail != '%')
3966#endif
3967 ))
3968 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 }
3971 }
3972
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003973#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974# ifdef UNIX
3975 if (src[1] == '{' && *tail != '}')
3976# else
3977 if (*src == '%' && *tail != '%')
3978# endif
3979 var = NULL;
3980 else
3981 {
3982# ifdef UNIX
3983 if (src[1] == '{')
3984# else
3985 if (*src == '%')
3986#endif
3987 ++tail;
3988#endif
3989 *var = NUL;
3990 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003991#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 }
3993#endif
3994 }
3995 /* home directory */
3996 else if ( src[1] == NUL
3997 || vim_ispathsep(src[1])
3998 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
3999 {
4000 var = homedir;
4001 tail = src + 1;
4002 }
4003 else /* user directory */
4004 {
4005#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4006 /*
4007 * Copy ~user to dst[], so we can put a NUL after it.
4008 */
4009 tail = src;
4010 var = dst;
4011 c = dstlen - 1;
4012 while ( c-- > 0
4013 && *tail
4014 && vim_isfilec(*tail)
4015 && !vim_ispathsep(*tail))
4016 *var++ = *tail++;
4017 *var = NUL;
4018# ifdef UNIX
4019 /*
4020 * If the system supports getpwnam(), use it.
4021 * Otherwise, or if getpwnam() fails, the shell is used to
4022 * expand ~user. This is slower and may fail if the shell
4023 * does not support ~user (old versions of /bin/sh).
4024 */
4025# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4026 {
4027 struct passwd *pw;
4028
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004029 /* Note: memory allocated by getpwnam() is never freed.
4030 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 pw = getpwnam((char *)dst + 1);
4032 if (pw != NULL)
4033 var = (char_u *)pw->pw_dir;
4034 else
4035 var = NULL;
4036 }
4037 if (var == NULL)
4038# endif
4039 {
4040 expand_T xpc;
4041
4042 ExpandInit(&xpc);
4043 xpc.xp_context = EXPAND_FILES;
4044 var = ExpandOne(&xpc, dst, NULL,
4045 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 mustfree = TRUE;
4047 }
4048
4049# else /* !UNIX, thus VMS */
4050 /*
4051 * USER_HOME is a comma-separated list of
4052 * directories to search for the user account in.
4053 */
4054 {
4055 char_u test[MAXPATHL], paths[MAXPATHL];
4056 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004057 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058
4059 STRCPY(paths, USER_HOME);
4060 next_path = paths;
4061 while (*next_path)
4062 {
4063 for (path = next_path; *next_path && *next_path != ',';
4064 next_path++);
4065 if (*next_path)
4066 *next_path++ = NUL;
4067 STRCPY(test, path);
4068 STRCAT(test, "/");
4069 STRCAT(test, dst + 1);
4070 if (mch_stat(test, &st) == 0)
4071 {
4072 var = alloc(STRLEN(test) + 1);
4073 STRCPY(var, test);
4074 mustfree = TRUE;
4075 break;
4076 }
4077 }
4078 }
4079# endif /* UNIX */
4080#else
4081 /* cannot expand user's home directory, so don't try */
4082 var = NULL;
4083 tail = (char_u *)""; /* for gcc */
4084#endif /* UNIX || VMS */
4085 }
4086
4087#ifdef BACKSLASH_IN_FILENAME
4088 /* If 'shellslash' is set change backslashes to forward slashes.
4089 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4090 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4091 {
4092 char_u *p = vim_strsave(var);
4093
4094 if (p != NULL)
4095 {
4096 if (mustfree)
4097 vim_free(var);
4098 var = p;
4099 mustfree = TRUE;
4100 forward_slash(var);
4101 }
4102 }
4103#endif
4104
4105 /* If "var" contains white space, escape it with a backslash.
4106 * Required for ":e ~/tt" when $HOME includes a space. */
4107 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4108 {
4109 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4110
4111 if (p != NULL)
4112 {
4113 if (mustfree)
4114 vim_free(var);
4115 var = p;
4116 mustfree = TRUE;
4117 }
4118 }
4119
4120 if (var != NULL && *var != NUL
4121 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4122 {
4123 STRCPY(dst, var);
4124 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004125 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 /* if var[] ends in a path separator and tail[] starts
4127 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004128 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4130 && dst[-1] != ':'
4131#endif
4132 && vim_ispathsep(*tail))
4133 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004134 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 src = tail;
4136 copy_char = FALSE;
4137 }
4138 if (mustfree)
4139 vim_free(var);
4140 }
4141
4142 if (copy_char) /* copy at least one char */
4143 {
4144 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004145 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004146 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4147 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 */
4149 at_start = FALSE;
4150 if (src[0] == '\\' && src[1] != NUL)
4151 {
4152 *dst++ = *src++;
4153 --dstlen;
4154 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004155 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 at_start = TRUE;
4157 *dst++ = *src++;
4158 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004159
4160 if (startstr != NULL && src - startstr_len >= srcp
4161 && STRNCMP(src - startstr_len, startstr, startstr_len) == 0)
4162 at_start = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 }
4164 }
4165 *dst = NUL;
4166}
4167
4168/*
4169 * Vim's version of getenv().
4170 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004171 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004172 * "mustfree" is set to TRUE when returned is allocated, it must be
4173 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 */
4175 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004176vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177{
4178 char_u *p;
4179 char_u *pend;
4180 int vimruntime;
4181
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004182#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 /* use "C:/" when $HOME is not set */
4184 if (STRCMP(name, "HOME") == 0)
4185 return homedir;
4186#endif
4187
4188 p = mch_getenv(name);
4189 if (p != NULL && *p == NUL) /* empty is the same as not set */
4190 p = NULL;
4191
4192 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004193 {
4194#if defined(FEAT_MBYTE) && defined(WIN3264)
4195 if (enc_utf8)
4196 {
4197 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004198 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004199
4200 /* Convert from active codepage to UTF-8. Other conversions are
4201 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004202 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004203 if (pp != NULL)
4204 {
4205 p = pp;
4206 *mustfree = TRUE;
4207 }
4208 }
4209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212
4213 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4214 if (!vimruntime && STRCMP(name, "VIM") != 0)
4215 return NULL;
4216
4217 /*
4218 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4219 * Don't do this when default_vimruntime_dir is non-empty.
4220 */
4221 if (vimruntime
4222#ifdef HAVE_PATHDEF
4223 && *default_vimruntime_dir == NUL
4224#endif
4225 )
4226 {
4227 p = mch_getenv((char_u *)"VIM");
4228 if (p != NULL && *p == NUL) /* empty is the same as not set */
4229 p = NULL;
4230 if (p != NULL)
4231 {
4232 p = vim_version_dir(p);
4233 if (p != NULL)
4234 *mustfree = TRUE;
4235 else
4236 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004237
4238#if defined(FEAT_MBYTE) && defined(WIN3264)
4239 if (enc_utf8)
4240 {
4241 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004242 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004243
4244 /* Convert from active codepage to UTF-8. Other conversions
4245 * are not done, because they would fail for non-ASCII
4246 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004247 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004248 if (pp != NULL)
4249 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004250 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004251 vim_free(p);
4252 p = pp;
4253 *mustfree = TRUE;
4254 }
4255 }
4256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 }
4258 }
4259
4260 /*
4261 * When expanding $VIM or $VIMRUNTIME fails, try using:
4262 * - the directory name from 'helpfile' (unless it contains '$')
4263 * - the executable name from argv[0]
4264 */
4265 if (p == NULL)
4266 {
4267 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4268 p = p_hf;
4269#ifdef USE_EXE_NAME
4270 /*
4271 * Use the name of the executable, obtained from argv[0].
4272 */
4273 else
4274 p = exe_name;
4275#endif
4276 if (p != NULL)
4277 {
4278 /* remove the file name */
4279 pend = gettail(p);
4280
4281 /* remove "doc/" from 'helpfile', if present */
4282 if (p == p_hf)
4283 pend = remove_tail(p, pend, (char_u *)"doc");
4284
4285#ifdef USE_EXE_NAME
4286# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004287 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 if (p == exe_name)
4289 {
4290 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004291 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004293 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4294 if (pend1 != pend)
4295 {
4296 pnew = alloc((unsigned)(pend1 - p) + 15);
4297 if (pnew != NULL)
4298 {
4299 STRNCPY(pnew, p, (pend1 - p));
4300 STRCPY(pnew + (pend1 - p), "Resources/vim");
4301 p = pnew;
4302 pend = p + STRLEN(p);
4303 }
4304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 }
4306# endif
4307 /* remove "src/" from exe_name, if present */
4308 if (p == exe_name)
4309 pend = remove_tail(p, pend, (char_u *)"src");
4310#endif
4311
4312 /* for $VIM, remove "runtime/" or "vim54/", if present */
4313 if (!vimruntime)
4314 {
4315 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4316 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4317 }
4318
4319 /* remove trailing path separator */
4320#ifndef MACOS_CLASSIC
4321 /* With MacOS path (with colons) the final colon is required */
Bram Moolenaare21877a2008-02-13 09:58:14 +00004322 /* to avoid confusion between absolute and relative path */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004323 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 --pend;
4325#endif
4326
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004327#ifdef MACOS_X
4328 if (p == exe_name || p == p_hf)
4329#endif
4330 /* check that the result is a directory name */
4331 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332
4333 if (p != NULL && !mch_isdir(p))
4334 {
4335 vim_free(p);
4336 p = NULL;
4337 }
4338 else
4339 {
4340#ifdef USE_EXE_NAME
4341 /* may add "/vim54" or "/runtime" if it exists */
4342 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4343 {
4344 vim_free(p);
4345 p = pend;
4346 }
4347#endif
4348 *mustfree = TRUE;
4349 }
4350 }
4351 }
4352
4353#ifdef HAVE_PATHDEF
4354 /* When there is a pathdef.c file we can use default_vim_dir and
4355 * default_vimruntime_dir */
4356 if (p == NULL)
4357 {
4358 /* Only use default_vimruntime_dir when it is not empty */
4359 if (vimruntime && *default_vimruntime_dir != NUL)
4360 {
4361 p = default_vimruntime_dir;
4362 *mustfree = FALSE;
4363 }
4364 else if (*default_vim_dir != NUL)
4365 {
4366 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4367 *mustfree = TRUE;
4368 else
4369 {
4370 p = default_vim_dir;
4371 *mustfree = FALSE;
4372 }
4373 }
4374 }
4375#endif
4376
4377 /*
4378 * Set the environment variable, so that the new value can be found fast
4379 * next time, and others can also use it (e.g. Perl).
4380 */
4381 if (p != NULL)
4382 {
4383 if (vimruntime)
4384 {
4385 vim_setenv((char_u *)"VIMRUNTIME", p);
4386 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 }
4388 else
4389 {
4390 vim_setenv((char_u *)"VIM", p);
4391 didset_vim = TRUE;
4392 }
4393 }
4394 return p;
4395}
4396
4397/*
4398 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4399 * Return NULL if not, return its name in allocated memory otherwise.
4400 */
4401 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004402vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403{
4404 char_u *p;
4405
4406 if (vimdir == NULL || *vimdir == NUL)
4407 return NULL;
4408 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4409 if (p != NULL && mch_isdir(p))
4410 return p;
4411 vim_free(p);
4412 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4413 if (p != NULL && mch_isdir(p))
4414 return p;
4415 vim_free(p);
4416 return NULL;
4417}
4418
4419/*
4420 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4421 * the length of "name/". Otherwise return "pend".
4422 */
4423 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004424remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425{
4426 int len = (int)STRLEN(name) + 1;
4427 char_u *newend = pend - len;
4428
4429 if (newend >= p
4430 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004431 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 return newend;
4433 return pend;
4434}
4435
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 * Our portable version of setenv.
4438 */
4439 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004440vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441{
4442#ifdef HAVE_SETENV
4443 mch_setenv((char *)name, (char *)val, 1);
4444#else
4445 char_u *envbuf;
4446
4447 /*
4448 * Putenv does not copy the string, it has to remain
4449 * valid. The allocated memory will never be freed.
4450 */
4451 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4452 if (envbuf != NULL)
4453 {
4454 sprintf((char *)envbuf, "%s=%s", name, val);
4455 putenv((char *)envbuf);
Bram Moolenaar972c3b82017-01-12 21:44:49 +01004456# ifdef libintl_putenv
4457 libintl_putenv((char *)envbuf);
4458# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 }
4460#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004461#ifdef FEAT_GETTEXT
4462 /*
4463 * When setting $VIMRUNTIME adjust the directory to find message
4464 * translations to $VIMRUNTIME/lang.
4465 */
4466 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4467 {
4468 char_u *buf = concat_str(val, (char_u *)"/lang");
4469
4470 if (buf != NULL)
4471 {
4472 bindtextdomain(VIMPACKAGE, (char *)buf);
4473 vim_free(buf);
4474 }
4475 }
4476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477}
4478
4479#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4480/*
4481 * Function given to ExpandGeneric() to obtain an environment variable name.
4482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004484get_env_name(
4485 expand_T *xp UNUSED,
4486 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487{
4488# if defined(AMIGA) || defined(__MRC__) || defined(__SC__)
4489 /*
4490 * No environ[] on the Amiga and on the Mac (using MPW).
4491 */
4492 return NULL;
4493# else
4494# ifndef __WIN32__
4495 /* Borland C++ 5.2 has this in a header file. */
4496 extern char **environ;
4497# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004498# define ENVNAMELEN 100
4499 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 char_u *str;
4501 int n;
4502
4503 str = (char_u *)environ[idx];
4504 if (str == NULL)
4505 return NULL;
4506
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004507 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 {
4509 if (str[n] == '=' || str[n] == NUL)
4510 break;
4511 name[n] = str[n];
4512 }
4513 name[n] = NUL;
4514 return name;
4515# endif
4516}
Bram Moolenaar24305862012-08-15 14:05:05 +02004517
4518/*
4519 * Find all user names for user completion.
4520 * Done only once and then cached.
4521 */
4522 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004523init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004524{
Bram Moolenaar24305862012-08-15 14:05:05 +02004525 static int lazy_init_done = FALSE;
4526
4527 if (lazy_init_done)
4528 return;
4529
4530 lazy_init_done = TRUE;
4531 ga_init2(&ga_users, sizeof(char_u *), 20);
4532
4533# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4534 {
4535 char_u* user;
4536 struct passwd* pw;
4537
4538 setpwent();
4539 while ((pw = getpwent()) != NULL)
4540 /* pw->pw_name shouldn't be NULL but just in case... */
4541 if (pw->pw_name != NULL)
4542 {
4543 if (ga_grow(&ga_users, 1) == FAIL)
4544 break;
4545 user = vim_strsave((char_u*)pw->pw_name);
4546 if (user == NULL)
4547 break;
4548 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4549 }
4550 endpwent();
4551 }
4552# endif
4553}
4554
4555/*
4556 * Function given to ExpandGeneric() to obtain an user names.
4557 */
4558 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004559get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004560{
4561 init_users();
4562 if (idx < ga_users.ga_len)
4563 return ((char_u **)ga_users.ga_data)[idx];
4564 return NULL;
4565}
4566
4567/*
4568 * Check whether name matches a user name. Return:
4569 * 0 if name does not match any user name.
4570 * 1 if name partially matches the beginning of a user name.
4571 * 2 is name fully matches a user name.
4572 */
Bram Moolenaar9b578142016-01-30 19:39:49 +01004573int match_user(char_u* name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004574{
4575 int i;
4576 int n = (int)STRLEN(name);
4577 int result = 0;
4578
4579 init_users();
4580 for (i = 0; i < ga_users.ga_len; i++)
4581 {
4582 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4583 return 2; /* full match */
4584 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4585 result = 1; /* partial match */
4586 }
4587 return result;
4588}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589#endif
4590
4591/*
4592 * Replace home directory by "~" in each space or comma separated file name in
4593 * 'src'.
4594 * If anything fails (except when out of space) dst equals src.
4595 */
4596 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004597home_replace(
4598 buf_T *buf, /* when not NULL, check for help files */
4599 char_u *src, /* input file name */
4600 char_u *dst, /* where to put the result */
4601 int dstlen, /* maximum length of the result */
4602 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 spaces and commas in the file name. */
4604{
4605 size_t dirlen = 0, envlen = 0;
4606 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004607 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 char_u *p;
4609
4610 if (src == NULL)
4611 {
4612 *dst = NUL;
4613 return;
4614 }
4615
4616 /*
4617 * If the file is a help file, remove the path completely.
4618 */
4619 if (buf != NULL && buf->b_help)
4620 {
4621 STRCPY(dst, gettail(src));
4622 return;
4623 }
4624
4625 /*
4626 * We check both the value of the $HOME environment variable and the
4627 * "real" home directory.
4628 */
4629 if (homedir != NULL)
4630 dirlen = STRLEN(homedir);
4631
4632#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004633 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004635 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4636#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004637 /* Empty is the same as not set. */
4638 if (homedir_env != NULL && *homedir_env == NUL)
4639 homedir_env = NULL;
4640
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004641#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004642 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004643 {
4644 int usedlen = 0;
4645 int flen;
4646 char_u *fbuf = NULL;
4647
4648 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004649 (void)modify_fname((char_u *)":p", &usedlen,
4650 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004651 flen = (int)STRLEN(homedir_env);
4652 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4653 /* Remove the trailing / that is added to a directory. */
4654 homedir_env[flen - 1] = NUL;
4655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656#endif
4657
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 if (homedir_env != NULL)
4659 envlen = STRLEN(homedir_env);
4660
4661 if (!one)
4662 src = skipwhite(src);
4663 while (*src && dstlen > 0)
4664 {
4665 /*
4666 * Here we are at the beginning of a file name.
4667 * First, check to see if the beginning of the file name matches
4668 * $HOME or the "real" home directory. Check that there is a '/'
4669 * after the match (so that if e.g. the file is "/home/pieter/bla",
4670 * and the home directory is "/home/piet", the file does not end up
4671 * as "~er/bla" (which would seem to indicate the file "bla" in user
4672 * er's home directory)).
4673 */
4674 p = homedir;
4675 len = dirlen;
4676 for (;;)
4677 {
4678 if ( len
4679 && fnamencmp(src, p, len) == 0
4680 && (vim_ispathsep(src[len])
4681 || (!one && (src[len] == ',' || src[len] == ' '))
4682 || src[len] == NUL))
4683 {
4684 src += len;
4685 if (--dstlen > 0)
4686 *dst++ = '~';
4687
4688 /*
4689 * If it's just the home directory, add "/".
4690 */
4691 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4692 *dst++ = '/';
4693 break;
4694 }
4695 if (p == homedir_env)
4696 break;
4697 p = homedir_env;
4698 len = envlen;
4699 }
4700
4701 /* if (!one) skip to separator: space or comma */
4702 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4703 *dst++ = *src++;
4704 /* skip separator */
4705 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4706 *dst++ = *src++;
4707 }
4708 /* if (dstlen == 0) out of space, what to do??? */
4709
4710 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004711
4712 if (homedir_env != homedir_env_orig)
4713 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714}
4715
4716/*
4717 * Like home_replace, store the replaced string in allocated memory.
4718 * When something fails, NULL is returned.
4719 */
4720 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004721home_replace_save(
4722 buf_T *buf, /* when not NULL, check for help files */
4723 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724{
4725 char_u *dst;
4726 unsigned len;
4727
4728 len = 3; /* space for "~/" and trailing NUL */
4729 if (src != NULL) /* just in case */
4730 len += (unsigned)STRLEN(src);
4731 dst = alloc(len);
4732 if (dst != NULL)
4733 home_replace(buf, src, dst, len, TRUE);
4734 return dst;
4735}
4736
4737/*
4738 * Compare two file names and return:
4739 * FPC_SAME if they both exist and are the same file.
4740 * FPC_SAMEX if they both don't exist and have the same file name.
4741 * FPC_DIFF if they both exist and are different files.
4742 * FPC_NOTX if they both don't exist.
4743 * FPC_DIFFX if one of them doesn't exist.
4744 * For the first name environment variables are expanded
4745 */
4746 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004747fullpathcmp(
4748 char_u *s1,
4749 char_u *s2,
4750 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751{
4752#ifdef UNIX
4753 char_u exp1[MAXPATHL];
4754 char_u full1[MAXPATHL];
4755 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02004756 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 int r1, r2;
4758
4759 expand_env(s1, exp1, MAXPATHL);
4760 r1 = mch_stat((char *)exp1, &st1);
4761 r2 = mch_stat((char *)s2, &st2);
4762 if (r1 != 0 && r2 != 0)
4763 {
4764 /* if mch_stat() doesn't work, may compare the names */
4765 if (checkname)
4766 {
4767 if (fnamecmp(exp1, s2) == 0)
4768 return FPC_SAMEX;
4769 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4770 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4771 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4772 return FPC_SAMEX;
4773 }
4774 return FPC_NOTX;
4775 }
4776 if (r1 != 0 || r2 != 0)
4777 return FPC_DIFFX;
4778 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4779 return FPC_SAME;
4780 return FPC_DIFF;
4781#else
4782 char_u *exp1; /* expanded s1 */
4783 char_u *full1; /* full path of s1 */
4784 char_u *full2; /* full path of s2 */
4785 int retval = FPC_DIFF;
4786 int r1, r2;
4787
4788 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4789 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4790 {
4791 full1 = exp1 + MAXPATHL;
4792 full2 = full1 + MAXPATHL;
4793
4794 expand_env(s1, exp1, MAXPATHL);
4795 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4796 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4797
4798 /* If vim_FullName() fails, the file probably doesn't exist. */
4799 if (r1 != OK && r2 != OK)
4800 {
4801 if (checkname && fnamecmp(exp1, s2) == 0)
4802 retval = FPC_SAMEX;
4803 else
4804 retval = FPC_NOTX;
4805 }
4806 else if (r1 != OK || r2 != OK)
4807 retval = FPC_DIFFX;
4808 else if (fnamecmp(full1, full2))
4809 retval = FPC_DIFF;
4810 else
4811 retval = FPC_SAME;
4812 vim_free(exp1);
4813 }
4814 return retval;
4815#endif
4816}
4817
4818/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004819 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004820 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004821 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 */
4823 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004824gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825{
4826 char_u *p1, *p2;
4827
4828 if (fname == NULL)
4829 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01004830 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01004832 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 p1 = p2 + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004834 mb_ptr_adv(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 }
4836 return p1;
4837}
4838
Bram Moolenaar31710262010-08-13 13:36:15 +02004839#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004840static char_u *gettail_dir(char_u *fname);
Bram Moolenaar31710262010-08-13 13:36:15 +02004841
4842/*
4843 * Return the end of the directory name, on the first path
4844 * separator:
4845 * "/path/file", "/path/dir/", "/path//dir", "/file"
4846 * ^ ^ ^ ^
4847 */
4848 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004849gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02004850{
4851 char_u *dir_end = fname;
4852 char_u *next_dir_end = fname;
4853 int look_for_sep = TRUE;
4854 char_u *p;
4855
4856 for (p = fname; *p != NUL; )
4857 {
4858 if (vim_ispathsep(*p))
4859 {
4860 if (look_for_sep)
4861 {
4862 next_dir_end = p;
4863 look_for_sep = FALSE;
4864 }
4865 }
4866 else
4867 {
4868 if (!look_for_sep)
4869 dir_end = next_dir_end;
4870 look_for_sep = TRUE;
4871 }
4872 mb_ptr_adv(p);
4873 }
4874 return dir_end;
4875}
4876#endif
4877
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004879 * Get pointer to tail of "fname", including path separators. Putting a NUL
4880 * here leaves the directory name. Takes care of "c:/" and "//".
4881 * Always returns a valid pointer.
4882 */
4883 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004884gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004885{
4886 char_u *p;
4887 char_u *t;
4888
4889 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4890 t = gettail(fname);
4891 while (t > p && after_pathsep(fname, t))
4892 --t;
4893#ifdef VMS
4894 /* path separator is part of the path */
4895 ++t;
4896#endif
4897 return t;
4898}
4899
4900/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 * get the next path component (just after the next path separator).
4902 */
4903 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004904getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905{
4906 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004907 mb_ptr_adv(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 if (*fname)
4909 ++fname;
4910 return fname;
4911}
4912
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913/*
4914 * Get a pointer to one character past the head of a path name.
4915 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4916 * If there is no head, path is returned.
4917 */
4918 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004919get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920{
4921 char_u *retval;
4922
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004923#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 /* may skip "c:" */
4925 if (isalpha(path[0]) && path[1] == ':')
4926 retval = path + 2;
4927 else
4928 retval = path;
4929#else
4930# if defined(AMIGA)
4931 /* may skip "label:" */
4932 retval = vim_strchr(path, ':');
4933 if (retval == NULL)
4934 retval = path;
4935# else /* Unix */
4936 retval = path;
4937# endif
4938#endif
4939
4940 while (vim_ispathsep(*retval))
4941 ++retval;
4942
4943 return retval;
4944}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945
4946/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01004947 * Return TRUE if 'c' is a path separator.
4948 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 */
4950 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004951vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952{
Bram Moolenaare60acc12011-05-10 16:41:25 +02004953#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02004955#else
4956# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004958# else
4959# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
4961 return (c == ':' || c == '[' || c == ']' || c == '/'
4962 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02004963# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004965# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02004967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968}
4969
Bram Moolenaar69c35002013-11-04 02:54:12 +01004970/*
4971 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
4972 */
4973 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004974vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01004975{
4976 return vim_ispathsep(c)
4977#ifdef BACKSLASH_IN_FILENAME
4978 && c != ':'
4979#endif
4980 ;
4981}
4982
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983#if defined(FEAT_SEARCHPATH) || defined(PROTO)
4984/*
4985 * return TRUE if 'c' is a path list separator.
4986 */
4987 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004988vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989{
4990#ifdef UNIX
4991 return (c == ':');
4992#else
Bram Moolenaar25394022007-05-10 19:06:20 +00004993 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994#endif
4995}
4996#endif
4997
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004998#if defined(FEAT_GUI_TABLINE) || defined(FEAT_WINDOWS) \
4999 || defined(FEAT_EVAL) || defined(PROTO)
5000/*
5001 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5002 * It's done in-place.
5003 */
5004 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005005shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005006{
5007 char_u *tail, *s, *d;
5008 int skip = FALSE;
5009
5010 tail = gettail(str);
5011 d = str;
5012 for (s = str; ; ++s)
5013 {
5014 if (s >= tail) /* copy the whole tail */
5015 {
5016 *d++ = *s;
5017 if (*s == NUL)
5018 break;
5019 }
5020 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5021 {
5022 *d++ = *s;
5023 skip = FALSE;
5024 }
5025 else if (!skip)
5026 {
5027 *d++ = *s; /* copy next char */
5028 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5029 skip = TRUE;
5030# ifdef FEAT_MBYTE
5031 if (has_mbyte)
5032 {
5033 int l = mb_ptr2len(s);
5034
5035 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005036 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005037 }
5038# endif
5039 }
5040 }
5041}
5042#endif
5043
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005044/*
5045 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5046 * Also returns TRUE if there is no directory name.
5047 * "fname" must be writable!.
5048 */
5049 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005050dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005051{
5052 char_u *p;
5053 int c;
5054 int retval;
5055
5056 p = gettail_sep(fname);
5057 if (p == fname)
5058 return TRUE;
5059 c = *p;
5060 *p = NUL;
5061 retval = mch_isdir(fname);
5062 *p = c;
5063 return retval;
5064}
5065
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005067 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5068 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 */
5070 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005071vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005073#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005075#else
5076 if (p_fic)
5077 return MB_STRICMP(x, y);
5078 return STRCMP(x, y);
5079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080}
5081
5082 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005083vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005085#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005086 char_u *px = x;
5087 char_u *py = y;
5088 int cx = NUL;
5089 int cy = NUL;
5090
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005091 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005093 cx = PTR2CHAR(px);
5094 cy = PTR2CHAR(py);
5095 if (cx == NUL || cy == NUL
5096 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5097 && !(cx == '/' && cy == '\\')
5098 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005100 len -= MB_PTR2LEN(px);
5101 px += MB_PTR2LEN(px);
5102 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 }
5104 if (len == 0)
5105 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005106 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005107#else
5108 if (p_fic)
5109 return MB_STRNICMP(x, y, len);
5110 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005112}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113
5114/*
5115 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005116 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 */
5118 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005119concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120{
5121 char_u *dest;
5122
5123 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5124 if (dest != NULL)
5125 {
5126 STRCPY(dest, fname1);
5127 if (sep)
5128 add_pathsep(dest);
5129 STRCAT(dest, fname2);
5130 }
5131 return dest;
5132}
5133
Bram Moolenaard6754642005-01-17 22:18:45 +00005134/*
5135 * Concatenate two strings and return the result in allocated memory.
5136 * Returns NULL when out of memory.
5137 */
5138 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005139concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005140{
5141 char_u *dest;
5142 size_t l = STRLEN(str1);
5143
5144 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5145 if (dest != NULL)
5146 {
5147 STRCPY(dest, str1);
5148 STRCPY(dest + l, str2);
5149 }
5150 return dest;
5151}
Bram Moolenaard6754642005-01-17 22:18:45 +00005152
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153/*
5154 * Add a path separator to a file name, unless it already ends in a path
5155 * separator.
5156 */
5157 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005158add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005160 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 STRCAT(p, PATHSEPSTR);
5162}
5163
5164/*
5165 * FullName_save - Make an allocated copy of a full file name.
5166 * Returns NULL when out of memory.
5167 */
5168 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005169FullName_save(
5170 char_u *fname,
5171 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005172 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173{
5174 char_u *buf;
5175 char_u *new_fname = NULL;
5176
5177 if (fname == NULL)
5178 return NULL;
5179
5180 buf = alloc((unsigned)MAXPATHL);
5181 if (buf != NULL)
5182 {
5183 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5184 new_fname = vim_strsave(buf);
5185 else
5186 new_fname = vim_strsave(fname);
5187 vim_free(buf);
5188 }
5189 return new_fname;
5190}
5191
5192#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5193
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005194static char_u *skip_string(char_u *p);
5195static pos_T *ind_find_start_comment(void);
5196static pos_T *ind_find_start_CORS(void);
5197static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198
5199/*
5200 * Find the start of a comment, not knowing if we are in a comment right now.
5201 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005202 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005204 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005205ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005206{
5207 return find_start_comment(curbuf->b_ind_maxcomment);
5208}
5209
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005211find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212{
5213 pos_T *pos;
5214 char_u *line;
5215 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005216 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005218 for (;;)
5219 {
5220 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5221 if (pos == NULL)
5222 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005224 /*
5225 * Check if the comment start we found is inside a string.
5226 * If it is then restrict the search to below this line and try again.
5227 */
5228 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005229 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005230 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005231 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005232 break;
5233 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5234 if (cur_maxcomment <= 0)
5235 {
5236 pos = NULL;
5237 break;
5238 }
5239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 return pos;
5241}
5242
5243/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005244 * Find the start of a comment or raw string, not knowing if we are in a
5245 * comment or raw string right now.
5246 * Search starts at w_cursor.lnum and goes backwards.
5247 * Return NULL when not inside a comment or raw string.
5248 * "CORS" -> Comment Or Raw String
5249 */
5250 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005251ind_find_start_CORS(void) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005252{
Bram Moolenaar089af182015-10-07 11:41:49 +02005253 static pos_T comment_pos_copy;
5254 pos_T *comment_pos;
5255 pos_T *rs_pos;
5256
5257 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5258 if (comment_pos != NULL)
5259 {
5260 /* Need to make a copy of the static pos in findmatchlimit(),
5261 * calling find_start_rawstring() may change it. */
5262 comment_pos_copy = *comment_pos;
5263 comment_pos = &comment_pos_copy;
5264 }
5265 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005266
5267 /* If comment_pos is before rs_pos the raw string is inside the comment.
5268 * If rs_pos is before comment_pos the comment is inside the raw string. */
5269 if (comment_pos == NULL || (rs_pos != NULL && lt(*rs_pos, *comment_pos)))
5270 return rs_pos;
5271 return comment_pos;
5272}
5273
5274/*
5275 * Find the start of a raw string, not knowing if we are in one right now.
5276 * Search starts at w_cursor.lnum and goes backwards.
5277 * Return NULL when not inside a raw string.
5278 */
5279 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005280find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005281{
5282 pos_T *pos;
5283 char_u *line;
5284 char_u *p;
5285 int cur_maxcomment = ind_maxcomment;
5286
5287 for (;;)
5288 {
5289 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5290 if (pos == NULL)
5291 break;
5292
5293 /*
5294 * Check if the raw string start we found is inside a string.
5295 * If it is then restrict the search to below this line and try again.
5296 */
5297 line = ml_get(pos->lnum);
5298 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5299 p = skip_string(p);
5300 if ((colnr_T)(p - line) <= pos->col)
5301 break;
5302 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5303 if (cur_maxcomment <= 0)
5304 {
5305 pos = NULL;
5306 break;
5307 }
5308 }
5309 return pos;
5310}
5311
5312/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 * Skip to the end of a "string" and a 'c' character.
5314 * If there is no string or character, return argument unmodified.
5315 */
5316 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005317skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318{
5319 int i;
5320
5321 /*
5322 * We loop, because strings may be concatenated: "date""time".
5323 */
5324 for ( ; ; ++p)
5325 {
5326 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5327 {
5328 if (!p[1]) /* ' at end of line */
5329 break;
5330 i = 2;
5331 if (p[1] == '\\') /* '\n' or '\000' */
5332 {
5333 ++i;
5334 while (vim_isdigit(p[i - 1])) /* '\000' */
5335 ++i;
5336 }
5337 if (p[i] == '\'') /* check for trailing ' */
5338 {
5339 p += i;
5340 continue;
5341 }
5342 }
5343 else if (p[0] == '"') /* start of string */
5344 {
5345 for (++p; p[0]; ++p)
5346 {
5347 if (p[0] == '\\' && p[1] != NUL)
5348 ++p;
5349 else if (p[0] == '"') /* end of string */
5350 break;
5351 }
5352 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005353 continue; /* continue for another string */
5354 }
5355 else if (p[0] == 'R' && p[1] == '"')
5356 {
5357 /* Raw string: R"[delim](...)[delim]" */
5358 char_u *delim = p + 2;
5359 char_u *paren = vim_strchr(delim, '(');
5360
5361 if (paren != NULL)
5362 {
5363 size_t delim_len = paren - delim;
5364
5365 for (p += 3; *p; ++p)
5366 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5367 && p[delim_len + 1] == '"')
5368 {
5369 p += delim_len + 1;
5370 break;
5371 }
5372 if (p[0] == '"')
5373 continue; /* continue for another string */
5374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 }
5376 break; /* no string found */
5377 }
5378 if (!*p)
5379 --p; /* backup from NUL */
5380 return p;
5381}
5382#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5383
5384#if defined(FEAT_CINDENT) || defined(PROTO)
5385
5386/*
5387 * Do C or expression indenting on the current line.
5388 */
5389 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005390do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391{
5392# ifdef FEAT_EVAL
5393 if (*curbuf->b_p_inde != NUL)
5394 fixthisline(get_expr_indent);
5395 else
5396# endif
5397 fixthisline(get_c_indent);
5398}
5399
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005400/* Find result cache for cpp_baseclass */
5401typedef struct {
5402 int found;
5403 lpos_T lpos;
5404} cpp_baseclass_cache_T;
5405
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406/*
5407 * Functions for C-indenting.
5408 * Most of this originally comes from Eric Fischer.
5409 */
5410/*
5411 * Below "XXX" means that this function may unlock the current line.
5412 */
5413
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005414static char_u *cin_skipcomment(char_u *);
5415static int cin_nocode(char_u *);
5416static pos_T *find_line_comment(void);
5417static int cin_has_js_key(char_u *text);
5418static int cin_islabel_skip(char_u **);
5419static int cin_isdefault(char_u *);
5420static char_u *after_label(char_u *l);
5421static int get_indent_nolabel(linenr_T lnum);
5422static int skip_label(linenr_T, char_u **pp);
5423static int cin_first_id_amount(void);
5424static int cin_get_equal_amount(linenr_T lnum);
5425static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005426static int cin_iscomment(char_u *);
5427static int cin_islinecomment(char_u *);
5428static int cin_isterminated(char_u *, int, int);
5429static int cin_isinit(void);
5430static int cin_isfuncdecl(char_u **, linenr_T, linenr_T);
5431static int cin_isif(char_u *);
5432static int cin_iselse(char_u *);
5433static int cin_isdo(char_u *);
5434static int cin_iswhileofdo(char_u *, linenr_T);
5435static int cin_is_if_for_while_before_offset(char_u *line, int *poffset);
5436static int cin_iswhileofdo_end(int terminated);
5437static int cin_isbreak(char_u *);
5438static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached);
5439static int get_baseclass_amount(int col);
5440static int cin_ends_in(char_u *, char_u *, char_u *);
5441static int cin_starts_with(char_u *s, char *word);
5442static int cin_skip2pos(pos_T *trypos);
5443static pos_T *find_start_brace(void);
5444static pos_T *find_match_paren(int);
5445static pos_T *find_match_char(int c, int ind_maxparen);
5446static int corr_ind_maxparen(pos_T *startpos);
5447static int find_last_paren(char_u *l, int start, int end);
5448static int find_match(int lookfor, linenr_T ourscope);
5449static int cin_is_cpp_namespace(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450
5451/*
5452 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005453 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 */
5455 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005456cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457{
5458 while (*s)
5459 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005460 char_u *prev_s = s;
5461
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005463
5464 /* Perl/shell # comment comment continues until eol. Require a space
5465 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005466 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005467 {
5468 s += STRLEN(s);
5469 break;
5470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 if (*s != '/')
5472 break;
5473 ++s;
5474 if (*s == '/') /* slash-slash comment continues till eol */
5475 {
5476 s += STRLEN(s);
5477 break;
5478 }
5479 if (*s != '*')
5480 break;
5481 for (++s; *s; ++s) /* skip slash-star comment */
5482 if (s[0] == '*' && s[1] == '/')
5483 {
5484 s += 2;
5485 break;
5486 }
5487 }
5488 return s;
5489}
5490
5491/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005492 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 * not considered code.
5494 */
5495 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005496cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497{
5498 return *cin_skipcomment(s) == NUL;
5499}
5500
5501/*
5502 * Check previous lines for a "//" line comment, skipping over blank lines.
5503 */
5504 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005505find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506{
5507 static pos_T pos;
5508 char_u *line;
5509 char_u *p;
5510
5511 pos = curwin->w_cursor;
5512 while (--pos.lnum > 0)
5513 {
5514 line = ml_get(pos.lnum);
5515 p = skipwhite(line);
5516 if (cin_islinecomment(p))
5517 {
5518 pos.col = (int)(p - line);
5519 return &pos;
5520 }
5521 if (*p != NUL)
5522 break;
5523 }
5524 return NULL;
5525}
5526
5527/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005528 * Return TRUE if "text" starts with "key:".
5529 */
5530 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005531cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005532{
5533 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005534 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005535
5536 if (*s == '\'' || *s == '"')
5537 {
5538 /* can be 'key': or "key": */
5539 quote = *s;
5540 ++s;
5541 }
5542 if (!vim_isIDc(*s)) /* need at least one ID character */
5543 return FALSE;
5544
5545 while (vim_isIDc(*s))
5546 ++s;
5547 if (*s == quote)
5548 ++s;
5549
5550 s = cin_skipcomment(s);
5551
5552 /* "::" is not a label, it's C++ */
5553 return (*s == ':' && s[1] != ':');
5554}
5555
5556/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005558 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 */
5560 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005561cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562{
5563 if (!vim_isIDc(**s)) /* need at least one ID character */
5564 return FALSE;
5565
5566 while (vim_isIDc(**s))
5567 (*s)++;
5568
5569 *s = cin_skipcomment(*s);
5570
5571 /* "::" is not a label, it's C++ */
5572 return (**s == ':' && *++*s != ':');
5573}
5574
5575/*
5576 * Recognize a label: "label:".
5577 * Note: curwin->w_cursor must be where we are looking for the label.
5578 */
5579 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005580cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581{
5582 char_u *s;
5583
5584 s = cin_skipcomment(ml_get_curline());
5585
5586 /*
5587 * Exclude "default" from labels, since it should be indented
5588 * like a switch label. Same for C++ scope declarations.
5589 */
5590 if (cin_isdefault(s))
5591 return FALSE;
5592 if (cin_isscopedecl(s))
5593 return FALSE;
5594
5595 if (cin_islabel_skip(&s))
5596 {
5597 /*
5598 * Only accept a label if the previous line is terminated or is a case
5599 * label.
5600 */
5601 pos_T cursor_save;
5602 pos_T *trypos;
5603 char_u *line;
5604
5605 cursor_save = curwin->w_cursor;
5606 while (curwin->w_cursor.lnum > 1)
5607 {
5608 --curwin->w_cursor.lnum;
5609
5610 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005611 * If we're in a comment or raw string now, skip to the start of
5612 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 */
5614 curwin->w_cursor.col = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005615 if ((trypos = ind_find_start_CORS()) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 curwin->w_cursor = *trypos;
5617
5618 line = ml_get_curline();
5619 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5620 continue;
5621 if (*(line = cin_skipcomment(line)) == NUL)
5622 continue;
5623
5624 curwin->w_cursor = cursor_save;
5625 if (cin_isterminated(line, TRUE, FALSE)
5626 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005627 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 || (cin_islabel_skip(&line) && cin_nocode(line)))
5629 return TRUE;
5630 return FALSE;
5631 }
5632 curwin->w_cursor = cursor_save;
5633 return TRUE; /* label at start of file??? */
5634 }
5635 return FALSE;
5636}
5637
5638/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005639 * Recognize structure initialization and enumerations:
5640 * "[typedef] [static|public|protected|private] enum"
5641 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 */
5643 static int
5644cin_isinit(void)
5645{
5646 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005647 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648
5649 s = cin_skipcomment(ml_get_curline());
5650
Bram Moolenaar75342212013-03-07 13:13:52 +01005651 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 s = cin_skipcomment(s + 7);
5653
Bram Moolenaar75342212013-03-07 13:13:52 +01005654 for (;;)
5655 {
5656 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005657
Bram Moolenaar75342212013-03-07 13:13:52 +01005658 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5659 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005660 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005661 if (cin_starts_with(s, skip[i]))
5662 {
5663 s = cin_skipcomment(s + l);
5664 l = 0;
5665 break;
5666 }
5667 }
5668 if (l != 0)
5669 break;
5670 }
5671
5672 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 return TRUE;
5674
5675 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5676 return TRUE;
5677
5678 return FALSE;
5679}
5680
5681/*
5682 * Recognize a switch label: "case .*:" or "default:".
5683 */
5684 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005685cin_iscase(
5686 char_u *s,
5687 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688{
5689 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005690 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 {
5692 for (s += 4; *s; ++s)
5693 {
5694 s = cin_skipcomment(s);
5695 if (*s == ':')
5696 {
5697 if (s[1] == ':') /* skip over "::" for C++ */
5698 ++s;
5699 else
5700 return TRUE;
5701 }
5702 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005703 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5705 return FALSE; /* stop at comment */
5706 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005707 {
5708 /* JS etc. */
5709 if (strict)
5710 return FALSE; /* stop at string */
5711 else
5712 return TRUE;
5713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 }
5715 return FALSE;
5716 }
5717
5718 if (cin_isdefault(s))
5719 return TRUE;
5720 return FALSE;
5721}
5722
5723/*
5724 * Recognize a "default" switch label.
5725 */
5726 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005727cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728{
5729 return (STRNCMP(s, "default", 7) == 0
5730 && *(s = cin_skipcomment(s + 7)) == ':'
5731 && s[1] != ':');
5732}
5733
5734/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005735 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 */
5737 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005738cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739{
5740 int i;
5741
5742 s = cin_skipcomment(s);
5743 if (STRNCMP(s, "public", 6) == 0)
5744 i = 6;
5745 else if (STRNCMP(s, "protected", 9) == 0)
5746 i = 9;
5747 else if (STRNCMP(s, "private", 7) == 0)
5748 i = 7;
5749 else
5750 return FALSE;
5751 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5752}
5753
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005754/* Maximum number of lines to search back for a "namespace" line. */
5755#define FIND_NAMESPACE_LIM 20
5756
5757/*
5758 * Recognize a "namespace" scope declaration.
5759 */
5760 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005761cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005762{
5763 char_u *p;
5764 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005765 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005766
5767 s = cin_skipcomment(s);
5768 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5769 {
5770 p = cin_skipcomment(skipwhite(s + 9));
5771 while (*p != NUL)
5772 {
5773 if (vim_iswhite(*p))
5774 {
5775 has_name = TRUE; /* found end of a name */
5776 p = cin_skipcomment(skipwhite(p));
5777 }
5778 else if (*p == '{')
5779 {
5780 break;
5781 }
5782 else if (vim_iswordc(*p))
5783 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005784 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005785 if (has_name)
5786 return FALSE; /* word character after skipping past name */
5787 ++p;
5788 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005789 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
5790 {
5791 if (!has_name_start || has_name)
5792 return FALSE;
5793 /* C++ 17 nested namespace */
5794 p += 3;
5795 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005796 else
5797 {
5798 return FALSE;
5799 }
5800 }
5801 return TRUE;
5802 }
5803 return FALSE;
5804}
5805
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806/*
5807 * Return a pointer to the first non-empty non-comment character after a ':'.
5808 * Return NULL if not found.
5809 * case 234: a = b;
5810 * ^
5811 */
5812 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005813after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814{
5815 for ( ; *l; ++l)
5816 {
5817 if (*l == ':')
5818 {
5819 if (l[1] == ':') /* skip over "::" for C++ */
5820 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005821 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 break;
5823 }
5824 else if (*l == '\'' && l[1] && l[2] == '\'')
5825 l += 2; /* skip over 'x' */
5826 }
5827 if (*l == NUL)
5828 return NULL;
5829 l = cin_skipcomment(l + 1);
5830 if (*l == NUL)
5831 return NULL;
5832 return l;
5833}
5834
5835/*
5836 * Get indent of line "lnum", skipping a label.
5837 * Return 0 if there is nothing after the label.
5838 */
5839 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005840get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841{
5842 char_u *l;
5843 pos_T fp;
5844 colnr_T col;
5845 char_u *p;
5846
5847 l = ml_get(lnum);
5848 p = after_label(l);
5849 if (p == NULL)
5850 return 0;
5851
5852 fp.col = (colnr_T)(p - l);
5853 fp.lnum = lnum;
5854 getvcol(curwin, &fp, &col, NULL, NULL);
5855 return (int)col;
5856}
5857
5858/*
5859 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005860 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 * label: if (asdf && asdfasdf)
5862 * ^
5863 */
5864 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005865skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866{
5867 char_u *l;
5868 int amount;
5869 pos_T cursor_save;
5870
5871 cursor_save = curwin->w_cursor;
5872 curwin->w_cursor.lnum = lnum;
5873 l = ml_get_curline();
5874 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005875 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 {
5877 amount = get_indent_nolabel(lnum);
5878 l = after_label(ml_get_curline());
5879 if (l == NULL) /* just in case */
5880 l = ml_get_curline();
5881 }
5882 else
5883 {
5884 amount = get_indent();
5885 l = ml_get_curline();
5886 }
5887 *pp = l;
5888
5889 curwin->w_cursor = cursor_save;
5890 return amount;
5891}
5892
5893/*
5894 * Return the indent of the first variable name after a type in a declaration.
5895 * int a, indent of "a"
5896 * static struct foo b, indent of "b"
5897 * enum bla c, indent of "c"
5898 * Returns zero when it doesn't look like a declaration.
5899 */
5900 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005901cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902{
5903 char_u *line, *p, *s;
5904 int len;
5905 pos_T fp;
5906 colnr_T col;
5907
5908 line = ml_get_curline();
5909 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005910 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 if (len == 6 && STRNCMP(p, "static", 6) == 0)
5912 {
5913 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005914 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 }
5916 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
5917 p = skipwhite(p + 6);
5918 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
5919 p = skipwhite(p + 4);
5920 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
5921 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
5922 {
5923 s = skipwhite(p + len);
5924 if ((STRNCMP(s, "int", 3) == 0 && vim_iswhite(s[3]))
5925 || (STRNCMP(s, "long", 4) == 0 && vim_iswhite(s[4]))
5926 || (STRNCMP(s, "short", 5) == 0 && vim_iswhite(s[5]))
5927 || (STRNCMP(s, "char", 4) == 0 && vim_iswhite(s[4])))
5928 p = s;
5929 }
5930 for (len = 0; vim_isIDc(p[len]); ++len)
5931 ;
5932 if (len == 0 || !vim_iswhite(p[len]) || cin_nocode(p))
5933 return 0;
5934
5935 p = skipwhite(p + len);
5936 fp.lnum = curwin->w_cursor.lnum;
5937 fp.col = (colnr_T)(p - line);
5938 getvcol(curwin, &fp, &col, NULL, NULL);
5939 return (int)col;
5940}
5941
5942/*
5943 * Return the indent of the first non-blank after an equal sign.
5944 * char *foo = "here";
5945 * Return zero if no (useful) equal sign found.
5946 * Return -1 if the line above "lnum" ends in a backslash.
5947 * foo = "asdf\
5948 * asdf\
5949 * here";
5950 */
5951 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005952cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953{
5954 char_u *line;
5955 char_u *s;
5956 colnr_T col;
5957 pos_T fp;
5958
5959 if (lnum > 1)
5960 {
5961 line = ml_get(lnum - 1);
5962 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
5963 return -1;
5964 }
5965
5966 line = s = ml_get(lnum);
5967 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
5968 {
5969 if (cin_iscomment(s)) /* ignore comments */
5970 s = cin_skipcomment(s);
5971 else
5972 ++s;
5973 }
5974 if (*s != '=')
5975 return 0;
5976
5977 s = skipwhite(s + 1);
5978 if (cin_nocode(s))
5979 return 0;
5980
5981 if (*s == '"') /* nice alignment for continued strings */
5982 ++s;
5983
5984 fp.lnum = lnum;
5985 fp.col = (colnr_T)(s - line);
5986 getvcol(curwin, &fp, &col, NULL, NULL);
5987 return (int)col;
5988}
5989
5990/*
5991 * Recognize a preprocessor statement: Any line that starts with '#'.
5992 */
5993 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005994cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02005996 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 return TRUE;
5998 return FALSE;
5999}
6000
6001/*
6002 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6003 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6004 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006005 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 */
6007 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006008cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009{
6010 char_u *line = *pp;
6011 linenr_T lnum = *lnump;
6012 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006013 int candidate_amount = *amount;
6014
6015 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6016 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006018 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 {
6020 if (cin_ispreproc(line))
6021 {
6022 retval = TRUE;
6023 *lnump = lnum;
6024 break;
6025 }
6026 if (lnum == 1)
6027 break;
6028 line = ml_get(--lnum);
6029 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6030 break;
6031 }
6032
6033 if (lnum != *lnump)
6034 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006035 if (retval)
6036 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 return retval;
6038}
6039
6040/*
6041 * Recognize the start of a C or C++ comment.
6042 */
6043 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006044cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045{
6046 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6047}
6048
6049/*
6050 * Recognize the start of a "//" comment.
6051 */
6052 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006053cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054{
6055 return (p[0] == '/' && p[1] == '/');
6056}
6057
6058/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006059 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6060 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006062 * If a line begins with an "else", only consider it terminated if no unmatched
6063 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 * Return the character terminating the line (ending char's have precedence if
6065 * both apply in order to determine initializations).
6066 */
6067 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006068cin_isterminated(
6069 char_u *s,
6070 int incl_open, /* include '{' at the end as terminator */
6071 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006073 char_u found_start = 0;
6074 unsigned n_open = 0;
6075 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076
6077 s = cin_skipcomment(s);
6078
6079 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6080 found_start = *s;
6081
Bram Moolenaar496f9512011-05-19 16:35:09 +02006082 if (!found_start)
6083 is_else = cin_iselse(s);
6084
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 while (*s)
6086 {
6087 /* skip over comments, "" strings and 'c'haracters */
6088 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006089 if (*s == '}' && n_open > 0)
6090 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006091 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006092 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 && cin_nocode(s + 1))
6094 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006095 else if (*s == '{')
6096 {
6097 if (incl_open && cin_nocode(s + 1))
6098 return *s;
6099 else
6100 ++n_open;
6101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102
6103 if (*s)
6104 s++;
6105 }
6106 return found_start;
6107}
6108
6109/*
6110 * Recognize the basic picture of a function declaration -- it needs to
6111 * have an open paren somewhere and a close paren at the end of the line and
6112 * no semicolons anywhere.
6113 * When a line ends in a comma we continue looking in the next line.
6114 * "sp" points to a string with the line. When looking at other lines it must
6115 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006116 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006117 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 */
6119 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006120cin_isfuncdecl(
6121 char_u **sp,
6122 linenr_T first_lnum,
6123 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124{
6125 char_u *s;
6126 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006127 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006129 pos_T *trypos;
6130 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131
6132 if (sp == NULL)
6133 s = ml_get(lnum);
6134 else
6135 s = *sp;
6136
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006137 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006138 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006139 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006140 {
6141 lnum = trypos->lnum;
6142 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006143 {
6144 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006145 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006146 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006147
6148 s = ml_get(lnum);
6149 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006150 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006151
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006152 /* Ignore line starting with #. */
6153 if (cin_ispreproc(s))
6154 return FALSE;
6155
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6157 {
6158 if (cin_iscomment(s)) /* ignore comments */
6159 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006160 else if (*s == ':')
6161 {
6162 if (*(s + 1) == ':')
6163 s += 2;
6164 else
6165 /* To avoid a mistake in the following situation:
6166 * A::A(int a, int b)
6167 * : a(0) // <--not a function decl
6168 * , b(0)
6169 * {...
6170 */
6171 return FALSE;
6172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 else
6174 ++s;
6175 }
6176 if (*s != '(')
6177 return FALSE; /* ';', ' or " before any () or no '(' */
6178
6179 while (*s && *s != ';' && *s != '\'' && *s != '"')
6180 {
6181 if (*s == ')' && cin_nocode(s + 1))
6182 {
6183 /* ')' at the end: may have found a match
6184 * Check for he previous line not to end in a backslash:
6185 * #if defined(x) && \
6186 * defined(y)
6187 */
6188 lnum = first_lnum - 1;
6189 s = ml_get(lnum);
6190 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6191 retval = TRUE;
6192 goto done;
6193 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006194 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006196 int comma = (*s == ',');
6197
6198 /* ',' at the end: continue looking in the next line.
6199 * At the end: check for ',' in the next line, for this style:
6200 * func(arg1
6201 * , arg2) */
6202 for (;;)
6203 {
6204 if (lnum >= curbuf->b_ml.ml_line_count)
6205 break;
6206 s = ml_get(++lnum);
6207 if (!cin_ispreproc(s))
6208 break;
6209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 if (lnum >= curbuf->b_ml.ml_line_count)
6211 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006212 /* Require a comma at end of the line or a comma or ')' at the
6213 * start of next line. */
6214 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006215 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006216 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006217 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 }
6219 else if (cin_iscomment(s)) /* ignore comments */
6220 s = cin_skipcomment(s);
6221 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006222 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006224 just_started = FALSE;
6225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 }
6227
6228done:
6229 if (lnum != first_lnum && sp != NULL)
6230 *sp = ml_get(first_lnum);
6231
6232 return retval;
6233}
6234
6235 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006236cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006238 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239}
6240
6241 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006242cin_iselse(
6243 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244{
6245 if (*p == '}') /* accept "} else" */
6246 p = cin_skipcomment(p + 1);
6247 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6248}
6249
6250 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006251cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252{
6253 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6254}
6255
6256/*
6257 * Check if this is a "while" that should have a matching "do".
6258 * We only accept a "while (condition) ;", with only white space between the
6259 * ')' and ';'. The condition may be spread over several lines.
6260 */
6261 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006262cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263{
6264 pos_T cursor_save;
6265 pos_T *trypos;
6266 int retval = FALSE;
6267
6268 p = cin_skipcomment(p);
6269 if (*p == '}') /* accept "} while (cond);" */
6270 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006271 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 {
6273 cursor_save = curwin->w_cursor;
6274 curwin->w_cursor.lnum = lnum;
6275 curwin->w_cursor.col = 0;
6276 p = ml_get_curline();
6277 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6278 {
6279 ++p;
6280 ++curwin->w_cursor.col;
6281 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006282 if ((trypos = findmatchlimit(NULL, 0, 0,
6283 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6285 retval = TRUE;
6286 curwin->w_cursor = cursor_save;
6287 }
6288 return retval;
6289}
6290
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006291/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006292 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006293 * Return 0 if there is none.
6294 * Otherwise return !0 and update "*poffset" to point to the place where the
6295 * string was found.
6296 */
6297 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006298cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006299{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006300 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006301
6302 if (offset-- < 2)
6303 return 0;
6304 while (offset > 2 && vim_iswhite(line[offset]))
6305 --offset;
6306
6307 offset -= 1;
6308 if (!STRNCMP(line + offset, "if", 2))
6309 goto probablyFound;
6310
6311 if (offset >= 1)
6312 {
6313 offset -= 1;
6314 if (!STRNCMP(line + offset, "for", 3))
6315 goto probablyFound;
6316
6317 if (offset >= 2)
6318 {
6319 offset -= 2;
6320 if (!STRNCMP(line + offset, "while", 5))
6321 goto probablyFound;
6322 }
6323 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006324 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006325
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006326probablyFound:
6327 if (!offset || !vim_isIDc(line[offset - 1]))
6328 {
6329 *poffset = offset;
6330 return 1;
6331 }
6332 return 0;
6333}
6334
6335/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006336 * Return TRUE if we are at the end of a do-while.
6337 * do
6338 * nothing;
6339 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006340 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006341 * Adjust the cursor to the line with "while".
6342 */
6343 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006344cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006345{
6346 char_u *line;
6347 char_u *p;
6348 char_u *s;
6349 pos_T *trypos;
6350 int i;
6351
6352 if (terminated != ';') /* there must be a ';' at the end */
6353 return FALSE;
6354
6355 p = line = ml_get_curline();
6356 while (*p != NUL)
6357 {
6358 p = cin_skipcomment(p);
6359 if (*p == ')')
6360 {
6361 s = skipwhite(p + 1);
6362 if (*s == ';' && cin_nocode(s + 1))
6363 {
6364 /* Found ");" at end of the line, now check there is "while"
6365 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006366 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006367 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006368 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006369 if (trypos != NULL)
6370 {
6371 s = cin_skipcomment(ml_get(trypos->lnum));
6372 if (*s == '}') /* accept "} while (cond);" */
6373 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006374 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006375 {
6376 curwin->w_cursor.lnum = trypos->lnum;
6377 return TRUE;
6378 }
6379 }
6380
6381 /* Searching may have made "line" invalid, get it again. */
6382 line = ml_get_curline();
6383 p = line + i;
6384 }
6385 }
6386 if (*p != NUL)
6387 ++p;
6388 }
6389 return FALSE;
6390}
6391
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006393cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394{
6395 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6396}
6397
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006398/*
6399 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 * constructor-initialization. eg:
6401 *
6402 * class MyClass :
6403 * baseClass <-- here
6404 * class MyClass : public baseClass,
6405 * anotherBaseClass <-- here (should probably lineup ??)
6406 * MyClass::MyClass(...) :
6407 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006408 *
6409 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 */
6411 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006412cin_is_cpp_baseclass(
6413 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006415 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 char_u *s;
6417 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006418 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006419 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006421 if (pos->lnum <= lnum)
6422 return cached->found; /* Use the cached result */
6423
6424 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006426 s = skipwhite(line);
6427 if (*s == '#') /* skip #define FOO x ? (x) : x */
6428 return FALSE;
6429 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 if (*s == NUL)
6431 return FALSE;
6432
6433 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6434
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006435 /* Search for a line starting with '#', empty, ending in ';' or containing
6436 * '{' or '}' and start below it. This handles the following situations:
6437 * a = cond ?
6438 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006439 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006440 * func::foo()
6441 * : something
6442 * {}
6443 * Foo::Foo (int one, int two)
6444 * : something(4),
6445 * somethingelse(3)
6446 * {}
6447 */
6448 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006450 line = ml_get(lnum - 1);
6451 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006452 if (*s == '#' || *s == NUL)
6453 break;
6454 while (*s != NUL)
6455 {
6456 s = cin_skipcomment(s);
6457 if (*s == '{' || *s == '}'
6458 || (*s == ';' && cin_nocode(s + 1)))
6459 break;
6460 if (*s != NUL)
6461 ++s;
6462 }
6463 if (*s != NUL)
6464 break;
6465 --lnum;
6466 }
6467
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006468 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006469 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006470 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006471 for (;;)
6472 {
6473 if (*s == NUL)
6474 {
6475 if (lnum == curwin->w_cursor.lnum)
6476 break;
6477 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006478 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006479 s = line;
6480 }
6481 if (s == line)
6482 {
6483 /* don't recognize "case (foo):" as a baseclass */
6484 if (cin_iscase(s, FALSE))
6485 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006486 s = cin_skipcomment(line);
6487 if (*s == NUL)
6488 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006489 }
6490
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006491 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006492 s = skip_string(s) + 1;
6493 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 {
6495 if (s[1] == ':')
6496 {
6497 /* skip double colon. It can't be a constructor
6498 * initialization any more */
6499 lookfor_ctor_init = FALSE;
6500 s = cin_skipcomment(s + 2);
6501 }
6502 else if (lookfor_ctor_init || class_or_struct)
6503 {
6504 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006505 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 cpp_base_class = TRUE;
6507 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006508 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 s = cin_skipcomment(s + 1);
6510 }
6511 else
6512 s = cin_skipcomment(s + 1);
6513 }
6514 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6515 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6516 {
6517 class_or_struct = TRUE;
6518 lookfor_ctor_init = FALSE;
6519
6520 if (*s == 'c')
6521 s = cin_skipcomment(s + 5);
6522 else
6523 s = cin_skipcomment(s + 6);
6524 }
6525 else
6526 {
6527 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6528 {
6529 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6530 }
6531 else if (s[0] == ')')
6532 {
6533 /* Constructor-initialization is assumed if we come across
6534 * something like "):" */
6535 class_or_struct = FALSE;
6536 lookfor_ctor_init = TRUE;
6537 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006538 else if (s[0] == '?')
6539 {
6540 /* Avoid seeing '() :' after '?' as constructor init. */
6541 return FALSE;
6542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 else if (!vim_isIDc(s[0]))
6544 {
6545 /* if it is not an identifier, we are wrong */
6546 class_or_struct = FALSE;
6547 lookfor_ctor_init = FALSE;
6548 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006549 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 {
6551 /* it can't be a constructor-initialization any more */
6552 lookfor_ctor_init = FALSE;
6553
6554 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006555 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006556 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 }
6558
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006559 /* When the line ends in a comma don't align with it. */
6560 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006561 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006562
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 s = cin_skipcomment(s + 1);
6564 }
6565 }
6566
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006567 cached->found = cpp_base_class;
6568 if (cpp_base_class)
6569 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 return cpp_base_class;
6571}
6572
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006573 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006574get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006575{
6576 int amount;
6577 colnr_T vcol;
6578 pos_T *trypos;
6579
6580 if (col == 0)
6581 {
6582 amount = get_indent();
6583 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006584 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006585 amount = get_indent_lnum(trypos->lnum); /* XXX */
6586 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006587 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006588 }
6589 else
6590 {
6591 curwin->w_cursor.col = col;
6592 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6593 amount = (int)vcol;
6594 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006595 if (amount < curbuf->b_ind_cpp_baseclass)
6596 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006597 return amount;
6598}
6599
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600/*
6601 * Return TRUE if string "s" ends with the string "find", possibly followed by
6602 * white space and comments. Skip strings and comments.
6603 * Ignore "ignore" after "find" if it's not NULL.
6604 */
6605 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006606cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607{
6608 char_u *p = s;
6609 char_u *r;
6610 int len = (int)STRLEN(find);
6611
6612 while (*p != NUL)
6613 {
6614 p = cin_skipcomment(p);
6615 if (STRNCMP(p, find, len) == 0)
6616 {
6617 r = skipwhite(p + len);
6618 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6619 r = skipwhite(r + STRLEN(ignore));
6620 if (cin_nocode(r))
6621 return TRUE;
6622 }
6623 if (*p != NUL)
6624 ++p;
6625 }
6626 return FALSE;
6627}
6628
6629/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006630 * Return TRUE when "s" starts with "word" and then a non-ID character.
6631 */
6632 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006633cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006634{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006635 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006636
6637 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6638}
6639
6640/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 * Skip strings, chars and comments until at or past "trypos".
6642 * Return the column found.
6643 */
6644 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006645cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646{
6647 char_u *line;
6648 char_u *p;
6649
6650 p = line = ml_get(trypos->lnum);
6651 while (*p && (colnr_T)(p - line) < trypos->col)
6652 {
6653 if (cin_iscomment(p))
6654 p = cin_skipcomment(p);
6655 else
6656 {
6657 p = skip_string(p);
6658 ++p;
6659 }
6660 }
6661 return (int)(p - line);
6662}
6663
6664/*
6665 * Find the '{' at the start of the block we are in.
6666 * Return NULL if no match found.
6667 * Ignore a '{' that is in a comment, makes indenting the next three lines
6668 * work. */
6669/* foo() */
6670/* { */
6671/* } */
6672
6673 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006674find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675{
6676 pos_T cursor_save;
6677 pos_T *trypos;
6678 pos_T *pos;
6679 static pos_T pos_copy;
6680
6681 cursor_save = curwin->w_cursor;
6682 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6683 {
6684 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6685 trypos = &pos_copy;
6686 curwin->w_cursor = *trypos;
6687 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006688 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006690 && (pos = ind_find_start_CORS()) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 break;
6692 if (pos != NULL)
6693 curwin->w_cursor.lnum = pos->lnum;
6694 }
6695 curwin->w_cursor = cursor_save;
6696 return trypos;
6697}
6698
6699/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006700 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006701 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 */
6703 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006704find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006706 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006707}
6708
6709 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006710find_match_char (int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006711{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 pos_T cursor_save;
6713 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006714 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006715 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716
6717 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006718 ind_maxp_wk = ind_maxparen;
6719retry:
6720 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 {
6722 /* check if the ( is in a // comment */
6723 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01006724 {
6725 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
6726 if (ind_maxp_wk > 0)
6727 {
6728 curwin->w_cursor = *trypos;
6729 curwin->w_cursor.col = 0; /* XXX */
6730 goto retry;
6731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 else
6735 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01006736 pos_T *trypos_wk;
6737
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6739 trypos = &pos_copy;
6740 curwin->w_cursor = *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006741 if ((trypos_wk = ind_find_start_CORS()) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01006742 {
6743 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
6744 - trypos_wk->lnum);
6745 if (ind_maxp_wk > 0)
6746 {
6747 curwin->w_cursor = *trypos_wk;
6748 goto retry;
6749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 }
6753 }
6754 curwin->w_cursor = cursor_save;
6755 return trypos;
6756}
6757
6758/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006759 * Find the matching '(', ignoring it if it is in a comment or before an
6760 * unmatched {.
6761 * Return NULL if no match found.
6762 */
6763 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006764find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02006765{
6766 pos_T *trypos = find_match_paren(ind_maxparen);
6767
6768 if (trypos != NULL)
6769 {
6770 pos_T *tryposBrace = find_start_brace();
6771
6772 /* If both an unmatched '(' and '{' is found. Ignore the '('
6773 * position if the '{' is further down. */
6774 if (tryposBrace != NULL
6775 && (trypos->lnum != tryposBrace->lnum
6776 ? trypos->lnum < tryposBrace->lnum
6777 : trypos->col < tryposBrace->col))
6778 trypos = NULL;
6779 }
6780 return trypos;
6781}
6782
6783/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 * Return ind_maxparen corrected for the difference in line number between the
6785 * cursor position and "startpos". This makes sure that searching for a
6786 * matching paren above the cursor line doesn't find a match because of
6787 * looking a few lines further.
6788 */
6789 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006790corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791{
6792 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6793
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006794 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
6795 return curbuf->b_ind_maxparen - (int)n;
6796 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797}
6798
6799/*
6800 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006801 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 */
6803 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006804find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805{
6806 int i;
6807 int retval = FALSE;
6808 int open_count = 0;
6809
6810 curwin->w_cursor.col = 0; /* default is start of line */
6811
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006812 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 {
6814 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6815 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6816 if (l[i] == start)
6817 ++open_count;
6818 else if (l[i] == end)
6819 {
6820 if (open_count > 0)
6821 --open_count;
6822 else
6823 {
6824 curwin->w_cursor.col = i;
6825 retval = TRUE;
6826 }
6827 }
6828 }
6829 return retval;
6830}
6831
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006832/*
6833 * Parse 'cinoptions' and set the values in "curbuf".
6834 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
6835 */
6836 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006837parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006838{
6839 char_u *p;
6840 char_u *l;
6841 char_u *digits;
6842 int n;
6843 int divider;
6844 int fraction = 0;
6845 int sw = (int)get_sw_value(buf);
6846
6847 /*
6848 * Set the default values.
6849 */
6850 /* Spaces from a block's opening brace the prevailing indent for that
6851 * block should be. */
6852 buf->b_ind_level = sw;
6853
6854 /* Spaces from the edge of the line an open brace that's at the end of a
6855 * line is imagined to be. */
6856 buf->b_ind_open_imag = 0;
6857
6858 /* Spaces from the prevailing indent for a line that is not preceded by
6859 * an opening brace. */
6860 buf->b_ind_no_brace = 0;
6861
6862 /* Column where the first { of a function should be located }. */
6863 buf->b_ind_first_open = 0;
6864
6865 /* Spaces from the prevailing indent a leftmost open brace should be
6866 * located. */
6867 buf->b_ind_open_extra = 0;
6868
6869 /* Spaces from the matching open brace (real location for one at the left
6870 * edge; imaginary location from one that ends a line) the matching close
6871 * brace should be located. */
6872 buf->b_ind_close_extra = 0;
6873
6874 /* Spaces from the edge of the line an open brace sitting in the leftmost
6875 * column is imagined to be. */
6876 buf->b_ind_open_left_imag = 0;
6877
6878 /* Spaces jump labels should be shifted to the left if N is non-negative,
6879 * otherwise the jump label will be put to column 1. */
6880 buf->b_ind_jump_label = -1;
6881
6882 /* Spaces from the switch() indent a "case xx" label should be located. */
6883 buf->b_ind_case = sw;
6884
6885 /* Spaces from the "case xx:" code after a switch() should be located. */
6886 buf->b_ind_case_code = sw;
6887
6888 /* Lineup break at end of case in switch() with case label. */
6889 buf->b_ind_case_break = 0;
6890
6891 /* Spaces from the class declaration indent a scope declaration label
6892 * should be located. */
6893 buf->b_ind_scopedecl = sw;
6894
6895 /* Spaces from the scope declaration label code should be located. */
6896 buf->b_ind_scopedecl_code = sw;
6897
6898 /* Amount K&R-style parameters should be indented. */
6899 buf->b_ind_param = sw;
6900
6901 /* Amount a function type spec should be indented. */
6902 buf->b_ind_func_type = sw;
6903
6904 /* Amount a cpp base class declaration or constructor initialization
6905 * should be indented. */
6906 buf->b_ind_cpp_baseclass = sw;
6907
6908 /* additional spaces beyond the prevailing indent a continuation line
6909 * should be located. */
6910 buf->b_ind_continuation = sw;
6911
6912 /* Spaces from the indent of the line with an unclosed parentheses. */
6913 buf->b_ind_unclosed = sw * 2;
6914
6915 /* Spaces from the indent of the line with an unclosed parentheses, which
6916 * itself is also unclosed. */
6917 buf->b_ind_unclosed2 = sw;
6918
6919 /* Suppress ignoring spaces from the indent of a line starting with an
6920 * unclosed parentheses. */
6921 buf->b_ind_unclosed_noignore = 0;
6922
6923 /* If the opening paren is the last nonwhite character on the line, and
6924 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
6925 * context (for very long lines). */
6926 buf->b_ind_unclosed_wrapped = 0;
6927
6928 /* Suppress ignoring white space when lining up with the character after
6929 * an unclosed parentheses. */
6930 buf->b_ind_unclosed_whiteok = 0;
6931
6932 /* Indent a closing parentheses under the line start of the matching
6933 * opening parentheses. */
6934 buf->b_ind_matching_paren = 0;
6935
6936 /* Indent a closing parentheses under the previous line. */
6937 buf->b_ind_paren_prev = 0;
6938
6939 /* Extra indent for comments. */
6940 buf->b_ind_comment = 0;
6941
6942 /* Spaces from the comment opener when there is nothing after it. */
6943 buf->b_ind_in_comment = 3;
6944
6945 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
6946 * after the comment opener. */
6947 buf->b_ind_in_comment2 = 0;
6948
6949 /* Max lines to search for an open paren. */
6950 buf->b_ind_maxparen = 20;
6951
6952 /* Max lines to search for an open comment. */
6953 buf->b_ind_maxcomment = 70;
6954
6955 /* Handle braces for java code. */
6956 buf->b_ind_java = 0;
6957
6958 /* Not to confuse JS object properties with labels. */
6959 buf->b_ind_js = 0;
6960
6961 /* Handle blocked cases correctly. */
6962 buf->b_ind_keep_case_label = 0;
6963
6964 /* Handle C++ namespace. */
6965 buf->b_ind_cpp_namespace = 0;
6966
6967 /* Handle continuation lines containing conditions of if(), for() and
6968 * while(). */
6969 buf->b_ind_if_for_while = 0;
6970
6971 for (p = buf->b_p_cino; *p; )
6972 {
6973 l = p++;
6974 if (*p == '-')
6975 ++p;
6976 digits = p; /* remember where the digits start */
6977 n = getdigits(&p);
6978 divider = 0;
6979 if (*p == '.') /* ".5s" means a fraction */
6980 {
6981 fraction = atol((char *)++p);
6982 while (VIM_ISDIGIT(*p))
6983 {
6984 ++p;
6985 if (divider)
6986 divider *= 10;
6987 else
6988 divider = 10;
6989 }
6990 }
6991 if (*p == 's') /* "2s" means two times 'shiftwidth' */
6992 {
6993 if (p == digits)
6994 n = sw; /* just "s" is one 'shiftwidth' */
6995 else
6996 {
6997 n *= sw;
6998 if (divider)
6999 n += (sw * fraction + divider / 2) / divider;
7000 }
7001 ++p;
7002 }
7003 if (l[1] == '-')
7004 n = -n;
7005
7006 /* When adding an entry here, also update the default 'cinoptions' in
7007 * doc/indent.txt, and add explanation for it! */
7008 switch (*l)
7009 {
7010 case '>': buf->b_ind_level = n; break;
7011 case 'e': buf->b_ind_open_imag = n; break;
7012 case 'n': buf->b_ind_no_brace = n; break;
7013 case 'f': buf->b_ind_first_open = n; break;
7014 case '{': buf->b_ind_open_extra = n; break;
7015 case '}': buf->b_ind_close_extra = n; break;
7016 case '^': buf->b_ind_open_left_imag = n; break;
7017 case 'L': buf->b_ind_jump_label = n; break;
7018 case ':': buf->b_ind_case = n; break;
7019 case '=': buf->b_ind_case_code = n; break;
7020 case 'b': buf->b_ind_case_break = n; break;
7021 case 'p': buf->b_ind_param = n; break;
7022 case 't': buf->b_ind_func_type = n; break;
7023 case '/': buf->b_ind_comment = n; break;
7024 case 'c': buf->b_ind_in_comment = n; break;
7025 case 'C': buf->b_ind_in_comment2 = n; break;
7026 case 'i': buf->b_ind_cpp_baseclass = n; break;
7027 case '+': buf->b_ind_continuation = n; break;
7028 case '(': buf->b_ind_unclosed = n; break;
7029 case 'u': buf->b_ind_unclosed2 = n; break;
7030 case 'U': buf->b_ind_unclosed_noignore = n; break;
7031 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7032 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7033 case 'm': buf->b_ind_matching_paren = n; break;
7034 case 'M': buf->b_ind_paren_prev = n; break;
7035 case ')': buf->b_ind_maxparen = n; break;
7036 case '*': buf->b_ind_maxcomment = n; break;
7037 case 'g': buf->b_ind_scopedecl = n; break;
7038 case 'h': buf->b_ind_scopedecl_code = n; break;
7039 case 'j': buf->b_ind_java = n; break;
7040 case 'J': buf->b_ind_js = n; break;
7041 case 'l': buf->b_ind_keep_case_label = n; break;
7042 case '#': buf->b_ind_hash_comment = n; break;
7043 case 'N': buf->b_ind_cpp_namespace = n; break;
7044 case 'k': buf->b_ind_if_for_while = n; break;
7045 }
7046 if (*p == ',')
7047 ++p;
7048 }
7049}
7050
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007051/*
7052 * Return the desired indent for C code.
7053 * Return -1 if the indent should be left alone (inside a raw string).
7054 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007056get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 pos_T cur_curpos;
7059 int amount;
7060 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007061 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 colnr_T col;
7063 char_u *theline;
7064 char_u *linecopy;
7065 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007066 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007068 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 pos_T our_paren_pos;
7070 char_u *start;
7071 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007072#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073#define BRACE_AT_START 2 /* '{' is at start of line */
7074#define BRACE_AT_END 3 /* '{' is at end of line */
7075 linenr_T ourscope;
7076 char_u *l;
7077 char_u *look;
7078 char_u terminated;
7079 int lookfor;
7080#define LOOKFOR_INITIAL 0
7081#define LOOKFOR_IF 1
7082#define LOOKFOR_DO 2
7083#define LOOKFOR_CASE 3
7084#define LOOKFOR_ANY 4
7085#define LOOKFOR_TERM 5
7086#define LOOKFOR_UNTERM 6
7087#define LOOKFOR_SCOPEDECL 7
7088#define LOOKFOR_NOBREAK 8
7089#define LOOKFOR_CPP_BASECLASS 9
7090#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007091#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007092#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093
7094 int whilelevel;
7095 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 int n;
7097 int iscase;
7098 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007099 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007101 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007102 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007103 int js_cur_has_key = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007104 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007106 /* make a copy, value is changed below */
7107 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108
7109 /* remember where the cursor was when we started */
7110 cur_curpos = curwin->w_cursor;
7111
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007112 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007113 if (cur_curpos.lnum == 1)
7114 return 0;
7115
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 /* Get a copy of the current contents of the line.
7117 * This is required, because only the most recent line obtained with
7118 * ml_get is valid! */
7119 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7120 if (linecopy == NULL)
7121 return 0;
7122
7123 /*
7124 * In insert mode and the cursor is on a ')' truncate the line at the
7125 * cursor position. We don't want to line up with the matching '(' when
7126 * inserting new stuff.
7127 * For unknown reasons the cursor might be past the end of the line, thus
7128 * check for that.
7129 */
7130 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007131 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 && linecopy[curwin->w_cursor.col] == ')')
7133 linecopy[curwin->w_cursor.col] = NUL;
7134
7135 theline = skipwhite(linecopy);
7136
7137 /* move the cursor to the start of the line */
7138
7139 curwin->w_cursor.col = 0;
7140
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007141 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007142
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007144 * If we are inside a raw string don't change the indent.
7145 * Ignore a raw string inside a comment.
7146 */
7147 comment_pos = ind_find_start_comment();
7148 if (comment_pos != NULL)
7149 {
7150 /* findmatchlimit() static pos is overwritten, make a copy */
7151 tryposCopy = *comment_pos;
7152 comment_pos = &tryposCopy;
7153 }
7154 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
7155 if (trypos != NULL && (comment_pos == NULL || lt(*trypos, *comment_pos)))
7156 {
7157 amount = -1;
7158 goto laterend;
7159 }
7160
7161 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 * #defines and so on always go at the left when included in 'cinkeys'.
7163 */
7164 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007165 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007166 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007167 goto theend;
7168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169
7170 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007171 * Is it a non-case label? Then that goes at the left margin too unless:
7172 * - JS flag is set.
7173 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007175 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007176 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 {
7178 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007179 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 }
7181
7182 /*
7183 * If we're inside a "//" comment and there is a "//" comment in a
7184 * previous line, lineup with that one.
7185 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007186 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 && (trypos = find_line_comment()) != NULL) /* XXX */
7188 {
7189 /* find how indented the line beginning the comment is */
7190 getvcol(curwin, trypos, &col, NULL, NULL);
7191 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007192 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 }
7194
7195 /*
7196 * If we're inside a comment and not looking at the start of the
7197 * comment, try using the 'comments' option.
7198 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007199 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 {
7201 int lead_start_len = 2;
7202 int lead_middle_len = 1;
7203 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7204 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7205 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7206 char_u *p;
7207 int start_align = 0;
7208 int start_off = 0;
7209 int done = FALSE;
7210
7211 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007212 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007214 *lead_start = NUL;
7215 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216
7217 p = curbuf->b_p_com;
7218 while (*p != NUL)
7219 {
7220 int align = 0;
7221 int off = 0;
7222 int what = 0;
7223
7224 while (*p != NUL && *p != ':')
7225 {
7226 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7227 what = *p++;
7228 else if (*p == COM_LEFT || *p == COM_RIGHT)
7229 align = *p++;
7230 else if (VIM_ISDIGIT(*p) || *p == '-')
7231 off = getdigits(&p);
7232 else
7233 ++p;
7234 }
7235
7236 if (*p == ':')
7237 ++p;
7238 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7239 if (what == COM_START)
7240 {
7241 STRCPY(lead_start, lead_end);
7242 lead_start_len = (int)STRLEN(lead_start);
7243 start_off = off;
7244 start_align = align;
7245 }
7246 else if (what == COM_MIDDLE)
7247 {
7248 STRCPY(lead_middle, lead_end);
7249 lead_middle_len = (int)STRLEN(lead_middle);
7250 }
7251 else if (what == COM_END)
7252 {
7253 /* If our line starts with the middle comment string, line it
7254 * up with the comment opener per the 'comments' option. */
7255 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7256 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7257 {
7258 done = TRUE;
7259 if (curwin->w_cursor.lnum > 1)
7260 {
7261 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007262 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 * the middle comment string matches in the previous
7264 * line, use the indent of that line. XXX */
7265 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7266 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7267 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7268 else if (STRNCMP(look, lead_middle,
7269 lead_middle_len) == 0)
7270 {
7271 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7272 break;
7273 }
7274 /* If the start comment string doesn't match with the
7275 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007276 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 lead_start, lead_start_len) != 0)
7278 continue;
7279 }
7280 if (start_off != 0)
7281 amount += start_off;
7282 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007283 amount += vim_strsize(lead_start)
7284 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 break;
7286 }
7287
7288 /* If our line starts with the end comment string, line it up
7289 * with the middle comment */
7290 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7291 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7292 {
7293 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7294 /* XXX */
7295 if (off != 0)
7296 amount += off;
7297 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007298 amount += vim_strsize(lead_start)
7299 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 done = TRUE;
7301 break;
7302 }
7303 }
7304 }
7305
7306 /* If our line starts with an asterisk, line up with the
7307 * asterisk in the comment opener; otherwise, line up
7308 * with the first character of the comment text.
7309 */
7310 if (done)
7311 ;
7312 else if (theline[0] == '*')
7313 amount += 1;
7314 else
7315 {
7316 /*
7317 * If we are more than one line away from the comment opener, take
7318 * the indent of the previous non-empty line. If 'cino' has "CO"
7319 * and we are just below the comment opener and there are any
7320 * white characters after it line up with the text after it;
7321 * otherwise, add the amount specified by "c" in 'cino'
7322 */
7323 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007324 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 {
7326 if (linewhite(lnum)) /* skip blank lines */
7327 continue;
7328 amount = get_indent_lnum(lnum); /* XXX */
7329 break;
7330 }
7331 if (amount == -1) /* use the comment opener */
7332 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007333 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007335 start = ml_get(comment_pos->lnum);
7336 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007338 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007340 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007342 if (curbuf->b_ind_in_comment2 || *look == NUL)
7343 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 }
7345 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007346 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 }
7348
7349 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007350 * Are we looking at a ']' that has a match?
7351 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007352 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007353 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7354 {
7355 /* align with the line containing the '['. */
7356 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007357 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007358 }
7359
7360 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 * Are we inside parentheses or braces?
7362 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007363 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007364 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007365 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 || trypos != NULL)
7367 {
7368 if (trypos != NULL && tryposBrace != NULL)
7369 {
7370 /* Both an unmatched '(' and '{' is found. Use the one which is
7371 * closer to the current cursor position, set the other to NULL. */
7372 if (trypos->lnum != tryposBrace->lnum
7373 ? trypos->lnum < tryposBrace->lnum
7374 : trypos->col < tryposBrace->col)
7375 trypos = NULL;
7376 else
7377 tryposBrace = NULL;
7378 }
7379
7380 if (trypos != NULL)
7381 {
7382 /*
7383 * If the matching paren is more than one line away, use the indent of
7384 * a previous non-empty line that matches the same paren.
7385 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007386 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007388 /* Line up with the start of the matching paren line. */
7389 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7390 }
7391 else
7392 {
7393 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007394 our_paren_pos = *trypos;
7395 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007397 l = skipwhite(ml_get(lnum));
7398 if (cin_nocode(l)) /* skip comment lines */
7399 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007400 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007401 continue; /* ignore #define, #if, etc. */
7402 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007404 /* Skip a comment or raw string. XXX */
7405 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007406 {
7407 lnum = trypos->lnum + 1;
7408 continue;
7409 }
7410
7411 /* XXX */
7412 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007413 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007414 && trypos->lnum == our_paren_pos.lnum
7415 && trypos->col == our_paren_pos.col)
7416 {
7417 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007419 if (theline[0] == ')')
7420 {
7421 if (our_paren_pos.lnum != lnum
7422 && cur_amount > amount)
7423 cur_amount = amount;
7424 amount = -1;
7425 }
7426 break;
7427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 }
7429 }
7430
7431 /*
7432 * Line up with line where the matching paren is. XXX
7433 * If the line starts with a '(' or the indent for unclosed
7434 * parentheses is zero, line up with the unclosed parentheses.
7435 */
7436 if (amount == -1)
7437 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007438 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007439 int is_if_for_while = 0;
7440
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007441 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007442 {
7443 /* Look for the outermost opening parenthesis on this line
7444 * and check whether it belongs to an "if", "for" or "while". */
7445
7446 pos_T cursor_save = curwin->w_cursor;
7447 pos_T outermost;
7448 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007449
7450 trypos = &our_paren_pos;
7451 do {
7452 outermost = *trypos;
7453 curwin->w_cursor.lnum = outermost.lnum;
7454 curwin->w_cursor.col = outermost.col;
7455
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007456 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007457 } while (trypos && trypos->lnum == outermost.lnum);
7458
7459 curwin->w_cursor = cursor_save;
7460
7461 line = ml_get(outermost.lnum);
7462
7463 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007464 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007465 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007466
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007467 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007468 look = skipwhite(look);
7469 if (*look == '(')
7470 {
7471 linenr_T save_lnum = curwin->w_cursor.lnum;
7472 char_u *line;
7473 int look_col;
7474
7475 /* Ignore a '(' in front of the line that has a match before
7476 * our matching '('. */
7477 curwin->w_cursor.lnum = our_paren_pos.lnum;
7478 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007479 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007480 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007481 if ((trypos = findmatchlimit(NULL, ')', 0,
7482 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007483 != NULL
7484 && trypos->lnum == our_paren_pos.lnum
7485 && trypos->col < our_paren_pos.col)
7486 ignore_paren_col = trypos->col + 1;
7487
7488 curwin->w_cursor.lnum = save_lnum;
7489 look = ml_get(our_paren_pos.lnum) + look_col;
7490 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007491 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7492 && is_if_for_while == 0)
7493 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007494 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 {
7496 /*
7497 * If we're looking at a close paren, line up right there;
7498 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007499 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 * the last nonwhite character of the line, use either the
7501 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007502 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 * lines).
7504 */
7505 if (theline[0] != ')')
7506 {
7507 cur_amount = MAXCOL;
7508 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007509 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 && cin_ends_in(l, (char_u *)"(", NULL))
7511 {
7512 /* look for opening unmatched paren, indent one level
7513 * for each additional level */
7514 n = 1;
7515 for (col = 0; col < our_paren_pos.col; ++col)
7516 {
7517 switch (l[col])
7518 {
7519 case '(':
7520 case '{': ++n;
7521 break;
7522
7523 case ')':
7524 case '}': if (n > 1)
7525 --n;
7526 break;
7527 }
7528 }
7529
7530 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007531 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007533 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 our_paren_pos.col++;
7535 else
7536 {
7537 col = our_paren_pos.col + 1;
7538 while (vim_iswhite(l[col]))
7539 col++;
7540 if (l[col] != NUL) /* In case of trailing space */
7541 our_paren_pos.col = col;
7542 else
7543 our_paren_pos.col++;
7544 }
7545 }
7546
7547 /*
7548 * Find how indented the paren is, or the character after it
7549 * if we did the above "if".
7550 */
7551 if (our_paren_pos.col > 0)
7552 {
7553 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7554 if (cur_amount > (int)col)
7555 cur_amount = col;
7556 }
7557 }
7558
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007559 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 {
7561 /* Line up with the start of the matching paren line. */
7562 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007563 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7564 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007565 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 {
7567 if (cur_amount != MAXCOL)
7568 amount = cur_amount;
7569 }
7570 else
7571 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007572 /* Add b_ind_unclosed2 for each '(' before our matching one,
7573 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007575 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 {
7577 --our_paren_pos.col;
7578 switch (*ml_get_pos(&our_paren_pos))
7579 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007580 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 col = our_paren_pos.col;
7582 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007583 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 col = MAXCOL;
7585 break;
7586 }
7587 }
7588
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007589 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 * braces */
7591 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007592 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 else
7594 {
7595 curwin->w_cursor.lnum = our_paren_pos.lnum;
7596 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007597 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7598 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007599 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007601 {
7602 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007603 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007604 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007605 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 }
7608 /*
7609 * For a line starting with ')' use the minimum of the two
7610 * positions, to avoid giving it more indent than the previous
7611 * lines:
7612 * func_long_name( if (x
7613 * arg && yy
7614 * ) ^ not here ) ^ not here
7615 */
7616 if (cur_amount < amount)
7617 amount = cur_amount;
7618 }
7619 }
7620
7621 /* add extra indent for a comment */
7622 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007623 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 else
7626 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007627 /*
7628 * We are inside braces, there is a { before this line at the position
7629 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007630 * Make a copy of tryposBrace, it may point to pos_copy inside
7631 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007632 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007633 tryposCopy = *tryposBrace;
7634 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 ourscope = trypos->lnum;
7637 start = ml_get(ourscope);
7638
7639 /*
7640 * Now figure out how indented the line is in general.
7641 * If the brace was at the start of the line, we use that;
7642 * otherwise, check out the indentation of the line as
7643 * a whole and then add the "imaginary indent" to that.
7644 */
7645 look = skipwhite(start);
7646 if (*look == '{')
7647 {
7648 getvcol(curwin, trypos, &col, NULL, NULL);
7649 amount = col;
7650 if (*start == '{')
7651 start_brace = BRACE_IN_COL0;
7652 else
7653 start_brace = BRACE_AT_START;
7654 }
7655 else
7656 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007657 /* That opening brace might have been on a continuation
7658 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 curwin->w_cursor.lnum = ourscope;
7660
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007661 /* Position the cursor over the rightmost paren, so that
7662 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 lnum = ourscope;
7664 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007665 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7666 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 lnum = trypos->lnum;
7668
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007669 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 * case 1: if (asdf &&
7671 * ldfd) {
7672 * }
7673 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007674 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7675 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007677 else if (curbuf->b_ind_js)
7678 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007680 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681
7682 start_brace = BRACE_AT_END;
7683 }
7684
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007685 /* For Javascript check if the line starts with "key:". */
7686 if (curbuf->b_ind_js)
7687 js_cur_has_key = cin_has_js_key(theline);
7688
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007690 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 * we want to be. otherwise, add the amount of room
7692 * that an indent is supposed to be.
7693 */
7694 if (theline[0] == '}')
7695 {
7696 /*
7697 * they may want closing braces to line up with something
7698 * other than the open brace. indulge them, if so.
7699 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007700 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 }
7702 else
7703 {
7704 /*
7705 * If we're looking at an "else", try to find an "if"
7706 * to match it with.
7707 * If we're looking at a "while", try to find a "do"
7708 * to match it with.
7709 */
7710 lookfor = LOOKFOR_INITIAL;
7711 if (cin_iselse(theline))
7712 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007713 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 lookfor = LOOKFOR_DO;
7715 if (lookfor != LOOKFOR_INITIAL)
7716 {
7717 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007718 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 {
7720 amount = get_indent(); /* XXX */
7721 goto theend;
7722 }
7723 }
7724
7725 /*
7726 * We get here if we are not on an "while-of-do" or "else" (or
7727 * failed to find a matching "if").
7728 * Search backwards for something to line up with.
7729 * First set amount for when we don't find anything.
7730 */
7731
7732 /*
7733 * if the '{' is _really_ at the left margin, use the imaginary
7734 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007735 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 */
7737
7738 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7739 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007740 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007741 lookfor_cpp_namespace = TRUE;
7742 }
7743 else if (start_brace == BRACE_AT_START &&
7744 lookfor_cpp_namespace) /* '{' is at start */
7745 {
7746
7747 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 }
7749 else
7750 {
7751 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007752 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007753 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007754
7755 l = skipwhite(ml_get_curline());
7756 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007757 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 else
7760 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007761 /* Compensate for adding b_ind_open_extra later. */
7762 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 if (amount < 0)
7764 amount = 0;
7765 }
7766 }
7767
7768 lookfor_break = FALSE;
7769
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007770 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 {
7772 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007773 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 }
7775 else if (cin_isscopedecl(theline)) /* private:, ... */
7776 {
7777 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007778 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 }
7780 else
7781 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007782 if (curbuf->b_ind_case_break && cin_isbreak(theline))
7783 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 lookfor_break = TRUE;
7785
7786 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007787 /* b_ind_level from start of block */
7788 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 }
7790 scope_amount = amount;
7791 whilelevel = 0;
7792
7793 /*
7794 * Search backwards. If we find something we recognize, line up
7795 * with that.
7796 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007797 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 * the usual amount relative to the conditional
7799 * that opens the block.
7800 */
7801 curwin->w_cursor = cur_curpos;
7802 for (;;)
7803 {
7804 curwin->w_cursor.lnum--;
7805 curwin->w_cursor.col = 0;
7806
7807 /*
7808 * If we went all the way back to the start of our scope, line
7809 * up with it.
7810 */
7811 if (curwin->w_cursor.lnum <= ourscope)
7812 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007813 /* We reached end of scope:
7814 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007816 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 * don't add ind_continuation, otherwise it is a variable
7818 * declaration:
7819 * int x,
7820 * here; <-- add ind_continuation
7821 */
7822 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7823 {
7824 if (curwin->w_cursor.lnum == 0
7825 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007826 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007828 /* nothing found (abuse curbuf->b_ind_maxparen as
7829 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 * initialization) */
7831 if (cont_amount > 0)
7832 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007833 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 amount += ind_continuation;
7835 break;
7836 }
7837
7838 l = ml_get_curline();
7839
7840 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007841 * If we're in a comment or raw string now, skip to
7842 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007844 trypos = ind_find_start_CORS();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 if (trypos != NULL)
7846 {
7847 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007848 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 continue;
7850 }
7851
7852 /*
7853 * Skip preprocessor directives and blank lines.
7854 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007855 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
7856 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 continue;
7858
7859 if (cin_nocode(l))
7860 continue;
7861
7862 terminated = cin_isterminated(l, FALSE, TRUE);
7863
7864 /*
7865 * If we are at top level and the line looks like a
7866 * function declaration, we are done
7867 * (it's a variable declaration).
7868 */
7869 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007870 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 {
7872 /* if the line is terminated with another ','
7873 * it is a continued variable initialization.
7874 * don't add extra indent.
7875 * TODO: does not work, if a function
7876 * declaration is split over multiple lines:
7877 * cin_isfuncdecl returns FALSE then.
7878 */
7879 if (terminated == ',')
7880 break;
7881
7882 /* if it es a enum declaration or an assignment,
7883 * we are done.
7884 */
7885 if (terminated != ';' && cin_isinit())
7886 break;
7887
7888 /* nothing useful found */
7889 if (terminated == 0 || terminated == '{')
7890 continue;
7891 }
7892
7893 if (terminated != ';')
7894 {
7895 /* Skip parens and braces. Position the cursor
7896 * over the rightmost paren, so that matching it
7897 * will take us back to the start of the line.
7898 */ /* XXX */
7899 trypos = NULL;
7900 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007901 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007902 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903
7904 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007905 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906
7907 if (trypos != NULL)
7908 {
7909 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007910 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 continue;
7912 }
7913 }
7914
7915 /* it's a variable declaration, add indentation
7916 * like in
7917 * int a,
7918 * b;
7919 */
7920 if (cont_amount > 0)
7921 amount = cont_amount;
7922 else
7923 amount += ind_continuation;
7924 }
7925 else if (lookfor == LOOKFOR_UNTERM)
7926 {
7927 if (cont_amount > 0)
7928 amount = cont_amount;
7929 else
7930 amount += ind_continuation;
7931 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02007932 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007933 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02007934 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01007935 && lookfor != LOOKFOR_CPP_BASECLASS
7936 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02007937 {
7938 amount = scope_amount;
7939 if (theline[0] == '{')
7940 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007941 amount += curbuf->b_ind_open_extra;
7942 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007943 }
7944 }
7945
7946 if (lookfor_cpp_namespace)
7947 {
7948 /*
7949 * Looking for C++ namespace, need to look further
7950 * back.
7951 */
7952 if (curwin->w_cursor.lnum == ourscope)
7953 continue;
7954
7955 if (curwin->w_cursor.lnum == 0
7956 || curwin->w_cursor.lnum
7957 < ourscope - FIND_NAMESPACE_LIM)
7958 break;
7959
7960 l = ml_get_curline();
7961
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007962 /* If we're in a comment or raw string now, skip
7963 * to the start of it. */
7964 trypos = ind_find_start_CORS();
Bram Moolenaare79d1532011-10-04 18:03:47 +02007965 if (trypos != NULL)
7966 {
7967 curwin->w_cursor.lnum = trypos->lnum + 1;
7968 curwin->w_cursor.col = 0;
7969 continue;
7970 }
7971
7972 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007973 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
7974 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02007975 continue;
7976
7977 /* Finally the actual check for "namespace". */
7978 if (cin_is_cpp_namespace(l))
7979 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007980 amount += curbuf->b_ind_cpp_namespace
7981 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007982 break;
7983 }
7984
7985 if (cin_nocode(l))
7986 continue;
7987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 }
7989 break;
7990 }
7991
7992 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007993 * If we're in a comment or raw string now, skip to the start
7994 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007996 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 {
7998 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007999 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 continue;
8001 }
8002
8003 l = ml_get_curline();
8004
8005 /*
8006 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008007 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008009 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 if (iscase || cin_isscopedecl(l))
8011 {
8012 /* we are only looking for cpp base class
8013 * declaration/initialization any longer */
8014 if (lookfor == LOOKFOR_CPP_BASECLASS)
8015 break;
8016
8017 /* When looking for a "do" we are not interested in
8018 * labels. */
8019 if (whilelevel > 0)
8020 continue;
8021
8022 /*
8023 * case xx:
8024 * c = 99 + <- this indent plus continuation
8025 *-> here;
8026 */
8027 if (lookfor == LOOKFOR_UNTERM
8028 || lookfor == LOOKFOR_ENUM_OR_INIT)
8029 {
8030 if (cont_amount > 0)
8031 amount = cont_amount;
8032 else
8033 amount += ind_continuation;
8034 break;
8035 }
8036
8037 /*
8038 * case xx: <- line up with this case
8039 * x = 333;
8040 * case yy:
8041 */
8042 if ( (iscase && lookfor == LOOKFOR_CASE)
8043 || (iscase && lookfor_break)
8044 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8045 {
8046 /*
8047 * Check that this case label is not for another
8048 * switch()
8049 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008050 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008051 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 {
8053 amount = get_indent(); /* XXX */
8054 break;
8055 }
8056 continue;
8057 }
8058
8059 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8060
8061 /*
8062 * case xx: if (cond) <- line up with this if
8063 * y = y + 1;
8064 * -> s = 99;
8065 *
8066 * case xx:
8067 * if (cond) <- line up with this line
8068 * y = y + 1;
8069 * -> s = 99;
8070 */
8071 if (lookfor == LOOKFOR_TERM)
8072 {
8073 if (n)
8074 amount = n;
8075
8076 if (!lookfor_break)
8077 break;
8078 }
8079
8080 /*
8081 * case xx: x = x + 1; <- line up with this x
8082 * -> y = y + 1;
8083 *
8084 * case xx: if (cond) <- line up with this if
8085 * -> y = y + 1;
8086 */
8087 if (n)
8088 {
8089 amount = n;
8090 l = after_label(ml_get_curline());
8091 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008092 {
8093 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008094 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008095 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008096 amount += curbuf->b_ind_level
8097 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 break;
8100 }
8101
8102 /*
8103 * Try to get the indent of a statement before the switch
8104 * label. If nothing is found, line up relative to the
8105 * switch label.
8106 * break; <- may line up with this line
8107 * case xx:
8108 * -> y = 1;
8109 */
8110 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008111 ? curbuf->b_ind_case_code
8112 : curbuf->b_ind_scopedecl_code);
8113 lookfor = curbuf->b_ind_case_break
8114 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 continue;
8116 }
8117
8118 /*
8119 * Looking for a switch() label or C++ scope declaration,
8120 * ignore other lines, skip {}-blocks.
8121 */
8122 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8123 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008124 if (find_last_paren(l, '{', '}')
8125 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008126 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008128 curwin->w_cursor.col = 0;
8129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 continue;
8131 }
8132
8133 /*
8134 * Ignore jump labels with nothing after them.
8135 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008136 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 {
8138 l = after_label(ml_get_curline());
8139 if (l == NULL || cin_nocode(l))
8140 continue;
8141 }
8142
8143 /*
8144 * Ignore #defines, #if, etc.
8145 * Ignore comment and empty lines.
8146 * (need to get the line again, cin_islabel() may have
8147 * unlocked it)
8148 */
8149 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008150 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 || cin_nocode(l))
8152 continue;
8153
8154 /*
8155 * Are we at the start of a cpp base class declaration or
8156 * constructor initialization?
8157 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008158 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008159 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008160 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008161 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008162 l = ml_get_curline();
8163 }
8164 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {
8166 if (lookfor == LOOKFOR_UNTERM)
8167 {
8168 if (cont_amount > 0)
8169 amount = cont_amount;
8170 else
8171 amount += ind_continuation;
8172 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008173 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008175 /* Need to find start of the declaration. */
8176 lookfor = LOOKFOR_UNTERM;
8177 ind_continuation = 0;
8178 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 }
8180 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008181 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008182 amount = get_baseclass_amount(
8183 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 break;
8185 }
8186 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8187 {
8188 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008189 * declaration or initialization before the opening brace.
8190 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 if (cin_isterminated(l, TRUE, FALSE))
8192 break;
8193 else
8194 continue;
8195 }
8196
8197 /*
8198 * What happens next depends on the line being terminated.
8199 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008200 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 * 123,
8202 * sizeof
8203 * here
8204 * Otherwise check whether it is a enumeration or structure
8205 * initialisation (not indented) or a variable declaration
8206 * (indented).
8207 */
8208 terminated = cin_isterminated(l, FALSE, TRUE);
8209
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008210 if (js_cur_has_key)
8211 {
8212 js_cur_has_key = 0; /* only check the first line */
8213 if (curbuf->b_ind_js && terminated == ',')
8214 {
8215 /* For Javascript we might be inside an object:
8216 * key: something, <- align with this
8217 * key: something
8218 * or:
8219 * key: something + <- align with this
8220 * something,
8221 * key: something
8222 */
8223 lookfor = LOOKFOR_JS_KEY;
8224 }
8225 }
8226 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8227 {
8228 amount = get_indent();
8229 break;
8230 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008231 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008232 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008233 if (tryposBrace != NULL && tryposBrace->lnum
8234 >= curwin->w_cursor.lnum)
8235 break;
8236 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008237 /* line below current line is the one that starts a
8238 * (possibly broken) line ending in a comma. */
8239 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008240 else
8241 {
8242 amount = get_indent();
8243 if (curwin->w_cursor.lnum - 1 == ourscope)
8244 /* line above is start of the scope, thus current
8245 * line is the one that stars a (possibly broken)
8246 * line ending in a comma. */
8247 break;
8248 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008249 }
8250
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8252 && terminated == ','))
8253 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008254 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8255 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008256 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 /*
8258 * if we're in the middle of a paren thing,
8259 * go back to the line that starts it so
8260 * we can get the right prevailing indent
8261 * if ( foo &&
8262 * bar )
8263 */
8264 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008265 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008267 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 */
8269 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008270 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008271 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8272 || (trypos->lnum == tryposBrace->lnum
8273 && trypos->col < tryposBrace->col)))
8274 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275
8276 /*
8277 * If we are looking for ',', we also look for matching
8278 * braces.
8279 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008280 if (trypos == NULL && terminated == ','
8281 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008282 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283
8284 if (trypos != NULL)
8285 {
8286 /*
8287 * Check if we are on a case label now. This is
8288 * handled above.
8289 * case xx: if ( asdf &&
8290 * asdf)
8291 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008292 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008294 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 {
8296 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008297 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 continue;
8299 }
8300 }
8301
8302 /*
8303 * Skip over continuation lines to find the one to get the
8304 * indent from
8305 * char *usethis = "bla\
8306 * bla",
8307 * here;
8308 */
8309 if (terminated == ',')
8310 {
8311 while (curwin->w_cursor.lnum > 1)
8312 {
8313 l = ml_get(curwin->w_cursor.lnum - 1);
8314 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8315 break;
8316 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008317 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 }
8319 }
8320
8321 /*
8322 * Get indent and pointer to text for current line,
8323 * ignoring any jump label. XXX
8324 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008325 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008326 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008327 else
8328 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 /*
8330 * If this is just above the line we are indenting, and it
8331 * starts with a '{', line it up with this line.
8332 * while (not)
8333 * -> {
8334 * }
8335 */
8336 if (terminated != ',' && lookfor != LOOKFOR_TERM
8337 && theline[0] == '{')
8338 {
8339 amount = cur_amount;
8340 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008341 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 * doesn't start with a '{', which must have a match
8343 * in the same line (scope is the same). Probably:
8344 * { 1, 2 },
8345 * -> { 3, 4 }
8346 */
8347 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008348 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008350 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 {
8352 /* have to look back, whether it is a cpp base
8353 * class declaration or initialization */
8354 lookfor = LOOKFOR_CPP_BASECLASS;
8355 continue;
8356 }
8357 break;
8358 }
8359
8360 /*
8361 * Check if we are after an "if", "while", etc.
8362 * Also allow " } else".
8363 */
8364 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8365 {
8366 /*
8367 * Found an unterminated line after an if (), line up
8368 * with the last one.
8369 * if (cond)
8370 * 100 +
8371 * -> here;
8372 */
8373 if (lookfor == LOOKFOR_UNTERM
8374 || lookfor == LOOKFOR_ENUM_OR_INIT)
8375 {
8376 if (cont_amount > 0)
8377 amount = cont_amount;
8378 else
8379 amount += ind_continuation;
8380 break;
8381 }
8382
8383 /*
8384 * If this is just above the line we are indenting, we
8385 * are finished.
8386 * while (not)
8387 * -> here;
8388 * Otherwise this indent can be used when the line
8389 * before this is terminated.
8390 * yyy;
8391 * if (stat)
8392 * while (not)
8393 * xxx;
8394 * -> here;
8395 */
8396 amount = cur_amount;
8397 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008398 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 if (lookfor != LOOKFOR_TERM)
8400 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008401 amount += curbuf->b_ind_level
8402 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 break;
8404 }
8405
8406 /*
8407 * Special trick: when expecting the while () after a
8408 * do, line up with the while()
8409 * do
8410 * x = 1;
8411 * -> here
8412 */
8413 l = skipwhite(ml_get_curline());
8414 if (cin_isdo(l))
8415 {
8416 if (whilelevel == 0)
8417 break;
8418 --whilelevel;
8419 }
8420
8421 /*
8422 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008423 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 * Need to use the scope of this "else". XXX
8425 * If whilelevel != 0 continue looking for a "do {".
8426 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008427 if (cin_iselse(l) && whilelevel == 0)
8428 {
8429 /* If we're looking at "} else", let's make sure we
8430 * find the opening brace of the enclosing scope,
8431 * not the one from "if () {". */
8432 if (*l == '}')
8433 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008434 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008435
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008436 if ((trypos = find_start_brace()) == NULL
8437 || find_match(LOOKFOR_IF, trypos->lnum)
8438 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008439 break;
8440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 }
8442
8443 /*
8444 * If we're below an unterminated line that is not an
8445 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008446 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 * the line before this one.
8448 */
8449 else
8450 {
8451 /*
8452 * Found two unterminated lines on a row, line up with
8453 * the last one.
8454 * c = 99 +
8455 * 100 +
8456 * -> here;
8457 */
8458 if (lookfor == LOOKFOR_UNTERM)
8459 {
8460 /* When line ends in a comma add extra indent */
8461 if (terminated == ',')
8462 amount += ind_continuation;
8463 break;
8464 }
8465
8466 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8467 {
8468 /* Found two lines ending in ',', lineup with the
8469 * lowest one, but check for cpp base class
8470 * declaration/initialization, if it is an
8471 * opening brace or we are looking just for
8472 * enumerations/initializations. */
8473 if (terminated == ',')
8474 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008475 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 break;
8477
8478 lookfor = LOOKFOR_CPP_BASECLASS;
8479 continue;
8480 }
8481
8482 /* Ignore unterminated lines in between, but
8483 * reduce indent. */
8484 if (amount > cur_amount)
8485 amount = cur_amount;
8486 }
8487 else
8488 {
8489 /*
8490 * Found first unterminated line on a row, may
8491 * line up with this line, remember its indent
8492 * 100 +
8493 * -> here;
8494 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008495 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008497
8498 n = (int)STRLEN(l);
8499 if (terminated == ',' && (*skipwhite(l) == ']'
8500 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008501 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502
8503 /*
8504 * If previous line ends in ',', check whether we
8505 * are in an initialization or enum
8506 * struct xxx =
8507 * {
8508 * sizeof a,
8509 * 124 };
8510 * or a normal possible continuation line.
8511 * but only, of no other statement has been found
8512 * yet.
8513 */
8514 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8515 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008516 if (curbuf->b_ind_js)
8517 {
8518 /* Search for a line ending in a comma
8519 * and line up with the line below it
8520 * (could be the current line).
8521 * some = [
8522 * 1, <- line up here
8523 * 2,
8524 * some = [
8525 * 3 + <- line up here
8526 * 4 *
8527 * 5,
8528 * 6,
8529 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008530 if (cin_iscomment(skipwhite(l)))
8531 break;
8532 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008533 trypos = find_match_char('[',
8534 curbuf->b_ind_maxparen);
8535 if (trypos != NULL)
8536 {
8537 if (trypos->lnum
8538 == curwin->w_cursor.lnum - 1)
8539 {
8540 /* Current line is first inside
8541 * [], line up with it. */
8542 break;
8543 }
8544 ourscope = trypos->lnum;
8545 }
8546 }
8547 else
8548 {
8549 lookfor = LOOKFOR_ENUM_OR_INIT;
8550 cont_amount = cin_first_id_amount();
8551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 }
8553 else
8554 {
8555 if (lookfor == LOOKFOR_INITIAL
8556 && *l != NUL
8557 && l[STRLEN(l) - 1] == '\\')
8558 /* XXX */
8559 cont_amount = cin_get_equal_amount(
8560 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008561 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008562 && lookfor != LOOKFOR_JS_KEY
8563 && lookfor != LOOKFOR_COMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 lookfor = LOOKFOR_UNTERM;
8565 }
8566 }
8567 }
8568 }
8569
8570 /*
8571 * Check if we are after a while (cond);
8572 * If so: Ignore until the matching "do".
8573 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008574 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 {
8576 /*
8577 * Found an unterminated line after a while ();, line up
8578 * with the last one.
8579 * while (cond);
8580 * 100 + <- line up with this one
8581 * -> here;
8582 */
8583 if (lookfor == LOOKFOR_UNTERM
8584 || lookfor == LOOKFOR_ENUM_OR_INIT)
8585 {
8586 if (cont_amount > 0)
8587 amount = cont_amount;
8588 else
8589 amount += ind_continuation;
8590 break;
8591 }
8592
8593 if (whilelevel == 0)
8594 {
8595 lookfor = LOOKFOR_TERM;
8596 amount = get_indent(); /* XXX */
8597 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008598 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 }
8600 ++whilelevel;
8601 }
8602
8603 /*
8604 * We are after a "normal" statement.
8605 * If we had another statement we can stop now and use the
8606 * indent of that other statement.
8607 * Otherwise the indent of the current statement may be used,
8608 * search backwards for the next "normal" statement.
8609 */
8610 else
8611 {
8612 /*
8613 * Skip single break line, if before a switch label. It
8614 * may be lined up with the case label.
8615 */
8616 if (lookfor == LOOKFOR_NOBREAK
8617 && cin_isbreak(skipwhite(ml_get_curline())))
8618 {
8619 lookfor = LOOKFOR_ANY;
8620 continue;
8621 }
8622
8623 /*
8624 * Handle "do {" line.
8625 */
8626 if (whilelevel > 0)
8627 {
8628 l = cin_skipcomment(ml_get_curline());
8629 if (cin_isdo(l))
8630 {
8631 amount = get_indent(); /* XXX */
8632 --whilelevel;
8633 continue;
8634 }
8635 }
8636
8637 /*
8638 * Found a terminated line above an unterminated line. Add
8639 * the amount for a continuation line.
8640 * x = 1;
8641 * y = foo +
8642 * -> here;
8643 * or
8644 * int x = 1;
8645 * int foo,
8646 * -> here;
8647 */
8648 if (lookfor == LOOKFOR_UNTERM
8649 || lookfor == LOOKFOR_ENUM_OR_INIT)
8650 {
8651 if (cont_amount > 0)
8652 amount = cont_amount;
8653 else
8654 amount += ind_continuation;
8655 break;
8656 }
8657
8658 /*
8659 * Found a terminated line above a terminated line or "if"
8660 * etc. line. Use the amount of the line below us.
8661 * x = 1; x = 1;
8662 * if (asdf) y = 2;
8663 * while (asdf) ->here;
8664 * here;
8665 * ->foo;
8666 */
8667 if (lookfor == LOOKFOR_TERM)
8668 {
8669 if (!lookfor_break && whilelevel == 0)
8670 break;
8671 }
8672
8673 /*
8674 * First line above the one we're indenting is terminated.
8675 * To know what needs to be done look further backward for
8676 * a terminated line.
8677 */
8678 else
8679 {
8680 /*
8681 * position the cursor over the rightmost paren, so
8682 * that matching it will take us back to the start of
8683 * the line. Helps for:
8684 * func(asdr,
8685 * asdfasdf);
8686 * here;
8687 */
8688term_again:
8689 l = ml_get_curline();
8690 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008691 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008692 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 {
8694 /*
8695 * Check if we are on a case label now. This is
8696 * handled above.
8697 * case xx: if ( asdf &&
8698 * asdf)
8699 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008700 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008702 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 {
8704 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008705 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 continue;
8707 }
8708 }
8709
8710 /* When aligning with the case statement, don't align
8711 * with a statement after it.
8712 * case 1: { <-- don't use this { position
8713 * stat;
8714 * }
8715 * case 2:
8716 * stat;
8717 * }
8718 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008719 iscase = (curbuf->b_ind_keep_case_label
8720 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721
8722 /*
8723 * Get indent and pointer to text for current line,
8724 * ignoring any jump label.
8725 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008726 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727
8728 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008729 amount += curbuf->b_ind_open_extra;
8730 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008731 l = skipwhite(l);
8732 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008733 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8735
8736 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008737 * When a terminated line starts with "else" skip to
8738 * the matching "if":
8739 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008740 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008741 * Need to use the scope of this "else". XXX
8742 * If whilelevel != 0 continue looking for a "do {".
8743 */
8744 if (lookfor == LOOKFOR_TERM
8745 && *l != '}'
8746 && cin_iselse(l)
8747 && whilelevel == 0)
8748 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008749 if ((trypos = find_start_brace()) == NULL
8750 || find_match(LOOKFOR_IF, trypos->lnum)
8751 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008752 break;
8753 continue;
8754 }
8755
8756 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 * If we're at the end of a block, skip to the start of
8758 * that block.
8759 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008760 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008761 if (find_last_paren(l, '{', '}') /* XXX */
8762 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008764 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 /* if not "else {" check for terminated again */
8766 /* but skip block for "} else {" */
8767 l = cin_skipcomment(ml_get_curline());
8768 if (*l == '}' || !cin_iselse(l))
8769 goto term_again;
8770 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008771 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 }
8773 }
8774 }
8775 }
8776 }
8777 }
8778
8779 /* add extra indent for a comment */
8780 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008781 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008782
8783 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008784 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
8785 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008786
8787 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008789
8790 /*
8791 * ok -- we're not inside any sort of structure at all!
8792 *
8793 * This means we're at the top level, and everything should
8794 * basically just match where the previous line is, except
8795 * for the lines immediately following a function declaration,
8796 * which are K&R-style parameters and need to be indented.
8797 *
8798 * if our line starts with an open brace, forget about any
8799 * prevailing indent and make sure it looks like the start
8800 * of a function
8801 */
8802
8803 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008805 amount = curbuf->b_ind_first_open;
8806 goto theend;
8807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008809 /*
8810 * If the NEXT line is a function declaration, the current
8811 * line needs to be indented as a function type spec.
8812 * Don't do this if the current line looks like a comment or if the
8813 * current line is terminated, ie. ends in ';', or if the current line
8814 * contains { or }: "void f() {\n if (1)"
8815 */
8816 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8817 && !cin_nocode(theline)
8818 && vim_strchr(theline, '{') == NULL
8819 && vim_strchr(theline, '}') == NULL
8820 && !cin_ends_in(theline, (char_u *)":", NULL)
8821 && !cin_ends_in(theline, (char_u *)",", NULL)
8822 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
8823 cur_curpos.lnum + 1)
8824 && !cin_isterminated(theline, FALSE, TRUE))
8825 {
8826 amount = curbuf->b_ind_func_type;
8827 goto theend;
8828 }
8829
8830 /* search backwards until we find something we recognize */
8831 amount = 0;
8832 curwin->w_cursor = cur_curpos;
8833 while (curwin->w_cursor.lnum > 1)
8834 {
8835 curwin->w_cursor.lnum--;
8836 curwin->w_cursor.col = 0;
8837
8838 l = ml_get_curline();
8839
8840 /*
8841 * If we're in a comment or raw string now, skip to the start
8842 * of it.
8843 */ /* XXX */
8844 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008846 curwin->w_cursor.lnum = trypos->lnum + 1;
8847 curwin->w_cursor.col = 0;
8848 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 }
8850
8851 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008852 * Are we at the start of a cpp base class declaration or
8853 * constructor initialization?
8854 */ /* XXX */
8855 n = FALSE;
8856 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008858 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
8859 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008861 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008863 /* XXX */
8864 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
8865 break;
8866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008868 /*
8869 * Skip preprocessor directives and blank lines.
8870 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008871 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008872 continue;
8873
8874 if (cin_nocode(l))
8875 continue;
8876
8877 /*
8878 * If the previous line ends in ',', use one level of
8879 * indentation:
8880 * int foo,
8881 * bar;
8882 * do this before checking for '}' in case of eg.
8883 * enum foobar
8884 * {
8885 * ...
8886 * } foo,
8887 * bar;
8888 */
8889 n = 0;
8890 if (cin_ends_in(l, (char_u *)",", NULL)
8891 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
8892 {
8893 /* take us back to opening paren */
8894 if (find_last_paren(l, '(', ')')
8895 && (trypos = find_match_paren(
8896 curbuf->b_ind_maxparen)) != NULL)
8897 curwin->w_cursor = *trypos;
8898
8899 /* For a line ending in ',' that is a continuation line go
8900 * back to the first line with a backslash:
8901 * char *foo = "bla\
8902 * bla",
8903 * here;
8904 */
8905 while (n == 0 && curwin->w_cursor.lnum > 1)
8906 {
8907 l = ml_get(curwin->w_cursor.lnum - 1);
8908 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8909 break;
8910 --curwin->w_cursor.lnum;
8911 curwin->w_cursor.col = 0;
8912 }
8913
8914 amount = get_indent(); /* XXX */
8915
8916 if (amount == 0)
8917 amount = cin_first_id_amount();
8918 if (amount == 0)
8919 amount = ind_continuation;
8920 break;
8921 }
8922
8923 /*
8924 * If the line looks like a function declaration, and we're
8925 * not in a comment, put it the left margin.
8926 */
8927 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
8928 break;
8929 l = ml_get_curline();
8930
8931 /*
8932 * Finding the closing '}' of a previous function. Put
8933 * current line at the left margin. For when 'cino' has "fs".
8934 */
8935 if (*skipwhite(l) == '}')
8936 break;
8937
8938 /* (matching {)
8939 * If the previous line ends on '};' (maybe followed by
8940 * comments) align at column 0. For example:
8941 * char *string_array[] = { "foo",
8942 * / * x * / "b};ar" }; / * foobar * /
8943 */
8944 if (cin_ends_in(l, (char_u *)"};", NULL))
8945 break;
8946
8947 /*
8948 * If the previous line ends on '[' we are probably in an
8949 * array constant:
8950 * something = [
8951 * 234, <- extra indent
8952 */
8953 if (cin_ends_in(l, (char_u *)"[", NULL))
8954 {
8955 amount = get_indent() + ind_continuation;
8956 break;
8957 }
8958
8959 /*
8960 * Find a line only has a semicolon that belongs to a previous
8961 * line ending in '}', e.g. before an #endif. Don't increase
8962 * indent then.
8963 */
8964 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
8965 {
8966 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967
8968 while (curwin->w_cursor.lnum > 1)
8969 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008970 look = ml_get(--curwin->w_cursor.lnum);
8971 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008972 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008974 }
8975 if (curwin->w_cursor.lnum > 0
8976 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008979 curwin->w_cursor = curpos_save;
8980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008982 /*
8983 * If the PREVIOUS line is a function declaration, the current
8984 * line (and the ones that follow) needs to be indented as
8985 * parameters.
8986 */
8987 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
8988 {
8989 amount = curbuf->b_ind_param;
8990 break;
8991 }
8992
8993 /*
8994 * If the previous line ends in ';' and the line before the
8995 * previous line ends in ',' or '\', ident to column zero:
8996 * int foo,
8997 * bar;
8998 * indent_to_0 here;
8999 */
9000 if (cin_ends_in(l, (char_u *)";", NULL))
9001 {
9002 l = ml_get(curwin->w_cursor.lnum - 1);
9003 if (cin_ends_in(l, (char_u *)",", NULL)
9004 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9005 break;
9006 l = ml_get_curline();
9007 }
9008
9009 /*
9010 * Doesn't look like anything interesting -- so just
9011 * use the indent of this line.
9012 *
9013 * Position the cursor over the rightmost paren, so that
9014 * matching it will take us back to the start of the line.
9015 */
9016 find_last_paren(l, '(', ')');
9017
9018 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9019 curwin->w_cursor = *trypos;
9020 amount = get_indent(); /* XXX */
9021 break;
9022 }
9023
9024 /* add extra indent for a comment */
9025 if (cin_iscomment(theline))
9026 amount += curbuf->b_ind_comment;
9027
9028 /* add extra indent if the previous line ended in a backslash:
9029 * "asdfasdf\
9030 * here";
9031 * char *foo = "asdf\
9032 * here";
9033 */
9034 if (cur_curpos.lnum > 1)
9035 {
9036 l = ml_get(cur_curpos.lnum - 1);
9037 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9038 {
9039 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9040 if (cur_amount > 0)
9041 amount = cur_amount;
9042 else if (cur_amount == 0)
9043 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044 }
9045 }
9046
9047theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009048 if (amount < 0)
9049 amount = 0;
9050
9051laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 /* put the cursor back where it belongs */
9053 curwin->w_cursor = cur_curpos;
9054
9055 vim_free(linecopy);
9056
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 return amount;
9058}
9059
9060 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009061find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062{
9063 char_u *look;
9064 pos_T *theirscope;
9065 char_u *mightbeif;
9066 int elselevel;
9067 int whilelevel;
9068
9069 if (lookfor == LOOKFOR_IF)
9070 {
9071 elselevel = 1;
9072 whilelevel = 0;
9073 }
9074 else
9075 {
9076 elselevel = 0;
9077 whilelevel = 1;
9078 }
9079
9080 curwin->w_cursor.col = 0;
9081
9082 while (curwin->w_cursor.lnum > ourscope + 1)
9083 {
9084 curwin->w_cursor.lnum--;
9085 curwin->w_cursor.col = 0;
9086
9087 look = cin_skipcomment(ml_get_curline());
9088 if (cin_iselse(look)
9089 || cin_isif(look)
9090 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009091 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 {
9093 /*
9094 * if we've gone outside the braces entirely,
9095 * we must be out of scope...
9096 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009097 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098 if (theirscope == NULL)
9099 break;
9100
9101 /*
9102 * and if the brace enclosing this is further
9103 * back than the one enclosing the else, we're
9104 * out of luck too.
9105 */
9106 if (theirscope->lnum < ourscope)
9107 break;
9108
9109 /*
9110 * and if they're enclosed in a *deeper* brace,
9111 * then we can ignore it because it's in a
9112 * different scope...
9113 */
9114 if (theirscope->lnum > ourscope)
9115 continue;
9116
9117 /*
9118 * if it was an "else" (that's not an "else if")
9119 * then we need to go back to another if, so
9120 * increment elselevel
9121 */
9122 look = cin_skipcomment(ml_get_curline());
9123 if (cin_iselse(look))
9124 {
9125 mightbeif = cin_skipcomment(look + 4);
9126 if (!cin_isif(mightbeif))
9127 ++elselevel;
9128 continue;
9129 }
9130
9131 /*
9132 * if it was a "while" then we need to go back to
9133 * another "do", so increment whilelevel. XXX
9134 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009135 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 {
9137 ++whilelevel;
9138 continue;
9139 }
9140
9141 /* If it's an "if" decrement elselevel */
9142 look = cin_skipcomment(ml_get_curline());
9143 if (cin_isif(look))
9144 {
9145 elselevel--;
9146 /*
9147 * When looking for an "if" ignore "while"s that
9148 * get in the way.
9149 */
9150 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9151 whilelevel = 0;
9152 }
9153
9154 /* If it's a "do" decrement whilelevel */
9155 if (cin_isdo(look))
9156 whilelevel--;
9157
9158 /*
9159 * if we've used up all the elses, then
9160 * this must be the if that we want!
9161 * match the indent level of that if.
9162 */
9163 if (elselevel <= 0 && whilelevel <= 0)
9164 {
9165 return OK;
9166 }
9167 }
9168 }
9169 return FAIL;
9170}
9171
9172# if defined(FEAT_EVAL) || defined(PROTO)
9173/*
9174 * Get indent level from 'indentexpr'.
9175 */
9176 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009177get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178{
9179 int indent;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009180 pos_T save_pos;
9181 colnr_T save_curswant;
9182 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009184 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9185 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009187 /* Save and restore cursor position and curswant, in case it was changed
9188 * via :normal commands */
9189 save_pos = curwin->w_cursor;
9190 save_curswant = curwin->w_curswant;
9191 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009193 if (use_sandbox)
9194 ++sandbox;
9195 ++textlock;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02009196 indent = (int)eval_to_number(curbuf->b_p_inde);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009197 if (use_sandbox)
9198 --sandbox;
9199 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200
9201 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9202 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9203 * command. */
9204 save_State = State;
9205 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009206 curwin->w_cursor = save_pos;
9207 curwin->w_curswant = save_curswant;
9208 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 check_cursor();
9210 State = save_State;
9211
9212 /* If there is an error, just keep the current indent. */
9213 if (indent < 0)
9214 indent = get_indent();
9215
9216 return indent;
9217}
9218# endif
9219
9220#endif /* FEAT_CINDENT */
9221
9222#if defined(FEAT_LISP) || defined(PROTO)
9223
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009224static int lisp_match(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225
9226 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009227lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228{
9229 char_u buf[LSIZE];
9230 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009231 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232
9233 while (*word != NUL)
9234 {
9235 (void)copy_option_part(&word, buf, LSIZE, ",");
9236 len = (int)STRLEN(buf);
9237 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9238 return TRUE;
9239 }
9240 return FALSE;
9241}
9242
9243/*
9244 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9245 * The incompatible newer method is quite a bit better at indenting
9246 * code in lisp-like languages than the traditional one; it's still
9247 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9248 *
9249 * TODO:
9250 * Findmatch() should be adapted for lisp, also to make showmatch
9251 * work correctly: now (v5.3) it seems all C/C++ oriented:
9252 * - it does not recognize the #\( and #\) notations as character literals
9253 * - it doesn't know about comments starting with a semicolon
9254 * - it incorrectly interprets '(' as a character literal
9255 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009256 * Update from Sergey Khorev:
9257 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 */
9259 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009260get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009262 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 int amount;
9264 char_u *that;
9265 colnr_T col;
9266 colnr_T firsttry;
9267 int parencount, quotecount;
9268 int vi_lisp;
9269
9270 /* Set vi_lisp to use the vi-compatible method */
9271 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9272
9273 realpos = curwin->w_cursor;
9274 curwin->w_cursor.col = 0;
9275
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009276 if ((pos = findmatch(NULL, '(')) == NULL)
9277 pos = findmatch(NULL, '[');
9278 else
9279 {
9280 paren = *pos;
9281 pos = findmatch(NULL, '[');
9282 if (pos == NULL || ltp(pos, &paren))
9283 pos = &paren;
9284 }
9285 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 {
9287 /* Extra trick: Take the indent of the first previous non-white
9288 * line that is at the same () level. */
9289 amount = -1;
9290 parencount = 0;
9291
9292 while (--curwin->w_cursor.lnum >= pos->lnum)
9293 {
9294 if (linewhite(curwin->w_cursor.lnum))
9295 continue;
9296 for (that = ml_get_curline(); *that != NUL; ++that)
9297 {
9298 if (*that == ';')
9299 {
9300 while (*(that + 1) != NUL)
9301 ++that;
9302 continue;
9303 }
9304 if (*that == '\\')
9305 {
9306 if (*(that + 1) != NUL)
9307 ++that;
9308 continue;
9309 }
9310 if (*that == '"' && *(that + 1) != NUL)
9311 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009312 while (*++that && *that != '"')
9313 {
9314 /* skipping escaped characters in the string */
9315 if (*that == '\\')
9316 {
9317 if (*++that == NUL)
9318 break;
9319 if (that[1] == NUL)
9320 {
9321 ++that;
9322 break;
9323 }
9324 }
9325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009327 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009329 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 --parencount;
9331 }
9332 if (parencount == 0)
9333 {
9334 amount = get_indent();
9335 break;
9336 }
9337 }
9338
9339 if (amount == -1)
9340 {
9341 curwin->w_cursor.lnum = pos->lnum;
9342 curwin->w_cursor.col = pos->col;
9343 col = pos->col;
9344
9345 that = ml_get_curline();
9346
9347 if (vi_lisp && get_indent() == 0)
9348 amount = 2;
9349 else
9350 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009351 char_u *line = that;
9352
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 amount = 0;
9354 while (*that && col)
9355 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009356 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 col--;
9358 }
9359
9360 /*
9361 * Some keywords require "body" indenting rules (the
9362 * non-standard-lisp ones are Scheme special forms):
9363 *
9364 * (let ((a 1)) instead (let ((a 1))
9365 * (...)) of (...))
9366 */
9367
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009368 if (!vi_lisp && (*that == '(' || *that == '[')
9369 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 amount += 2;
9371 else
9372 {
9373 that++;
9374 amount++;
9375 firsttry = amount;
9376
9377 while (vim_iswhite(*that))
9378 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009379 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 ++that;
9381 }
9382
9383 if (*that && *that != ';') /* not a comment line */
9384 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009385 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009387 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 firsttry++;
9389
9390 parencount = 0;
9391 quotecount = 0;
9392
9393 if (vi_lisp
9394 || (*that != '"'
9395 && *that != '\''
9396 && *that != '#'
9397 && (*that < '0' || *that > '9')))
9398 {
9399 while (*that
9400 && (!vim_iswhite(*that)
9401 || quotecount
9402 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009403 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 && !quotecount
9405 && !parencount
9406 && vi_lisp)))
9407 {
9408 if (*that == '"')
9409 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009410 if ((*that == '(' || *that == '[')
9411 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009413 if ((*that == ')' || *that == ']')
9414 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 --parencount;
9416 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009417 amount += lbr_chartabsize_adv(
9418 line, &that, (colnr_T)amount);
9419 amount += lbr_chartabsize_adv(
9420 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421 }
9422 }
9423 while (vim_iswhite(*that))
9424 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009425 amount += lbr_chartabsize(
9426 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427 that++;
9428 }
9429 if (!*that || *that == ';')
9430 amount = firsttry;
9431 }
9432 }
9433 }
9434 }
9435 }
9436 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009437 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438
9439 curwin->w_cursor = realpos;
9440
9441 return amount;
9442}
9443#endif /* FEAT_LISP */
9444
9445 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009446prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009448#if defined(SIGHUP) && defined(SIG_IGN)
9449 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9450 * makes Vim exit and then handling SIGHUP causes various reentrance
9451 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009452 signal(SIGHUP, SIG_IGN);
9453#endif
9454
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455#ifdef FEAT_GUI
9456 if (gui.in_use)
9457 {
9458 gui.dying = TRUE;
9459 out_trash(); /* trash any pending output */
9460 }
9461 else
9462#endif
9463 {
9464 windgoto((int)Rows - 1, 0);
9465
9466 /*
9467 * Switch terminal mode back now, so messages end up on the "normal"
9468 * screen (if there are two screens).
9469 */
9470 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009471 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 out_flush();
9473 }
9474}
9475
9476/*
9477 * Preserve files and exit.
9478 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009479 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9480 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 */
9482 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009483preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484{
9485 buf_T *buf;
9486
9487 prepare_to_exit();
9488
Bram Moolenaar4770d092006-01-12 23:22:24 +00009489 /* Setting this will prevent free() calls. That avoids calling free()
9490 * recursively when free() was invoked with a bad pointer. */
9491 really_exiting = TRUE;
9492
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 out_str(IObuff);
9494 screen_start(); /* don't know where cursor is now */
9495 out_flush();
9496
9497 ml_close_notmod(); /* close all not-modified buffers */
9498
Bram Moolenaar29323592016-07-24 22:04:11 +02009499 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 {
9501 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9502 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009503 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 screen_start(); /* don't know where cursor is now */
9505 out_flush();
9506 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9507 break;
9508 }
9509 }
9510
9511 ml_close_all(FALSE); /* close all memfiles, without deleting */
9512
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009513 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514
9515 getout(1);
9516}
9517
9518/*
9519 * return TRUE if "fname" exists.
9520 */
9521 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009522vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009524 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525
9526 if (mch_stat((char *)fname, &st))
9527 return FALSE;
9528 return TRUE;
9529}
9530
9531/*
9532 * Check for CTRL-C pressed, but only once in a while.
9533 * Should be used instead of ui_breakcheck() for functions that check for
9534 * each line in the file. Calling ui_breakcheck() each time takes too much
9535 * time, because it can be a system call.
9536 */
9537
9538#ifndef BREAKCHECK_SKIP
9539# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9540# define BREAKCHECK_SKIP 200
9541# else
9542# define BREAKCHECK_SKIP 32
9543# endif
9544#endif
9545
9546static int breakcheck_count = 0;
9547
9548 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009549line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550{
9551 if (++breakcheck_count >= BREAKCHECK_SKIP)
9552 {
9553 breakcheck_count = 0;
9554 ui_breakcheck();
9555 }
9556}
9557
9558/*
9559 * Like line_breakcheck() but check 10 times less often.
9560 */
9561 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009562fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563{
9564 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9565 {
9566 breakcheck_count = 0;
9567 ui_breakcheck();
9568 }
9569}
9570
9571/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009572 * Invoke expand_wildcards() for one pattern.
9573 * Expand items like "%:h" before the expansion.
9574 * Returns OK or FAIL.
9575 */
9576 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009577expand_wildcards_eval(
9578 char_u **pat, /* pointer to input pattern */
9579 int *num_file, /* resulting number of files */
9580 char_u ***file, /* array of resulting files */
9581 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009582{
9583 int ret = FAIL;
9584 char_u *eval_pat = NULL;
9585 char_u *exp_pat = *pat;
9586 char_u *ignored_msg;
9587 int usedlen;
9588
9589 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9590 {
9591 ++emsg_off;
9592 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9593 NULL, &ignored_msg, NULL);
9594 --emsg_off;
9595 if (eval_pat != NULL)
9596 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9597 }
9598
9599 if (exp_pat != NULL)
9600 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9601
9602 if (eval_pat != NULL)
9603 {
9604 vim_free(exp_pat);
9605 vim_free(eval_pat);
9606 }
9607
9608 return ret;
9609}
9610
9611/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9613 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009614 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 */
9616 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009617expand_wildcards(
9618 int num_pat, /* number of input patterns */
9619 char_u **pat, /* array of input patterns */
9620 int *num_files, /* resulting number of files */
9621 char_u ***files, /* array of resulting files */
9622 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623{
9624 int retval;
9625 int i, j;
9626 char_u *p;
9627 int non_suf_match; /* number without matching suffix */
9628
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009629 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630
9631 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009632 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 return retval;
9634
9635#ifdef FEAT_WILDIGN
9636 /*
9637 * Remove names that match 'wildignore'.
9638 */
9639 if (*p_wig)
9640 {
9641 char_u *ffname;
9642
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009643 /* check all files in (*files)[] */
9644 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009646 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 if (ffname == NULL) /* out of memory */
9648 break;
9649# ifdef VMS
9650 vms_remove_version(ffname);
9651# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009652 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009654 /* remove this matching files from the list */
9655 vim_free((*files)[i]);
9656 for (j = i; j + 1 < *num_files; ++j)
9657 (*files)[j] = (*files)[j + 1];
9658 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 --i;
9660 }
9661 vim_free(ffname);
9662 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009663
9664 /* If the number of matches is now zero, we fail. */
9665 if (*num_files == 0)
9666 {
9667 vim_free(*files);
9668 *files = NULL;
9669 return FAIL;
9670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 }
9672#endif
9673
9674 /*
9675 * Move the names where 'suffixes' match to the end.
9676 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009677 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 {
9679 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009680 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009682 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 {
9684 /*
9685 * Move the name without matching suffix to the front
9686 * of the list.
9687 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009688 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009690 (*files)[j] = (*files)[j - 1];
9691 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692 }
9693 }
9694 }
9695
9696 return retval;
9697}
9698
9699/*
9700 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9701 */
9702 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009703match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704{
9705 int fnamelen, setsuflen;
9706 char_u *setsuf;
9707#define MAXSUFLEN 30 /* maximum length of a file suffix */
9708 char_u suf_buf[MAXSUFLEN];
9709
9710 fnamelen = (int)STRLEN(fname);
9711 setsuflen = 0;
9712 for (setsuf = p_su; *setsuf; )
9713 {
9714 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009715 if (setsuflen == 0)
9716 {
9717 char_u *tail = gettail(fname);
9718
9719 /* empty entry: match name without a '.' */
9720 if (vim_strchr(tail, '.') == NULL)
9721 {
9722 setsuflen = 1;
9723 break;
9724 }
9725 }
9726 else
9727 {
9728 if (fnamelen >= setsuflen
9729 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9730 (size_t)setsuflen) == 0)
9731 break;
9732 setsuflen = 0;
9733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 }
9735 return (setsuflen != 0);
9736}
9737
9738#if !defined(NO_EXPANDPATH) || defined(PROTO)
9739
9740# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009741static int vim_backtick(char_u *p);
9742static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743# endif
9744
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009745# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746/*
9747 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9748 * it's shared between these systems.
9749 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009750# if defined(PROTO)
9751# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752# else
9753# ifdef __BORLANDC__
9754# define _cdecl _RTLENTRYF
9755# endif
9756# endif
9757
9758/*
9759 * comparison function for qsort in dos_expandpath()
9760 */
9761 static int _cdecl
9762pstrcmp(const void *a, const void *b)
9763{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009764 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765}
9766
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009768 * Recursively expand one path component into all matching files and/or
9769 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 * Return the number of matches found.
9771 * "path" has backslashes before chars that are not to be expanded, starting
9772 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009773 * Return the number of matches found.
9774 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 */
9776 static int
9777dos_expandpath(
9778 garray_T *gap,
9779 char_u *path,
9780 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009781 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009782 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009784 char_u *buf;
9785 char_u *path_end;
9786 char_u *p, *s, *e;
9787 int start_len = gap->ga_len;
9788 char_u *pat;
9789 regmatch_T regmatch;
9790 int starts_with_dot;
9791 int matches;
9792 int len;
9793 int starstar = FALSE;
9794 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 WIN32_FIND_DATA fb;
9796 HANDLE hFind = (HANDLE)0;
9797# ifdef FEAT_MBYTE
9798 WIN32_FIND_DATAW wfb;
9799 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9800# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009802 int ok;
9803
9804 /* Expanding "**" may take a long time, check for CTRL-C. */
9805 if (stardepth > 0)
9806 {
9807 ui_breakcheck();
9808 if (got_int)
9809 return 0;
9810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811
Bram Moolenaar7314efd2015-10-31 15:32:52 +01009812 /* Make room for file name. When doing encoding conversion the actual
9813 * length may be quite a bit longer, thus use the maximum possible length. */
9814 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815 if (buf == NULL)
9816 return 0;
9817
9818 /*
9819 * Find the first part in the path name that contains a wildcard or a ~1.
9820 * Copy it into buf, including the preceding characters.
9821 */
9822 p = buf;
9823 s = buf;
9824 e = NULL;
9825 path_end = path;
9826 while (*path_end != NUL)
9827 {
9828 /* May ignore a wildcard that has a backslash before it; it will
9829 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9830 if (path_end >= path + wildoff && rem_backslash(path_end))
9831 *p++ = *path_end++;
9832 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9833 {
9834 if (e != NULL)
9835 break;
9836 s = p + 1;
9837 }
9838 else if (path_end >= path + wildoff
9839 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9840 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009841# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 if (has_mbyte)
9843 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009844 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 STRNCPY(p, path_end, len);
9846 p += len;
9847 path_end += len;
9848 }
9849 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009850# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851 *p++ = *path_end++;
9852 }
9853 e = p;
9854 *e = NUL;
9855
9856 /* now we have one wildcard component between s and e */
9857 /* Remove backslashes between "wildoff" and the start of the wildcard
9858 * component. */
9859 for (p = buf + wildoff; p < s; ++p)
9860 if (rem_backslash(p))
9861 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009862 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 --e;
9864 --s;
9865 }
9866
Bram Moolenaar231334e2005-07-25 20:46:57 +00009867 /* Check for "**" between "s" and "e". */
9868 for (p = s; p < e; ++p)
9869 if (p[0] == '*' && p[1] == '*')
9870 starstar = TRUE;
9871
Bram Moolenaard82103e2016-01-17 17:04:05 +01009872 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9874 if (pat == NULL)
9875 {
9876 vim_free(buf);
9877 return 0;
9878 }
9879
9880 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009881 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009882 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883 regmatch.rm_ic = TRUE; /* Always ignore case */
9884 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009885 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009886 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 vim_free(pat);
9888
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009889 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 {
9891 vim_free(buf);
9892 return 0;
9893 }
9894
9895 /* remember the pattern or file name being looked for */
9896 matchname = vim_strsave(s);
9897
Bram Moolenaar231334e2005-07-25 20:46:57 +00009898 /* If "**" is by itself, this is the first time we encounter it and more
9899 * is following then find matches without any directory. */
9900 if (!didstar && stardepth < 100 && starstar && e - s == 2
9901 && *path_end == '/')
9902 {
9903 STRCPY(s, path_end + 1);
9904 ++stardepth;
9905 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9906 --stardepth;
9907 }
9908
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 /* Scan all files in the directory with "dir/ *.*" */
9910 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911# ifdef FEAT_MBYTE
9912 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
9913 {
9914 /* The active codepage differs from 'encoding'. Attempt using the
9915 * wide function. If it fails because it is not implemented fall back
9916 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009917 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 if (wn != NULL)
9919 {
9920 hFind = FindFirstFileW(wn, &wfb);
9921 if (hFind == INVALID_HANDLE_VALUE
9922 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
9923 {
9924 vim_free(wn);
9925 wn = NULL;
9926 }
9927 }
9928 }
9929
9930 if (wn == NULL)
9931# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009932 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934
9935 while (ok)
9936 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937# ifdef FEAT_MBYTE
9938 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009939 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940 else
9941# endif
9942 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 /* Ignore entries starting with a dot, unless when asked for. Accept
9944 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +01009945 if ((p[0] != '.' || starts_with_dot
9946 || ((flags & EW_DODOT)
9947 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009949 || (regmatch.regprog != NULL
9950 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009951 || ((flags & EW_NOTWILD)
9952 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009956
9957 if (starstar && stardepth < 100)
9958 {
9959 /* For "**" in the pattern first go deeper in the tree to
9960 * find matches. */
9961 STRCPY(buf + len, "/**");
9962 STRCPY(buf + len + 3, path_end);
9963 ++stardepth;
9964 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
9965 --stardepth;
9966 }
9967
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 STRCPY(buf + len, path_end);
9969 if (mch_has_exp_wildcard(path_end))
9970 {
9971 /* need to expand another component of the path */
9972 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009973 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 }
9975 else
9976 {
9977 /* no more wildcards, check if there is a match */
9978 /* remove backslashes for the remaining components only */
9979 if (*path_end != 0)
9980 backslash_halve(buf + len + 1);
9981 if (mch_getperm(buf) >= 0) /* add existing file */
9982 addfile(gap, buf, flags);
9983 }
9984 }
9985
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986# ifdef FEAT_MBYTE
9987 if (wn != NULL)
9988 {
9989 vim_free(p);
9990 ok = FindNextFileW(hFind, &wfb);
9991 }
9992 else
9993# endif
9994 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995
9996 /* If no more matches and no match was used, try expanding the name
9997 * itself. Finds the long name of a short filename. */
9998 if (!ok && matchname != NULL && gap->ga_len == start_len)
9999 {
10000 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001 FindClose(hFind);
10002# ifdef FEAT_MBYTE
10003 if (wn != NULL)
10004 {
10005 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010006 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 if (wn != NULL)
10008 hFind = FindFirstFileW(wn, &wfb);
10009 }
10010 if (wn == NULL)
10011# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010012 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014 vim_free(matchname);
10015 matchname = NULL;
10016 }
10017 }
10018
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 FindClose(hFind);
10020# ifdef FEAT_MBYTE
10021 vim_free(wn);
10022# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010024 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025 vim_free(matchname);
10026
10027 matches = gap->ga_len - start_len;
10028 if (matches > 0)
10029 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10030 sizeof(char_u *), pstrcmp);
10031 return matches;
10032}
10033
10034 int
10035mch_expandpath(
10036 garray_T *gap,
10037 char_u *path,
10038 int flags) /* EW_* flags */
10039{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010040 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010042# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043
Bram Moolenaar231334e2005-07-25 20:46:57 +000010044#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10045 || defined(PROTO)
10046/*
10047 * Unix style wildcard expansion code.
10048 * It's here because it's used both for Unix and Mac.
10049 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010050static int pstrcmp(const void *, const void *);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010051
10052 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010053pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010054{
10055 return (pathcmp(*(char **)a, *(char **)b, -1));
10056}
10057
10058/*
10059 * Recursively expand one path component into all matching files and/or
10060 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10061 * "path" has backslashes before chars that are not to be expanded, starting
10062 * at "path + wildoff".
10063 * Return the number of matches found.
10064 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10065 */
10066 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010067unix_expandpath(
10068 garray_T *gap,
10069 char_u *path,
10070 int wildoff,
10071 int flags, /* EW_* flags */
10072 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010073{
10074 char_u *buf;
10075 char_u *path_end;
10076 char_u *p, *s, *e;
10077 int start_len = gap->ga_len;
10078 char_u *pat;
10079 regmatch_T regmatch;
10080 int starts_with_dot;
10081 int matches;
10082 int len;
10083 int starstar = FALSE;
10084 static int stardepth = 0; /* depth for "**" expansion */
10085
10086 DIR *dirp;
10087 struct dirent *dp;
10088
10089 /* Expanding "**" may take a long time, check for CTRL-C. */
10090 if (stardepth > 0)
10091 {
10092 ui_breakcheck();
10093 if (got_int)
10094 return 0;
10095 }
10096
10097 /* make room for file name */
10098 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10099 if (buf == NULL)
10100 return 0;
10101
10102 /*
10103 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010104 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010105 * Copy it into "buf", including the preceding characters.
10106 */
10107 p = buf;
10108 s = buf;
10109 e = NULL;
10110 path_end = path;
10111 while (*path_end != NUL)
10112 {
10113 /* May ignore a wildcard that has a backslash before it; it will
10114 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10115 if (path_end >= path + wildoff && rem_backslash(path_end))
10116 *p++ = *path_end++;
10117 else if (*path_end == '/')
10118 {
10119 if (e != NULL)
10120 break;
10121 s = p + 1;
10122 }
10123 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010124 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010125 || (!p_fic && (flags & EW_ICASE)
10126 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010127 e = p;
10128#ifdef FEAT_MBYTE
10129 if (has_mbyte)
10130 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010131 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010132 STRNCPY(p, path_end, len);
10133 p += len;
10134 path_end += len;
10135 }
10136 else
10137#endif
10138 *p++ = *path_end++;
10139 }
10140 e = p;
10141 *e = NUL;
10142
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010143 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010144 /* Remove backslashes between "wildoff" and the start of the wildcard
10145 * component. */
10146 for (p = buf + wildoff; p < s; ++p)
10147 if (rem_backslash(p))
10148 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010149 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010150 --e;
10151 --s;
10152 }
10153
10154 /* Check for "**" between "s" and "e". */
10155 for (p = s; p < e; ++p)
10156 if (p[0] == '*' && p[1] == '*')
10157 starstar = TRUE;
10158
10159 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010160 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010161 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10162 if (pat == NULL)
10163 {
10164 vim_free(buf);
10165 return 0;
10166 }
10167
10168 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010169 if (flags & EW_ICASE)
10170 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10171 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010172 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010173 if (flags & (EW_NOERROR | EW_NOTWILD))
10174 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010175 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010176 if (flags & (EW_NOERROR | EW_NOTWILD))
10177 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010178 vim_free(pat);
10179
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010180 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010181 {
10182 vim_free(buf);
10183 return 0;
10184 }
10185
10186 /* If "**" is by itself, this is the first time we encounter it and more
10187 * is following then find matches without any directory. */
10188 if (!didstar && stardepth < 100 && starstar && e - s == 2
10189 && *path_end == '/')
10190 {
10191 STRCPY(s, path_end + 1);
10192 ++stardepth;
10193 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10194 --stardepth;
10195 }
10196
10197 /* open the directory for scanning */
10198 *s = NUL;
10199 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10200
10201 /* Find all matching entries */
10202 if (dirp != NULL)
10203 {
10204 for (;;)
10205 {
10206 dp = readdir(dirp);
10207 if (dp == NULL)
10208 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010209 if ((dp->d_name[0] != '.' || starts_with_dot
10210 || ((flags & EW_DODOT)
10211 && dp->d_name[1] != NUL
10212 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010213 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10214 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010215 || ((flags & EW_NOTWILD)
10216 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010217 {
10218 STRCPY(s, dp->d_name);
10219 len = STRLEN(buf);
10220
10221 if (starstar && stardepth < 100)
10222 {
10223 /* For "**" in the pattern first go deeper in the tree to
10224 * find matches. */
10225 STRCPY(buf + len, "/**");
10226 STRCPY(buf + len + 3, path_end);
10227 ++stardepth;
10228 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10229 --stardepth;
10230 }
10231
10232 STRCPY(buf + len, path_end);
10233 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10234 {
10235 /* need to expand another component of the path */
10236 /* remove backslashes for the remaining components only */
10237 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10238 }
10239 else
10240 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010241 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010242
Bram Moolenaar231334e2005-07-25 20:46:57 +000010243 /* no more wildcards, check if there is a match */
10244 /* remove backslashes for the remaining components only */
10245 if (*path_end != NUL)
10246 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010247 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010248 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010249 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010250 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010251#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010252 size_t precomp_len = STRLEN(buf)+1;
10253 char_u *precomp_buf =
10254 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010255
Bram Moolenaar231334e2005-07-25 20:46:57 +000010256 if (precomp_buf)
10257 {
10258 mch_memmove(buf, precomp_buf, precomp_len);
10259 vim_free(precomp_buf);
10260 }
10261#endif
10262 addfile(gap, buf, flags);
10263 }
10264 }
10265 }
10266 }
10267
10268 closedir(dirp);
10269 }
10270
10271 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010272 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010273
10274 matches = gap->ga_len - start_len;
10275 if (matches > 0)
10276 qsort(((char_u **)gap->ga_data) + start_len, matches,
10277 sizeof(char_u *), pstrcmp);
10278 return matches;
10279}
10280#endif
10281
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010282#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010283static int find_previous_pathsep(char_u *path, char_u **psep);
10284static int is_unique(char_u *maybe_unique, garray_T *gap, int i);
10285static void expand_path_option(char_u *curdir, garray_T *gap);
10286static char_u *get_path_cutoff(char_u *fname, garray_T *gap);
10287static void uniquefy_paths(garray_T *gap, char_u *pattern);
10288static int expand_in_path(garray_T *gap, char_u *pattern, int flags);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010289
10290/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010291 * Moves "*psep" back to the previous path separator in "path".
10292 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010293 */
10294 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010295find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010296{
10297 /* skip the current separator */
10298 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010299 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010300
10301 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010302 while (*psep > path)
10303 {
10304 if (vim_ispathsep(**psep))
10305 return OK;
10306 mb_ptr_back(path, *psep);
10307 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010308
10309 return FAIL;
10310}
10311
10312/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010313 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10314 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010315 */
10316 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010317is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010318{
10319 int j;
10320 int candidate_len;
10321 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010322 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010323 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010324
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010325 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010326 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010327 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010328 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010329
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010330 candidate_len = (int)STRLEN(maybe_unique);
10331 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010332 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010333 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010334
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010335 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010336 if (fnamecmp(maybe_unique, rival) == 0
10337 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010338 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010339 }
10340
Bram Moolenaar162bd912010-07-28 22:29:10 +020010341 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010342}
10343
10344/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010345 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010346 * paths are expanded to their equivalent fullpath. This includes the "."
10347 * (relative to current buffer directory) and empty path (relative to current
10348 * directory) notations.
10349 *
10350 * TODO: handle upward search (;) and path limiter (**N) notations by
10351 * expanding each into their equivalent path(s).
10352 */
10353 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010354expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010355{
10356 char_u *path_option = *curbuf->b_p_path == NUL
10357 ? p_path : curbuf->b_p_path;
10358 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010359 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010360 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010361
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010362 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010363 return;
10364
10365 while (*path_option != NUL)
10366 {
10367 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10368
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010369 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010370 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010371 /* Relative to current buffer:
10372 * "/path/file" + "." -> "/path/"
10373 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010374 if (curbuf->b_ffname == NULL)
10375 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010376 p = gettail(curbuf->b_ffname);
10377 len = (int)(p - curbuf->b_ffname);
10378 if (len + (int)STRLEN(buf) >= MAXPATHL)
10379 continue;
10380 if (buf[1] == NUL)
10381 buf[len] = NUL;
10382 else
10383 STRMOVE(buf + len, buf + 2);
10384 mch_memmove(buf, curbuf->b_ffname, len);
10385 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010386 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010387 else if (buf[0] == NUL)
10388 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010389 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010390 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010391 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010392 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010393 else if (!mch_isFullName(buf))
10394 {
10395 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010396 len = (int)STRLEN(curdir);
10397 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010398 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010399 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010400 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010401 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010402 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010403 }
10404
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010405 if (ga_grow(gap, 1) == FAIL)
10406 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010407
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010408# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010409 /* Avoid the path ending in a backslash, it fails when a comma is
10410 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010411 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010412 if (buf[len - 1] == '\\')
10413 buf[len - 1] = '/';
10414# endif
10415
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010416 p = vim_strsave(buf);
10417 if (p == NULL)
10418 break;
10419 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010420 }
10421
10422 vim_free(buf);
10423}
10424
10425/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010426 * Returns a pointer to the file or directory name in "fname" that matches the
10427 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010428 *
10429 * path: /foo/bar/baz
10430 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010431 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010432 */
10433 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010434get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010435{
10436 int i;
10437 int maxlen = 0;
10438 char_u **path_part = (char_u **)gap->ga_data;
10439 char_u *cutoff = NULL;
10440
10441 for (i = 0; i < gap->ga_len; i++)
10442 {
10443 int j = 0;
10444
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010445 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010446# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010447 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10448#endif
10449 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010450 j++;
10451 if (j > maxlen)
10452 {
10453 maxlen = j;
10454 cutoff = &fname[j];
10455 }
10456 }
10457
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010458 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010459 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010460 while (vim_ispathsep(*cutoff))
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010461 mb_ptr_adv(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010462
10463 return cutoff;
10464}
10465
10466/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010467 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10468 * that they are unique with respect to each other while conserving the part
10469 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010470 */
10471 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010472uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010473{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010474 int i;
10475 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010476 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010477 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010478 char_u *pat;
10479 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010480 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010481 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010482 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010483 char_u **in_curdir = NULL;
10484 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010485
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010486 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010487 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010488
10489 /*
10490 * We need to prepend a '*' at the beginning of file_pattern so that the
10491 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010492 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010493 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010494 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010495 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010496 if (file_pattern == NULL)
10497 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010498 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010499 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010500 STRCAT(file_pattern, pattern);
10501 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10502 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010503 if (pat == NULL)
10504 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010505
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010506 regmatch.rm_ic = TRUE; /* always ignore case */
10507 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10508 vim_free(pat);
10509 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010510 return;
10511
Bram Moolenaar162bd912010-07-28 22:29:10 +020010512 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010513 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010514 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010515 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010516
10517 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010518 if (in_curdir == NULL)
10519 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010520
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010521 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010522 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010523 char_u *path = fnames[i];
10524 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010525 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010526 char_u *pathsep_p;
10527 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010528
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010529 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010530 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010531 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010532 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010533 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010534
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010535 /* Shorten the filename while maintaining its uniqueness */
10536 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010537
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010538 /* Don't assume all files can be reached without path when search
10539 * pattern starts with star star slash, so only remove path_cutoff
10540 * when possible. */
10541 if (pattern[0] == '*' && pattern[1] == '*'
10542 && vim_ispathsep_nocolon(pattern[2])
10543 && path_cutoff != NULL
10544 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10545 && is_unique(path_cutoff, gap, i))
10546 {
10547 sort_again = TRUE;
10548 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10549 }
10550 else
10551 {
10552 /* Here all files can be reached without path, so get shortest
10553 * unique path. We start at the end of the path. */
10554 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010555
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010556 while (find_previous_pathsep(path, &pathsep_p))
10557 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10558 && is_unique(pathsep_p + 1, gap, i)
10559 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10560 {
10561 sort_again = TRUE;
10562 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10563 break;
10564 }
10565 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010566
10567 if (mch_isFullName(path))
10568 {
10569 /*
10570 * Last resort: shorten relative to curdir if possible.
10571 * 'possible' means:
10572 * 1. It is under the current directory.
10573 * 2. The result is actually shorter than the original.
10574 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010575 * Before curdir After
10576 * /foo/bar/file.txt /foo/bar ./file.txt
10577 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10578 * /file.txt / /file.txt
10579 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010580 */
10581 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010582 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010583#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010584 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010585 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010586 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010587 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010588 && !vim_ispathsep(*short_name)
10589#endif
10590 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010591 {
10592 STRCPY(path, ".");
10593 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010594 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010595 }
10596 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010597 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010598 }
10599
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010600 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010601 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010602 {
10603 char_u *rel_path;
10604 char_u *path = in_curdir[i];
10605
10606 if (path == NULL)
10607 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010608
10609 /* If the {filename} is not unique, change it to ./{filename}.
10610 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010611 short_name = shorten_fname(path, curdir);
10612 if (short_name == NULL)
10613 short_name = path;
10614 if (is_unique(short_name, gap, i))
10615 {
10616 STRCPY(fnames[i], short_name);
10617 continue;
10618 }
10619
10620 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10621 if (rel_path == NULL)
10622 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010623 STRCPY(rel_path, ".");
10624 add_pathsep(rel_path);
10625 STRCAT(rel_path, short_name);
10626
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010627 vim_free(fnames[i]);
10628 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010629 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010630 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010631 }
10632
Bram Moolenaar162bd912010-07-28 22:29:10 +020010633theend:
10634 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010635 if (in_curdir != NULL)
10636 {
10637 for (i = 0; i < gap->ga_len; i++)
10638 vim_free(in_curdir[i]);
10639 vim_free(in_curdir);
10640 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010641 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010642 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010643
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010644 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010645 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010646}
10647
10648/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010649 * Calls globpath() with 'path' values for the given pattern and stores the
10650 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010651 * Returns the total number of matches.
10652 */
10653 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010654expand_in_path(
10655 garray_T *gap,
10656 char_u *pattern,
10657 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010658{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010659 char_u *curdir;
10660 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010661 char_u *paths = NULL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010662
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010663 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010664 return 0;
10665 mch_dirname(curdir, MAXPATHL);
10666
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010667 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010668 expand_path_option(curdir, &path_ga);
10669 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010670 if (path_ga.ga_len == 0)
10671 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010672
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010673 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010674 ga_clear_strings(&path_ga);
10675 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010676 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010677
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010678 globpath(paths, pattern, gap, (flags & EW_ICASE) ? WILD_ICASE : 0);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010679 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010680
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010681 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010682}
10683#endif
10684
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010685#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10686/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010687 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10688 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010689 */
10690 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010691remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010692{
10693 int i;
10694 int j;
10695 char_u **fnames = (char_u **)gap->ga_data;
10696
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010697 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010698 for (i = gap->ga_len - 1; i > 0; --i)
10699 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10700 {
10701 vim_free(fnames[i]);
10702 for (j = i + 1; j < gap->ga_len; ++j)
10703 fnames[j - 1] = fnames[j];
10704 --gap->ga_len;
10705 }
10706}
10707#endif
10708
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010709static int has_env_var(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010710
10711/*
10712 * Return TRUE if "p" contains what looks like an environment variable.
10713 * Allowing for escaping.
10714 */
10715 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010716has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010717{
10718 for ( ; *p; mb_ptr_adv(p))
10719 {
10720 if (*p == '\\' && p[1] != NUL)
10721 ++p;
10722 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010723#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010724 "$%"
10725#else
10726 "$"
10727#endif
10728 , *p) != NULL)
10729 return TRUE;
10730 }
10731 return FALSE;
10732}
10733
10734#ifdef SPECIAL_WILDCHAR
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010735static int has_special_wildchar(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010736
10737/*
10738 * Return TRUE if "p" contains a special wildcard character.
10739 * Allowing for escaping.
10740 */
10741 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010742has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010743{
10744 for ( ; *p; mb_ptr_adv(p))
10745 {
10746 if (*p == '\\' && p[1] != NUL)
10747 ++p;
10748 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10749 return TRUE;
10750 }
10751 return FALSE;
10752}
10753#endif
10754
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755/*
10756 * Generic wildcard expansion code.
10757 *
10758 * Characters in "pat" that should not be expanded must be preceded with a
10759 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10760 *
10761 * Return FAIL when no single file was found. In this case "num_file" is not
10762 * set, and "file" may contain an error message.
10763 * Return OK when some files found. "num_file" is set to the number of
10764 * matches, "file" to the array of matches. Call FreeWild() later.
10765 */
10766 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010767gen_expand_wildcards(
10768 int num_pat, /* number of input patterns */
10769 char_u **pat, /* array of input patterns */
10770 int *num_file, /* resulting number of files */
10771 char_u ***file, /* array of resulting files */
10772 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773{
10774 int i;
10775 garray_T ga;
10776 char_u *p;
10777 static int recursive = FALSE;
10778 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020010779 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010780#if defined(FEAT_SEARCHPATH)
10781 int did_expand_in_path = FALSE;
10782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783
10784 /*
10785 * expand_env() is called to expand things like "~user". If this fails,
10786 * it calls ExpandOne(), which brings us back here. In this case, always
10787 * call the machine specific expansion function, if possible. Otherwise,
10788 * return FAIL.
10789 */
10790 if (recursive)
10791#ifdef SPECIAL_WILDCHAR
10792 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10793#else
10794 return FAIL;
10795#endif
10796
10797#ifdef SPECIAL_WILDCHAR
10798 /*
10799 * If there are any special wildcard characters which we cannot handle
10800 * here, call machine specific function for all the expansion. This
10801 * avoids starting the shell for each argument separately.
10802 * For `=expr` do use the internal function.
10803 */
10804 for (i = 0; i < num_pat; i++)
10805 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010806 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807# ifdef VIM_BACKTICK
10808 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10809# endif
10810 )
10811 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10812 }
10813#endif
10814
10815 recursive = TRUE;
10816
10817 /*
10818 * The matching file names are stored in a growarray. Init it empty.
10819 */
10820 ga_init2(&ga, (int)sizeof(char_u *), 30);
10821
10822 for (i = 0; i < num_pat; ++i)
10823 {
10824 add_pat = -1;
10825 p = pat[i];
10826
10827#ifdef VIM_BACKTICK
10828 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020010829 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010830 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020010831 if (add_pat == -1)
10832 retval = FAIL;
10833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 else
10835#endif
10836 {
10837 /*
10838 * First expand environment variables, "~/" and "~user/".
10839 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010840 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010842 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 if (p == NULL)
10844 p = pat[i];
10845#ifdef UNIX
10846 /*
10847 * On Unix, if expand_env() can't expand an environment
10848 * variable, use the shell to do that. Discard previously
10849 * found file names and start all over again.
10850 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010851 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852 {
10853 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010854 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020010856 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 recursive = FALSE;
10858 return i;
10859 }
10860#endif
10861 }
10862
10863 /*
10864 * If there are wildcards: Expand file names and add each match to
10865 * the list. If there is no match, and EW_NOTFOUND is given, add
10866 * the pattern.
10867 * If there are no wildcards: Add the file name if it exists or
10868 * when EW_NOTFOUND is given.
10869 */
10870 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010871 {
10872#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010873 if ((flags & EW_PATH)
10874 && !mch_isFullName(p)
10875 && !(p[0] == '.'
10876 && (vim_ispathsep(p[1])
10877 || (p[1] == '.' && vim_ispathsep(p[2]))))
10878 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010879 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010880 /* :find completion where 'path' is used.
10881 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010882 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010883 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010884 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010885 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010886 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010887 else
10888#endif
10889 add_pat = mch_expandpath(&ga, p, flags);
10890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891 }
10892
10893 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
10894 {
10895 char_u *t = backslash_halve_save(p);
10896
10897#if defined(MACOS_CLASSIC)
10898 slash_to_colon(t);
10899#endif
10900 /* When EW_NOTFOUND is used, always add files and dirs. Makes
10901 * "vim c:/" work. */
10902 if (flags & EW_NOTFOUND)
10903 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020010904 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 addfile(&ga, t, flags);
10906 vim_free(t);
10907 }
10908
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010909#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010910 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010911 uniquefy_paths(&ga, p);
10912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 if (p != pat[i])
10914 vim_free(p);
10915 }
10916
10917 *num_file = ga.ga_len;
10918 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
10919
10920 recursive = FALSE;
10921
Bram Moolenaar336bd622016-01-17 18:23:58 +010010922 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923}
10924
10925# ifdef VIM_BACKTICK
10926
10927/*
10928 * Return TRUE if we can expand this backtick thing here.
10929 */
10930 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010931vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932{
10933 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
10934}
10935
10936/*
10937 * Expand an item in `backticks` by executing it as a command.
10938 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020010939 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940 */
10941 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010942expand_backtick(
10943 garray_T *gap,
10944 char_u *pat,
10945 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946{
10947 char_u *p;
10948 char_u *cmd;
10949 char_u *buffer;
10950 int cnt = 0;
10951 int i;
10952
10953 /* Create the command: lop off the backticks. */
10954 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
10955 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020010956 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957
10958#ifdef FEAT_EVAL
10959 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000010960 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 else
10962#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010963 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010964 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 vim_free(cmd);
10966 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020010967 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968
10969 cmd = buffer;
10970 while (*cmd != NUL)
10971 {
10972 cmd = skipwhite(cmd); /* skip over white space */
10973 p = cmd;
10974 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
10975 ++p;
10976 /* add an entry if it is not empty */
10977 if (p > cmd)
10978 {
10979 i = *p;
10980 *p = NUL;
10981 addfile(gap, cmd, flags);
10982 *p = i;
10983 ++cnt;
10984 }
10985 cmd = p;
10986 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
10987 ++cmd;
10988 }
10989
10990 vim_free(buffer);
10991 return cnt;
10992}
10993# endif /* VIM_BACKTICK */
10994
10995/*
10996 * Add a file to a file list. Accepted flags:
10997 * EW_DIR add directories
10998 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010999 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000 * EW_NOTFOUND add even when it doesn't exist
11001 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011002 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003 */
11004 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011005addfile(
11006 garray_T *gap,
11007 char_u *f, /* filename */
11008 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009{
11010 char_u *p;
11011 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011012 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011014 /* if the file/dir/link doesn't exist, may not add it */
11015 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011016 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017 return;
11018
11019#ifdef FNAME_ILLEGAL
11020 /* if the file/dir contains illegal characters, don't add it */
11021 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11022 return;
11023#endif
11024
11025 isdir = mch_isdir(f);
11026 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11027 return;
11028
Bram Moolenaarb5971142015-03-21 17:32:19 +010011029 /* If the file isn't executable, may not add it. Do accept directories.
11030 * When invoked from expand_shellcmd() do not use $PATH. */
11031 if (!isdir && (flags & EW_EXEC)
11032 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011033 return;
11034
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035 /* Make room for another item in the file list. */
11036 if (ga_grow(gap, 1) == FAIL)
11037 return;
11038
11039 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11040 if (p == NULL)
11041 return;
11042
11043 STRCPY(p, f);
11044#ifdef BACKSLASH_IN_FILENAME
11045 slash_adjust(p);
11046#endif
11047 /*
11048 * Append a slash or backslash after directory names if none is present.
11049 */
11050#ifndef DONT_ADD_PATHSEP_TO_DIR
11051 if (isdir && (flags & EW_ADDSLASH))
11052 add_pathsep(p);
11053#endif
11054 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055}
11056#endif /* !NO_EXPANDPATH */
11057
11058#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11059
11060#ifndef SEEK_SET
11061# define SEEK_SET 0
11062#endif
11063#ifndef SEEK_END
11064# define SEEK_END 2
11065#endif
11066
11067/*
11068 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011069 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11070 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071 * Returns an allocated string, or NULL for error.
11072 */
11073 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011074get_cmd_output(
11075 char_u *cmd,
11076 char_u *infile, /* optional input file name */
11077 int flags, /* can be SHELL_SILENT */
11078 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079{
11080 char_u *tempname;
11081 char_u *command;
11082 char_u *buffer = NULL;
11083 int len;
11084 int i = 0;
11085 FILE *fd;
11086
11087 if (check_restricted() || check_secure())
11088 return NULL;
11089
11090 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011091 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 {
11093 EMSG(_(e_notmp));
11094 return NULL;
11095 }
11096
11097 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011098 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099 if (command == NULL)
11100 goto done;
11101
11102 /*
11103 * Call the shell to execute the command (errors are ignored).
11104 * Don't check timestamps here.
11105 */
11106 ++no_check_timestamps;
11107 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11108 --no_check_timestamps;
11109
11110 vim_free(command);
11111
11112 /*
11113 * read the names from the file into memory
11114 */
11115# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011116 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117 fd = mch_fopen((char *)tempname, "r");
11118# else
11119 fd = mch_fopen((char *)tempname, READBIN);
11120# endif
11121
11122 if (fd == NULL)
11123 {
11124 EMSG2(_(e_notopen), tempname);
11125 goto done;
11126 }
11127
11128 fseek(fd, 0L, SEEK_END);
11129 len = ftell(fd); /* get size of temp file */
11130 fseek(fd, 0L, SEEK_SET);
11131
11132 buffer = alloc(len + 1);
11133 if (buffer != NULL)
11134 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11135 fclose(fd);
11136 mch_remove(tempname);
11137 if (buffer == NULL)
11138 goto done;
11139#ifdef VMS
11140 len = i; /* VMS doesn't give us what we asked for... */
11141#endif
11142 if (i != len)
11143 {
11144 EMSG2(_(e_notread), tempname);
11145 vim_free(buffer);
11146 buffer = NULL;
11147 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011148 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011149 {
11150 /* Change NUL into SOH, otherwise the string is truncated. */
11151 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011152 if (buffer[i] == NUL)
11153 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011154
Bram Moolenaar162bd912010-07-28 22:29:10 +020011155 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011156 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011157 else
11158 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159
11160done:
11161 vim_free(tempname);
11162 return buffer;
11163}
11164#endif
11165
11166/*
11167 * Free the list of files returned by expand_wildcards() or other expansion
11168 * functions.
11169 */
11170 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011171FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011173 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175 while (count--)
11176 vim_free(files[count]);
11177 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178}
11179
11180/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011181 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011182 * Don't do this when still processing a command or a mapping.
11183 * Don't do this when inside a ":normal" command.
11184 */
11185 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011186goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011187{
11188 return (p_im && stuff_empty() && typebuf_typed());
11189}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011190
11191/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011192 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011193 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11194 * - Remove any argument. E.g., "csh -f" -> "csh".
11195 * But don't allow a space in the path, so that this works:
11196 * "/usr/bin/csh --rcfile ~/.cshrc"
11197 * But don't do that for Windows, it's common to have a space in the path.
11198 */
11199 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011200get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011201{
11202 char_u *p;
11203
11204#ifdef WIN3264
11205 p = gettail(p_sh);
11206 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11207#else
11208 p = skiptowhite(p_sh);
11209 if (*p == NUL)
11210 {
11211 /* No white space, use the tail. */
11212 p = vim_strsave(gettail(p_sh));
11213 }
11214 else
11215 {
11216 char_u *p1, *p2;
11217
11218 /* Find the last path separator before the space. */
11219 p1 = p_sh;
11220 for (p2 = p_sh; p2 < p; mb_ptr_adv(p2))
11221 if (vim_ispathsep(*p2))
11222 p1 = p2 + 1;
11223 p = vim_strnsave(p1, (int)(p - p1));
11224 }
11225#endif
11226 return p;
11227}