blob: b5394a410d35287dc21b7698c92a19637ec8b9fc [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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{
2180#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002181 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 int n;
2183
2184 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';
2190
2191 ins_char_bytes(buf, n);
2192}
2193
2194 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002195ins_char_bytes(char_u *buf, int charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196{
2197 int c = buf[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198#endif
2199 int newlen; /* nr of bytes inserted */
2200 int oldlen; /* nr of bytes deleted (0 when not replacing) */
2201 char_u *p;
2202 char_u *newp;
2203 char_u *oldp;
2204 int linelen; /* length of old line including NUL */
2205 colnr_T col;
2206 linenr_T lnum = curwin->w_cursor.lnum;
2207 int i;
2208
2209#ifdef FEAT_VIRTUALEDIT
2210 /* Break tabs if needed. */
2211 if (virtual_active() && curwin->w_cursor.coladd > 0)
2212 coladvance_force(getviscol());
2213#endif
2214
2215 col = curwin->w_cursor.col;
2216 oldp = ml_get(lnum);
2217 linelen = (int)STRLEN(oldp) + 1;
2218
2219 /* The lengths default to the values for when not replacing. */
2220 oldlen = 0;
2221#ifdef FEAT_MBYTE
2222 newlen = charlen;
2223#else
2224 newlen = 1;
2225#endif
2226
2227 if (State & REPLACE_FLAG)
2228 {
2229#ifdef FEAT_VREPLACE
2230 if (State & VREPLACE_FLAG)
2231 {
2232 colnr_T new_vcol = 0; /* init for GCC */
2233 colnr_T vcol;
2234 int old_list;
2235#ifndef FEAT_MBYTE
2236 char_u buf[2];
2237#endif
2238
2239 /*
2240 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2241 * Returns the old value of list, so when finished,
2242 * curwin->w_p_list should be set back to this.
2243 */
2244 old_list = curwin->w_p_list;
2245 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2246 curwin->w_p_list = FALSE;
2247
2248 /*
2249 * In virtual replace mode each character may replace one or more
2250 * characters (zero if it's a TAB). Count the number of bytes to
2251 * be deleted to make room for the new character, counting screen
2252 * cells. May result in adding spaces to fill a gap.
2253 */
2254 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2255#ifndef FEAT_MBYTE
2256 buf[0] = c;
2257 buf[1] = NUL;
2258#endif
2259 new_vcol = vcol + chartabsize(buf, vcol);
2260 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2261 {
2262 vcol += chartabsize(oldp + col + oldlen, vcol);
2263 /* Don't need to remove a TAB that takes us to the right
2264 * position. */
2265 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2266 break;
2267#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002268 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269#else
2270 ++oldlen;
2271#endif
2272 /* Deleted a bit too much, insert spaces. */
2273 if (vcol > new_vcol)
2274 newlen += vcol - new_vcol;
2275 }
2276 curwin->w_p_list = old_list;
2277 }
2278 else
2279#endif
2280 if (oldp[col] != NUL)
2281 {
2282 /* normal replace */
2283#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002284 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285#else
2286 oldlen = 1;
2287#endif
2288 }
2289
2290
2291 /* Push the replaced bytes onto the replace stack, so that they can be
2292 * put back when BS is used. The bytes of a multi-byte character are
2293 * done the other way around, so that the first byte is popped off
2294 * first (it tells the byte length of the character). */
2295 replace_push(NUL);
2296 for (i = 0; i < oldlen; ++i)
2297 {
2298#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002299 if (has_mbyte)
2300 i += replace_push_mb(oldp + col + i) - 1;
2301 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002303 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 }
2305 }
2306
2307 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2308 if (newp == NULL)
2309 return;
2310
2311 /* Copy bytes before the cursor. */
2312 if (col > 0)
2313 mch_memmove(newp, oldp, (size_t)col);
2314
2315 /* Copy bytes after the changed character(s). */
2316 p = newp + col;
2317 mch_memmove(p + newlen, oldp + col + oldlen,
2318 (size_t)(linelen - col - oldlen));
2319
2320 /* Insert or overwrite the new character. */
2321#ifdef FEAT_MBYTE
2322 mch_memmove(p, buf, charlen);
2323 i = charlen;
2324#else
2325 *p = c;
2326 i = 1;
2327#endif
2328
2329 /* Fill with spaces when necessary. */
2330 while (i < newlen)
2331 p[i++] = ' ';
2332
2333 /* Replace the line in the buffer. */
2334 ml_replace(lnum, newp, FALSE);
2335
2336 /* mark the buffer as changed and prepare for displaying */
2337 changed_bytes(lnum, col);
2338
2339 /*
2340 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2341 * show the match for right parens and braces.
2342 */
2343 if (p_sm && (State & INSERT)
2344 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002345#ifdef FEAT_INS_EXPAND
2346 && !ins_compl_active()
2347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002349 {
2350#ifdef FEAT_MBYTE
2351 if (has_mbyte)
2352 showmatch(mb_ptr2char(buf));
2353 else
2354#endif
2355 showmatch(c);
2356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357
2358#ifdef FEAT_RIGHTLEFT
2359 if (!p_ri || (State & REPLACE_FLAG))
2360#endif
2361 {
2362 /* Normal insert: move cursor right */
2363#ifdef FEAT_MBYTE
2364 curwin->w_cursor.col += charlen;
2365#else
2366 ++curwin->w_cursor.col;
2367#endif
2368 }
2369 /*
2370 * TODO: should try to update w_row here, to avoid recomputing it later.
2371 */
2372}
2373
2374/*
2375 * Insert a string at the cursor position.
2376 * Note: Does NOT handle Replace mode.
2377 * Caller must have prepared for undo.
2378 */
2379 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002380ins_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381{
2382 char_u *oldp, *newp;
2383 int newlen = (int)STRLEN(s);
2384 int oldlen;
2385 colnr_T col;
2386 linenr_T lnum = curwin->w_cursor.lnum;
2387
2388#ifdef FEAT_VIRTUALEDIT
2389 if (virtual_active() && curwin->w_cursor.coladd > 0)
2390 coladvance_force(getviscol());
2391#endif
2392
2393 col = curwin->w_cursor.col;
2394 oldp = ml_get(lnum);
2395 oldlen = (int)STRLEN(oldp);
2396
2397 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2398 if (newp == NULL)
2399 return;
2400 if (col > 0)
2401 mch_memmove(newp, oldp, (size_t)col);
2402 mch_memmove(newp + col, s, (size_t)newlen);
2403 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2404 ml_replace(lnum, newp, FALSE);
2405 changed_bytes(lnum, col);
2406 curwin->w_cursor.col += newlen;
2407}
2408
2409/*
2410 * Delete one character under the cursor.
2411 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2412 * Caller must have prepared for undo.
2413 *
2414 * return FAIL for failure, OK otherwise
2415 */
2416 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002417del_char(int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418{
2419#ifdef FEAT_MBYTE
2420 if (has_mbyte)
2421 {
2422 /* Make sure the cursor is at the start of a character. */
2423 mb_adjust_cursor();
2424 if (*ml_get_cursor() == NUL)
2425 return FAIL;
2426 return del_chars(1L, fixpos);
2427 }
2428#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002429 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430}
2431
2432#if defined(FEAT_MBYTE) || defined(PROTO)
2433/*
2434 * Like del_bytes(), but delete characters instead of bytes.
2435 */
2436 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002437del_chars(long count, int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438{
2439 long bytes = 0;
2440 long i;
2441 char_u *p;
2442 int l;
2443
2444 p = ml_get_cursor();
2445 for (i = 0; i < count && *p != NUL; ++i)
2446 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002447 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 bytes += l;
2449 p += l;
2450 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002451 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452}
2453#endif
2454
2455/*
2456 * Delete "count" bytes under the cursor.
2457 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2458 * Caller must have prepared for undo.
2459 *
2460 * return FAIL for failure, OK otherwise
2461 */
2462 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002463del_bytes(
2464 long count,
2465 int fixpos_arg,
2466 int use_delcombine UNUSED) /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467{
2468 char_u *oldp, *newp;
2469 colnr_T oldlen;
2470 linenr_T lnum = curwin->w_cursor.lnum;
2471 colnr_T col = curwin->w_cursor.col;
2472 int was_alloced;
2473 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002474 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475
2476 oldp = ml_get(lnum);
2477 oldlen = (int)STRLEN(oldp);
2478
2479 /*
2480 * Can't do anything when the cursor is on the NUL after the line.
2481 */
2482 if (col >= oldlen)
2483 return FAIL;
2484
2485#ifdef FEAT_MBYTE
2486 /* If 'delcombine' is set and deleting (less than) one character, only
2487 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002488 if (p_deco && use_delcombine && enc_utf8
2489 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002491 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 int n;
2493
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002494 (void)utfc_ptr2char(oldp + col, cc);
2495 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 {
2497 /* Find the last composing char, there can be several. */
2498 n = col;
2499 do
2500 {
2501 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002502 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 n += count;
2504 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2505 fixpos = 0;
2506 }
2507 }
2508#endif
2509
2510 /*
2511 * When count is too big, reduce it.
2512 */
2513 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2514 if (movelen <= 1)
2515 {
2516 /*
2517 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002518 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2519 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002521 if (col > 0 && fixpos && restart_edit == 0
2522#ifdef FEAT_VIRTUALEDIT
2523 && (ve_flags & VE_ONEMORE) == 0
2524#endif
2525 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
2527 --curwin->w_cursor.col;
2528#ifdef FEAT_VIRTUALEDIT
2529 curwin->w_cursor.coladd = 0;
2530#endif
2531#ifdef FEAT_MBYTE
2532 if (has_mbyte)
2533 curwin->w_cursor.col -=
2534 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2535#endif
2536 }
2537 count = oldlen - col;
2538 movelen = 1;
2539 }
2540
2541 /*
2542 * If the old line has been allocated the deletion can be done in the
2543 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002544 * Can't do this when using Netbeans, because we would need to invoke
2545 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002546 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002549 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002550 was_alloced = FALSE;
2551 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002553 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 if (was_alloced)
2555 newp = oldp; /* use same allocated memory */
2556 else
2557 { /* need to allocate a new line */
2558 newp = alloc((unsigned)(oldlen + 1 - count));
2559 if (newp == NULL)
2560 return FAIL;
2561 mch_memmove(newp, oldp, (size_t)col);
2562 }
2563 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2564 if (!was_alloced)
2565 ml_replace(lnum, newp, FALSE);
2566
2567 /* mark the buffer as changed and prepare for displaying */
2568 changed_bytes(lnum, curwin->w_cursor.col);
2569
2570 return OK;
2571}
2572
2573/*
2574 * Delete from cursor to end of line.
2575 * Caller must have prepared for undo.
2576 *
2577 * return FAIL for failure, OK otherwise
2578 */
2579 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002580truncate_line(
2581 int fixpos) /* if TRUE fix the cursor position when done */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582{
2583 char_u *newp;
2584 linenr_T lnum = curwin->w_cursor.lnum;
2585 colnr_T col = curwin->w_cursor.col;
2586
2587 if (col == 0)
2588 newp = vim_strsave((char_u *)"");
2589 else
2590 newp = vim_strnsave(ml_get(lnum), col);
2591
2592 if (newp == NULL)
2593 return FAIL;
2594
2595 ml_replace(lnum, newp, FALSE);
2596
2597 /* mark the buffer as changed and prepare for displaying */
2598 changed_bytes(lnum, curwin->w_cursor.col);
2599
2600 /*
2601 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2602 */
2603 if (fixpos && curwin->w_cursor.col > 0)
2604 --curwin->w_cursor.col;
2605
2606 return OK;
2607}
2608
2609/*
2610 * Delete "nlines" lines at the cursor.
2611 * Saves the lines for undo first if "undo" is TRUE.
2612 */
2613 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002614del_lines(
2615 long nlines, /* number of lines to delete */
2616 int undo) /* if TRUE, prepare for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617{
2618 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002619 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620
2621 if (nlines <= 0)
2622 return;
2623
2624 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002625 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 return;
2627
2628 for (n = 0; n < nlines; )
2629 {
2630 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2631 break;
2632
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002633 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 ++n;
2635
2636 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002637 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 break;
2639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002641 /* Correct the cursor position before calling deleted_lines_mark(), it may
2642 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 curwin->w_cursor.col = 0;
2644 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002645
2646 /* adjust marks, mark the buffer as changed and prepare for displaying */
2647 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648}
2649
2650 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002651gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652{
2653 char_u *ptr = ml_get_pos(pos);
2654
2655#ifdef FEAT_MBYTE
2656 if (has_mbyte)
2657 return (*mb_ptr2char)(ptr);
2658#endif
2659 return (int)*ptr;
2660}
2661
2662 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002663gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664{
2665#ifdef FEAT_MBYTE
2666 if (has_mbyte)
2667 return (*mb_ptr2char)(ml_get_cursor());
2668#endif
2669 return (int)*ml_get_cursor();
2670}
2671
2672/*
2673 * Write a character at the current cursor position.
2674 * It is directly written into the block.
2675 */
2676 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002677pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678{
2679 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2680 + curwin->w_cursor.col) = c;
2681}
2682
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683/*
2684 * When extra == 0: Return TRUE if the cursor is before or on the first
2685 * non-blank in the line.
2686 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2687 * the line.
2688 */
2689 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002690inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691{
2692 char_u *ptr;
2693 colnr_T col;
2694
2695 for (col = 0, ptr = ml_get_curline(); vim_iswhite(*ptr); ++col)
2696 ++ptr;
2697 if (col >= curwin->w_cursor.col + extra)
2698 return TRUE;
2699 else
2700 return FALSE;
2701}
2702
2703/*
2704 * Skip to next part of an option argument: Skip space and comma.
2705 */
2706 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002707skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708{
2709 if (*p == ',')
2710 ++p;
2711 while (*p == ' ')
2712 ++p;
2713 return p;
2714}
2715
2716/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002717 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 *
2719 * Most often called through changed_bytes() and changed_lines(), which also
2720 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002721 *
2722 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 */
2724 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002725changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726{
2727#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2728 /* The text of the preediting area is inserted, but this doesn't
2729 * mean a change of the buffer yet. That is delayed until the
2730 * text is committed. (this means preedit becomes empty) */
2731 if (im_is_preediting() && !xim_changed_while_preediting)
2732 return;
2733 xim_changed_while_preediting = FALSE;
2734#endif
2735
2736 if (!curbuf->b_changed)
2737 {
2738 int save_msg_scroll = msg_scroll;
2739
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002740 /* Give a warning about changing a read-only file. This may also
2741 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002743
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 /* Create a swap file if that is wanted.
2745 * Don't do this for "nofile" and "nowrite" buffer types. */
2746 if (curbuf->b_may_swap
2747#ifdef FEAT_QUICKFIX
2748 && !bt_dontwrite(curbuf)
2749#endif
2750 )
2751 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002752 int save_need_wait_return = need_wait_return;
2753
2754 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 ml_open_file(curbuf);
2756
2757 /* The ml_open_file() can cause an ATTENTION message.
2758 * Wait two seconds, to make sure the user reads this unexpected
2759 * message. Since we could be anywhere, call wait_return() now,
2760 * and don't let the emsg() set msg_scroll. */
2761 if (need_wait_return && emsg_silent == 0)
2762 {
2763 out_flush();
2764 ui_delay(2000L, TRUE);
2765 wait_return(TRUE);
2766 msg_scroll = save_msg_scroll;
2767 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002768 else
2769 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002771 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 }
2773 ++curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774}
2775
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002776/*
2777 * Internal part of changed(), no user interaction.
2778 */
2779 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002780changed_int(void)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002781{
2782 curbuf->b_changed = TRUE;
2783 ml_setflags(curbuf);
2784#ifdef FEAT_WINDOWS
2785 check_status(curbuf);
2786 redraw_tabline = TRUE;
2787#endif
2788#ifdef FEAT_TITLE
2789 need_maketitle = TRUE; /* set window title later */
2790#endif
2791}
2792
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002793static void changedOneline(buf_T *buf, linenr_T lnum);
2794static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
2795static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796
2797/*
2798 * Changed bytes within a single line for the current buffer.
2799 * - marks the windows on this buffer to be redisplayed
2800 * - marks the buffer changed by calling changed()
2801 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002802 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 */
2804 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002805changed_bytes(linenr_T lnum, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002807 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002809
2810#ifdef FEAT_DIFF
2811 /* Diff highlighting in other diff windows may need to be updated too. */
2812 if (curwin->w_p_diff)
2813 {
2814 win_T *wp;
2815 linenr_T wlnum;
2816
2817 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2818 if (wp->w_p_diff && wp != curwin)
2819 {
2820 redraw_win_later(wp, VALID);
2821 wlnum = diff_lnum_win(lnum, wp);
2822 if (wlnum > 0)
2823 changedOneline(wp->w_buffer, wlnum);
2824 }
2825 }
2826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827}
2828
2829 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002830changedOneline(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002832 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 {
2834 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002835 if (lnum < buf->b_mod_top)
2836 buf->b_mod_top = lnum;
2837 else if (lnum >= buf->b_mod_bot)
2838 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 }
2840 else
2841 {
2842 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002843 buf->b_mod_set = TRUE;
2844 buf->b_mod_top = lnum;
2845 buf->b_mod_bot = lnum + 1;
2846 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 }
2848}
2849
2850/*
2851 * Appended "count" lines below line "lnum" in the current buffer.
2852 * Must be called AFTER the change and after mark_adjust().
2853 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2854 */
2855 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002856appended_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857{
2858 changed_lines(lnum + 1, 0, lnum + 1, count);
2859}
2860
2861/*
2862 * Like appended_lines(), but adjust marks first.
2863 */
2864 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002865appended_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866{
Bram Moolenaar82faa252016-06-04 20:14:07 +02002867 /* Skip mark_adjust when adding a line after the last one, there can't
2868 * be marks there. */
2869 if (lnum + count < curbuf->b_ml.ml_line_count)
2870 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 changed_lines(lnum + 1, 0, lnum + 1, count);
2872}
2873
2874/*
2875 * Deleted "count" lines at line "lnum" in the current buffer.
2876 * Must be called AFTER the change and after mark_adjust().
2877 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2878 */
2879 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002880deleted_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881{
2882 changed_lines(lnum, 0, lnum + count, -count);
2883}
2884
2885/*
2886 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002887 * Make sure the cursor is on a valid line before calling, a GUI callback may
2888 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 */
2890 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002891deleted_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892{
2893 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
2894 changed_lines(lnum, 0, lnum + count, -count);
2895}
2896
2897/*
2898 * Changed lines for the current buffer.
2899 * Must be called AFTER the change and after mark_adjust().
2900 * - mark the buffer changed by calling changed()
2901 * - mark the windows on this buffer to be redisplayed
2902 * - invalidate cached values
2903 * "lnum" is the first line that needs displaying, "lnume" the first line
2904 * below the changed lines (BEFORE the change).
2905 * When only inserting lines, "lnum" and "lnume" are equal.
2906 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002907 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 */
2909 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002910changed_lines(
2911 linenr_T lnum, /* first line with change */
2912 colnr_T col, /* column in first line with change */
2913 linenr_T lnume, /* line below last changed line */
2914 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002916 changed_lines_buf(curbuf, lnum, lnume, xtra);
2917
2918#ifdef FEAT_DIFF
2919 if (xtra == 0 && curwin->w_p_diff)
2920 {
2921 /* When the number of lines doesn't change then mark_adjust() isn't
2922 * called and other diff buffers still need to be marked for
2923 * displaying. */
2924 win_T *wp;
2925 linenr_T wlnum;
2926
2927 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2928 if (wp->w_p_diff && wp != curwin)
2929 {
2930 redraw_win_later(wp, VALID);
2931 wlnum = diff_lnum_win(lnum, wp);
2932 if (wlnum > 0)
2933 changed_lines_buf(wp->w_buffer, wlnum,
2934 lnume - lnum + wlnum, 0L);
2935 }
2936 }
2937#endif
2938
2939 changed_common(lnum, col, lnume, xtra);
2940}
2941
2942 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002943changed_lines_buf(
2944 buf_T *buf,
2945 linenr_T lnum, /* first line with change */
2946 linenr_T lnume, /* line below last changed line */
2947 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002948{
2949 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 {
2951 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002952 if (lnum < buf->b_mod_top)
2953 buf->b_mod_top = lnum;
2954 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 {
2956 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002957 buf->b_mod_bot += xtra;
2958 if (buf->b_mod_bot < lnum)
2959 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00002961 if (lnume + xtra > buf->b_mod_bot)
2962 buf->b_mod_bot = lnume + xtra;
2963 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 }
2965 else
2966 {
2967 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002968 buf->b_mod_set = TRUE;
2969 buf->b_mod_top = lnum;
2970 buf->b_mod_bot = lnume + xtra;
2971 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973}
2974
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002975/*
2976 * Common code for when a change is was made.
2977 * See changed_lines() for the arguments.
2978 * Careful: may trigger autocommands that reload the buffer.
2979 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002981changed_common(
2982 linenr_T lnum,
2983 colnr_T col,
2984 linenr_T lnume,
2985 long xtra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986{
2987 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00002988#ifdef FEAT_WINDOWS
2989 tabpage_T *tp;
2990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 int i;
2992#ifdef FEAT_JUMPLIST
2993 int cols;
2994 pos_T *p;
2995 int add;
2996#endif
2997
2998 /* mark the buffer as modified */
2999 changed();
3000
3001 /* set the '. mark */
3002 if (!cmdmod.keepjumps)
3003 {
3004 curbuf->b_last_change.lnum = lnum;
3005 curbuf->b_last_change.col = col;
3006
3007#ifdef FEAT_JUMPLIST
3008 /* Create a new entry if a new undo-able change was started or we
3009 * don't have an entry yet. */
3010 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3011 {
3012 if (curbuf->b_changelistlen == 0)
3013 add = TRUE;
3014 else
3015 {
3016 /* Don't create a new entry when the line number is the same
3017 * as the last one and the column is not too far away. Avoids
3018 * creating many entries for typing "xxxxx". */
3019 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3020 if (p->lnum != lnum)
3021 add = TRUE;
3022 else
3023 {
3024 cols = comp_textwidth(FALSE);
3025 if (cols == 0)
3026 cols = 79;
3027 add = (p->col + cols < col || col + cols < p->col);
3028 }
3029 }
3030 if (add)
3031 {
3032 /* This is the first of a new sequence of undo-able changes
3033 * and it's at some distance of the last change. Use a new
3034 * position in the changelist. */
3035 curbuf->b_new_change = FALSE;
3036
3037 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3038 {
3039 /* changelist is full: remove oldest entry */
3040 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3041 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3042 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003043 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 {
3045 /* Correct position in changelist for other windows on
3046 * this buffer. */
3047 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3048 --wp->w_changelistidx;
3049 }
3050 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003051 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 {
3053 /* For other windows, if the position in the changelist is
3054 * at the end it stays at the end. */
3055 if (wp->w_buffer == curbuf
3056 && wp->w_changelistidx == curbuf->b_changelistlen)
3057 ++wp->w_changelistidx;
3058 }
3059 ++curbuf->b_changelistlen;
3060 }
3061 }
3062 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3063 curbuf->b_last_change;
3064 /* The current window is always after the last change, so that "g,"
3065 * takes you back to it. */
3066 curwin->w_changelistidx = curbuf->b_changelistlen;
3067#endif
3068 }
3069
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003070 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 {
3072 if (wp->w_buffer == curbuf)
3073 {
3074 /* Mark this window to be redrawn later. */
3075 if (wp->w_redr_type < VALID)
3076 wp->w_redr_type = VALID;
3077
3078 /* Check if a change in the buffer has invalidated the cached
3079 * values for the cursor. */
3080#ifdef FEAT_FOLDING
3081 /*
3082 * Update the folds for this window. Can't postpone this, because
3083 * a following operator might work on the whole fold: ">>dd".
3084 */
3085 foldUpdate(wp, lnum, lnume + xtra - 1);
3086
3087 /* The change may cause lines above or below the change to become
3088 * included in a fold. Set lnum/lnume to the first/last line that
3089 * might be displayed differently.
3090 * Set w_cline_folded here as an efficient way to update it when
3091 * inserting lines just above a closed fold. */
3092 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3093 if (wp->w_cursor.lnum == lnum)
3094 wp->w_cline_folded = i;
3095 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3096 if (wp->w_cursor.lnum == lnume)
3097 wp->w_cline_folded = i;
3098
3099 /* If the changed line is in a range of previously folded lines,
3100 * compare with the first line in that range. */
3101 if (wp->w_cursor.lnum <= lnum)
3102 {
3103 i = find_wl_entry(wp, lnum);
3104 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3105 changed_line_abv_curs_win(wp);
3106 }
3107#endif
3108
3109 if (wp->w_cursor.lnum > lnum)
3110 changed_line_abv_curs_win(wp);
3111 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3112 changed_cline_bef_curs_win(wp);
3113 if (wp->w_botline >= lnum)
3114 {
3115 /* Assume that botline doesn't change (inserted lines make
3116 * other lines scroll down below botline). */
3117 approximate_botline_win(wp);
3118 }
3119
3120 /* Check if any w_lines[] entries have become invalid.
3121 * For entries below the change: Correct the lnums for
3122 * inserted/deleted lines. Makes it possible to stop displaying
3123 * after the change. */
3124 for (i = 0; i < wp->w_lines_valid; ++i)
3125 if (wp->w_lines[i].wl_valid)
3126 {
3127 if (wp->w_lines[i].wl_lnum >= lnum)
3128 {
3129 if (wp->w_lines[i].wl_lnum < lnume)
3130 {
3131 /* line included in change */
3132 wp->w_lines[i].wl_valid = FALSE;
3133 }
3134 else if (xtra != 0)
3135 {
3136 /* line below change */
3137 wp->w_lines[i].wl_lnum += xtra;
3138#ifdef FEAT_FOLDING
3139 wp->w_lines[i].wl_lastlnum += xtra;
3140#endif
3141 }
3142 }
3143#ifdef FEAT_FOLDING
3144 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3145 {
3146 /* change somewhere inside this range of folded lines,
3147 * may need to be redrawn */
3148 wp->w_lines[i].wl_valid = FALSE;
3149 }
3150#endif
3151 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003152
3153#ifdef FEAT_FOLDING
3154 /* Take care of side effects for setting w_topline when folds have
3155 * changed. Esp. when the buffer was changed in another window. */
3156 if (hasAnyFolding(wp))
3157 set_topline(wp, wp->w_topline);
3158#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003159 /* relative numbering may require updating more */
3160 if (wp->w_p_rnu)
3161 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 }
3163 }
3164
3165 /* Call update_screen() later, which checks out what needs to be redrawn,
3166 * since it notices b_mod_set and then uses b_mod_*. */
3167 if (must_redraw < VALID)
3168 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003169
3170#ifdef FEAT_AUTOCMD
3171 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003172 if (lnum <= curwin->w_cursor.lnum
3173 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003174 last_cursormoved.lnum = 0;
3175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176}
3177
3178/*
3179 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3180 */
3181 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003182unchanged(
3183 buf_T *buf,
3184 int ff) /* also reset 'fileformat' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003186 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 {
3188 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003189 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 if (ff)
3191 save_file_ff(buf);
3192#ifdef FEAT_WINDOWS
3193 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003194 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195#endif
3196#ifdef FEAT_TITLE
3197 need_maketitle = TRUE; /* set window title later */
3198#endif
3199 }
3200 ++buf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201#ifdef FEAT_NETBEANS_INTG
3202 netbeans_unmodified(buf);
3203#endif
3204}
3205
3206#if defined(FEAT_WINDOWS) || defined(PROTO)
3207/*
3208 * check_status: called when the status bars for the buffer 'buf'
3209 * need to be updated
3210 */
3211 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003212check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213{
3214 win_T *wp;
3215
3216 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3217 if (wp->w_buffer == buf && wp->w_status_height)
3218 {
3219 wp->w_redr_status = TRUE;
3220 if (must_redraw < VALID)
3221 must_redraw = VALID;
3222 }
3223}
3224#endif
3225
3226/*
3227 * If the file is readonly, give a warning message with the first change.
3228 * Don't do this for autocommands.
3229 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003230 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003232 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 */
3234 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003235change_warning(
3236 int col) /* column for message; non-zero when in insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 mode and 'showmode' is on */
3238{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003239 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3240
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 if (curbuf->b_did_warn == FALSE
3242 && curbufIsChanged() == 0
3243#ifdef FEAT_AUTOCMD
3244 && !autocmd_busy
3245#endif
3246 && curbuf->b_p_ro)
3247 {
3248#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003249 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003251 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 if (!curbuf->b_p_ro)
3253 return;
3254#endif
3255 /*
3256 * Do what msg() does, but with a column offset if the warning should
3257 * be after the mode message.
3258 */
3259 msg_start();
3260 if (msg_row == Rows - 1)
3261 msg_col = col;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003262 msg_source(hl_attr(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003263 MSG_PUTS_ATTR(_(w_readonly), hl_attr(HLF_W) | MSG_HIST);
3264#ifdef FEAT_EVAL
3265 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 msg_clr_eos();
3268 (void)msg_end();
3269 if (msg_silent == 0 && !silent_mode)
3270 {
3271 out_flush();
3272 ui_delay(1000L, TRUE); /* give the user time to think about it */
3273 }
3274 curbuf->b_did_warn = TRUE;
3275 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3276 if (msg_row < Rows - 1)
3277 showmode();
3278 }
3279}
3280
3281/*
3282 * Ask for a reply from the user, a 'y' or a 'n'.
3283 * No other characters are accepted, the message is repeated until a valid
3284 * reply is entered or CTRL-C is hit.
3285 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3286 * from any buffers but directly from the user.
3287 *
3288 * return the 'y' or 'n'
3289 */
3290 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003291ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292{
3293 int r = ' ';
3294 int save_State = State;
3295
3296 if (exiting) /* put terminal in raw mode for this question */
3297 settmode(TMODE_RAW);
3298 ++no_wait_return;
3299#ifdef USE_ON_FLY_SCROLL
3300 dont_scroll = TRUE; /* disallow scrolling here */
3301#endif
3302 State = CONFIRM; /* mouse behaves like with :confirm */
3303#ifdef FEAT_MOUSE
3304 setmouse(); /* disables mouse for xterm */
3305#endif
3306 ++no_mapping;
3307 ++allow_keys; /* no mapping here, but recognize keys */
3308
3309 while (r != 'y' && r != 'n')
3310 {
3311 /* same highlighting as for wait_return */
3312 smsg_attr(hl_attr(HLF_R), (char_u *)"%s (y/n)?", str);
3313 if (direct)
3314 r = get_keystroke();
3315 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003316 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 if (r == Ctrl_C || r == ESC)
3318 r = 'n';
3319 msg_putchar(r); /* show what you typed */
3320 out_flush();
3321 }
3322 --no_wait_return;
3323 State = save_State;
3324#ifdef FEAT_MOUSE
3325 setmouse();
3326#endif
3327 --no_mapping;
3328 --allow_keys;
3329
3330 return r;
3331}
3332
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003333#if defined(FEAT_MOUSE) || defined(PROTO)
3334/*
3335 * Return TRUE if "c" is a mouse key.
3336 */
3337 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003338is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003339{
3340 return c == K_LEFTMOUSE
3341 || c == K_LEFTMOUSE_NM
3342 || c == K_LEFTDRAG
3343 || c == K_LEFTRELEASE
3344 || c == K_LEFTRELEASE_NM
3345 || c == K_MIDDLEMOUSE
3346 || c == K_MIDDLEDRAG
3347 || c == K_MIDDLERELEASE
3348 || c == K_RIGHTMOUSE
3349 || c == K_RIGHTDRAG
3350 || c == K_RIGHTRELEASE
3351 || c == K_MOUSEDOWN
3352 || c == K_MOUSEUP
3353 || c == K_MOUSELEFT
3354 || c == K_MOUSERIGHT
3355 || c == K_X1MOUSE
3356 || c == K_X1DRAG
3357 || c == K_X1RELEASE
3358 || c == K_X2MOUSE
3359 || c == K_X2DRAG
3360 || c == K_X2RELEASE;
3361}
3362#endif
3363
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364/*
3365 * Get a key stroke directly from the user.
3366 * Ignores mouse clicks and scrollbar events, except a click for the left
3367 * button (used at the more prompt).
3368 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3369 * Disadvantage: typeahead is ignored.
3370 * Translates the interrupt character for unix to ESC.
3371 */
3372 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003373get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003375 char_u *buf = NULL;
3376 int buflen = 150;
3377 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 int len = 0;
3379 int n;
3380 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003381 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382
3383 mapped_ctrl_c = FALSE; /* mappings are not used here */
3384 for (;;)
3385 {
3386 cursor_on();
3387 out_flush();
3388
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003389 /* Leave some room for check_termcode() to insert a key code into (max
3390 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3391 * bytes. */
3392 maxlen = (buflen - 6 - len) / 3;
3393 if (buf == NULL)
3394 buf = alloc(buflen);
3395 else if (maxlen < 10)
3396 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003397 char_u *t_buf = buf;
3398
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003399 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003400 * escape sequence. */
3401 buflen += 100;
3402 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003403 if (buf == NULL)
3404 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003405 maxlen = (buflen - 6 - len) / 3;
3406 }
3407 if (buf == NULL)
3408 {
3409 do_outofmem_msg((long_u)buflen);
3410 return ESC; /* panic! */
3411 }
3412
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003414 * terminal code to complete. */
3415 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 if (n > 0)
3417 {
3418 /* Replace zero and CSI by a special key code. */
3419 n = fix_input_buffer(buf + len, n, FALSE);
3420 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003421 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003423 else if (len > 0)
3424 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425
Bram Moolenaar4395a712006-09-05 18:57:57 +00003426 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003427 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003428 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003430
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003431 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003432 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003433 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003434 {
3435 /* Redrawing was postponed, do it now. */
3436 update_screen(0);
3437 setcursor(); /* put cursor back where it belongs */
3438 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003439 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003440 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003441 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003443 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 continue;
3445
3446 /* Handle modifier and/or special key code. */
3447 n = buf[0];
3448 if (n == K_SPECIAL)
3449 {
3450 n = TO_SPECIAL(buf[1], buf[2]);
3451 if (buf[1] == KS_MODIFIER
3452 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003453#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003454 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003455#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003456#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 || n == K_VER_SCROLLBAR
3458 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459#endif
3460 )
3461 {
3462 if (buf[1] == KS_MODIFIER)
3463 mod_mask = buf[2];
3464 len -= 3;
3465 if (len > 0)
3466 mch_memmove(buf, buf + 3, (size_t)len);
3467 continue;
3468 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003469 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 }
3471#ifdef FEAT_MBYTE
3472 if (has_mbyte)
3473 {
3474 if (MB_BYTE2LEN(n) > len)
3475 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003476 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 n = (*mb_ptr2char)(buf);
3478 }
3479#endif
3480#ifdef UNIX
3481 if (n == intr_char)
3482 n = ESC;
3483#endif
3484 break;
3485 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003486 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487
3488 mapped_ctrl_c = save_mapped_ctrl_c;
3489 return n;
3490}
3491
3492/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003493 * Get a number from the user.
3494 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 */
3496 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003497get_number(
3498 int colon, /* allow colon to abort */
3499 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500{
3501 int n = 0;
3502 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003503 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003505 if (mouse_used != NULL)
3506 *mouse_used = FALSE;
3507
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 /* When not printing messages, the user won't know what to type, return a
3509 * zero (as if CR was hit). */
3510 if (msg_silent != 0)
3511 return 0;
3512
3513#ifdef USE_ON_FLY_SCROLL
3514 dont_scroll = TRUE; /* disallow scrolling here */
3515#endif
3516 ++no_mapping;
3517 ++allow_keys; /* no mapping here, but recognize keys */
3518 for (;;)
3519 {
3520 windgoto(msg_row, msg_col);
3521 c = safe_vgetc();
3522 if (VIM_ISDIGIT(c))
3523 {
3524 n = n * 10 + c - '0';
3525 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003526 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 }
3528 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3529 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003530 if (typed > 0)
3531 {
3532 MSG_PUTS("\b \b");
3533 --typed;
3534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003537#ifdef FEAT_MOUSE
3538 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3539 {
3540 *mouse_used = TRUE;
3541 n = mouse_row + 1;
3542 break;
3543 }
3544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 else if (n == 0 && c == ':' && colon)
3546 {
3547 stuffcharReadbuff(':');
3548 if (!exmode_active)
3549 cmdline_row = msg_row;
3550 skip_redraw = TRUE; /* skip redraw once */
3551 do_redraw = FALSE;
3552 break;
3553 }
3554 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3555 break;
3556 }
3557 --no_mapping;
3558 --allow_keys;
3559 return n;
3560}
3561
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003562/*
3563 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003564 * When "mouse_used" is not NULL allow using the mouse and in that case return
3565 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003566 */
3567 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003568prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003569{
3570 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003571 int save_cmdline_row;
3572 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003573
3574 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003575 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003576 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003577 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003578 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003579
Bram Moolenaar203335e2006-09-03 14:35:42 +00003580 /* Set the state such that text can be selected/copied/pasted and we still
3581 * get mouse events. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003582 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003583 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003584 save_State = State;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003585 State = CMDLINE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003586
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003587 i = get_number(TRUE, mouse_used);
3588 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003589 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003590 /* don't call wait_return() now */
3591 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003592 cmdline_row = msg_row - 1;
3593 need_wait_return = FALSE;
3594 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003595 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003596 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003597 else
3598 cmdline_row = save_cmdline_row;
3599 State = save_State;
3600
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003601 return i;
3602}
3603
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003605msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606{
3607 long pn;
3608
3609 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3611 return;
3612
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003613 /* We don't want to overwrite another important message, but do overwrite
3614 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3615 * then "put" reports the last action. */
3616 if (keep_msg != NULL && !keep_msg_more)
3617 return;
3618
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 if (n > 0)
3620 pn = n;
3621 else
3622 pn = -n;
3623
3624 if (pn > p_report)
3625 {
3626 if (pn == 1)
3627 {
3628 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003629 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3630 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003632 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3633 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 }
3635 else
3636 {
3637 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003638 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3639 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003641 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3642 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 }
3644 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003645 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 if (msg(msg_buf))
3647 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003648 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003649 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 }
3651 }
3652}
3653
3654/*
3655 * flush map and typeahead buffers and give a warning for an error
3656 */
3657 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003658beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659{
3660 if (emsg_silent == 0)
3661 {
3662 flush_buffers(FALSE);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003663 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 }
3665}
3666
3667/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003668 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 */
3670 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003671vim_beep(
3672 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673{
3674 if (emsg_silent == 0)
3675 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003676 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3677 {
3678 if (p_vb
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679#ifdef FEAT_GUI
Bram Moolenaar165bc692015-07-21 17:53:25 +02003680 /* While the GUI is starting up the termcap is set for the
3681 * GUI but the output still goes to a terminal. */
3682 && !(gui.in_use && gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683#endif
Bram Moolenaar165bc692015-07-21 17:53:25 +02003684 )
Bram Moolenaar165bc692015-07-21 17:53:25 +02003685 out_str(T_VB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02003687 out_char(BELL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003689
3690 /* When 'verbose' is set and we are sourcing a script or executing a
3691 * function give the user a hint where the beep comes from. */
3692 if (vim_strchr(p_debug, 'e') != NULL)
3693 {
3694 msg_source(hl_attr(HLF_W));
3695 msg_attr((char_u *)_("Beep!"), hl_attr(HLF_W));
3696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 }
3698}
3699
3700/*
3701 * To get the "real" home directory:
3702 * - get value of $HOME
3703 * For Unix:
3704 * - go to that directory
3705 * - do mch_dirname() to get the real name of that directory.
3706 * This also works with mounts and links.
3707 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3708 */
3709static char_u *homedir = NULL;
3710
3711 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003712init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713{
3714 char_u *var;
3715
Bram Moolenaar05159a02005-02-26 23:04:13 +00003716 /* In case we are called a second time (when 'encoding' changes). */
3717 vim_free(homedir);
3718 homedir = NULL;
3719
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720#ifdef VMS
3721 var = mch_getenv((char_u *)"SYS$LOGIN");
3722#else
3723 var = mch_getenv((char_u *)"HOME");
3724#endif
3725
3726 if (var != NULL && *var == NUL) /* empty is same as not set */
3727 var = NULL;
3728
3729#ifdef WIN3264
3730 /*
3731 * Weird but true: $HOME may contain an indirect reference to another
3732 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3733 * when $HOME is being set.
3734 */
3735 if (var != NULL && *var == '%')
3736 {
3737 char_u *p;
3738 char_u *exp;
3739
3740 p = vim_strchr(var + 1, '%');
3741 if (p != NULL)
3742 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003743 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 exp = mch_getenv(NameBuff);
3745 if (exp != NULL && *exp != NUL
3746 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3747 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003748 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 var = NameBuff;
3750 /* Also set $HOME, it's needed for _viminfo. */
3751 vim_setenv((char_u *)"HOME", NameBuff);
3752 }
3753 }
3754 }
3755
3756 /*
3757 * Typically, $HOME is not defined on Windows, unless the user has
3758 * specifically defined it for Vim's sake. However, on Windows NT
3759 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3760 * each user. Try constructing $HOME from these.
3761 */
3762 if (var == NULL)
3763 {
3764 char_u *homedrive, *homepath;
3765
3766 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3767 homepath = mch_getenv((char_u *)"HOMEPATH");
Bram Moolenaar6f977012010-01-06 17:53:38 +01003768 if (homepath == NULL || *homepath == NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003769 homepath = (char_u *)"\\";
Bram Moolenaar6f977012010-01-06 17:53:38 +01003770 if (homedrive != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3772 {
3773 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3774 if (NameBuff[0] != NUL)
3775 {
3776 var = NameBuff;
3777 /* Also set $HOME, it's needed for _viminfo. */
3778 vim_setenv((char_u *)"HOME", NameBuff);
3779 }
3780 }
3781 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003782
3783# if defined(FEAT_MBYTE)
3784 if (enc_utf8 && var != NULL)
3785 {
3786 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003787 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003788
3789 /* Convert from active codepage to UTF-8. Other conversions are
3790 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003791 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003792 if (pp != NULL)
3793 {
3794 homedir = pp;
3795 return;
3796 }
3797 }
3798# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799#endif
3800
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003801#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 /*
3803 * Default home dir is C:/
3804 * Best assumption we can make in such a situation.
3805 */
3806 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003807 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808#endif
3809 if (var != NULL)
3810 {
3811#ifdef UNIX
3812 /*
3813 * Change to the directory and get the actual path. This resolves
3814 * links. Don't do it when we can't return.
3815 */
3816 if (mch_dirname(NameBuff, MAXPATHL) == OK
3817 && mch_chdir((char *)NameBuff) == 0)
3818 {
3819 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3820 var = IObuff;
3821 if (mch_chdir((char *)NameBuff) != 0)
3822 EMSG(_(e_prev_dir));
3823 }
3824#endif
3825 homedir = vim_strsave(var);
3826 }
3827}
3828
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003829#if defined(EXITFREE) || defined(PROTO)
3830 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003831free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003832{
3833 vim_free(homedir);
3834}
Bram Moolenaar24305862012-08-15 14:05:05 +02003835
3836# ifdef FEAT_CMDL_COMPL
3837 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003838free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02003839{
3840 ga_clear_strings(&ga_users);
3841}
3842# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003843#endif
3844
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003846 * Call expand_env() and store the result in an allocated string.
3847 * This is not very memory efficient, this expects the result to be freed
3848 * again soon.
3849 */
3850 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003851expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003852{
3853 return expand_env_save_opt(src, FALSE);
3854}
3855
3856/*
3857 * Idem, but when "one" is TRUE handle the string as one file name, only
3858 * expand "~" at the start.
3859 */
3860 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003861expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003862{
3863 char_u *p;
3864
3865 p = alloc(MAXPATHL);
3866 if (p != NULL)
3867 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3868 return p;
3869}
3870
3871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 * Expand environment variable with path name.
3873 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003874 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 * If anything fails no expansion is done and dst equals src.
3876 */
3877 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003878expand_env(
3879 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
3880 char_u *dst, /* where to put the result */
3881 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003883 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884}
3885
3886 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003887expand_env_esc(
3888 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
3889 char_u *dst, /* where to put the result */
3890 int dstlen, /* maximum length of the result */
3891 int esc, /* escape spaces in expanded variables */
3892 int one, /* "srcp" is one file name */
3893 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003895 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 char_u *tail;
3897 int c;
3898 char_u *var;
3899 int copy_char;
3900 int mustfree; /* var was allocated, need to free it later */
3901 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003902 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003904 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003905 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003906
3907 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 --dstlen; /* leave one char space for "\," */
3909 while (*src && dstlen > 0)
3910 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003911#ifdef FEAT_EVAL
3912 /* Skip over `=expr`. */
3913 if (src[0] == '`' && src[1] == '=')
3914 {
3915 size_t len;
3916
3917 var = src;
3918 src += 2;
3919 (void)skip_expr(&src);
3920 if (*src == '`')
3921 ++src;
3922 len = src - var;
3923 if (len > (size_t)dstlen)
3924 len = dstlen;
3925 vim_strncpy(dst, var, len);
3926 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02003927 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003928 continue;
3929 }
3930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003932 if ((*src == '$'
3933#ifdef VMS
3934 && at_start
3935#endif
3936 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003937#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 || *src == '%'
3939#endif
3940 || (*src == '~' && at_start))
3941 {
3942 mustfree = FALSE;
3943
3944 /*
3945 * The variable name is copied into dst temporarily, because it may
3946 * be a string in read-only memory and a NUL needs to be appended.
3947 */
3948 if (*src != '~') /* environment var */
3949 {
3950 tail = src + 1;
3951 var = dst;
3952 c = dstlen - 1;
3953
3954#ifdef UNIX
3955 /* Unix has ${var-name} type environment vars */
3956 if (*tail == '{' && !vim_isIDc('{'))
3957 {
3958 tail++; /* ignore '{' */
3959 while (c-- > 0 && *tail && *tail != '}')
3960 *var++ = *tail++;
3961 }
3962 else
3963#endif
3964 {
3965 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003966#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 || (*src == '%' && *tail != '%')
3968#endif
3969 ))
3970 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 }
3973 }
3974
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003975#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976# ifdef UNIX
3977 if (src[1] == '{' && *tail != '}')
3978# else
3979 if (*src == '%' && *tail != '%')
3980# endif
3981 var = NULL;
3982 else
3983 {
3984# ifdef UNIX
3985 if (src[1] == '{')
3986# else
3987 if (*src == '%')
3988#endif
3989 ++tail;
3990#endif
3991 *var = NUL;
3992 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003993#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 }
3995#endif
3996 }
3997 /* home directory */
3998 else if ( src[1] == NUL
3999 || vim_ispathsep(src[1])
4000 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4001 {
4002 var = homedir;
4003 tail = src + 1;
4004 }
4005 else /* user directory */
4006 {
4007#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4008 /*
4009 * Copy ~user to dst[], so we can put a NUL after it.
4010 */
4011 tail = src;
4012 var = dst;
4013 c = dstlen - 1;
4014 while ( c-- > 0
4015 && *tail
4016 && vim_isfilec(*tail)
4017 && !vim_ispathsep(*tail))
4018 *var++ = *tail++;
4019 *var = NUL;
4020# ifdef UNIX
4021 /*
4022 * If the system supports getpwnam(), use it.
4023 * Otherwise, or if getpwnam() fails, the shell is used to
4024 * expand ~user. This is slower and may fail if the shell
4025 * does not support ~user (old versions of /bin/sh).
4026 */
4027# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4028 {
4029 struct passwd *pw;
4030
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004031 /* Note: memory allocated by getpwnam() is never freed.
4032 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 pw = getpwnam((char *)dst + 1);
4034 if (pw != NULL)
4035 var = (char_u *)pw->pw_dir;
4036 else
4037 var = NULL;
4038 }
4039 if (var == NULL)
4040# endif
4041 {
4042 expand_T xpc;
4043
4044 ExpandInit(&xpc);
4045 xpc.xp_context = EXPAND_FILES;
4046 var = ExpandOne(&xpc, dst, NULL,
4047 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 mustfree = TRUE;
4049 }
4050
4051# else /* !UNIX, thus VMS */
4052 /*
4053 * USER_HOME is a comma-separated list of
4054 * directories to search for the user account in.
4055 */
4056 {
4057 char_u test[MAXPATHL], paths[MAXPATHL];
4058 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004059 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060
4061 STRCPY(paths, USER_HOME);
4062 next_path = paths;
4063 while (*next_path)
4064 {
4065 for (path = next_path; *next_path && *next_path != ',';
4066 next_path++);
4067 if (*next_path)
4068 *next_path++ = NUL;
4069 STRCPY(test, path);
4070 STRCAT(test, "/");
4071 STRCAT(test, dst + 1);
4072 if (mch_stat(test, &st) == 0)
4073 {
4074 var = alloc(STRLEN(test) + 1);
4075 STRCPY(var, test);
4076 mustfree = TRUE;
4077 break;
4078 }
4079 }
4080 }
4081# endif /* UNIX */
4082#else
4083 /* cannot expand user's home directory, so don't try */
4084 var = NULL;
4085 tail = (char_u *)""; /* for gcc */
4086#endif /* UNIX || VMS */
4087 }
4088
4089#ifdef BACKSLASH_IN_FILENAME
4090 /* If 'shellslash' is set change backslashes to forward slashes.
4091 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4092 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4093 {
4094 char_u *p = vim_strsave(var);
4095
4096 if (p != NULL)
4097 {
4098 if (mustfree)
4099 vim_free(var);
4100 var = p;
4101 mustfree = TRUE;
4102 forward_slash(var);
4103 }
4104 }
4105#endif
4106
4107 /* If "var" contains white space, escape it with a backslash.
4108 * Required for ":e ~/tt" when $HOME includes a space. */
4109 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4110 {
4111 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4112
4113 if (p != NULL)
4114 {
4115 if (mustfree)
4116 vim_free(var);
4117 var = p;
4118 mustfree = TRUE;
4119 }
4120 }
4121
4122 if (var != NULL && *var != NUL
4123 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4124 {
4125 STRCPY(dst, var);
4126 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004127 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 /* if var[] ends in a path separator and tail[] starts
4129 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004130 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4132 && dst[-1] != ':'
4133#endif
4134 && vim_ispathsep(*tail))
4135 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004136 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 src = tail;
4138 copy_char = FALSE;
4139 }
4140 if (mustfree)
4141 vim_free(var);
4142 }
4143
4144 if (copy_char) /* copy at least one char */
4145 {
4146 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004147 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004148 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4149 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 */
4151 at_start = FALSE;
4152 if (src[0] == '\\' && src[1] != NUL)
4153 {
4154 *dst++ = *src++;
4155 --dstlen;
4156 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004157 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 at_start = TRUE;
4159 *dst++ = *src++;
4160 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004161
4162 if (startstr != NULL && src - startstr_len >= srcp
4163 && STRNCMP(src - startstr_len, startstr, startstr_len) == 0)
4164 at_start = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 }
4166 }
4167 *dst = NUL;
4168}
4169
4170/*
4171 * Vim's version of getenv().
4172 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004173 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004174 * "mustfree" is set to TRUE when returned is allocated, it must be
4175 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 */
4177 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004178vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179{
4180 char_u *p;
4181 char_u *pend;
4182 int vimruntime;
4183
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004184#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 /* use "C:/" when $HOME is not set */
4186 if (STRCMP(name, "HOME") == 0)
4187 return homedir;
4188#endif
4189
4190 p = mch_getenv(name);
4191 if (p != NULL && *p == NUL) /* empty is the same as not set */
4192 p = NULL;
4193
4194 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004195 {
4196#if defined(FEAT_MBYTE) && defined(WIN3264)
4197 if (enc_utf8)
4198 {
4199 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004200 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004201
4202 /* Convert from active codepage to UTF-8. Other conversions are
4203 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004204 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004205 if (pp != NULL)
4206 {
4207 p = pp;
4208 *mustfree = TRUE;
4209 }
4210 }
4211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214
4215 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4216 if (!vimruntime && STRCMP(name, "VIM") != 0)
4217 return NULL;
4218
4219 /*
4220 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4221 * Don't do this when default_vimruntime_dir is non-empty.
4222 */
4223 if (vimruntime
4224#ifdef HAVE_PATHDEF
4225 && *default_vimruntime_dir == NUL
4226#endif
4227 )
4228 {
4229 p = mch_getenv((char_u *)"VIM");
4230 if (p != NULL && *p == NUL) /* empty is the same as not set */
4231 p = NULL;
4232 if (p != NULL)
4233 {
4234 p = vim_version_dir(p);
4235 if (p != NULL)
4236 *mustfree = TRUE;
4237 else
4238 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004239
4240#if defined(FEAT_MBYTE) && defined(WIN3264)
4241 if (enc_utf8)
4242 {
4243 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004244 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004245
4246 /* Convert from active codepage to UTF-8. Other conversions
4247 * are not done, because they would fail for non-ASCII
4248 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004249 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004250 if (pp != NULL)
4251 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004252 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004253 vim_free(p);
4254 p = pp;
4255 *mustfree = TRUE;
4256 }
4257 }
4258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 }
4260 }
4261
4262 /*
4263 * When expanding $VIM or $VIMRUNTIME fails, try using:
4264 * - the directory name from 'helpfile' (unless it contains '$')
4265 * - the executable name from argv[0]
4266 */
4267 if (p == NULL)
4268 {
4269 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4270 p = p_hf;
4271#ifdef USE_EXE_NAME
4272 /*
4273 * Use the name of the executable, obtained from argv[0].
4274 */
4275 else
4276 p = exe_name;
4277#endif
4278 if (p != NULL)
4279 {
4280 /* remove the file name */
4281 pend = gettail(p);
4282
4283 /* remove "doc/" from 'helpfile', if present */
4284 if (p == p_hf)
4285 pend = remove_tail(p, pend, (char_u *)"doc");
4286
4287#ifdef USE_EXE_NAME
4288# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004289 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 if (p == exe_name)
4291 {
4292 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004293 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004295 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4296 if (pend1 != pend)
4297 {
4298 pnew = alloc((unsigned)(pend1 - p) + 15);
4299 if (pnew != NULL)
4300 {
4301 STRNCPY(pnew, p, (pend1 - p));
4302 STRCPY(pnew + (pend1 - p), "Resources/vim");
4303 p = pnew;
4304 pend = p + STRLEN(p);
4305 }
4306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 }
4308# endif
4309 /* remove "src/" from exe_name, if present */
4310 if (p == exe_name)
4311 pend = remove_tail(p, pend, (char_u *)"src");
4312#endif
4313
4314 /* for $VIM, remove "runtime/" or "vim54/", if present */
4315 if (!vimruntime)
4316 {
4317 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4318 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4319 }
4320
4321 /* remove trailing path separator */
4322#ifndef MACOS_CLASSIC
4323 /* With MacOS path (with colons) the final colon is required */
Bram Moolenaare21877a2008-02-13 09:58:14 +00004324 /* to avoid confusion between absolute and relative path */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004325 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 --pend;
4327#endif
4328
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004329#ifdef MACOS_X
4330 if (p == exe_name || p == p_hf)
4331#endif
4332 /* check that the result is a directory name */
4333 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334
4335 if (p != NULL && !mch_isdir(p))
4336 {
4337 vim_free(p);
4338 p = NULL;
4339 }
4340 else
4341 {
4342#ifdef USE_EXE_NAME
4343 /* may add "/vim54" or "/runtime" if it exists */
4344 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4345 {
4346 vim_free(p);
4347 p = pend;
4348 }
4349#endif
4350 *mustfree = TRUE;
4351 }
4352 }
4353 }
4354
4355#ifdef HAVE_PATHDEF
4356 /* When there is a pathdef.c file we can use default_vim_dir and
4357 * default_vimruntime_dir */
4358 if (p == NULL)
4359 {
4360 /* Only use default_vimruntime_dir when it is not empty */
4361 if (vimruntime && *default_vimruntime_dir != NUL)
4362 {
4363 p = default_vimruntime_dir;
4364 *mustfree = FALSE;
4365 }
4366 else if (*default_vim_dir != NUL)
4367 {
4368 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4369 *mustfree = TRUE;
4370 else
4371 {
4372 p = default_vim_dir;
4373 *mustfree = FALSE;
4374 }
4375 }
4376 }
4377#endif
4378
4379 /*
4380 * Set the environment variable, so that the new value can be found fast
4381 * next time, and others can also use it (e.g. Perl).
4382 */
4383 if (p != NULL)
4384 {
4385 if (vimruntime)
4386 {
4387 vim_setenv((char_u *)"VIMRUNTIME", p);
4388 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 }
4390 else
4391 {
4392 vim_setenv((char_u *)"VIM", p);
4393 didset_vim = TRUE;
4394 }
4395 }
4396 return p;
4397}
4398
4399/*
4400 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4401 * Return NULL if not, return its name in allocated memory otherwise.
4402 */
4403 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004404vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405{
4406 char_u *p;
4407
4408 if (vimdir == NULL || *vimdir == NUL)
4409 return NULL;
4410 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4411 if (p != NULL && mch_isdir(p))
4412 return p;
4413 vim_free(p);
4414 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4415 if (p != NULL && mch_isdir(p))
4416 return p;
4417 vim_free(p);
4418 return NULL;
4419}
4420
4421/*
4422 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4423 * the length of "name/". Otherwise return "pend".
4424 */
4425 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004426remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427{
4428 int len = (int)STRLEN(name) + 1;
4429 char_u *newend = pend - len;
4430
4431 if (newend >= p
4432 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004433 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 return newend;
4435 return pend;
4436}
4437
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 * Our portable version of setenv.
4440 */
4441 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004442vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443{
4444#ifdef HAVE_SETENV
4445 mch_setenv((char *)name, (char *)val, 1);
4446#else
4447 char_u *envbuf;
4448
4449 /*
4450 * Putenv does not copy the string, it has to remain
4451 * valid. The allocated memory will never be freed.
4452 */
4453 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4454 if (envbuf != NULL)
4455 {
4456 sprintf((char *)envbuf, "%s=%s", name, val);
4457 putenv((char *)envbuf);
4458 }
4459#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004460#ifdef FEAT_GETTEXT
4461 /*
4462 * When setting $VIMRUNTIME adjust the directory to find message
4463 * translations to $VIMRUNTIME/lang.
4464 */
4465 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4466 {
4467 char_u *buf = concat_str(val, (char_u *)"/lang");
4468
4469 if (buf != NULL)
4470 {
4471 bindtextdomain(VIMPACKAGE, (char *)buf);
4472 vim_free(buf);
4473 }
4474 }
4475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476}
4477
4478#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4479/*
4480 * Function given to ExpandGeneric() to obtain an environment variable name.
4481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004483get_env_name(
4484 expand_T *xp UNUSED,
4485 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486{
4487# if defined(AMIGA) || defined(__MRC__) || defined(__SC__)
4488 /*
4489 * No environ[] on the Amiga and on the Mac (using MPW).
4490 */
4491 return NULL;
4492# else
4493# ifndef __WIN32__
4494 /* Borland C++ 5.2 has this in a header file. */
4495 extern char **environ;
4496# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004497# define ENVNAMELEN 100
4498 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 char_u *str;
4500 int n;
4501
4502 str = (char_u *)environ[idx];
4503 if (str == NULL)
4504 return NULL;
4505
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004506 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 {
4508 if (str[n] == '=' || str[n] == NUL)
4509 break;
4510 name[n] = str[n];
4511 }
4512 name[n] = NUL;
4513 return name;
4514# endif
4515}
Bram Moolenaar24305862012-08-15 14:05:05 +02004516
4517/*
4518 * Find all user names for user completion.
4519 * Done only once and then cached.
4520 */
4521 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004522init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004523{
Bram Moolenaar24305862012-08-15 14:05:05 +02004524 static int lazy_init_done = FALSE;
4525
4526 if (lazy_init_done)
4527 return;
4528
4529 lazy_init_done = TRUE;
4530 ga_init2(&ga_users, sizeof(char_u *), 20);
4531
4532# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4533 {
4534 char_u* user;
4535 struct passwd* pw;
4536
4537 setpwent();
4538 while ((pw = getpwent()) != NULL)
4539 /* pw->pw_name shouldn't be NULL but just in case... */
4540 if (pw->pw_name != NULL)
4541 {
4542 if (ga_grow(&ga_users, 1) == FAIL)
4543 break;
4544 user = vim_strsave((char_u*)pw->pw_name);
4545 if (user == NULL)
4546 break;
4547 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4548 }
4549 endpwent();
4550 }
4551# endif
4552}
4553
4554/*
4555 * Function given to ExpandGeneric() to obtain an user names.
4556 */
4557 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004558get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004559{
4560 init_users();
4561 if (idx < ga_users.ga_len)
4562 return ((char_u **)ga_users.ga_data)[idx];
4563 return NULL;
4564}
4565
4566/*
4567 * Check whether name matches a user name. Return:
4568 * 0 if name does not match any user name.
4569 * 1 if name partially matches the beginning of a user name.
4570 * 2 is name fully matches a user name.
4571 */
Bram Moolenaar9b578142016-01-30 19:39:49 +01004572int match_user(char_u* name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004573{
4574 int i;
4575 int n = (int)STRLEN(name);
4576 int result = 0;
4577
4578 init_users();
4579 for (i = 0; i < ga_users.ga_len; i++)
4580 {
4581 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4582 return 2; /* full match */
4583 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4584 result = 1; /* partial match */
4585 }
4586 return result;
4587}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588#endif
4589
4590/*
4591 * Replace home directory by "~" in each space or comma separated file name in
4592 * 'src'.
4593 * If anything fails (except when out of space) dst equals src.
4594 */
4595 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004596home_replace(
4597 buf_T *buf, /* when not NULL, check for help files */
4598 char_u *src, /* input file name */
4599 char_u *dst, /* where to put the result */
4600 int dstlen, /* maximum length of the result */
4601 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 spaces and commas in the file name. */
4603{
4604 size_t dirlen = 0, envlen = 0;
4605 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004606 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 char_u *p;
4608
4609 if (src == NULL)
4610 {
4611 *dst = NUL;
4612 return;
4613 }
4614
4615 /*
4616 * If the file is a help file, remove the path completely.
4617 */
4618 if (buf != NULL && buf->b_help)
4619 {
4620 STRCPY(dst, gettail(src));
4621 return;
4622 }
4623
4624 /*
4625 * We check both the value of the $HOME environment variable and the
4626 * "real" home directory.
4627 */
4628 if (homedir != NULL)
4629 dirlen = STRLEN(homedir);
4630
4631#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004632 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004634 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4635#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004636 /* Empty is the same as not set. */
4637 if (homedir_env != NULL && *homedir_env == NUL)
4638 homedir_env = NULL;
4639
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004640#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004641 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004642 {
4643 int usedlen = 0;
4644 int flen;
4645 char_u *fbuf = NULL;
4646
4647 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004648 (void)modify_fname((char_u *)":p", &usedlen,
4649 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004650 flen = (int)STRLEN(homedir_env);
4651 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4652 /* Remove the trailing / that is added to a directory. */
4653 homedir_env[flen - 1] = NUL;
4654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655#endif
4656
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 if (homedir_env != NULL)
4658 envlen = STRLEN(homedir_env);
4659
4660 if (!one)
4661 src = skipwhite(src);
4662 while (*src && dstlen > 0)
4663 {
4664 /*
4665 * Here we are at the beginning of a file name.
4666 * First, check to see if the beginning of the file name matches
4667 * $HOME or the "real" home directory. Check that there is a '/'
4668 * after the match (so that if e.g. the file is "/home/pieter/bla",
4669 * and the home directory is "/home/piet", the file does not end up
4670 * as "~er/bla" (which would seem to indicate the file "bla" in user
4671 * er's home directory)).
4672 */
4673 p = homedir;
4674 len = dirlen;
4675 for (;;)
4676 {
4677 if ( len
4678 && fnamencmp(src, p, len) == 0
4679 && (vim_ispathsep(src[len])
4680 || (!one && (src[len] == ',' || src[len] == ' '))
4681 || src[len] == NUL))
4682 {
4683 src += len;
4684 if (--dstlen > 0)
4685 *dst++ = '~';
4686
4687 /*
4688 * If it's just the home directory, add "/".
4689 */
4690 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4691 *dst++ = '/';
4692 break;
4693 }
4694 if (p == homedir_env)
4695 break;
4696 p = homedir_env;
4697 len = envlen;
4698 }
4699
4700 /* if (!one) skip to separator: space or comma */
4701 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4702 *dst++ = *src++;
4703 /* skip separator */
4704 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4705 *dst++ = *src++;
4706 }
4707 /* if (dstlen == 0) out of space, what to do??? */
4708
4709 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004710
4711 if (homedir_env != homedir_env_orig)
4712 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713}
4714
4715/*
4716 * Like home_replace, store the replaced string in allocated memory.
4717 * When something fails, NULL is returned.
4718 */
4719 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004720home_replace_save(
4721 buf_T *buf, /* when not NULL, check for help files */
4722 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723{
4724 char_u *dst;
4725 unsigned len;
4726
4727 len = 3; /* space for "~/" and trailing NUL */
4728 if (src != NULL) /* just in case */
4729 len += (unsigned)STRLEN(src);
4730 dst = alloc(len);
4731 if (dst != NULL)
4732 home_replace(buf, src, dst, len, TRUE);
4733 return dst;
4734}
4735
4736/*
4737 * Compare two file names and return:
4738 * FPC_SAME if they both exist and are the same file.
4739 * FPC_SAMEX if they both don't exist and have the same file name.
4740 * FPC_DIFF if they both exist and are different files.
4741 * FPC_NOTX if they both don't exist.
4742 * FPC_DIFFX if one of them doesn't exist.
4743 * For the first name environment variables are expanded
4744 */
4745 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004746fullpathcmp(
4747 char_u *s1,
4748 char_u *s2,
4749 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750{
4751#ifdef UNIX
4752 char_u exp1[MAXPATHL];
4753 char_u full1[MAXPATHL];
4754 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02004755 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 int r1, r2;
4757
4758 expand_env(s1, exp1, MAXPATHL);
4759 r1 = mch_stat((char *)exp1, &st1);
4760 r2 = mch_stat((char *)s2, &st2);
4761 if (r1 != 0 && r2 != 0)
4762 {
4763 /* if mch_stat() doesn't work, may compare the names */
4764 if (checkname)
4765 {
4766 if (fnamecmp(exp1, s2) == 0)
4767 return FPC_SAMEX;
4768 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4769 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4770 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4771 return FPC_SAMEX;
4772 }
4773 return FPC_NOTX;
4774 }
4775 if (r1 != 0 || r2 != 0)
4776 return FPC_DIFFX;
4777 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4778 return FPC_SAME;
4779 return FPC_DIFF;
4780#else
4781 char_u *exp1; /* expanded s1 */
4782 char_u *full1; /* full path of s1 */
4783 char_u *full2; /* full path of s2 */
4784 int retval = FPC_DIFF;
4785 int r1, r2;
4786
4787 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4788 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4789 {
4790 full1 = exp1 + MAXPATHL;
4791 full2 = full1 + MAXPATHL;
4792
4793 expand_env(s1, exp1, MAXPATHL);
4794 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4795 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4796
4797 /* If vim_FullName() fails, the file probably doesn't exist. */
4798 if (r1 != OK && r2 != OK)
4799 {
4800 if (checkname && fnamecmp(exp1, s2) == 0)
4801 retval = FPC_SAMEX;
4802 else
4803 retval = FPC_NOTX;
4804 }
4805 else if (r1 != OK || r2 != OK)
4806 retval = FPC_DIFFX;
4807 else if (fnamecmp(full1, full2))
4808 retval = FPC_DIFF;
4809 else
4810 retval = FPC_SAME;
4811 vim_free(exp1);
4812 }
4813 return retval;
4814#endif
4815}
4816
4817/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004818 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004819 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004820 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 */
4822 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004823gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824{
4825 char_u *p1, *p2;
4826
4827 if (fname == NULL)
4828 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01004829 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01004831 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 p1 = p2 + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004833 mb_ptr_adv(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 }
4835 return p1;
4836}
4837
Bram Moolenaar31710262010-08-13 13:36:15 +02004838#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004839static char_u *gettail_dir(char_u *fname);
Bram Moolenaar31710262010-08-13 13:36:15 +02004840
4841/*
4842 * Return the end of the directory name, on the first path
4843 * separator:
4844 * "/path/file", "/path/dir/", "/path//dir", "/file"
4845 * ^ ^ ^ ^
4846 */
4847 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004848gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02004849{
4850 char_u *dir_end = fname;
4851 char_u *next_dir_end = fname;
4852 int look_for_sep = TRUE;
4853 char_u *p;
4854
4855 for (p = fname; *p != NUL; )
4856 {
4857 if (vim_ispathsep(*p))
4858 {
4859 if (look_for_sep)
4860 {
4861 next_dir_end = p;
4862 look_for_sep = FALSE;
4863 }
4864 }
4865 else
4866 {
4867 if (!look_for_sep)
4868 dir_end = next_dir_end;
4869 look_for_sep = TRUE;
4870 }
4871 mb_ptr_adv(p);
4872 }
4873 return dir_end;
4874}
4875#endif
4876
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004878 * Get pointer to tail of "fname", including path separators. Putting a NUL
4879 * here leaves the directory name. Takes care of "c:/" and "//".
4880 * Always returns a valid pointer.
4881 */
4882 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004883gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004884{
4885 char_u *p;
4886 char_u *t;
4887
4888 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4889 t = gettail(fname);
4890 while (t > p && after_pathsep(fname, t))
4891 --t;
4892#ifdef VMS
4893 /* path separator is part of the path */
4894 ++t;
4895#endif
4896 return t;
4897}
4898
4899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 * get the next path component (just after the next path separator).
4901 */
4902 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004903getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904{
4905 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004906 mb_ptr_adv(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 if (*fname)
4908 ++fname;
4909 return fname;
4910}
4911
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912/*
4913 * Get a pointer to one character past the head of a path name.
4914 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4915 * If there is no head, path is returned.
4916 */
4917 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004918get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919{
4920 char_u *retval;
4921
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004922#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 /* may skip "c:" */
4924 if (isalpha(path[0]) && path[1] == ':')
4925 retval = path + 2;
4926 else
4927 retval = path;
4928#else
4929# if defined(AMIGA)
4930 /* may skip "label:" */
4931 retval = vim_strchr(path, ':');
4932 if (retval == NULL)
4933 retval = path;
4934# else /* Unix */
4935 retval = path;
4936# endif
4937#endif
4938
4939 while (vim_ispathsep(*retval))
4940 ++retval;
4941
4942 return retval;
4943}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944
4945/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01004946 * Return TRUE if 'c' is a path separator.
4947 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 */
4949 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004950vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951{
Bram Moolenaare60acc12011-05-10 16:41:25 +02004952#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02004954#else
4955# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004957# else
4958# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
4960 return (c == ':' || c == '[' || c == ']' || c == '/'
4961 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02004962# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004964# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02004966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967}
4968
Bram Moolenaar69c35002013-11-04 02:54:12 +01004969/*
4970 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
4971 */
4972 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004973vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01004974{
4975 return vim_ispathsep(c)
4976#ifdef BACKSLASH_IN_FILENAME
4977 && c != ':'
4978#endif
4979 ;
4980}
4981
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982#if defined(FEAT_SEARCHPATH) || defined(PROTO)
4983/*
4984 * return TRUE if 'c' is a path list separator.
4985 */
4986 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004987vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988{
4989#ifdef UNIX
4990 return (c == ':');
4991#else
Bram Moolenaar25394022007-05-10 19:06:20 +00004992 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993#endif
4994}
4995#endif
4996
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004997#if defined(FEAT_GUI_TABLINE) || defined(FEAT_WINDOWS) \
4998 || defined(FEAT_EVAL) || defined(PROTO)
4999/*
5000 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5001 * It's done in-place.
5002 */
5003 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005004shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005005{
5006 char_u *tail, *s, *d;
5007 int skip = FALSE;
5008
5009 tail = gettail(str);
5010 d = str;
5011 for (s = str; ; ++s)
5012 {
5013 if (s >= tail) /* copy the whole tail */
5014 {
5015 *d++ = *s;
5016 if (*s == NUL)
5017 break;
5018 }
5019 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5020 {
5021 *d++ = *s;
5022 skip = FALSE;
5023 }
5024 else if (!skip)
5025 {
5026 *d++ = *s; /* copy next char */
5027 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5028 skip = TRUE;
5029# ifdef FEAT_MBYTE
5030 if (has_mbyte)
5031 {
5032 int l = mb_ptr2len(s);
5033
5034 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005035 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005036 }
5037# endif
5038 }
5039 }
5040}
5041#endif
5042
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005043/*
5044 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5045 * Also returns TRUE if there is no directory name.
5046 * "fname" must be writable!.
5047 */
5048 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005049dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005050{
5051 char_u *p;
5052 int c;
5053 int retval;
5054
5055 p = gettail_sep(fname);
5056 if (p == fname)
5057 return TRUE;
5058 c = *p;
5059 *p = NUL;
5060 retval = mch_isdir(fname);
5061 *p = c;
5062 return retval;
5063}
5064
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005066 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5067 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 */
5069 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005070vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005072#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005074#else
5075 if (p_fic)
5076 return MB_STRICMP(x, y);
5077 return STRCMP(x, y);
5078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079}
5080
5081 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005082vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005084#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005085 char_u *px = x;
5086 char_u *py = y;
5087 int cx = NUL;
5088 int cy = NUL;
5089
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005090 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005092 cx = PTR2CHAR(px);
5093 cy = PTR2CHAR(py);
5094 if (cx == NUL || cy == NUL
5095 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5096 && !(cx == '/' && cy == '\\')
5097 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005099 len -= MB_PTR2LEN(px);
5100 px += MB_PTR2LEN(px);
5101 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 }
5103 if (len == 0)
5104 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005105 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005106#else
5107 if (p_fic)
5108 return MB_STRNICMP(x, y, len);
5109 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005111}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112
5113/*
5114 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005115 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 */
5117 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005118concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119{
5120 char_u *dest;
5121
5122 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5123 if (dest != NULL)
5124 {
5125 STRCPY(dest, fname1);
5126 if (sep)
5127 add_pathsep(dest);
5128 STRCAT(dest, fname2);
5129 }
5130 return dest;
5131}
5132
Bram Moolenaard6754642005-01-17 22:18:45 +00005133/*
5134 * Concatenate two strings and return the result in allocated memory.
5135 * Returns NULL when out of memory.
5136 */
5137 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005138concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005139{
5140 char_u *dest;
5141 size_t l = STRLEN(str1);
5142
5143 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5144 if (dest != NULL)
5145 {
5146 STRCPY(dest, str1);
5147 STRCPY(dest + l, str2);
5148 }
5149 return dest;
5150}
Bram Moolenaard6754642005-01-17 22:18:45 +00005151
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152/*
5153 * Add a path separator to a file name, unless it already ends in a path
5154 * separator.
5155 */
5156 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005157add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005159 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 STRCAT(p, PATHSEPSTR);
5161}
5162
5163/*
5164 * FullName_save - Make an allocated copy of a full file name.
5165 * Returns NULL when out of memory.
5166 */
5167 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005168FullName_save(
5169 char_u *fname,
5170 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005171 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172{
5173 char_u *buf;
5174 char_u *new_fname = NULL;
5175
5176 if (fname == NULL)
5177 return NULL;
5178
5179 buf = alloc((unsigned)MAXPATHL);
5180 if (buf != NULL)
5181 {
5182 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5183 new_fname = vim_strsave(buf);
5184 else
5185 new_fname = vim_strsave(fname);
5186 vim_free(buf);
5187 }
5188 return new_fname;
5189}
5190
5191#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5192
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005193static char_u *skip_string(char_u *p);
5194static pos_T *ind_find_start_comment(void);
5195static pos_T *ind_find_start_CORS(void);
5196static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197
5198/*
5199 * Find the start of a comment, not knowing if we are in a comment right now.
5200 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005201 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005203 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005204ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005205{
5206 return find_start_comment(curbuf->b_ind_maxcomment);
5207}
5208
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005210find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211{
5212 pos_T *pos;
5213 char_u *line;
5214 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005215 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005217 for (;;)
5218 {
5219 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5220 if (pos == NULL)
5221 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005223 /*
5224 * Check if the comment start we found is inside a string.
5225 * If it is then restrict the search to below this line and try again.
5226 */
5227 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005228 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005229 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005230 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005231 break;
5232 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5233 if (cur_maxcomment <= 0)
5234 {
5235 pos = NULL;
5236 break;
5237 }
5238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 return pos;
5240}
5241
5242/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005243 * Find the start of a comment or raw string, not knowing if we are in a
5244 * comment or raw string right now.
5245 * Search starts at w_cursor.lnum and goes backwards.
5246 * Return NULL when not inside a comment or raw string.
5247 * "CORS" -> Comment Or Raw String
5248 */
5249 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005250ind_find_start_CORS(void) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005251{
Bram Moolenaar089af182015-10-07 11:41:49 +02005252 static pos_T comment_pos_copy;
5253 pos_T *comment_pos;
5254 pos_T *rs_pos;
5255
5256 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5257 if (comment_pos != NULL)
5258 {
5259 /* Need to make a copy of the static pos in findmatchlimit(),
5260 * calling find_start_rawstring() may change it. */
5261 comment_pos_copy = *comment_pos;
5262 comment_pos = &comment_pos_copy;
5263 }
5264 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005265
5266 /* If comment_pos is before rs_pos the raw string is inside the comment.
5267 * If rs_pos is before comment_pos the comment is inside the raw string. */
5268 if (comment_pos == NULL || (rs_pos != NULL && lt(*rs_pos, *comment_pos)))
5269 return rs_pos;
5270 return comment_pos;
5271}
5272
5273/*
5274 * Find the start of a raw string, not knowing if we are in one right now.
5275 * Search starts at w_cursor.lnum and goes backwards.
5276 * Return NULL when not inside a raw string.
5277 */
5278 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005279find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005280{
5281 pos_T *pos;
5282 char_u *line;
5283 char_u *p;
5284 int cur_maxcomment = ind_maxcomment;
5285
5286 for (;;)
5287 {
5288 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5289 if (pos == NULL)
5290 break;
5291
5292 /*
5293 * Check if the raw string start we found is inside a string.
5294 * If it is then restrict the search to below this line and try again.
5295 */
5296 line = ml_get(pos->lnum);
5297 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5298 p = skip_string(p);
5299 if ((colnr_T)(p - line) <= pos->col)
5300 break;
5301 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5302 if (cur_maxcomment <= 0)
5303 {
5304 pos = NULL;
5305 break;
5306 }
5307 }
5308 return pos;
5309}
5310
5311/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 * Skip to the end of a "string" and a 'c' character.
5313 * If there is no string or character, return argument unmodified.
5314 */
5315 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005316skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317{
5318 int i;
5319
5320 /*
5321 * We loop, because strings may be concatenated: "date""time".
5322 */
5323 for ( ; ; ++p)
5324 {
5325 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5326 {
5327 if (!p[1]) /* ' at end of line */
5328 break;
5329 i = 2;
5330 if (p[1] == '\\') /* '\n' or '\000' */
5331 {
5332 ++i;
5333 while (vim_isdigit(p[i - 1])) /* '\000' */
5334 ++i;
5335 }
5336 if (p[i] == '\'') /* check for trailing ' */
5337 {
5338 p += i;
5339 continue;
5340 }
5341 }
5342 else if (p[0] == '"') /* start of string */
5343 {
5344 for (++p; p[0]; ++p)
5345 {
5346 if (p[0] == '\\' && p[1] != NUL)
5347 ++p;
5348 else if (p[0] == '"') /* end of string */
5349 break;
5350 }
5351 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005352 continue; /* continue for another string */
5353 }
5354 else if (p[0] == 'R' && p[1] == '"')
5355 {
5356 /* Raw string: R"[delim](...)[delim]" */
5357 char_u *delim = p + 2;
5358 char_u *paren = vim_strchr(delim, '(');
5359
5360 if (paren != NULL)
5361 {
5362 size_t delim_len = paren - delim;
5363
5364 for (p += 3; *p; ++p)
5365 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5366 && p[delim_len + 1] == '"')
5367 {
5368 p += delim_len + 1;
5369 break;
5370 }
5371 if (p[0] == '"')
5372 continue; /* continue for another string */
5373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 }
5375 break; /* no string found */
5376 }
5377 if (!*p)
5378 --p; /* backup from NUL */
5379 return p;
5380}
5381#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5382
5383#if defined(FEAT_CINDENT) || defined(PROTO)
5384
5385/*
5386 * Do C or expression indenting on the current line.
5387 */
5388 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005389do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390{
5391# ifdef FEAT_EVAL
5392 if (*curbuf->b_p_inde != NUL)
5393 fixthisline(get_expr_indent);
5394 else
5395# endif
5396 fixthisline(get_c_indent);
5397}
5398
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005399/* Find result cache for cpp_baseclass */
5400typedef struct {
5401 int found;
5402 lpos_T lpos;
5403} cpp_baseclass_cache_T;
5404
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405/*
5406 * Functions for C-indenting.
5407 * Most of this originally comes from Eric Fischer.
5408 */
5409/*
5410 * Below "XXX" means that this function may unlock the current line.
5411 */
5412
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005413static char_u *cin_skipcomment(char_u *);
5414static int cin_nocode(char_u *);
5415static pos_T *find_line_comment(void);
5416static int cin_has_js_key(char_u *text);
5417static int cin_islabel_skip(char_u **);
5418static int cin_isdefault(char_u *);
5419static char_u *after_label(char_u *l);
5420static int get_indent_nolabel(linenr_T lnum);
5421static int skip_label(linenr_T, char_u **pp);
5422static int cin_first_id_amount(void);
5423static int cin_get_equal_amount(linenr_T lnum);
5424static int cin_ispreproc(char_u *);
5425static int cin_ispreproc_cont(char_u **pp, linenr_T *lnump);
5426static 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;
5765
5766 s = cin_skipcomment(s);
5767 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5768 {
5769 p = cin_skipcomment(skipwhite(s + 9));
5770 while (*p != NUL)
5771 {
5772 if (vim_iswhite(*p))
5773 {
5774 has_name = TRUE; /* found end of a name */
5775 p = cin_skipcomment(skipwhite(p));
5776 }
5777 else if (*p == '{')
5778 {
5779 break;
5780 }
5781 else if (vim_iswordc(*p))
5782 {
5783 if (has_name)
5784 return FALSE; /* word character after skipping past name */
5785 ++p;
5786 }
5787 else
5788 {
5789 return FALSE;
5790 }
5791 }
5792 return TRUE;
5793 }
5794 return FALSE;
5795}
5796
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797/*
5798 * Return a pointer to the first non-empty non-comment character after a ':'.
5799 * Return NULL if not found.
5800 * case 234: a = b;
5801 * ^
5802 */
5803 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005804after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805{
5806 for ( ; *l; ++l)
5807 {
5808 if (*l == ':')
5809 {
5810 if (l[1] == ':') /* skip over "::" for C++ */
5811 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005812 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 break;
5814 }
5815 else if (*l == '\'' && l[1] && l[2] == '\'')
5816 l += 2; /* skip over 'x' */
5817 }
5818 if (*l == NUL)
5819 return NULL;
5820 l = cin_skipcomment(l + 1);
5821 if (*l == NUL)
5822 return NULL;
5823 return l;
5824}
5825
5826/*
5827 * Get indent of line "lnum", skipping a label.
5828 * Return 0 if there is nothing after the label.
5829 */
5830 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005831get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832{
5833 char_u *l;
5834 pos_T fp;
5835 colnr_T col;
5836 char_u *p;
5837
5838 l = ml_get(lnum);
5839 p = after_label(l);
5840 if (p == NULL)
5841 return 0;
5842
5843 fp.col = (colnr_T)(p - l);
5844 fp.lnum = lnum;
5845 getvcol(curwin, &fp, &col, NULL, NULL);
5846 return (int)col;
5847}
5848
5849/*
5850 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005851 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 * label: if (asdf && asdfasdf)
5853 * ^
5854 */
5855 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005856skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857{
5858 char_u *l;
5859 int amount;
5860 pos_T cursor_save;
5861
5862 cursor_save = curwin->w_cursor;
5863 curwin->w_cursor.lnum = lnum;
5864 l = ml_get_curline();
5865 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005866 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 {
5868 amount = get_indent_nolabel(lnum);
5869 l = after_label(ml_get_curline());
5870 if (l == NULL) /* just in case */
5871 l = ml_get_curline();
5872 }
5873 else
5874 {
5875 amount = get_indent();
5876 l = ml_get_curline();
5877 }
5878 *pp = l;
5879
5880 curwin->w_cursor = cursor_save;
5881 return amount;
5882}
5883
5884/*
5885 * Return the indent of the first variable name after a type in a declaration.
5886 * int a, indent of "a"
5887 * static struct foo b, indent of "b"
5888 * enum bla c, indent of "c"
5889 * Returns zero when it doesn't look like a declaration.
5890 */
5891 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005892cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893{
5894 char_u *line, *p, *s;
5895 int len;
5896 pos_T fp;
5897 colnr_T col;
5898
5899 line = ml_get_curline();
5900 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005901 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 if (len == 6 && STRNCMP(p, "static", 6) == 0)
5903 {
5904 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005905 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 }
5907 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
5908 p = skipwhite(p + 6);
5909 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
5910 p = skipwhite(p + 4);
5911 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
5912 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
5913 {
5914 s = skipwhite(p + len);
5915 if ((STRNCMP(s, "int", 3) == 0 && vim_iswhite(s[3]))
5916 || (STRNCMP(s, "long", 4) == 0 && vim_iswhite(s[4]))
5917 || (STRNCMP(s, "short", 5) == 0 && vim_iswhite(s[5]))
5918 || (STRNCMP(s, "char", 4) == 0 && vim_iswhite(s[4])))
5919 p = s;
5920 }
5921 for (len = 0; vim_isIDc(p[len]); ++len)
5922 ;
5923 if (len == 0 || !vim_iswhite(p[len]) || cin_nocode(p))
5924 return 0;
5925
5926 p = skipwhite(p + len);
5927 fp.lnum = curwin->w_cursor.lnum;
5928 fp.col = (colnr_T)(p - line);
5929 getvcol(curwin, &fp, &col, NULL, NULL);
5930 return (int)col;
5931}
5932
5933/*
5934 * Return the indent of the first non-blank after an equal sign.
5935 * char *foo = "here";
5936 * Return zero if no (useful) equal sign found.
5937 * Return -1 if the line above "lnum" ends in a backslash.
5938 * foo = "asdf\
5939 * asdf\
5940 * here";
5941 */
5942 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005943cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944{
5945 char_u *line;
5946 char_u *s;
5947 colnr_T col;
5948 pos_T fp;
5949
5950 if (lnum > 1)
5951 {
5952 line = ml_get(lnum - 1);
5953 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
5954 return -1;
5955 }
5956
5957 line = s = ml_get(lnum);
5958 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
5959 {
5960 if (cin_iscomment(s)) /* ignore comments */
5961 s = cin_skipcomment(s);
5962 else
5963 ++s;
5964 }
5965 if (*s != '=')
5966 return 0;
5967
5968 s = skipwhite(s + 1);
5969 if (cin_nocode(s))
5970 return 0;
5971
5972 if (*s == '"') /* nice alignment for continued strings */
5973 ++s;
5974
5975 fp.lnum = lnum;
5976 fp.col = (colnr_T)(s - line);
5977 getvcol(curwin, &fp, &col, NULL, NULL);
5978 return (int)col;
5979}
5980
5981/*
5982 * Recognize a preprocessor statement: Any line that starts with '#'.
5983 */
5984 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005985cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02005987 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 return TRUE;
5989 return FALSE;
5990}
5991
5992/*
5993 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
5994 * continuation line of a preprocessor statement. Decrease "*lnump" to the
5995 * start and return the line in "*pp".
5996 */
5997 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005998cin_ispreproc_cont(char_u **pp, linenr_T *lnump)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999{
6000 char_u *line = *pp;
6001 linenr_T lnum = *lnump;
6002 int retval = FALSE;
6003
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006004 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 {
6006 if (cin_ispreproc(line))
6007 {
6008 retval = TRUE;
6009 *lnump = lnum;
6010 break;
6011 }
6012 if (lnum == 1)
6013 break;
6014 line = ml_get(--lnum);
6015 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6016 break;
6017 }
6018
6019 if (lnum != *lnump)
6020 *pp = ml_get(*lnump);
6021 return retval;
6022}
6023
6024/*
6025 * Recognize the start of a C or C++ comment.
6026 */
6027 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006028cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029{
6030 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6031}
6032
6033/*
6034 * Recognize the start of a "//" comment.
6035 */
6036 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006037cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038{
6039 return (p[0] == '/' && p[1] == '/');
6040}
6041
6042/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006043 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6044 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006046 * If a line begins with an "else", only consider it terminated if no unmatched
6047 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 * Return the character terminating the line (ending char's have precedence if
6049 * both apply in order to determine initializations).
6050 */
6051 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006052cin_isterminated(
6053 char_u *s,
6054 int incl_open, /* include '{' at the end as terminator */
6055 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006057 char_u found_start = 0;
6058 unsigned n_open = 0;
6059 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060
6061 s = cin_skipcomment(s);
6062
6063 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6064 found_start = *s;
6065
Bram Moolenaar496f9512011-05-19 16:35:09 +02006066 if (!found_start)
6067 is_else = cin_iselse(s);
6068
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 while (*s)
6070 {
6071 /* skip over comments, "" strings and 'c'haracters */
6072 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006073 if (*s == '}' && n_open > 0)
6074 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006075 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006076 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 && cin_nocode(s + 1))
6078 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006079 else if (*s == '{')
6080 {
6081 if (incl_open && cin_nocode(s + 1))
6082 return *s;
6083 else
6084 ++n_open;
6085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086
6087 if (*s)
6088 s++;
6089 }
6090 return found_start;
6091}
6092
6093/*
6094 * Recognize the basic picture of a function declaration -- it needs to
6095 * have an open paren somewhere and a close paren at the end of the line and
6096 * no semicolons anywhere.
6097 * When a line ends in a comma we continue looking in the next line.
6098 * "sp" points to a string with the line. When looking at other lines it must
6099 * be restored to the line. When it's NULL fetch lines here.
6100 * "lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006101 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 */
6103 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006104cin_isfuncdecl(
6105 char_u **sp,
6106 linenr_T first_lnum,
6107 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108{
6109 char_u *s;
6110 linenr_T lnum = first_lnum;
6111 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006112 pos_T *trypos;
6113 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114
6115 if (sp == NULL)
6116 s = ml_get(lnum);
6117 else
6118 s = *sp;
6119
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006120 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006121 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006122 {
6123 lnum = trypos->lnum;
6124 if (lnum < min_lnum)
6125 return FALSE;
6126
6127 s = ml_get(lnum);
6128 }
6129
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006130 /* Ignore line starting with #. */
6131 if (cin_ispreproc(s))
6132 return FALSE;
6133
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6135 {
6136 if (cin_iscomment(s)) /* ignore comments */
6137 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006138 else if (*s == ':')
6139 {
6140 if (*(s + 1) == ':')
6141 s += 2;
6142 else
6143 /* To avoid a mistake in the following situation:
6144 * A::A(int a, int b)
6145 * : a(0) // <--not a function decl
6146 * , b(0)
6147 * {...
6148 */
6149 return FALSE;
6150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 else
6152 ++s;
6153 }
6154 if (*s != '(')
6155 return FALSE; /* ';', ' or " before any () or no '(' */
6156
6157 while (*s && *s != ';' && *s != '\'' && *s != '"')
6158 {
6159 if (*s == ')' && cin_nocode(s + 1))
6160 {
6161 /* ')' at the end: may have found a match
6162 * Check for he previous line not to end in a backslash:
6163 * #if defined(x) && \
6164 * defined(y)
6165 */
6166 lnum = first_lnum - 1;
6167 s = ml_get(lnum);
6168 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6169 retval = TRUE;
6170 goto done;
6171 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006172 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006174 int comma = (*s == ',');
6175
6176 /* ',' at the end: continue looking in the next line.
6177 * At the end: check for ',' in the next line, for this style:
6178 * func(arg1
6179 * , arg2) */
6180 for (;;)
6181 {
6182 if (lnum >= curbuf->b_ml.ml_line_count)
6183 break;
6184 s = ml_get(++lnum);
6185 if (!cin_ispreproc(s))
6186 break;
6187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 if (lnum >= curbuf->b_ml.ml_line_count)
6189 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006190 /* Require a comma at end of the line or a comma or ')' at the
6191 * start of next line. */
6192 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006193 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006194 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006195 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 }
6197 else if (cin_iscomment(s)) /* ignore comments */
6198 s = cin_skipcomment(s);
6199 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006200 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006202 just_started = FALSE;
6203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 }
6205
6206done:
6207 if (lnum != first_lnum && sp != NULL)
6208 *sp = ml_get(first_lnum);
6209
6210 return retval;
6211}
6212
6213 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006214cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006216 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217}
6218
6219 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006220cin_iselse(
6221 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222{
6223 if (*p == '}') /* accept "} else" */
6224 p = cin_skipcomment(p + 1);
6225 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6226}
6227
6228 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006229cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230{
6231 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6232}
6233
6234/*
6235 * Check if this is a "while" that should have a matching "do".
6236 * We only accept a "while (condition) ;", with only white space between the
6237 * ')' and ';'. The condition may be spread over several lines.
6238 */
6239 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006240cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241{
6242 pos_T cursor_save;
6243 pos_T *trypos;
6244 int retval = FALSE;
6245
6246 p = cin_skipcomment(p);
6247 if (*p == '}') /* accept "} while (cond);" */
6248 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006249 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 {
6251 cursor_save = curwin->w_cursor;
6252 curwin->w_cursor.lnum = lnum;
6253 curwin->w_cursor.col = 0;
6254 p = ml_get_curline();
6255 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6256 {
6257 ++p;
6258 ++curwin->w_cursor.col;
6259 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006260 if ((trypos = findmatchlimit(NULL, 0, 0,
6261 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6263 retval = TRUE;
6264 curwin->w_cursor = cursor_save;
6265 }
6266 return retval;
6267}
6268
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006269/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006270 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006271 * Return 0 if there is none.
6272 * Otherwise return !0 and update "*poffset" to point to the place where the
6273 * string was found.
6274 */
6275 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006276cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006277{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006278 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006279
6280 if (offset-- < 2)
6281 return 0;
6282 while (offset > 2 && vim_iswhite(line[offset]))
6283 --offset;
6284
6285 offset -= 1;
6286 if (!STRNCMP(line + offset, "if", 2))
6287 goto probablyFound;
6288
6289 if (offset >= 1)
6290 {
6291 offset -= 1;
6292 if (!STRNCMP(line + offset, "for", 3))
6293 goto probablyFound;
6294
6295 if (offset >= 2)
6296 {
6297 offset -= 2;
6298 if (!STRNCMP(line + offset, "while", 5))
6299 goto probablyFound;
6300 }
6301 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006302 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006303
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006304probablyFound:
6305 if (!offset || !vim_isIDc(line[offset - 1]))
6306 {
6307 *poffset = offset;
6308 return 1;
6309 }
6310 return 0;
6311}
6312
6313/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006314 * Return TRUE if we are at the end of a do-while.
6315 * do
6316 * nothing;
6317 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006318 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006319 * Adjust the cursor to the line with "while".
6320 */
6321 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006322cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006323{
6324 char_u *line;
6325 char_u *p;
6326 char_u *s;
6327 pos_T *trypos;
6328 int i;
6329
6330 if (terminated != ';') /* there must be a ';' at the end */
6331 return FALSE;
6332
6333 p = line = ml_get_curline();
6334 while (*p != NUL)
6335 {
6336 p = cin_skipcomment(p);
6337 if (*p == ')')
6338 {
6339 s = skipwhite(p + 1);
6340 if (*s == ';' && cin_nocode(s + 1))
6341 {
6342 /* Found ");" at end of the line, now check there is "while"
6343 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006344 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006345 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006346 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006347 if (trypos != NULL)
6348 {
6349 s = cin_skipcomment(ml_get(trypos->lnum));
6350 if (*s == '}') /* accept "} while (cond);" */
6351 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006352 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006353 {
6354 curwin->w_cursor.lnum = trypos->lnum;
6355 return TRUE;
6356 }
6357 }
6358
6359 /* Searching may have made "line" invalid, get it again. */
6360 line = ml_get_curline();
6361 p = line + i;
6362 }
6363 }
6364 if (*p != NUL)
6365 ++p;
6366 }
6367 return FALSE;
6368}
6369
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006371cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372{
6373 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6374}
6375
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006376/*
6377 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 * constructor-initialization. eg:
6379 *
6380 * class MyClass :
6381 * baseClass <-- here
6382 * class MyClass : public baseClass,
6383 * anotherBaseClass <-- here (should probably lineup ??)
6384 * MyClass::MyClass(...) :
6385 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006386 *
6387 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 */
6389 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006390cin_is_cpp_baseclass(
6391 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006393 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 char_u *s;
6395 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006396 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006397 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006399 if (pos->lnum <= lnum)
6400 return cached->found; /* Use the cached result */
6401
6402 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006404 s = skipwhite(line);
6405 if (*s == '#') /* skip #define FOO x ? (x) : x */
6406 return FALSE;
6407 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 if (*s == NUL)
6409 return FALSE;
6410
6411 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6412
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006413 /* Search for a line starting with '#', empty, ending in ';' or containing
6414 * '{' or '}' and start below it. This handles the following situations:
6415 * a = cond ?
6416 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006417 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006418 * func::foo()
6419 * : something
6420 * {}
6421 * Foo::Foo (int one, int two)
6422 * : something(4),
6423 * somethingelse(3)
6424 * {}
6425 */
6426 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006428 line = ml_get(lnum - 1);
6429 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006430 if (*s == '#' || *s == NUL)
6431 break;
6432 while (*s != NUL)
6433 {
6434 s = cin_skipcomment(s);
6435 if (*s == '{' || *s == '}'
6436 || (*s == ';' && cin_nocode(s + 1)))
6437 break;
6438 if (*s != NUL)
6439 ++s;
6440 }
6441 if (*s != NUL)
6442 break;
6443 --lnum;
6444 }
6445
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006446 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006447 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006448 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006449 for (;;)
6450 {
6451 if (*s == NUL)
6452 {
6453 if (lnum == curwin->w_cursor.lnum)
6454 break;
6455 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006456 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006457 s = line;
6458 }
6459 if (s == line)
6460 {
6461 /* don't recognize "case (foo):" as a baseclass */
6462 if (cin_iscase(s, FALSE))
6463 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006464 s = cin_skipcomment(line);
6465 if (*s == NUL)
6466 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006467 }
6468
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006469 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006470 s = skip_string(s) + 1;
6471 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 {
6473 if (s[1] == ':')
6474 {
6475 /* skip double colon. It can't be a constructor
6476 * initialization any more */
6477 lookfor_ctor_init = FALSE;
6478 s = cin_skipcomment(s + 2);
6479 }
6480 else if (lookfor_ctor_init || class_or_struct)
6481 {
6482 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006483 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 cpp_base_class = TRUE;
6485 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006486 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 s = cin_skipcomment(s + 1);
6488 }
6489 else
6490 s = cin_skipcomment(s + 1);
6491 }
6492 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6493 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6494 {
6495 class_or_struct = TRUE;
6496 lookfor_ctor_init = FALSE;
6497
6498 if (*s == 'c')
6499 s = cin_skipcomment(s + 5);
6500 else
6501 s = cin_skipcomment(s + 6);
6502 }
6503 else
6504 {
6505 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6506 {
6507 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6508 }
6509 else if (s[0] == ')')
6510 {
6511 /* Constructor-initialization is assumed if we come across
6512 * something like "):" */
6513 class_or_struct = FALSE;
6514 lookfor_ctor_init = TRUE;
6515 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006516 else if (s[0] == '?')
6517 {
6518 /* Avoid seeing '() :' after '?' as constructor init. */
6519 return FALSE;
6520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 else if (!vim_isIDc(s[0]))
6522 {
6523 /* if it is not an identifier, we are wrong */
6524 class_or_struct = FALSE;
6525 lookfor_ctor_init = FALSE;
6526 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006527 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 {
6529 /* it can't be a constructor-initialization any more */
6530 lookfor_ctor_init = FALSE;
6531
6532 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006533 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006534 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 }
6536
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006537 /* When the line ends in a comma don't align with it. */
6538 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006539 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006540
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 s = cin_skipcomment(s + 1);
6542 }
6543 }
6544
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006545 cached->found = cpp_base_class;
6546 if (cpp_base_class)
6547 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 return cpp_base_class;
6549}
6550
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006551 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006552get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006553{
6554 int amount;
6555 colnr_T vcol;
6556 pos_T *trypos;
6557
6558 if (col == 0)
6559 {
6560 amount = get_indent();
6561 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006562 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006563 amount = get_indent_lnum(trypos->lnum); /* XXX */
6564 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006565 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006566 }
6567 else
6568 {
6569 curwin->w_cursor.col = col;
6570 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6571 amount = (int)vcol;
6572 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006573 if (amount < curbuf->b_ind_cpp_baseclass)
6574 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006575 return amount;
6576}
6577
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578/*
6579 * Return TRUE if string "s" ends with the string "find", possibly followed by
6580 * white space and comments. Skip strings and comments.
6581 * Ignore "ignore" after "find" if it's not NULL.
6582 */
6583 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006584cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585{
6586 char_u *p = s;
6587 char_u *r;
6588 int len = (int)STRLEN(find);
6589
6590 while (*p != NUL)
6591 {
6592 p = cin_skipcomment(p);
6593 if (STRNCMP(p, find, len) == 0)
6594 {
6595 r = skipwhite(p + len);
6596 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6597 r = skipwhite(r + STRLEN(ignore));
6598 if (cin_nocode(r))
6599 return TRUE;
6600 }
6601 if (*p != NUL)
6602 ++p;
6603 }
6604 return FALSE;
6605}
6606
6607/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006608 * Return TRUE when "s" starts with "word" and then a non-ID character.
6609 */
6610 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006611cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006612{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006613 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006614
6615 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6616}
6617
6618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 * Skip strings, chars and comments until at or past "trypos".
6620 * Return the column found.
6621 */
6622 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006623cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624{
6625 char_u *line;
6626 char_u *p;
6627
6628 p = line = ml_get(trypos->lnum);
6629 while (*p && (colnr_T)(p - line) < trypos->col)
6630 {
6631 if (cin_iscomment(p))
6632 p = cin_skipcomment(p);
6633 else
6634 {
6635 p = skip_string(p);
6636 ++p;
6637 }
6638 }
6639 return (int)(p - line);
6640}
6641
6642/*
6643 * Find the '{' at the start of the block we are in.
6644 * Return NULL if no match found.
6645 * Ignore a '{' that is in a comment, makes indenting the next three lines
6646 * work. */
6647/* foo() */
6648/* { */
6649/* } */
6650
6651 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006652find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653{
6654 pos_T cursor_save;
6655 pos_T *trypos;
6656 pos_T *pos;
6657 static pos_T pos_copy;
6658
6659 cursor_save = curwin->w_cursor;
6660 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6661 {
6662 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6663 trypos = &pos_copy;
6664 curwin->w_cursor = *trypos;
6665 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006666 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006668 && (pos = ind_find_start_CORS()) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 break;
6670 if (pos != NULL)
6671 curwin->w_cursor.lnum = pos->lnum;
6672 }
6673 curwin->w_cursor = cursor_save;
6674 return trypos;
6675}
6676
6677/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006678 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006679 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 */
6681 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006682find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006684 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006685}
6686
6687 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006688find_match_char (int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006689{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 pos_T cursor_save;
6691 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006692 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006693 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694
6695 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006696 ind_maxp_wk = ind_maxparen;
6697retry:
6698 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 {
6700 /* check if the ( is in a // comment */
6701 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01006702 {
6703 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
6704 if (ind_maxp_wk > 0)
6705 {
6706 curwin->w_cursor = *trypos;
6707 curwin->w_cursor.col = 0; /* XXX */
6708 goto retry;
6709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 else
6713 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01006714 pos_T *trypos_wk;
6715
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6717 trypos = &pos_copy;
6718 curwin->w_cursor = *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006719 if ((trypos_wk = ind_find_start_CORS()) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01006720 {
6721 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
6722 - trypos_wk->lnum);
6723 if (ind_maxp_wk > 0)
6724 {
6725 curwin->w_cursor = *trypos_wk;
6726 goto retry;
6727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 }
6731 }
6732 curwin->w_cursor = cursor_save;
6733 return trypos;
6734}
6735
6736/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006737 * Find the matching '(', ignoring it if it is in a comment or before an
6738 * unmatched {.
6739 * Return NULL if no match found.
6740 */
6741 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006742find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02006743{
6744 pos_T *trypos = find_match_paren(ind_maxparen);
6745
6746 if (trypos != NULL)
6747 {
6748 pos_T *tryposBrace = find_start_brace();
6749
6750 /* If both an unmatched '(' and '{' is found. Ignore the '('
6751 * position if the '{' is further down. */
6752 if (tryposBrace != NULL
6753 && (trypos->lnum != tryposBrace->lnum
6754 ? trypos->lnum < tryposBrace->lnum
6755 : trypos->col < tryposBrace->col))
6756 trypos = NULL;
6757 }
6758 return trypos;
6759}
6760
6761/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 * Return ind_maxparen corrected for the difference in line number between the
6763 * cursor position and "startpos". This makes sure that searching for a
6764 * matching paren above the cursor line doesn't find a match because of
6765 * looking a few lines further.
6766 */
6767 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006768corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769{
6770 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6771
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006772 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
6773 return curbuf->b_ind_maxparen - (int)n;
6774 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775}
6776
6777/*
6778 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006779 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 */
6781 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006782find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783{
6784 int i;
6785 int retval = FALSE;
6786 int open_count = 0;
6787
6788 curwin->w_cursor.col = 0; /* default is start of line */
6789
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006790 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 {
6792 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6793 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6794 if (l[i] == start)
6795 ++open_count;
6796 else if (l[i] == end)
6797 {
6798 if (open_count > 0)
6799 --open_count;
6800 else
6801 {
6802 curwin->w_cursor.col = i;
6803 retval = TRUE;
6804 }
6805 }
6806 }
6807 return retval;
6808}
6809
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006810/*
6811 * Parse 'cinoptions' and set the values in "curbuf".
6812 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
6813 */
6814 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006815parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006816{
6817 char_u *p;
6818 char_u *l;
6819 char_u *digits;
6820 int n;
6821 int divider;
6822 int fraction = 0;
6823 int sw = (int)get_sw_value(buf);
6824
6825 /*
6826 * Set the default values.
6827 */
6828 /* Spaces from a block's opening brace the prevailing indent for that
6829 * block should be. */
6830 buf->b_ind_level = sw;
6831
6832 /* Spaces from the edge of the line an open brace that's at the end of a
6833 * line is imagined to be. */
6834 buf->b_ind_open_imag = 0;
6835
6836 /* Spaces from the prevailing indent for a line that is not preceded by
6837 * an opening brace. */
6838 buf->b_ind_no_brace = 0;
6839
6840 /* Column where the first { of a function should be located }. */
6841 buf->b_ind_first_open = 0;
6842
6843 /* Spaces from the prevailing indent a leftmost open brace should be
6844 * located. */
6845 buf->b_ind_open_extra = 0;
6846
6847 /* Spaces from the matching open brace (real location for one at the left
6848 * edge; imaginary location from one that ends a line) the matching close
6849 * brace should be located. */
6850 buf->b_ind_close_extra = 0;
6851
6852 /* Spaces from the edge of the line an open brace sitting in the leftmost
6853 * column is imagined to be. */
6854 buf->b_ind_open_left_imag = 0;
6855
6856 /* Spaces jump labels should be shifted to the left if N is non-negative,
6857 * otherwise the jump label will be put to column 1. */
6858 buf->b_ind_jump_label = -1;
6859
6860 /* Spaces from the switch() indent a "case xx" label should be located. */
6861 buf->b_ind_case = sw;
6862
6863 /* Spaces from the "case xx:" code after a switch() should be located. */
6864 buf->b_ind_case_code = sw;
6865
6866 /* Lineup break at end of case in switch() with case label. */
6867 buf->b_ind_case_break = 0;
6868
6869 /* Spaces from the class declaration indent a scope declaration label
6870 * should be located. */
6871 buf->b_ind_scopedecl = sw;
6872
6873 /* Spaces from the scope declaration label code should be located. */
6874 buf->b_ind_scopedecl_code = sw;
6875
6876 /* Amount K&R-style parameters should be indented. */
6877 buf->b_ind_param = sw;
6878
6879 /* Amount a function type spec should be indented. */
6880 buf->b_ind_func_type = sw;
6881
6882 /* Amount a cpp base class declaration or constructor initialization
6883 * should be indented. */
6884 buf->b_ind_cpp_baseclass = sw;
6885
6886 /* additional spaces beyond the prevailing indent a continuation line
6887 * should be located. */
6888 buf->b_ind_continuation = sw;
6889
6890 /* Spaces from the indent of the line with an unclosed parentheses. */
6891 buf->b_ind_unclosed = sw * 2;
6892
6893 /* Spaces from the indent of the line with an unclosed parentheses, which
6894 * itself is also unclosed. */
6895 buf->b_ind_unclosed2 = sw;
6896
6897 /* Suppress ignoring spaces from the indent of a line starting with an
6898 * unclosed parentheses. */
6899 buf->b_ind_unclosed_noignore = 0;
6900
6901 /* If the opening paren is the last nonwhite character on the line, and
6902 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
6903 * context (for very long lines). */
6904 buf->b_ind_unclosed_wrapped = 0;
6905
6906 /* Suppress ignoring white space when lining up with the character after
6907 * an unclosed parentheses. */
6908 buf->b_ind_unclosed_whiteok = 0;
6909
6910 /* Indent a closing parentheses under the line start of the matching
6911 * opening parentheses. */
6912 buf->b_ind_matching_paren = 0;
6913
6914 /* Indent a closing parentheses under the previous line. */
6915 buf->b_ind_paren_prev = 0;
6916
6917 /* Extra indent for comments. */
6918 buf->b_ind_comment = 0;
6919
6920 /* Spaces from the comment opener when there is nothing after it. */
6921 buf->b_ind_in_comment = 3;
6922
6923 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
6924 * after the comment opener. */
6925 buf->b_ind_in_comment2 = 0;
6926
6927 /* Max lines to search for an open paren. */
6928 buf->b_ind_maxparen = 20;
6929
6930 /* Max lines to search for an open comment. */
6931 buf->b_ind_maxcomment = 70;
6932
6933 /* Handle braces for java code. */
6934 buf->b_ind_java = 0;
6935
6936 /* Not to confuse JS object properties with labels. */
6937 buf->b_ind_js = 0;
6938
6939 /* Handle blocked cases correctly. */
6940 buf->b_ind_keep_case_label = 0;
6941
6942 /* Handle C++ namespace. */
6943 buf->b_ind_cpp_namespace = 0;
6944
6945 /* Handle continuation lines containing conditions of if(), for() and
6946 * while(). */
6947 buf->b_ind_if_for_while = 0;
6948
6949 for (p = buf->b_p_cino; *p; )
6950 {
6951 l = p++;
6952 if (*p == '-')
6953 ++p;
6954 digits = p; /* remember where the digits start */
6955 n = getdigits(&p);
6956 divider = 0;
6957 if (*p == '.') /* ".5s" means a fraction */
6958 {
6959 fraction = atol((char *)++p);
6960 while (VIM_ISDIGIT(*p))
6961 {
6962 ++p;
6963 if (divider)
6964 divider *= 10;
6965 else
6966 divider = 10;
6967 }
6968 }
6969 if (*p == 's') /* "2s" means two times 'shiftwidth' */
6970 {
6971 if (p == digits)
6972 n = sw; /* just "s" is one 'shiftwidth' */
6973 else
6974 {
6975 n *= sw;
6976 if (divider)
6977 n += (sw * fraction + divider / 2) / divider;
6978 }
6979 ++p;
6980 }
6981 if (l[1] == '-')
6982 n = -n;
6983
6984 /* When adding an entry here, also update the default 'cinoptions' in
6985 * doc/indent.txt, and add explanation for it! */
6986 switch (*l)
6987 {
6988 case '>': buf->b_ind_level = n; break;
6989 case 'e': buf->b_ind_open_imag = n; break;
6990 case 'n': buf->b_ind_no_brace = n; break;
6991 case 'f': buf->b_ind_first_open = n; break;
6992 case '{': buf->b_ind_open_extra = n; break;
6993 case '}': buf->b_ind_close_extra = n; break;
6994 case '^': buf->b_ind_open_left_imag = n; break;
6995 case 'L': buf->b_ind_jump_label = n; break;
6996 case ':': buf->b_ind_case = n; break;
6997 case '=': buf->b_ind_case_code = n; break;
6998 case 'b': buf->b_ind_case_break = n; break;
6999 case 'p': buf->b_ind_param = n; break;
7000 case 't': buf->b_ind_func_type = n; break;
7001 case '/': buf->b_ind_comment = n; break;
7002 case 'c': buf->b_ind_in_comment = n; break;
7003 case 'C': buf->b_ind_in_comment2 = n; break;
7004 case 'i': buf->b_ind_cpp_baseclass = n; break;
7005 case '+': buf->b_ind_continuation = n; break;
7006 case '(': buf->b_ind_unclosed = n; break;
7007 case 'u': buf->b_ind_unclosed2 = n; break;
7008 case 'U': buf->b_ind_unclosed_noignore = n; break;
7009 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7010 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7011 case 'm': buf->b_ind_matching_paren = n; break;
7012 case 'M': buf->b_ind_paren_prev = n; break;
7013 case ')': buf->b_ind_maxparen = n; break;
7014 case '*': buf->b_ind_maxcomment = n; break;
7015 case 'g': buf->b_ind_scopedecl = n; break;
7016 case 'h': buf->b_ind_scopedecl_code = n; break;
7017 case 'j': buf->b_ind_java = n; break;
7018 case 'J': buf->b_ind_js = n; break;
7019 case 'l': buf->b_ind_keep_case_label = n; break;
7020 case '#': buf->b_ind_hash_comment = n; break;
7021 case 'N': buf->b_ind_cpp_namespace = n; break;
7022 case 'k': buf->b_ind_if_for_while = n; break;
7023 }
7024 if (*p == ',')
7025 ++p;
7026 }
7027}
7028
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007029/*
7030 * Return the desired indent for C code.
7031 * Return -1 if the indent should be left alone (inside a raw string).
7032 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007034get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 pos_T cur_curpos;
7037 int amount;
7038 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007039 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 colnr_T col;
7041 char_u *theline;
7042 char_u *linecopy;
7043 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007044 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007046 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 pos_T our_paren_pos;
7048 char_u *start;
7049 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007050#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051#define BRACE_AT_START 2 /* '{' is at start of line */
7052#define BRACE_AT_END 3 /* '{' is at end of line */
7053 linenr_T ourscope;
7054 char_u *l;
7055 char_u *look;
7056 char_u terminated;
7057 int lookfor;
7058#define LOOKFOR_INITIAL 0
7059#define LOOKFOR_IF 1
7060#define LOOKFOR_DO 2
7061#define LOOKFOR_CASE 3
7062#define LOOKFOR_ANY 4
7063#define LOOKFOR_TERM 5
7064#define LOOKFOR_UNTERM 6
7065#define LOOKFOR_SCOPEDECL 7
7066#define LOOKFOR_NOBREAK 8
7067#define LOOKFOR_CPP_BASECLASS 9
7068#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007069#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007070#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071
7072 int whilelevel;
7073 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 int n;
7075 int iscase;
7076 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007077 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007079 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007080 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007081 int js_cur_has_key = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007082 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007084 /* make a copy, value is changed below */
7085 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086
7087 /* remember where the cursor was when we started */
7088 cur_curpos = curwin->w_cursor;
7089
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007090 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007091 if (cur_curpos.lnum == 1)
7092 return 0;
7093
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 /* Get a copy of the current contents of the line.
7095 * This is required, because only the most recent line obtained with
7096 * ml_get is valid! */
7097 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7098 if (linecopy == NULL)
7099 return 0;
7100
7101 /*
7102 * In insert mode and the cursor is on a ')' truncate the line at the
7103 * cursor position. We don't want to line up with the matching '(' when
7104 * inserting new stuff.
7105 * For unknown reasons the cursor might be past the end of the line, thus
7106 * check for that.
7107 */
7108 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007109 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 && linecopy[curwin->w_cursor.col] == ')')
7111 linecopy[curwin->w_cursor.col] = NUL;
7112
7113 theline = skipwhite(linecopy);
7114
7115 /* move the cursor to the start of the line */
7116
7117 curwin->w_cursor.col = 0;
7118
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007119 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007120
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007122 * If we are inside a raw string don't change the indent.
7123 * Ignore a raw string inside a comment.
7124 */
7125 comment_pos = ind_find_start_comment();
7126 if (comment_pos != NULL)
7127 {
7128 /* findmatchlimit() static pos is overwritten, make a copy */
7129 tryposCopy = *comment_pos;
7130 comment_pos = &tryposCopy;
7131 }
7132 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
7133 if (trypos != NULL && (comment_pos == NULL || lt(*trypos, *comment_pos)))
7134 {
7135 amount = -1;
7136 goto laterend;
7137 }
7138
7139 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 * #defines and so on always go at the left when included in 'cinkeys'.
7141 */
7142 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007143 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007144 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007145 goto theend;
7146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147
7148 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007149 * Is it a non-case label? Then that goes at the left margin too unless:
7150 * - JS flag is set.
7151 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007153 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007154 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 {
7156 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007157 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 }
7159
7160 /*
7161 * If we're inside a "//" comment and there is a "//" comment in a
7162 * previous line, lineup with that one.
7163 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007164 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 && (trypos = find_line_comment()) != NULL) /* XXX */
7166 {
7167 /* find how indented the line beginning the comment is */
7168 getvcol(curwin, trypos, &col, NULL, NULL);
7169 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007170 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 }
7172
7173 /*
7174 * If we're inside a comment and not looking at the start of the
7175 * comment, try using the 'comments' option.
7176 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007177 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 {
7179 int lead_start_len = 2;
7180 int lead_middle_len = 1;
7181 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7182 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7183 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7184 char_u *p;
7185 int start_align = 0;
7186 int start_off = 0;
7187 int done = FALSE;
7188
7189 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007190 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007192 *lead_start = NUL;
7193 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194
7195 p = curbuf->b_p_com;
7196 while (*p != NUL)
7197 {
7198 int align = 0;
7199 int off = 0;
7200 int what = 0;
7201
7202 while (*p != NUL && *p != ':')
7203 {
7204 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7205 what = *p++;
7206 else if (*p == COM_LEFT || *p == COM_RIGHT)
7207 align = *p++;
7208 else if (VIM_ISDIGIT(*p) || *p == '-')
7209 off = getdigits(&p);
7210 else
7211 ++p;
7212 }
7213
7214 if (*p == ':')
7215 ++p;
7216 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7217 if (what == COM_START)
7218 {
7219 STRCPY(lead_start, lead_end);
7220 lead_start_len = (int)STRLEN(lead_start);
7221 start_off = off;
7222 start_align = align;
7223 }
7224 else if (what == COM_MIDDLE)
7225 {
7226 STRCPY(lead_middle, lead_end);
7227 lead_middle_len = (int)STRLEN(lead_middle);
7228 }
7229 else if (what == COM_END)
7230 {
7231 /* If our line starts with the middle comment string, line it
7232 * up with the comment opener per the 'comments' option. */
7233 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7234 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7235 {
7236 done = TRUE;
7237 if (curwin->w_cursor.lnum > 1)
7238 {
7239 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007240 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 * the middle comment string matches in the previous
7242 * line, use the indent of that line. XXX */
7243 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7244 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7245 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7246 else if (STRNCMP(look, lead_middle,
7247 lead_middle_len) == 0)
7248 {
7249 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7250 break;
7251 }
7252 /* If the start comment string doesn't match with the
7253 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007254 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 lead_start, lead_start_len) != 0)
7256 continue;
7257 }
7258 if (start_off != 0)
7259 amount += start_off;
7260 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007261 amount += vim_strsize(lead_start)
7262 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 break;
7264 }
7265
7266 /* If our line starts with the end comment string, line it up
7267 * with the middle comment */
7268 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7269 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7270 {
7271 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7272 /* XXX */
7273 if (off != 0)
7274 amount += off;
7275 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007276 amount += vim_strsize(lead_start)
7277 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 done = TRUE;
7279 break;
7280 }
7281 }
7282 }
7283
7284 /* If our line starts with an asterisk, line up with the
7285 * asterisk in the comment opener; otherwise, line up
7286 * with the first character of the comment text.
7287 */
7288 if (done)
7289 ;
7290 else if (theline[0] == '*')
7291 amount += 1;
7292 else
7293 {
7294 /*
7295 * If we are more than one line away from the comment opener, take
7296 * the indent of the previous non-empty line. If 'cino' has "CO"
7297 * and we are just below the comment opener and there are any
7298 * white characters after it line up with the text after it;
7299 * otherwise, add the amount specified by "c" in 'cino'
7300 */
7301 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007302 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 {
7304 if (linewhite(lnum)) /* skip blank lines */
7305 continue;
7306 amount = get_indent_lnum(lnum); /* XXX */
7307 break;
7308 }
7309 if (amount == -1) /* use the comment opener */
7310 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007311 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007313 start = ml_get(comment_pos->lnum);
7314 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007316 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007318 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007320 if (curbuf->b_ind_in_comment2 || *look == NUL)
7321 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 }
7323 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007324 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 }
7326
7327 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007328 * Are we looking at a ']' that has a match?
7329 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007330 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007331 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7332 {
7333 /* align with the line containing the '['. */
7334 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007335 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007336 }
7337
7338 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 * Are we inside parentheses or braces?
7340 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007341 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007342 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007343 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 || trypos != NULL)
7345 {
7346 if (trypos != NULL && tryposBrace != NULL)
7347 {
7348 /* Both an unmatched '(' and '{' is found. Use the one which is
7349 * closer to the current cursor position, set the other to NULL. */
7350 if (trypos->lnum != tryposBrace->lnum
7351 ? trypos->lnum < tryposBrace->lnum
7352 : trypos->col < tryposBrace->col)
7353 trypos = NULL;
7354 else
7355 tryposBrace = NULL;
7356 }
7357
7358 if (trypos != NULL)
7359 {
7360 /*
7361 * If the matching paren is more than one line away, use the indent of
7362 * a previous non-empty line that matches the same paren.
7363 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007364 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007366 /* Line up with the start of the matching paren line. */
7367 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7368 }
7369 else
7370 {
7371 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007372 our_paren_pos = *trypos;
7373 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007375 l = skipwhite(ml_get(lnum));
7376 if (cin_nocode(l)) /* skip comment lines */
7377 continue;
7378 if (cin_ispreproc_cont(&l, &lnum))
7379 continue; /* ignore #define, #if, etc. */
7380 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007382 /* Skip a comment or raw string. XXX */
7383 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007384 {
7385 lnum = trypos->lnum + 1;
7386 continue;
7387 }
7388
7389 /* XXX */
7390 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007391 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007392 && trypos->lnum == our_paren_pos.lnum
7393 && trypos->col == our_paren_pos.col)
7394 {
7395 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007397 if (theline[0] == ')')
7398 {
7399 if (our_paren_pos.lnum != lnum
7400 && cur_amount > amount)
7401 cur_amount = amount;
7402 amount = -1;
7403 }
7404 break;
7405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 }
7407 }
7408
7409 /*
7410 * Line up with line where the matching paren is. XXX
7411 * If the line starts with a '(' or the indent for unclosed
7412 * parentheses is zero, line up with the unclosed parentheses.
7413 */
7414 if (amount == -1)
7415 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007416 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007417 int is_if_for_while = 0;
7418
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007419 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007420 {
7421 /* Look for the outermost opening parenthesis on this line
7422 * and check whether it belongs to an "if", "for" or "while". */
7423
7424 pos_T cursor_save = curwin->w_cursor;
7425 pos_T outermost;
7426 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007427
7428 trypos = &our_paren_pos;
7429 do {
7430 outermost = *trypos;
7431 curwin->w_cursor.lnum = outermost.lnum;
7432 curwin->w_cursor.col = outermost.col;
7433
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007434 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007435 } while (trypos && trypos->lnum == outermost.lnum);
7436
7437 curwin->w_cursor = cursor_save;
7438
7439 line = ml_get(outermost.lnum);
7440
7441 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007442 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007443 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007444
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007445 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007446 look = skipwhite(look);
7447 if (*look == '(')
7448 {
7449 linenr_T save_lnum = curwin->w_cursor.lnum;
7450 char_u *line;
7451 int look_col;
7452
7453 /* Ignore a '(' in front of the line that has a match before
7454 * our matching '('. */
7455 curwin->w_cursor.lnum = our_paren_pos.lnum;
7456 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007457 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007458 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007459 if ((trypos = findmatchlimit(NULL, ')', 0,
7460 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007461 != NULL
7462 && trypos->lnum == our_paren_pos.lnum
7463 && trypos->col < our_paren_pos.col)
7464 ignore_paren_col = trypos->col + 1;
7465
7466 curwin->w_cursor.lnum = save_lnum;
7467 look = ml_get(our_paren_pos.lnum) + look_col;
7468 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007469 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7470 && is_if_for_while == 0)
7471 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007472 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 {
7474 /*
7475 * If we're looking at a close paren, line up right there;
7476 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007477 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 * the last nonwhite character of the line, use either the
7479 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007480 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 * lines).
7482 */
7483 if (theline[0] != ')')
7484 {
7485 cur_amount = MAXCOL;
7486 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007487 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 && cin_ends_in(l, (char_u *)"(", NULL))
7489 {
7490 /* look for opening unmatched paren, indent one level
7491 * for each additional level */
7492 n = 1;
7493 for (col = 0; col < our_paren_pos.col; ++col)
7494 {
7495 switch (l[col])
7496 {
7497 case '(':
7498 case '{': ++n;
7499 break;
7500
7501 case ')':
7502 case '}': if (n > 1)
7503 --n;
7504 break;
7505 }
7506 }
7507
7508 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007509 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007511 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 our_paren_pos.col++;
7513 else
7514 {
7515 col = our_paren_pos.col + 1;
7516 while (vim_iswhite(l[col]))
7517 col++;
7518 if (l[col] != NUL) /* In case of trailing space */
7519 our_paren_pos.col = col;
7520 else
7521 our_paren_pos.col++;
7522 }
7523 }
7524
7525 /*
7526 * Find how indented the paren is, or the character after it
7527 * if we did the above "if".
7528 */
7529 if (our_paren_pos.col > 0)
7530 {
7531 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7532 if (cur_amount > (int)col)
7533 cur_amount = col;
7534 }
7535 }
7536
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007537 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 {
7539 /* Line up with the start of the matching paren line. */
7540 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007541 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7542 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007543 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 {
7545 if (cur_amount != MAXCOL)
7546 amount = cur_amount;
7547 }
7548 else
7549 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007550 /* Add b_ind_unclosed2 for each '(' before our matching one,
7551 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007553 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 {
7555 --our_paren_pos.col;
7556 switch (*ml_get_pos(&our_paren_pos))
7557 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007558 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 col = our_paren_pos.col;
7560 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007561 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 col = MAXCOL;
7563 break;
7564 }
7565 }
7566
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007567 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 * braces */
7569 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007570 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 else
7572 {
7573 curwin->w_cursor.lnum = our_paren_pos.lnum;
7574 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007575 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7576 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007577 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007579 {
7580 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007581 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007582 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007583 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 }
7586 /*
7587 * For a line starting with ')' use the minimum of the two
7588 * positions, to avoid giving it more indent than the previous
7589 * lines:
7590 * func_long_name( if (x
7591 * arg && yy
7592 * ) ^ not here ) ^ not here
7593 */
7594 if (cur_amount < amount)
7595 amount = cur_amount;
7596 }
7597 }
7598
7599 /* add extra indent for a comment */
7600 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007601 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 else
7604 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007605 /*
7606 * We are inside braces, there is a { before this line at the position
7607 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007608 * Make a copy of tryposBrace, it may point to pos_copy inside
7609 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007610 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007611 tryposCopy = *tryposBrace;
7612 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 ourscope = trypos->lnum;
7615 start = ml_get(ourscope);
7616
7617 /*
7618 * Now figure out how indented the line is in general.
7619 * If the brace was at the start of the line, we use that;
7620 * otherwise, check out the indentation of the line as
7621 * a whole and then add the "imaginary indent" to that.
7622 */
7623 look = skipwhite(start);
7624 if (*look == '{')
7625 {
7626 getvcol(curwin, trypos, &col, NULL, NULL);
7627 amount = col;
7628 if (*start == '{')
7629 start_brace = BRACE_IN_COL0;
7630 else
7631 start_brace = BRACE_AT_START;
7632 }
7633 else
7634 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007635 /* That opening brace might have been on a continuation
7636 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 curwin->w_cursor.lnum = ourscope;
7638
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007639 /* Position the cursor over the rightmost paren, so that
7640 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 lnum = ourscope;
7642 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007643 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7644 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 lnum = trypos->lnum;
7646
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007647 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 * case 1: if (asdf &&
7649 * ldfd) {
7650 * }
7651 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007652 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7653 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007655 else if (curbuf->b_ind_js)
7656 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007658 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659
7660 start_brace = BRACE_AT_END;
7661 }
7662
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007663 /* For Javascript check if the line starts with "key:". */
7664 if (curbuf->b_ind_js)
7665 js_cur_has_key = cin_has_js_key(theline);
7666
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007668 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 * we want to be. otherwise, add the amount of room
7670 * that an indent is supposed to be.
7671 */
7672 if (theline[0] == '}')
7673 {
7674 /*
7675 * they may want closing braces to line up with something
7676 * other than the open brace. indulge them, if so.
7677 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007678 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 }
7680 else
7681 {
7682 /*
7683 * If we're looking at an "else", try to find an "if"
7684 * to match it with.
7685 * If we're looking at a "while", try to find a "do"
7686 * to match it with.
7687 */
7688 lookfor = LOOKFOR_INITIAL;
7689 if (cin_iselse(theline))
7690 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007691 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 lookfor = LOOKFOR_DO;
7693 if (lookfor != LOOKFOR_INITIAL)
7694 {
7695 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007696 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 {
7698 amount = get_indent(); /* XXX */
7699 goto theend;
7700 }
7701 }
7702
7703 /*
7704 * We get here if we are not on an "while-of-do" or "else" (or
7705 * failed to find a matching "if").
7706 * Search backwards for something to line up with.
7707 * First set amount for when we don't find anything.
7708 */
7709
7710 /*
7711 * if the '{' is _really_ at the left margin, use the imaginary
7712 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007713 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 */
7715
7716 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7717 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007718 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007719 lookfor_cpp_namespace = TRUE;
7720 }
7721 else if (start_brace == BRACE_AT_START &&
7722 lookfor_cpp_namespace) /* '{' is at start */
7723 {
7724
7725 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 }
7727 else
7728 {
7729 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007730 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007731 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007732
7733 l = skipwhite(ml_get_curline());
7734 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007735 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 else
7738 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007739 /* Compensate for adding b_ind_open_extra later. */
7740 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 if (amount < 0)
7742 amount = 0;
7743 }
7744 }
7745
7746 lookfor_break = FALSE;
7747
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007748 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 {
7750 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007751 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 }
7753 else if (cin_isscopedecl(theline)) /* private:, ... */
7754 {
7755 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007756 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 }
7758 else
7759 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007760 if (curbuf->b_ind_case_break && cin_isbreak(theline))
7761 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 lookfor_break = TRUE;
7763
7764 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007765 /* b_ind_level from start of block */
7766 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 }
7768 scope_amount = amount;
7769 whilelevel = 0;
7770
7771 /*
7772 * Search backwards. If we find something we recognize, line up
7773 * with that.
7774 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007775 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 * the usual amount relative to the conditional
7777 * that opens the block.
7778 */
7779 curwin->w_cursor = cur_curpos;
7780 for (;;)
7781 {
7782 curwin->w_cursor.lnum--;
7783 curwin->w_cursor.col = 0;
7784
7785 /*
7786 * If we went all the way back to the start of our scope, line
7787 * up with it.
7788 */
7789 if (curwin->w_cursor.lnum <= ourscope)
7790 {
7791 /* we reached end of scope:
7792 * if looking for a enum or structure initialization
7793 * go further back:
7794 * if it is an initializer (enum xxx or xxx =), then
7795 * don't add ind_continuation, otherwise it is a variable
7796 * declaration:
7797 * int x,
7798 * here; <-- add ind_continuation
7799 */
7800 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7801 {
7802 if (curwin->w_cursor.lnum == 0
7803 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007804 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007806 /* nothing found (abuse curbuf->b_ind_maxparen as
7807 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 * initialization) */
7809 if (cont_amount > 0)
7810 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007811 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 amount += ind_continuation;
7813 break;
7814 }
7815
7816 l = ml_get_curline();
7817
7818 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007819 * If we're in a comment or raw string now, skip to
7820 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007822 trypos = ind_find_start_CORS();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 if (trypos != NULL)
7824 {
7825 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007826 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 continue;
7828 }
7829
7830 /*
7831 * Skip preprocessor directives and blank lines.
7832 */
7833 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7834 continue;
7835
7836 if (cin_nocode(l))
7837 continue;
7838
7839 terminated = cin_isterminated(l, FALSE, TRUE);
7840
7841 /*
7842 * If we are at top level and the line looks like a
7843 * function declaration, we are done
7844 * (it's a variable declaration).
7845 */
7846 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007847 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 {
7849 /* if the line is terminated with another ','
7850 * it is a continued variable initialization.
7851 * don't add extra indent.
7852 * TODO: does not work, if a function
7853 * declaration is split over multiple lines:
7854 * cin_isfuncdecl returns FALSE then.
7855 */
7856 if (terminated == ',')
7857 break;
7858
7859 /* if it es a enum declaration or an assignment,
7860 * we are done.
7861 */
7862 if (terminated != ';' && cin_isinit())
7863 break;
7864
7865 /* nothing useful found */
7866 if (terminated == 0 || terminated == '{')
7867 continue;
7868 }
7869
7870 if (terminated != ';')
7871 {
7872 /* Skip parens and braces. Position the cursor
7873 * over the rightmost paren, so that matching it
7874 * will take us back to the start of the line.
7875 */ /* XXX */
7876 trypos = NULL;
7877 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007878 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007879 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880
7881 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007882 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883
7884 if (trypos != NULL)
7885 {
7886 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007887 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 continue;
7889 }
7890 }
7891
7892 /* it's a variable declaration, add indentation
7893 * like in
7894 * int a,
7895 * b;
7896 */
7897 if (cont_amount > 0)
7898 amount = cont_amount;
7899 else
7900 amount += ind_continuation;
7901 }
7902 else if (lookfor == LOOKFOR_UNTERM)
7903 {
7904 if (cont_amount > 0)
7905 amount = cont_amount;
7906 else
7907 amount += ind_continuation;
7908 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02007909 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007910 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02007911 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01007912 && lookfor != LOOKFOR_CPP_BASECLASS
7913 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02007914 {
7915 amount = scope_amount;
7916 if (theline[0] == '{')
7917 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007918 amount += curbuf->b_ind_open_extra;
7919 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007920 }
7921 }
7922
7923 if (lookfor_cpp_namespace)
7924 {
7925 /*
7926 * Looking for C++ namespace, need to look further
7927 * back.
7928 */
7929 if (curwin->w_cursor.lnum == ourscope)
7930 continue;
7931
7932 if (curwin->w_cursor.lnum == 0
7933 || curwin->w_cursor.lnum
7934 < ourscope - FIND_NAMESPACE_LIM)
7935 break;
7936
7937 l = ml_get_curline();
7938
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007939 /* If we're in a comment or raw string now, skip
7940 * to the start of it. */
7941 trypos = ind_find_start_CORS();
Bram Moolenaare79d1532011-10-04 18:03:47 +02007942 if (trypos != NULL)
7943 {
7944 curwin->w_cursor.lnum = trypos->lnum + 1;
7945 curwin->w_cursor.col = 0;
7946 continue;
7947 }
7948
7949 /* Skip preprocessor directives and blank lines. */
7950 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7951 continue;
7952
7953 /* Finally the actual check for "namespace". */
7954 if (cin_is_cpp_namespace(l))
7955 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007956 amount += curbuf->b_ind_cpp_namespace
7957 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007958 break;
7959 }
7960
7961 if (cin_nocode(l))
7962 continue;
7963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 }
7965 break;
7966 }
7967
7968 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007969 * If we're in a comment or raw string now, skip to the start
7970 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007972 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 {
7974 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007975 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 continue;
7977 }
7978
7979 l = ml_get_curline();
7980
7981 /*
7982 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00007983 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007985 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 if (iscase || cin_isscopedecl(l))
7987 {
7988 /* we are only looking for cpp base class
7989 * declaration/initialization any longer */
7990 if (lookfor == LOOKFOR_CPP_BASECLASS)
7991 break;
7992
7993 /* When looking for a "do" we are not interested in
7994 * labels. */
7995 if (whilelevel > 0)
7996 continue;
7997
7998 /*
7999 * case xx:
8000 * c = 99 + <- this indent plus continuation
8001 *-> here;
8002 */
8003 if (lookfor == LOOKFOR_UNTERM
8004 || lookfor == LOOKFOR_ENUM_OR_INIT)
8005 {
8006 if (cont_amount > 0)
8007 amount = cont_amount;
8008 else
8009 amount += ind_continuation;
8010 break;
8011 }
8012
8013 /*
8014 * case xx: <- line up with this case
8015 * x = 333;
8016 * case yy:
8017 */
8018 if ( (iscase && lookfor == LOOKFOR_CASE)
8019 || (iscase && lookfor_break)
8020 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8021 {
8022 /*
8023 * Check that this case label is not for another
8024 * switch()
8025 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008026 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008027 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 {
8029 amount = get_indent(); /* XXX */
8030 break;
8031 }
8032 continue;
8033 }
8034
8035 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8036
8037 /*
8038 * case xx: if (cond) <- line up with this if
8039 * y = y + 1;
8040 * -> s = 99;
8041 *
8042 * case xx:
8043 * if (cond) <- line up with this line
8044 * y = y + 1;
8045 * -> s = 99;
8046 */
8047 if (lookfor == LOOKFOR_TERM)
8048 {
8049 if (n)
8050 amount = n;
8051
8052 if (!lookfor_break)
8053 break;
8054 }
8055
8056 /*
8057 * case xx: x = x + 1; <- line up with this x
8058 * -> y = y + 1;
8059 *
8060 * case xx: if (cond) <- line up with this if
8061 * -> y = y + 1;
8062 */
8063 if (n)
8064 {
8065 amount = n;
8066 l = after_label(ml_get_curline());
8067 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008068 {
8069 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008070 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008071 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008072 amount += curbuf->b_ind_level
8073 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 break;
8076 }
8077
8078 /*
8079 * Try to get the indent of a statement before the switch
8080 * label. If nothing is found, line up relative to the
8081 * switch label.
8082 * break; <- may line up with this line
8083 * case xx:
8084 * -> y = 1;
8085 */
8086 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008087 ? curbuf->b_ind_case_code
8088 : curbuf->b_ind_scopedecl_code);
8089 lookfor = curbuf->b_ind_case_break
8090 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 continue;
8092 }
8093
8094 /*
8095 * Looking for a switch() label or C++ scope declaration,
8096 * ignore other lines, skip {}-blocks.
8097 */
8098 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8099 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008100 if (find_last_paren(l, '{', '}')
8101 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008102 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008104 curwin->w_cursor.col = 0;
8105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 continue;
8107 }
8108
8109 /*
8110 * Ignore jump labels with nothing after them.
8111 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008112 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 {
8114 l = after_label(ml_get_curline());
8115 if (l == NULL || cin_nocode(l))
8116 continue;
8117 }
8118
8119 /*
8120 * Ignore #defines, #if, etc.
8121 * Ignore comment and empty lines.
8122 * (need to get the line again, cin_islabel() may have
8123 * unlocked it)
8124 */
8125 l = ml_get_curline();
8126 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)
8127 || cin_nocode(l))
8128 continue;
8129
8130 /*
8131 * Are we at the start of a cpp base class declaration or
8132 * constructor initialization?
8133 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008134 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008135 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008136 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008137 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008138 l = ml_get_curline();
8139 }
8140 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 {
8142 if (lookfor == LOOKFOR_UNTERM)
8143 {
8144 if (cont_amount > 0)
8145 amount = cont_amount;
8146 else
8147 amount += ind_continuation;
8148 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008149 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008151 /* Need to find start of the declaration. */
8152 lookfor = LOOKFOR_UNTERM;
8153 ind_continuation = 0;
8154 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 }
8156 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008157 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008158 amount = get_baseclass_amount(
8159 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 break;
8161 }
8162 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8163 {
8164 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008165 * declaration or initialization before the opening brace.
8166 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 if (cin_isterminated(l, TRUE, FALSE))
8168 break;
8169 else
8170 continue;
8171 }
8172
8173 /*
8174 * What happens next depends on the line being terminated.
8175 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008176 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 * 123,
8178 * sizeof
8179 * here
8180 * Otherwise check whether it is a enumeration or structure
8181 * initialisation (not indented) or a variable declaration
8182 * (indented).
8183 */
8184 terminated = cin_isterminated(l, FALSE, TRUE);
8185
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008186 if (js_cur_has_key)
8187 {
8188 js_cur_has_key = 0; /* only check the first line */
8189 if (curbuf->b_ind_js && terminated == ',')
8190 {
8191 /* For Javascript we might be inside an object:
8192 * key: something, <- align with this
8193 * key: something
8194 * or:
8195 * key: something + <- align with this
8196 * something,
8197 * key: something
8198 */
8199 lookfor = LOOKFOR_JS_KEY;
8200 }
8201 }
8202 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8203 {
8204 amount = get_indent();
8205 break;
8206 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008207 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008208 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008209 if (tryposBrace != NULL && tryposBrace->lnum
8210 >= curwin->w_cursor.lnum)
8211 break;
8212 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008213 /* line below current line is the one that starts a
8214 * (possibly broken) line ending in a comma. */
8215 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008216 else
8217 {
8218 amount = get_indent();
8219 if (curwin->w_cursor.lnum - 1 == ourscope)
8220 /* line above is start of the scope, thus current
8221 * line is the one that stars a (possibly broken)
8222 * line ending in a comma. */
8223 break;
8224 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008225 }
8226
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8228 && terminated == ','))
8229 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008230 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8231 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008232 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 /*
8234 * if we're in the middle of a paren thing,
8235 * go back to the line that starts it so
8236 * we can get the right prevailing indent
8237 * if ( foo &&
8238 * bar )
8239 */
8240 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008241 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008243 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 */
8245 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008246 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008247 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8248 || (trypos->lnum == tryposBrace->lnum
8249 && trypos->col < tryposBrace->col)))
8250 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251
8252 /*
8253 * If we are looking for ',', we also look for matching
8254 * braces.
8255 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008256 if (trypos == NULL && terminated == ','
8257 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008258 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259
8260 if (trypos != NULL)
8261 {
8262 /*
8263 * Check if we are on a case label now. This is
8264 * handled above.
8265 * case xx: if ( asdf &&
8266 * asdf)
8267 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008268 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008270 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 {
8272 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008273 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 continue;
8275 }
8276 }
8277
8278 /*
8279 * Skip over continuation lines to find the one to get the
8280 * indent from
8281 * char *usethis = "bla\
8282 * bla",
8283 * here;
8284 */
8285 if (terminated == ',')
8286 {
8287 while (curwin->w_cursor.lnum > 1)
8288 {
8289 l = ml_get(curwin->w_cursor.lnum - 1);
8290 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8291 break;
8292 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008293 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 }
8295 }
8296
8297 /*
8298 * Get indent and pointer to text for current line,
8299 * ignoring any jump label. XXX
8300 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008301 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008302 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008303 else
8304 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 /*
8306 * If this is just above the line we are indenting, and it
8307 * starts with a '{', line it up with this line.
8308 * while (not)
8309 * -> {
8310 * }
8311 */
8312 if (terminated != ',' && lookfor != LOOKFOR_TERM
8313 && theline[0] == '{')
8314 {
8315 amount = cur_amount;
8316 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008317 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 * doesn't start with a '{', which must have a match
8319 * in the same line (scope is the same). Probably:
8320 * { 1, 2 },
8321 * -> { 3, 4 }
8322 */
8323 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008324 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008326 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 {
8328 /* have to look back, whether it is a cpp base
8329 * class declaration or initialization */
8330 lookfor = LOOKFOR_CPP_BASECLASS;
8331 continue;
8332 }
8333 break;
8334 }
8335
8336 /*
8337 * Check if we are after an "if", "while", etc.
8338 * Also allow " } else".
8339 */
8340 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8341 {
8342 /*
8343 * Found an unterminated line after an if (), line up
8344 * with the last one.
8345 * if (cond)
8346 * 100 +
8347 * -> here;
8348 */
8349 if (lookfor == LOOKFOR_UNTERM
8350 || lookfor == LOOKFOR_ENUM_OR_INIT)
8351 {
8352 if (cont_amount > 0)
8353 amount = cont_amount;
8354 else
8355 amount += ind_continuation;
8356 break;
8357 }
8358
8359 /*
8360 * If this is just above the line we are indenting, we
8361 * are finished.
8362 * while (not)
8363 * -> here;
8364 * Otherwise this indent can be used when the line
8365 * before this is terminated.
8366 * yyy;
8367 * if (stat)
8368 * while (not)
8369 * xxx;
8370 * -> here;
8371 */
8372 amount = cur_amount;
8373 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008374 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 if (lookfor != LOOKFOR_TERM)
8376 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008377 amount += curbuf->b_ind_level
8378 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 break;
8380 }
8381
8382 /*
8383 * Special trick: when expecting the while () after a
8384 * do, line up with the while()
8385 * do
8386 * x = 1;
8387 * -> here
8388 */
8389 l = skipwhite(ml_get_curline());
8390 if (cin_isdo(l))
8391 {
8392 if (whilelevel == 0)
8393 break;
8394 --whilelevel;
8395 }
8396
8397 /*
8398 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008399 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 * Need to use the scope of this "else". XXX
8401 * If whilelevel != 0 continue looking for a "do {".
8402 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008403 if (cin_iselse(l) && whilelevel == 0)
8404 {
8405 /* If we're looking at "} else", let's make sure we
8406 * find the opening brace of the enclosing scope,
8407 * not the one from "if () {". */
8408 if (*l == '}')
8409 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008410 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008411
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008412 if ((trypos = find_start_brace()) == NULL
8413 || find_match(LOOKFOR_IF, trypos->lnum)
8414 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008415 break;
8416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 }
8418
8419 /*
8420 * If we're below an unterminated line that is not an
8421 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008422 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 * the line before this one.
8424 */
8425 else
8426 {
8427 /*
8428 * Found two unterminated lines on a row, line up with
8429 * the last one.
8430 * c = 99 +
8431 * 100 +
8432 * -> here;
8433 */
8434 if (lookfor == LOOKFOR_UNTERM)
8435 {
8436 /* When line ends in a comma add extra indent */
8437 if (terminated == ',')
8438 amount += ind_continuation;
8439 break;
8440 }
8441
8442 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8443 {
8444 /* Found two lines ending in ',', lineup with the
8445 * lowest one, but check for cpp base class
8446 * declaration/initialization, if it is an
8447 * opening brace or we are looking just for
8448 * enumerations/initializations. */
8449 if (terminated == ',')
8450 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008451 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 break;
8453
8454 lookfor = LOOKFOR_CPP_BASECLASS;
8455 continue;
8456 }
8457
8458 /* Ignore unterminated lines in between, but
8459 * reduce indent. */
8460 if (amount > cur_amount)
8461 amount = cur_amount;
8462 }
8463 else
8464 {
8465 /*
8466 * Found first unterminated line on a row, may
8467 * line up with this line, remember its indent
8468 * 100 +
8469 * -> here;
8470 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008471 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008473
8474 n = (int)STRLEN(l);
8475 if (terminated == ',' && (*skipwhite(l) == ']'
8476 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008477 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478
8479 /*
8480 * If previous line ends in ',', check whether we
8481 * are in an initialization or enum
8482 * struct xxx =
8483 * {
8484 * sizeof a,
8485 * 124 };
8486 * or a normal possible continuation line.
8487 * but only, of no other statement has been found
8488 * yet.
8489 */
8490 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8491 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008492 if (curbuf->b_ind_js)
8493 {
8494 /* Search for a line ending in a comma
8495 * and line up with the line below it
8496 * (could be the current line).
8497 * some = [
8498 * 1, <- line up here
8499 * 2,
8500 * some = [
8501 * 3 + <- line up here
8502 * 4 *
8503 * 5,
8504 * 6,
8505 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008506 if (cin_iscomment(skipwhite(l)))
8507 break;
8508 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008509 trypos = find_match_char('[',
8510 curbuf->b_ind_maxparen);
8511 if (trypos != NULL)
8512 {
8513 if (trypos->lnum
8514 == curwin->w_cursor.lnum - 1)
8515 {
8516 /* Current line is first inside
8517 * [], line up with it. */
8518 break;
8519 }
8520 ourscope = trypos->lnum;
8521 }
8522 }
8523 else
8524 {
8525 lookfor = LOOKFOR_ENUM_OR_INIT;
8526 cont_amount = cin_first_id_amount();
8527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 }
8529 else
8530 {
8531 if (lookfor == LOOKFOR_INITIAL
8532 && *l != NUL
8533 && l[STRLEN(l) - 1] == '\\')
8534 /* XXX */
8535 cont_amount = cin_get_equal_amount(
8536 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008537 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008538 && lookfor != LOOKFOR_JS_KEY
8539 && lookfor != LOOKFOR_COMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 lookfor = LOOKFOR_UNTERM;
8541 }
8542 }
8543 }
8544 }
8545
8546 /*
8547 * Check if we are after a while (cond);
8548 * If so: Ignore until the matching "do".
8549 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008550 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 {
8552 /*
8553 * Found an unterminated line after a while ();, line up
8554 * with the last one.
8555 * while (cond);
8556 * 100 + <- line up with this one
8557 * -> here;
8558 */
8559 if (lookfor == LOOKFOR_UNTERM
8560 || lookfor == LOOKFOR_ENUM_OR_INIT)
8561 {
8562 if (cont_amount > 0)
8563 amount = cont_amount;
8564 else
8565 amount += ind_continuation;
8566 break;
8567 }
8568
8569 if (whilelevel == 0)
8570 {
8571 lookfor = LOOKFOR_TERM;
8572 amount = get_indent(); /* XXX */
8573 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008574 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 }
8576 ++whilelevel;
8577 }
8578
8579 /*
8580 * We are after a "normal" statement.
8581 * If we had another statement we can stop now and use the
8582 * indent of that other statement.
8583 * Otherwise the indent of the current statement may be used,
8584 * search backwards for the next "normal" statement.
8585 */
8586 else
8587 {
8588 /*
8589 * Skip single break line, if before a switch label. It
8590 * may be lined up with the case label.
8591 */
8592 if (lookfor == LOOKFOR_NOBREAK
8593 && cin_isbreak(skipwhite(ml_get_curline())))
8594 {
8595 lookfor = LOOKFOR_ANY;
8596 continue;
8597 }
8598
8599 /*
8600 * Handle "do {" line.
8601 */
8602 if (whilelevel > 0)
8603 {
8604 l = cin_skipcomment(ml_get_curline());
8605 if (cin_isdo(l))
8606 {
8607 amount = get_indent(); /* XXX */
8608 --whilelevel;
8609 continue;
8610 }
8611 }
8612
8613 /*
8614 * Found a terminated line above an unterminated line. Add
8615 * the amount for a continuation line.
8616 * x = 1;
8617 * y = foo +
8618 * -> here;
8619 * or
8620 * int x = 1;
8621 * int foo,
8622 * -> here;
8623 */
8624 if (lookfor == LOOKFOR_UNTERM
8625 || lookfor == LOOKFOR_ENUM_OR_INIT)
8626 {
8627 if (cont_amount > 0)
8628 amount = cont_amount;
8629 else
8630 amount += ind_continuation;
8631 break;
8632 }
8633
8634 /*
8635 * Found a terminated line above a terminated line or "if"
8636 * etc. line. Use the amount of the line below us.
8637 * x = 1; x = 1;
8638 * if (asdf) y = 2;
8639 * while (asdf) ->here;
8640 * here;
8641 * ->foo;
8642 */
8643 if (lookfor == LOOKFOR_TERM)
8644 {
8645 if (!lookfor_break && whilelevel == 0)
8646 break;
8647 }
8648
8649 /*
8650 * First line above the one we're indenting is terminated.
8651 * To know what needs to be done look further backward for
8652 * a terminated line.
8653 */
8654 else
8655 {
8656 /*
8657 * position the cursor over the rightmost paren, so
8658 * that matching it will take us back to the start of
8659 * the line. Helps for:
8660 * func(asdr,
8661 * asdfasdf);
8662 * here;
8663 */
8664term_again:
8665 l = ml_get_curline();
8666 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008667 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008668 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 {
8670 /*
8671 * Check if we are on a case label now. This is
8672 * handled above.
8673 * case xx: if ( asdf &&
8674 * asdf)
8675 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008676 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008678 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 {
8680 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008681 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 continue;
8683 }
8684 }
8685
8686 /* When aligning with the case statement, don't align
8687 * with a statement after it.
8688 * case 1: { <-- don't use this { position
8689 * stat;
8690 * }
8691 * case 2:
8692 * stat;
8693 * }
8694 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008695 iscase = (curbuf->b_ind_keep_case_label
8696 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697
8698 /*
8699 * Get indent and pointer to text for current line,
8700 * ignoring any jump label.
8701 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008702 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703
8704 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008705 amount += curbuf->b_ind_open_extra;
8706 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008707 l = skipwhite(l);
8708 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008709 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8711
8712 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008713 * When a terminated line starts with "else" skip to
8714 * the matching "if":
8715 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008716 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008717 * Need to use the scope of this "else". XXX
8718 * If whilelevel != 0 continue looking for a "do {".
8719 */
8720 if (lookfor == LOOKFOR_TERM
8721 && *l != '}'
8722 && cin_iselse(l)
8723 && whilelevel == 0)
8724 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008725 if ((trypos = find_start_brace()) == NULL
8726 || find_match(LOOKFOR_IF, trypos->lnum)
8727 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008728 break;
8729 continue;
8730 }
8731
8732 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 * If we're at the end of a block, skip to the start of
8734 * that block.
8735 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008736 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008737 if (find_last_paren(l, '{', '}') /* XXX */
8738 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008740 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 /* if not "else {" check for terminated again */
8742 /* but skip block for "} else {" */
8743 l = cin_skipcomment(ml_get_curline());
8744 if (*l == '}' || !cin_iselse(l))
8745 goto term_again;
8746 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008747 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 }
8749 }
8750 }
8751 }
8752 }
8753 }
8754
8755 /* add extra indent for a comment */
8756 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008757 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008758
8759 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008760 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
8761 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008762
8763 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008765
8766 /*
8767 * ok -- we're not inside any sort of structure at all!
8768 *
8769 * This means we're at the top level, and everything should
8770 * basically just match where the previous line is, except
8771 * for the lines immediately following a function declaration,
8772 * which are K&R-style parameters and need to be indented.
8773 *
8774 * if our line starts with an open brace, forget about any
8775 * prevailing indent and make sure it looks like the start
8776 * of a function
8777 */
8778
8779 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008781 amount = curbuf->b_ind_first_open;
8782 goto theend;
8783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008785 /*
8786 * If the NEXT line is a function declaration, the current
8787 * line needs to be indented as a function type spec.
8788 * Don't do this if the current line looks like a comment or if the
8789 * current line is terminated, ie. ends in ';', or if the current line
8790 * contains { or }: "void f() {\n if (1)"
8791 */
8792 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8793 && !cin_nocode(theline)
8794 && vim_strchr(theline, '{') == NULL
8795 && vim_strchr(theline, '}') == NULL
8796 && !cin_ends_in(theline, (char_u *)":", NULL)
8797 && !cin_ends_in(theline, (char_u *)",", NULL)
8798 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
8799 cur_curpos.lnum + 1)
8800 && !cin_isterminated(theline, FALSE, TRUE))
8801 {
8802 amount = curbuf->b_ind_func_type;
8803 goto theend;
8804 }
8805
8806 /* search backwards until we find something we recognize */
8807 amount = 0;
8808 curwin->w_cursor = cur_curpos;
8809 while (curwin->w_cursor.lnum > 1)
8810 {
8811 curwin->w_cursor.lnum--;
8812 curwin->w_cursor.col = 0;
8813
8814 l = ml_get_curline();
8815
8816 /*
8817 * If we're in a comment or raw string now, skip to the start
8818 * of it.
8819 */ /* XXX */
8820 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008822 curwin->w_cursor.lnum = trypos->lnum + 1;
8823 curwin->w_cursor.col = 0;
8824 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 }
8826
8827 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008828 * Are we at the start of a cpp base class declaration or
8829 * constructor initialization?
8830 */ /* XXX */
8831 n = FALSE;
8832 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008834 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
8835 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008837 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008839 /* XXX */
8840 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
8841 break;
8842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008844 /*
8845 * Skip preprocessor directives and blank lines.
8846 */
8847 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
8848 continue;
8849
8850 if (cin_nocode(l))
8851 continue;
8852
8853 /*
8854 * If the previous line ends in ',', use one level of
8855 * indentation:
8856 * int foo,
8857 * bar;
8858 * do this before checking for '}' in case of eg.
8859 * enum foobar
8860 * {
8861 * ...
8862 * } foo,
8863 * bar;
8864 */
8865 n = 0;
8866 if (cin_ends_in(l, (char_u *)",", NULL)
8867 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
8868 {
8869 /* take us back to opening paren */
8870 if (find_last_paren(l, '(', ')')
8871 && (trypos = find_match_paren(
8872 curbuf->b_ind_maxparen)) != NULL)
8873 curwin->w_cursor = *trypos;
8874
8875 /* For a line ending in ',' that is a continuation line go
8876 * back to the first line with a backslash:
8877 * char *foo = "bla\
8878 * bla",
8879 * here;
8880 */
8881 while (n == 0 && curwin->w_cursor.lnum > 1)
8882 {
8883 l = ml_get(curwin->w_cursor.lnum - 1);
8884 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8885 break;
8886 --curwin->w_cursor.lnum;
8887 curwin->w_cursor.col = 0;
8888 }
8889
8890 amount = get_indent(); /* XXX */
8891
8892 if (amount == 0)
8893 amount = cin_first_id_amount();
8894 if (amount == 0)
8895 amount = ind_continuation;
8896 break;
8897 }
8898
8899 /*
8900 * If the line looks like a function declaration, and we're
8901 * not in a comment, put it the left margin.
8902 */
8903 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
8904 break;
8905 l = ml_get_curline();
8906
8907 /*
8908 * Finding the closing '}' of a previous function. Put
8909 * current line at the left margin. For when 'cino' has "fs".
8910 */
8911 if (*skipwhite(l) == '}')
8912 break;
8913
8914 /* (matching {)
8915 * If the previous line ends on '};' (maybe followed by
8916 * comments) align at column 0. For example:
8917 * char *string_array[] = { "foo",
8918 * / * x * / "b};ar" }; / * foobar * /
8919 */
8920 if (cin_ends_in(l, (char_u *)"};", NULL))
8921 break;
8922
8923 /*
8924 * If the previous line ends on '[' we are probably in an
8925 * array constant:
8926 * something = [
8927 * 234, <- extra indent
8928 */
8929 if (cin_ends_in(l, (char_u *)"[", NULL))
8930 {
8931 amount = get_indent() + ind_continuation;
8932 break;
8933 }
8934
8935 /*
8936 * Find a line only has a semicolon that belongs to a previous
8937 * line ending in '}', e.g. before an #endif. Don't increase
8938 * indent then.
8939 */
8940 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
8941 {
8942 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943
8944 while (curwin->w_cursor.lnum > 1)
8945 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008946 look = ml_get(--curwin->w_cursor.lnum);
8947 if (!(cin_nocode(look) || cin_ispreproc_cont(
8948 &look, &curwin->w_cursor.lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008950 }
8951 if (curwin->w_cursor.lnum > 0
8952 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008955 curwin->w_cursor = curpos_save;
8956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008958 /*
8959 * If the PREVIOUS line is a function declaration, the current
8960 * line (and the ones that follow) needs to be indented as
8961 * parameters.
8962 */
8963 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
8964 {
8965 amount = curbuf->b_ind_param;
8966 break;
8967 }
8968
8969 /*
8970 * If the previous line ends in ';' and the line before the
8971 * previous line ends in ',' or '\', ident to column zero:
8972 * int foo,
8973 * bar;
8974 * indent_to_0 here;
8975 */
8976 if (cin_ends_in(l, (char_u *)";", NULL))
8977 {
8978 l = ml_get(curwin->w_cursor.lnum - 1);
8979 if (cin_ends_in(l, (char_u *)",", NULL)
8980 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
8981 break;
8982 l = ml_get_curline();
8983 }
8984
8985 /*
8986 * Doesn't look like anything interesting -- so just
8987 * use the indent of this line.
8988 *
8989 * Position the cursor over the rightmost paren, so that
8990 * matching it will take us back to the start of the line.
8991 */
8992 find_last_paren(l, '(', ')');
8993
8994 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
8995 curwin->w_cursor = *trypos;
8996 amount = get_indent(); /* XXX */
8997 break;
8998 }
8999
9000 /* add extra indent for a comment */
9001 if (cin_iscomment(theline))
9002 amount += curbuf->b_ind_comment;
9003
9004 /* add extra indent if the previous line ended in a backslash:
9005 * "asdfasdf\
9006 * here";
9007 * char *foo = "asdf\
9008 * here";
9009 */
9010 if (cur_curpos.lnum > 1)
9011 {
9012 l = ml_get(cur_curpos.lnum - 1);
9013 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9014 {
9015 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9016 if (cur_amount > 0)
9017 amount = cur_amount;
9018 else if (cur_amount == 0)
9019 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 }
9021 }
9022
9023theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009024 if (amount < 0)
9025 amount = 0;
9026
9027laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 /* put the cursor back where it belongs */
9029 curwin->w_cursor = cur_curpos;
9030
9031 vim_free(linecopy);
9032
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 return amount;
9034}
9035
9036 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009037find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038{
9039 char_u *look;
9040 pos_T *theirscope;
9041 char_u *mightbeif;
9042 int elselevel;
9043 int whilelevel;
9044
9045 if (lookfor == LOOKFOR_IF)
9046 {
9047 elselevel = 1;
9048 whilelevel = 0;
9049 }
9050 else
9051 {
9052 elselevel = 0;
9053 whilelevel = 1;
9054 }
9055
9056 curwin->w_cursor.col = 0;
9057
9058 while (curwin->w_cursor.lnum > ourscope + 1)
9059 {
9060 curwin->w_cursor.lnum--;
9061 curwin->w_cursor.col = 0;
9062
9063 look = cin_skipcomment(ml_get_curline());
9064 if (cin_iselse(look)
9065 || cin_isif(look)
9066 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009067 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 {
9069 /*
9070 * if we've gone outside the braces entirely,
9071 * we must be out of scope...
9072 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009073 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 if (theirscope == NULL)
9075 break;
9076
9077 /*
9078 * and if the brace enclosing this is further
9079 * back than the one enclosing the else, we're
9080 * out of luck too.
9081 */
9082 if (theirscope->lnum < ourscope)
9083 break;
9084
9085 /*
9086 * and if they're enclosed in a *deeper* brace,
9087 * then we can ignore it because it's in a
9088 * different scope...
9089 */
9090 if (theirscope->lnum > ourscope)
9091 continue;
9092
9093 /*
9094 * if it was an "else" (that's not an "else if")
9095 * then we need to go back to another if, so
9096 * increment elselevel
9097 */
9098 look = cin_skipcomment(ml_get_curline());
9099 if (cin_iselse(look))
9100 {
9101 mightbeif = cin_skipcomment(look + 4);
9102 if (!cin_isif(mightbeif))
9103 ++elselevel;
9104 continue;
9105 }
9106
9107 /*
9108 * if it was a "while" then we need to go back to
9109 * another "do", so increment whilelevel. XXX
9110 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009111 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 {
9113 ++whilelevel;
9114 continue;
9115 }
9116
9117 /* If it's an "if" decrement elselevel */
9118 look = cin_skipcomment(ml_get_curline());
9119 if (cin_isif(look))
9120 {
9121 elselevel--;
9122 /*
9123 * When looking for an "if" ignore "while"s that
9124 * get in the way.
9125 */
9126 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9127 whilelevel = 0;
9128 }
9129
9130 /* If it's a "do" decrement whilelevel */
9131 if (cin_isdo(look))
9132 whilelevel--;
9133
9134 /*
9135 * if we've used up all the elses, then
9136 * this must be the if that we want!
9137 * match the indent level of that if.
9138 */
9139 if (elselevel <= 0 && whilelevel <= 0)
9140 {
9141 return OK;
9142 }
9143 }
9144 }
9145 return FAIL;
9146}
9147
9148# if defined(FEAT_EVAL) || defined(PROTO)
9149/*
9150 * Get indent level from 'indentexpr'.
9151 */
9152 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009153get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154{
9155 int indent;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009156 pos_T save_pos;
9157 colnr_T save_curswant;
9158 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009160 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9161 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009163 /* Save and restore cursor position and curswant, in case it was changed
9164 * via :normal commands */
9165 save_pos = curwin->w_cursor;
9166 save_curswant = curwin->w_curswant;
9167 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009169 if (use_sandbox)
9170 ++sandbox;
9171 ++textlock;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02009172 indent = (int)eval_to_number(curbuf->b_p_inde);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009173 if (use_sandbox)
9174 --sandbox;
9175 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176
9177 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9178 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9179 * command. */
9180 save_State = State;
9181 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009182 curwin->w_cursor = save_pos;
9183 curwin->w_curswant = save_curswant;
9184 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 check_cursor();
9186 State = save_State;
9187
9188 /* If there is an error, just keep the current indent. */
9189 if (indent < 0)
9190 indent = get_indent();
9191
9192 return indent;
9193}
9194# endif
9195
9196#endif /* FEAT_CINDENT */
9197
9198#if defined(FEAT_LISP) || defined(PROTO)
9199
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009200static int lisp_match(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201
9202 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009203lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204{
9205 char_u buf[LSIZE];
9206 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009207 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208
9209 while (*word != NUL)
9210 {
9211 (void)copy_option_part(&word, buf, LSIZE, ",");
9212 len = (int)STRLEN(buf);
9213 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9214 return TRUE;
9215 }
9216 return FALSE;
9217}
9218
9219/*
9220 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9221 * The incompatible newer method is quite a bit better at indenting
9222 * code in lisp-like languages than the traditional one; it's still
9223 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9224 *
9225 * TODO:
9226 * Findmatch() should be adapted for lisp, also to make showmatch
9227 * work correctly: now (v5.3) it seems all C/C++ oriented:
9228 * - it does not recognize the #\( and #\) notations as character literals
9229 * - it doesn't know about comments starting with a semicolon
9230 * - it incorrectly interprets '(' as a character literal
9231 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009232 * Update from Sergey Khorev:
9233 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 */
9235 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009236get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009238 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009239 int amount;
9240 char_u *that;
9241 colnr_T col;
9242 colnr_T firsttry;
9243 int parencount, quotecount;
9244 int vi_lisp;
9245
9246 /* Set vi_lisp to use the vi-compatible method */
9247 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9248
9249 realpos = curwin->w_cursor;
9250 curwin->w_cursor.col = 0;
9251
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009252 if ((pos = findmatch(NULL, '(')) == NULL)
9253 pos = findmatch(NULL, '[');
9254 else
9255 {
9256 paren = *pos;
9257 pos = findmatch(NULL, '[');
9258 if (pos == NULL || ltp(pos, &paren))
9259 pos = &paren;
9260 }
9261 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 {
9263 /* Extra trick: Take the indent of the first previous non-white
9264 * line that is at the same () level. */
9265 amount = -1;
9266 parencount = 0;
9267
9268 while (--curwin->w_cursor.lnum >= pos->lnum)
9269 {
9270 if (linewhite(curwin->w_cursor.lnum))
9271 continue;
9272 for (that = ml_get_curline(); *that != NUL; ++that)
9273 {
9274 if (*that == ';')
9275 {
9276 while (*(that + 1) != NUL)
9277 ++that;
9278 continue;
9279 }
9280 if (*that == '\\')
9281 {
9282 if (*(that + 1) != NUL)
9283 ++that;
9284 continue;
9285 }
9286 if (*that == '"' && *(that + 1) != NUL)
9287 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009288 while (*++that && *that != '"')
9289 {
9290 /* skipping escaped characters in the string */
9291 if (*that == '\\')
9292 {
9293 if (*++that == NUL)
9294 break;
9295 if (that[1] == NUL)
9296 {
9297 ++that;
9298 break;
9299 }
9300 }
9301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009303 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009305 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306 --parencount;
9307 }
9308 if (parencount == 0)
9309 {
9310 amount = get_indent();
9311 break;
9312 }
9313 }
9314
9315 if (amount == -1)
9316 {
9317 curwin->w_cursor.lnum = pos->lnum;
9318 curwin->w_cursor.col = pos->col;
9319 col = pos->col;
9320
9321 that = ml_get_curline();
9322
9323 if (vi_lisp && get_indent() == 0)
9324 amount = 2;
9325 else
9326 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009327 char_u *line = that;
9328
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329 amount = 0;
9330 while (*that && col)
9331 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009332 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 col--;
9334 }
9335
9336 /*
9337 * Some keywords require "body" indenting rules (the
9338 * non-standard-lisp ones are Scheme special forms):
9339 *
9340 * (let ((a 1)) instead (let ((a 1))
9341 * (...)) of (...))
9342 */
9343
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009344 if (!vi_lisp && (*that == '(' || *that == '[')
9345 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 amount += 2;
9347 else
9348 {
9349 that++;
9350 amount++;
9351 firsttry = amount;
9352
9353 while (vim_iswhite(*that))
9354 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009355 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 ++that;
9357 }
9358
9359 if (*that && *that != ';') /* not a comment line */
9360 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009361 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009363 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 firsttry++;
9365
9366 parencount = 0;
9367 quotecount = 0;
9368
9369 if (vi_lisp
9370 || (*that != '"'
9371 && *that != '\''
9372 && *that != '#'
9373 && (*that < '0' || *that > '9')))
9374 {
9375 while (*that
9376 && (!vim_iswhite(*that)
9377 || quotecount
9378 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009379 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 && !quotecount
9381 && !parencount
9382 && vi_lisp)))
9383 {
9384 if (*that == '"')
9385 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009386 if ((*that == '(' || *that == '[')
9387 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009389 if ((*that == ')' || *that == ']')
9390 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 --parencount;
9392 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009393 amount += lbr_chartabsize_adv(
9394 line, &that, (colnr_T)amount);
9395 amount += lbr_chartabsize_adv(
9396 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 }
9398 }
9399 while (vim_iswhite(*that))
9400 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009401 amount += lbr_chartabsize(
9402 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 that++;
9404 }
9405 if (!*that || *that == ';')
9406 amount = firsttry;
9407 }
9408 }
9409 }
9410 }
9411 }
9412 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009413 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414
9415 curwin->w_cursor = realpos;
9416
9417 return amount;
9418}
9419#endif /* FEAT_LISP */
9420
9421 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009422prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009424#if defined(SIGHUP) && defined(SIG_IGN)
9425 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9426 * makes Vim exit and then handling SIGHUP causes various reentrance
9427 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009428 signal(SIGHUP, SIG_IGN);
9429#endif
9430
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431#ifdef FEAT_GUI
9432 if (gui.in_use)
9433 {
9434 gui.dying = TRUE;
9435 out_trash(); /* trash any pending output */
9436 }
9437 else
9438#endif
9439 {
9440 windgoto((int)Rows - 1, 0);
9441
9442 /*
9443 * Switch terminal mode back now, so messages end up on the "normal"
9444 * screen (if there are two screens).
9445 */
9446 settmode(TMODE_COOK);
9447#ifdef WIN3264
9448 if (can_end_termcap_mode(FALSE) == TRUE)
9449#endif
9450 stoptermcap();
9451 out_flush();
9452 }
9453}
9454
9455/*
9456 * Preserve files and exit.
9457 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009458 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9459 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 */
9461 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009462preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463{
9464 buf_T *buf;
9465
9466 prepare_to_exit();
9467
Bram Moolenaar4770d092006-01-12 23:22:24 +00009468 /* Setting this will prevent free() calls. That avoids calling free()
9469 * recursively when free() was invoked with a bad pointer. */
9470 really_exiting = TRUE;
9471
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 out_str(IObuff);
9473 screen_start(); /* don't know where cursor is now */
9474 out_flush();
9475
9476 ml_close_notmod(); /* close all not-modified buffers */
9477
9478 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9479 {
9480 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9481 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009482 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 screen_start(); /* don't know where cursor is now */
9484 out_flush();
9485 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9486 break;
9487 }
9488 }
9489
9490 ml_close_all(FALSE); /* close all memfiles, without deleting */
9491
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009492 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493
9494 getout(1);
9495}
9496
9497/*
9498 * return TRUE if "fname" exists.
9499 */
9500 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009501vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009503 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504
9505 if (mch_stat((char *)fname, &st))
9506 return FALSE;
9507 return TRUE;
9508}
9509
9510/*
9511 * Check for CTRL-C pressed, but only once in a while.
9512 * Should be used instead of ui_breakcheck() for functions that check for
9513 * each line in the file. Calling ui_breakcheck() each time takes too much
9514 * time, because it can be a system call.
9515 */
9516
9517#ifndef BREAKCHECK_SKIP
9518# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9519# define BREAKCHECK_SKIP 200
9520# else
9521# define BREAKCHECK_SKIP 32
9522# endif
9523#endif
9524
9525static int breakcheck_count = 0;
9526
9527 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009528line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529{
9530 if (++breakcheck_count >= BREAKCHECK_SKIP)
9531 {
9532 breakcheck_count = 0;
9533 ui_breakcheck();
9534 }
9535}
9536
9537/*
9538 * Like line_breakcheck() but check 10 times less often.
9539 */
9540 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009541fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542{
9543 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9544 {
9545 breakcheck_count = 0;
9546 ui_breakcheck();
9547 }
9548}
9549
9550/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009551 * Invoke expand_wildcards() for one pattern.
9552 * Expand items like "%:h" before the expansion.
9553 * Returns OK or FAIL.
9554 */
9555 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009556expand_wildcards_eval(
9557 char_u **pat, /* pointer to input pattern */
9558 int *num_file, /* resulting number of files */
9559 char_u ***file, /* array of resulting files */
9560 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009561{
9562 int ret = FAIL;
9563 char_u *eval_pat = NULL;
9564 char_u *exp_pat = *pat;
9565 char_u *ignored_msg;
9566 int usedlen;
9567
9568 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9569 {
9570 ++emsg_off;
9571 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9572 NULL, &ignored_msg, NULL);
9573 --emsg_off;
9574 if (eval_pat != NULL)
9575 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9576 }
9577
9578 if (exp_pat != NULL)
9579 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9580
9581 if (eval_pat != NULL)
9582 {
9583 vim_free(exp_pat);
9584 vim_free(eval_pat);
9585 }
9586
9587 return ret;
9588}
9589
9590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9592 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009593 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 */
9595 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009596expand_wildcards(
9597 int num_pat, /* number of input patterns */
9598 char_u **pat, /* array of input patterns */
9599 int *num_files, /* resulting number of files */
9600 char_u ***files, /* array of resulting files */
9601 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602{
9603 int retval;
9604 int i, j;
9605 char_u *p;
9606 int non_suf_match; /* number without matching suffix */
9607
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009608 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609
9610 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009611 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 return retval;
9613
9614#ifdef FEAT_WILDIGN
9615 /*
9616 * Remove names that match 'wildignore'.
9617 */
9618 if (*p_wig)
9619 {
9620 char_u *ffname;
9621
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009622 /* check all files in (*files)[] */
9623 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009625 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 if (ffname == NULL) /* out of memory */
9627 break;
9628# ifdef VMS
9629 vms_remove_version(ffname);
9630# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009631 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009633 /* remove this matching files from the list */
9634 vim_free((*files)[i]);
9635 for (j = i; j + 1 < *num_files; ++j)
9636 (*files)[j] = (*files)[j + 1];
9637 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638 --i;
9639 }
9640 vim_free(ffname);
9641 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009642
9643 /* If the number of matches is now zero, we fail. */
9644 if (*num_files == 0)
9645 {
9646 vim_free(*files);
9647 *files = NULL;
9648 return FAIL;
9649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 }
9651#endif
9652
9653 /*
9654 * Move the names where 'suffixes' match to the end.
9655 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009656 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 {
9658 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009659 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009661 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 {
9663 /*
9664 * Move the name without matching suffix to the front
9665 * of the list.
9666 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009667 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009669 (*files)[j] = (*files)[j - 1];
9670 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 }
9672 }
9673 }
9674
9675 return retval;
9676}
9677
9678/*
9679 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9680 */
9681 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009682match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683{
9684 int fnamelen, setsuflen;
9685 char_u *setsuf;
9686#define MAXSUFLEN 30 /* maximum length of a file suffix */
9687 char_u suf_buf[MAXSUFLEN];
9688
9689 fnamelen = (int)STRLEN(fname);
9690 setsuflen = 0;
9691 for (setsuf = p_su; *setsuf; )
9692 {
9693 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009694 if (setsuflen == 0)
9695 {
9696 char_u *tail = gettail(fname);
9697
9698 /* empty entry: match name without a '.' */
9699 if (vim_strchr(tail, '.') == NULL)
9700 {
9701 setsuflen = 1;
9702 break;
9703 }
9704 }
9705 else
9706 {
9707 if (fnamelen >= setsuflen
9708 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9709 (size_t)setsuflen) == 0)
9710 break;
9711 setsuflen = 0;
9712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 }
9714 return (setsuflen != 0);
9715}
9716
9717#if !defined(NO_EXPANDPATH) || defined(PROTO)
9718
9719# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009720static int vim_backtick(char_u *p);
9721static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722# endif
9723
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009724# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725/*
9726 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9727 * it's shared between these systems.
9728 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009729# if defined(PROTO)
9730# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731# else
9732# ifdef __BORLANDC__
9733# define _cdecl _RTLENTRYF
9734# endif
9735# endif
9736
9737/*
9738 * comparison function for qsort in dos_expandpath()
9739 */
9740 static int _cdecl
9741pstrcmp(const void *a, const void *b)
9742{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009743 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744}
9745
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009747 * Recursively expand one path component into all matching files and/or
9748 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 * Return the number of matches found.
9750 * "path" has backslashes before chars that are not to be expanded, starting
9751 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009752 * Return the number of matches found.
9753 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 */
9755 static int
9756dos_expandpath(
9757 garray_T *gap,
9758 char_u *path,
9759 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009760 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009761 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009763 char_u *buf;
9764 char_u *path_end;
9765 char_u *p, *s, *e;
9766 int start_len = gap->ga_len;
9767 char_u *pat;
9768 regmatch_T regmatch;
9769 int starts_with_dot;
9770 int matches;
9771 int len;
9772 int starstar = FALSE;
9773 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 WIN32_FIND_DATA fb;
9775 HANDLE hFind = (HANDLE)0;
9776# ifdef FEAT_MBYTE
9777 WIN32_FIND_DATAW wfb;
9778 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9779# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009781 int ok;
9782
9783 /* Expanding "**" may take a long time, check for CTRL-C. */
9784 if (stardepth > 0)
9785 {
9786 ui_breakcheck();
9787 if (got_int)
9788 return 0;
9789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790
Bram Moolenaar7314efd2015-10-31 15:32:52 +01009791 /* Make room for file name. When doing encoding conversion the actual
9792 * length may be quite a bit longer, thus use the maximum possible length. */
9793 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 if (buf == NULL)
9795 return 0;
9796
9797 /*
9798 * Find the first part in the path name that contains a wildcard or a ~1.
9799 * Copy it into buf, including the preceding characters.
9800 */
9801 p = buf;
9802 s = buf;
9803 e = NULL;
9804 path_end = path;
9805 while (*path_end != NUL)
9806 {
9807 /* May ignore a wildcard that has a backslash before it; it will
9808 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9809 if (path_end >= path + wildoff && rem_backslash(path_end))
9810 *p++ = *path_end++;
9811 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9812 {
9813 if (e != NULL)
9814 break;
9815 s = p + 1;
9816 }
9817 else if (path_end >= path + wildoff
9818 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9819 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009820# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 if (has_mbyte)
9822 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009823 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824 STRNCPY(p, path_end, len);
9825 p += len;
9826 path_end += len;
9827 }
9828 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009829# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 *p++ = *path_end++;
9831 }
9832 e = p;
9833 *e = NUL;
9834
9835 /* now we have one wildcard component between s and e */
9836 /* Remove backslashes between "wildoff" and the start of the wildcard
9837 * component. */
9838 for (p = buf + wildoff; p < s; ++p)
9839 if (rem_backslash(p))
9840 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009841 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 --e;
9843 --s;
9844 }
9845
Bram Moolenaar231334e2005-07-25 20:46:57 +00009846 /* Check for "**" between "s" and "e". */
9847 for (p = s; p < e; ++p)
9848 if (p[0] == '*' && p[1] == '*')
9849 starstar = TRUE;
9850
Bram Moolenaard82103e2016-01-17 17:04:05 +01009851 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9853 if (pat == NULL)
9854 {
9855 vim_free(buf);
9856 return 0;
9857 }
9858
9859 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009860 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009861 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 regmatch.rm_ic = TRUE; /* Always ignore case */
9863 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009864 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009865 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 vim_free(pat);
9867
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009868 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869 {
9870 vim_free(buf);
9871 return 0;
9872 }
9873
9874 /* remember the pattern or file name being looked for */
9875 matchname = vim_strsave(s);
9876
Bram Moolenaar231334e2005-07-25 20:46:57 +00009877 /* If "**" is by itself, this is the first time we encounter it and more
9878 * is following then find matches without any directory. */
9879 if (!didstar && stardepth < 100 && starstar && e - s == 2
9880 && *path_end == '/')
9881 {
9882 STRCPY(s, path_end + 1);
9883 ++stardepth;
9884 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9885 --stardepth;
9886 }
9887
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 /* Scan all files in the directory with "dir/ *.*" */
9889 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890# ifdef FEAT_MBYTE
9891 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
9892 {
9893 /* The active codepage differs from 'encoding'. Attempt using the
9894 * wide function. If it fails because it is not implemented fall back
9895 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009896 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 if (wn != NULL)
9898 {
9899 hFind = FindFirstFileW(wn, &wfb);
9900 if (hFind == INVALID_HANDLE_VALUE
9901 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
9902 {
9903 vim_free(wn);
9904 wn = NULL;
9905 }
9906 }
9907 }
9908
9909 if (wn == NULL)
9910# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009911 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913
9914 while (ok)
9915 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916# ifdef FEAT_MBYTE
9917 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009918 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 else
9920# endif
9921 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922 /* Ignore entries starting with a dot, unless when asked for. Accept
9923 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +01009924 if ((p[0] != '.' || starts_with_dot
9925 || ((flags & EW_DODOT)
9926 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009928 || (regmatch.regprog != NULL
9929 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009930 || ((flags & EW_NOTWILD)
9931 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009935
9936 if (starstar && stardepth < 100)
9937 {
9938 /* For "**" in the pattern first go deeper in the tree to
9939 * find matches. */
9940 STRCPY(buf + len, "/**");
9941 STRCPY(buf + len + 3, path_end);
9942 ++stardepth;
9943 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
9944 --stardepth;
9945 }
9946
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 STRCPY(buf + len, path_end);
9948 if (mch_has_exp_wildcard(path_end))
9949 {
9950 /* need to expand another component of the path */
9951 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009952 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 }
9954 else
9955 {
9956 /* no more wildcards, check if there is a match */
9957 /* remove backslashes for the remaining components only */
9958 if (*path_end != 0)
9959 backslash_halve(buf + len + 1);
9960 if (mch_getperm(buf) >= 0) /* add existing file */
9961 addfile(gap, buf, flags);
9962 }
9963 }
9964
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965# ifdef FEAT_MBYTE
9966 if (wn != NULL)
9967 {
9968 vim_free(p);
9969 ok = FindNextFileW(hFind, &wfb);
9970 }
9971 else
9972# endif
9973 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974
9975 /* If no more matches and no match was used, try expanding the name
9976 * itself. Finds the long name of a short filename. */
9977 if (!ok && matchname != NULL && gap->ga_len == start_len)
9978 {
9979 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 FindClose(hFind);
9981# ifdef FEAT_MBYTE
9982 if (wn != NULL)
9983 {
9984 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009985 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986 if (wn != NULL)
9987 hFind = FindFirstFileW(wn, &wfb);
9988 }
9989 if (wn == NULL)
9990# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009991 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993 vim_free(matchname);
9994 matchname = NULL;
9995 }
9996 }
9997
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 FindClose(hFind);
9999# ifdef FEAT_MBYTE
10000 vim_free(wn);
10001# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010003 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 vim_free(matchname);
10005
10006 matches = gap->ga_len - start_len;
10007 if (matches > 0)
10008 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10009 sizeof(char_u *), pstrcmp);
10010 return matches;
10011}
10012
10013 int
10014mch_expandpath(
10015 garray_T *gap,
10016 char_u *path,
10017 int flags) /* EW_* flags */
10018{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010019 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010021# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022
Bram Moolenaar231334e2005-07-25 20:46:57 +000010023#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10024 || defined(PROTO)
10025/*
10026 * Unix style wildcard expansion code.
10027 * It's here because it's used both for Unix and Mac.
10028 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010029static int pstrcmp(const void *, const void *);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010030
10031 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010032pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010033{
10034 return (pathcmp(*(char **)a, *(char **)b, -1));
10035}
10036
10037/*
10038 * Recursively expand one path component into all matching files and/or
10039 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10040 * "path" has backslashes before chars that are not to be expanded, starting
10041 * at "path + wildoff".
10042 * Return the number of matches found.
10043 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10044 */
10045 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010046unix_expandpath(
10047 garray_T *gap,
10048 char_u *path,
10049 int wildoff,
10050 int flags, /* EW_* flags */
10051 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010052{
10053 char_u *buf;
10054 char_u *path_end;
10055 char_u *p, *s, *e;
10056 int start_len = gap->ga_len;
10057 char_u *pat;
10058 regmatch_T regmatch;
10059 int starts_with_dot;
10060 int matches;
10061 int len;
10062 int starstar = FALSE;
10063 static int stardepth = 0; /* depth for "**" expansion */
10064
10065 DIR *dirp;
10066 struct dirent *dp;
10067
10068 /* Expanding "**" may take a long time, check for CTRL-C. */
10069 if (stardepth > 0)
10070 {
10071 ui_breakcheck();
10072 if (got_int)
10073 return 0;
10074 }
10075
10076 /* make room for file name */
10077 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10078 if (buf == NULL)
10079 return 0;
10080
10081 /*
10082 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010083 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010084 * Copy it into "buf", including the preceding characters.
10085 */
10086 p = buf;
10087 s = buf;
10088 e = NULL;
10089 path_end = path;
10090 while (*path_end != NUL)
10091 {
10092 /* May ignore a wildcard that has a backslash before it; it will
10093 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10094 if (path_end >= path + wildoff && rem_backslash(path_end))
10095 *p++ = *path_end++;
10096 else if (*path_end == '/')
10097 {
10098 if (e != NULL)
10099 break;
10100 s = p + 1;
10101 }
10102 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010103 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010104 || (!p_fic && (flags & EW_ICASE)
10105 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010106 e = p;
10107#ifdef FEAT_MBYTE
10108 if (has_mbyte)
10109 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010110 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010111 STRNCPY(p, path_end, len);
10112 p += len;
10113 path_end += len;
10114 }
10115 else
10116#endif
10117 *p++ = *path_end++;
10118 }
10119 e = p;
10120 *e = NUL;
10121
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010122 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010123 /* Remove backslashes between "wildoff" and the start of the wildcard
10124 * component. */
10125 for (p = buf + wildoff; p < s; ++p)
10126 if (rem_backslash(p))
10127 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010128 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010129 --e;
10130 --s;
10131 }
10132
10133 /* Check for "**" between "s" and "e". */
10134 for (p = s; p < e; ++p)
10135 if (p[0] == '*' && p[1] == '*')
10136 starstar = TRUE;
10137
10138 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010139 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010140 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10141 if (pat == NULL)
10142 {
10143 vim_free(buf);
10144 return 0;
10145 }
10146
10147 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010148 if (flags & EW_ICASE)
10149 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10150 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010151 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010152 if (flags & (EW_NOERROR | EW_NOTWILD))
10153 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010154 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010155 if (flags & (EW_NOERROR | EW_NOTWILD))
10156 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010157 vim_free(pat);
10158
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010159 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010160 {
10161 vim_free(buf);
10162 return 0;
10163 }
10164
10165 /* If "**" is by itself, this is the first time we encounter it and more
10166 * is following then find matches without any directory. */
10167 if (!didstar && stardepth < 100 && starstar && e - s == 2
10168 && *path_end == '/')
10169 {
10170 STRCPY(s, path_end + 1);
10171 ++stardepth;
10172 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10173 --stardepth;
10174 }
10175
10176 /* open the directory for scanning */
10177 *s = NUL;
10178 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10179
10180 /* Find all matching entries */
10181 if (dirp != NULL)
10182 {
10183 for (;;)
10184 {
10185 dp = readdir(dirp);
10186 if (dp == NULL)
10187 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010188 if ((dp->d_name[0] != '.' || starts_with_dot
10189 || ((flags & EW_DODOT)
10190 && dp->d_name[1] != NUL
10191 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010192 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10193 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010194 || ((flags & EW_NOTWILD)
10195 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010196 {
10197 STRCPY(s, dp->d_name);
10198 len = STRLEN(buf);
10199
10200 if (starstar && stardepth < 100)
10201 {
10202 /* For "**" in the pattern first go deeper in the tree to
10203 * find matches. */
10204 STRCPY(buf + len, "/**");
10205 STRCPY(buf + len + 3, path_end);
10206 ++stardepth;
10207 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10208 --stardepth;
10209 }
10210
10211 STRCPY(buf + len, path_end);
10212 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10213 {
10214 /* need to expand another component of the path */
10215 /* remove backslashes for the remaining components only */
10216 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10217 }
10218 else
10219 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010220 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010221
Bram Moolenaar231334e2005-07-25 20:46:57 +000010222 /* no more wildcards, check if there is a match */
10223 /* remove backslashes for the remaining components only */
10224 if (*path_end != NUL)
10225 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010226 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010227 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010228 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010229 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010230#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010231 size_t precomp_len = STRLEN(buf)+1;
10232 char_u *precomp_buf =
10233 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010234
Bram Moolenaar231334e2005-07-25 20:46:57 +000010235 if (precomp_buf)
10236 {
10237 mch_memmove(buf, precomp_buf, precomp_len);
10238 vim_free(precomp_buf);
10239 }
10240#endif
10241 addfile(gap, buf, flags);
10242 }
10243 }
10244 }
10245 }
10246
10247 closedir(dirp);
10248 }
10249
10250 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010251 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010252
10253 matches = gap->ga_len - start_len;
10254 if (matches > 0)
10255 qsort(((char_u **)gap->ga_data) + start_len, matches,
10256 sizeof(char_u *), pstrcmp);
10257 return matches;
10258}
10259#endif
10260
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010261#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010262static int find_previous_pathsep(char_u *path, char_u **psep);
10263static int is_unique(char_u *maybe_unique, garray_T *gap, int i);
10264static void expand_path_option(char_u *curdir, garray_T *gap);
10265static char_u *get_path_cutoff(char_u *fname, garray_T *gap);
10266static void uniquefy_paths(garray_T *gap, char_u *pattern);
10267static int expand_in_path(garray_T *gap, char_u *pattern, int flags);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010268
10269/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010270 * Moves "*psep" back to the previous path separator in "path".
10271 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010272 */
10273 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010274find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010275{
10276 /* skip the current separator */
10277 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010278 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010279
10280 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010281 while (*psep > path)
10282 {
10283 if (vim_ispathsep(**psep))
10284 return OK;
10285 mb_ptr_back(path, *psep);
10286 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010287
10288 return FAIL;
10289}
10290
10291/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010292 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10293 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010294 */
10295 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010296is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010297{
10298 int j;
10299 int candidate_len;
10300 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010301 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010302 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010303
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010304 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010305 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010306 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010307 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010308
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010309 candidate_len = (int)STRLEN(maybe_unique);
10310 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010311 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010312 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010313
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010314 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010315 if (fnamecmp(maybe_unique, rival) == 0
10316 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010317 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010318 }
10319
Bram Moolenaar162bd912010-07-28 22:29:10 +020010320 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010321}
10322
10323/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010324 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010325 * paths are expanded to their equivalent fullpath. This includes the "."
10326 * (relative to current buffer directory) and empty path (relative to current
10327 * directory) notations.
10328 *
10329 * TODO: handle upward search (;) and path limiter (**N) notations by
10330 * expanding each into their equivalent path(s).
10331 */
10332 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010333expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010334{
10335 char_u *path_option = *curbuf->b_p_path == NUL
10336 ? p_path : curbuf->b_p_path;
10337 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010338 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010339 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010340
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010341 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010342 return;
10343
10344 while (*path_option != NUL)
10345 {
10346 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10347
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010348 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010349 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010350 /* Relative to current buffer:
10351 * "/path/file" + "." -> "/path/"
10352 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010353 if (curbuf->b_ffname == NULL)
10354 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010355 p = gettail(curbuf->b_ffname);
10356 len = (int)(p - curbuf->b_ffname);
10357 if (len + (int)STRLEN(buf) >= MAXPATHL)
10358 continue;
10359 if (buf[1] == NUL)
10360 buf[len] = NUL;
10361 else
10362 STRMOVE(buf + len, buf + 2);
10363 mch_memmove(buf, curbuf->b_ffname, len);
10364 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010365 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010366 else if (buf[0] == NUL)
10367 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010368 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010369 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010370 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010371 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010372 else if (!mch_isFullName(buf))
10373 {
10374 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010375 len = (int)STRLEN(curdir);
10376 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010377 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010378 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010379 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010380 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010381 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010382 }
10383
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010384 if (ga_grow(gap, 1) == FAIL)
10385 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010386
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010387# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010388 /* Avoid the path ending in a backslash, it fails when a comma is
10389 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010390 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010391 if (buf[len - 1] == '\\')
10392 buf[len - 1] = '/';
10393# endif
10394
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010395 p = vim_strsave(buf);
10396 if (p == NULL)
10397 break;
10398 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010399 }
10400
10401 vim_free(buf);
10402}
10403
10404/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010405 * Returns a pointer to the file or directory name in "fname" that matches the
10406 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010407 *
10408 * path: /foo/bar/baz
10409 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010410 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010411 */
10412 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010413get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010414{
10415 int i;
10416 int maxlen = 0;
10417 char_u **path_part = (char_u **)gap->ga_data;
10418 char_u *cutoff = NULL;
10419
10420 for (i = 0; i < gap->ga_len; i++)
10421 {
10422 int j = 0;
10423
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010424 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010425# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010426 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10427#endif
10428 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010429 j++;
10430 if (j > maxlen)
10431 {
10432 maxlen = j;
10433 cutoff = &fname[j];
10434 }
10435 }
10436
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010437 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010438 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010439 while (vim_ispathsep(*cutoff))
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010440 mb_ptr_adv(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010441
10442 return cutoff;
10443}
10444
10445/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010446 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10447 * that they are unique with respect to each other while conserving the part
10448 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010449 */
10450 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010451uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010452{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010453 int i;
10454 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010455 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010456 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010457 char_u *pat;
10458 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010459 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010460 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010461 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010462 char_u **in_curdir = NULL;
10463 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010464
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010465 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010466 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010467
10468 /*
10469 * We need to prepend a '*' at the beginning of file_pattern so that the
10470 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010471 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010472 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010473 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010474 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010475 if (file_pattern == NULL)
10476 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010477 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010478 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010479 STRCAT(file_pattern, pattern);
10480 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10481 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010482 if (pat == NULL)
10483 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010484
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010485 regmatch.rm_ic = TRUE; /* always ignore case */
10486 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10487 vim_free(pat);
10488 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010489 return;
10490
Bram Moolenaar162bd912010-07-28 22:29:10 +020010491 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010492 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010493 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010494 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010495
10496 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010497 if (in_curdir == NULL)
10498 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010499
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010500 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010501 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010502 char_u *path = fnames[i];
10503 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010504 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010505 char_u *pathsep_p;
10506 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010507
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010508 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010509 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010510 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010511 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010512 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010513
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010514 /* Shorten the filename while maintaining its uniqueness */
10515 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010516
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010517 /* we start at the end of the path */
10518 pathsep_p = path + len - 1;
10519
10520 while (find_previous_pathsep(path, &pathsep_p))
10521 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10522 && is_unique(pathsep_p + 1, gap, i)
10523 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10524 {
10525 sort_again = TRUE;
10526 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10527 break;
10528 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010529
10530 if (mch_isFullName(path))
10531 {
10532 /*
10533 * Last resort: shorten relative to curdir if possible.
10534 * 'possible' means:
10535 * 1. It is under the current directory.
10536 * 2. The result is actually shorter than the original.
10537 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010538 * Before curdir After
10539 * /foo/bar/file.txt /foo/bar ./file.txt
10540 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10541 * /file.txt / /file.txt
10542 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010543 */
10544 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010545 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010546#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010547 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010548 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010549 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010550 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010551 && !vim_ispathsep(*short_name)
10552#endif
10553 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010554 {
10555 STRCPY(path, ".");
10556 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010557 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010558 }
10559 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010560 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010561 }
10562
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010563 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010564 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010565 {
10566 char_u *rel_path;
10567 char_u *path = in_curdir[i];
10568
10569 if (path == NULL)
10570 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010571
10572 /* If the {filename} is not unique, change it to ./{filename}.
10573 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010574 short_name = shorten_fname(path, curdir);
10575 if (short_name == NULL)
10576 short_name = path;
10577 if (is_unique(short_name, gap, i))
10578 {
10579 STRCPY(fnames[i], short_name);
10580 continue;
10581 }
10582
10583 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10584 if (rel_path == NULL)
10585 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010586 STRCPY(rel_path, ".");
10587 add_pathsep(rel_path);
10588 STRCAT(rel_path, short_name);
10589
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010590 vim_free(fnames[i]);
10591 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010592 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010593 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010594 }
10595
Bram Moolenaar162bd912010-07-28 22:29:10 +020010596theend:
10597 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010598 if (in_curdir != NULL)
10599 {
10600 for (i = 0; i < gap->ga_len; i++)
10601 vim_free(in_curdir[i]);
10602 vim_free(in_curdir);
10603 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010604 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010605 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010606
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010607 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010608 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010609}
10610
10611/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010612 * Calls globpath() with 'path' values for the given pattern and stores the
10613 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010614 * Returns the total number of matches.
10615 */
10616 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010617expand_in_path(
10618 garray_T *gap,
10619 char_u *pattern,
10620 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010621{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010622 char_u *curdir;
10623 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010624 char_u *paths = NULL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010625
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010626 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010627 return 0;
10628 mch_dirname(curdir, MAXPATHL);
10629
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010630 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010631 expand_path_option(curdir, &path_ga);
10632 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010633 if (path_ga.ga_len == 0)
10634 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010635
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010636 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010637 ga_clear_strings(&path_ga);
10638 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010639 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010640
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010641 globpath(paths, pattern, gap, (flags & EW_ICASE) ? WILD_ICASE : 0);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010642 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010643
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010644 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010645}
10646#endif
10647
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010648#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10649/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010650 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10651 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010652 */
10653 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010654remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010655{
10656 int i;
10657 int j;
10658 char_u **fnames = (char_u **)gap->ga_data;
10659
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010660 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010661 for (i = gap->ga_len - 1; i > 0; --i)
10662 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10663 {
10664 vim_free(fnames[i]);
10665 for (j = i + 1; j < gap->ga_len; ++j)
10666 fnames[j - 1] = fnames[j];
10667 --gap->ga_len;
10668 }
10669}
10670#endif
10671
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010672static int has_env_var(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010673
10674/*
10675 * Return TRUE if "p" contains what looks like an environment variable.
10676 * Allowing for escaping.
10677 */
10678 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010679has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010680{
10681 for ( ; *p; mb_ptr_adv(p))
10682 {
10683 if (*p == '\\' && p[1] != NUL)
10684 ++p;
10685 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010686#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010687 "$%"
10688#else
10689 "$"
10690#endif
10691 , *p) != NULL)
10692 return TRUE;
10693 }
10694 return FALSE;
10695}
10696
10697#ifdef SPECIAL_WILDCHAR
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010698static int has_special_wildchar(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010699
10700/*
10701 * Return TRUE if "p" contains a special wildcard character.
10702 * Allowing for escaping.
10703 */
10704 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010705has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010706{
10707 for ( ; *p; mb_ptr_adv(p))
10708 {
10709 if (*p == '\\' && p[1] != NUL)
10710 ++p;
10711 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10712 return TRUE;
10713 }
10714 return FALSE;
10715}
10716#endif
10717
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718/*
10719 * Generic wildcard expansion code.
10720 *
10721 * Characters in "pat" that should not be expanded must be preceded with a
10722 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10723 *
10724 * Return FAIL when no single file was found. In this case "num_file" is not
10725 * set, and "file" may contain an error message.
10726 * Return OK when some files found. "num_file" is set to the number of
10727 * matches, "file" to the array of matches. Call FreeWild() later.
10728 */
10729 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010730gen_expand_wildcards(
10731 int num_pat, /* number of input patterns */
10732 char_u **pat, /* array of input patterns */
10733 int *num_file, /* resulting number of files */
10734 char_u ***file, /* array of resulting files */
10735 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736{
10737 int i;
10738 garray_T ga;
10739 char_u *p;
10740 static int recursive = FALSE;
10741 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020010742 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010743#if defined(FEAT_SEARCHPATH)
10744 int did_expand_in_path = FALSE;
10745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746
10747 /*
10748 * expand_env() is called to expand things like "~user". If this fails,
10749 * it calls ExpandOne(), which brings us back here. In this case, always
10750 * call the machine specific expansion function, if possible. Otherwise,
10751 * return FAIL.
10752 */
10753 if (recursive)
10754#ifdef SPECIAL_WILDCHAR
10755 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10756#else
10757 return FAIL;
10758#endif
10759
10760#ifdef SPECIAL_WILDCHAR
10761 /*
10762 * If there are any special wildcard characters which we cannot handle
10763 * here, call machine specific function for all the expansion. This
10764 * avoids starting the shell for each argument separately.
10765 * For `=expr` do use the internal function.
10766 */
10767 for (i = 0; i < num_pat; i++)
10768 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010769 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770# ifdef VIM_BACKTICK
10771 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10772# endif
10773 )
10774 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10775 }
10776#endif
10777
10778 recursive = TRUE;
10779
10780 /*
10781 * The matching file names are stored in a growarray. Init it empty.
10782 */
10783 ga_init2(&ga, (int)sizeof(char_u *), 30);
10784
10785 for (i = 0; i < num_pat; ++i)
10786 {
10787 add_pat = -1;
10788 p = pat[i];
10789
10790#ifdef VIM_BACKTICK
10791 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020010792 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020010794 if (add_pat == -1)
10795 retval = FAIL;
10796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 else
10798#endif
10799 {
10800 /*
10801 * First expand environment variables, "~/" and "~user/".
10802 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010803 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010805 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806 if (p == NULL)
10807 p = pat[i];
10808#ifdef UNIX
10809 /*
10810 * On Unix, if expand_env() can't expand an environment
10811 * variable, use the shell to do that. Discard previously
10812 * found file names and start all over again.
10813 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010814 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 {
10816 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010817 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020010819 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 recursive = FALSE;
10821 return i;
10822 }
10823#endif
10824 }
10825
10826 /*
10827 * If there are wildcards: Expand file names and add each match to
10828 * the list. If there is no match, and EW_NOTFOUND is given, add
10829 * the pattern.
10830 * If there are no wildcards: Add the file name if it exists or
10831 * when EW_NOTFOUND is given.
10832 */
10833 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010834 {
10835#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010836 if ((flags & EW_PATH)
10837 && !mch_isFullName(p)
10838 && !(p[0] == '.'
10839 && (vim_ispathsep(p[1])
10840 || (p[1] == '.' && vim_ispathsep(p[2]))))
10841 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010842 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010843 /* :find completion where 'path' is used.
10844 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010845 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010846 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010847 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010848 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010849 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010850 else
10851#endif
10852 add_pat = mch_expandpath(&ga, p, flags);
10853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 }
10855
10856 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
10857 {
10858 char_u *t = backslash_halve_save(p);
10859
10860#if defined(MACOS_CLASSIC)
10861 slash_to_colon(t);
10862#endif
10863 /* When EW_NOTFOUND is used, always add files and dirs. Makes
10864 * "vim c:/" work. */
10865 if (flags & EW_NOTFOUND)
10866 addfile(&ga, t, flags | EW_DIR | EW_FILE);
10867 else if (mch_getperm(t) >= 0)
10868 addfile(&ga, t, flags);
10869 vim_free(t);
10870 }
10871
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010872#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010873 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010874 uniquefy_paths(&ga, p);
10875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876 if (p != pat[i])
10877 vim_free(p);
10878 }
10879
10880 *num_file = ga.ga_len;
10881 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
10882
10883 recursive = FALSE;
10884
Bram Moolenaar336bd622016-01-17 18:23:58 +010010885 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886}
10887
10888# ifdef VIM_BACKTICK
10889
10890/*
10891 * Return TRUE if we can expand this backtick thing here.
10892 */
10893 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010894vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895{
10896 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
10897}
10898
10899/*
10900 * Expand an item in `backticks` by executing it as a command.
10901 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020010902 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903 */
10904 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010905expand_backtick(
10906 garray_T *gap,
10907 char_u *pat,
10908 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909{
10910 char_u *p;
10911 char_u *cmd;
10912 char_u *buffer;
10913 int cnt = 0;
10914 int i;
10915
10916 /* Create the command: lop off the backticks. */
10917 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
10918 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020010919 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920
10921#ifdef FEAT_EVAL
10922 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000010923 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924 else
10925#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010926 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010927 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928 vim_free(cmd);
10929 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020010930 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931
10932 cmd = buffer;
10933 while (*cmd != NUL)
10934 {
10935 cmd = skipwhite(cmd); /* skip over white space */
10936 p = cmd;
10937 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
10938 ++p;
10939 /* add an entry if it is not empty */
10940 if (p > cmd)
10941 {
10942 i = *p;
10943 *p = NUL;
10944 addfile(gap, cmd, flags);
10945 *p = i;
10946 ++cnt;
10947 }
10948 cmd = p;
10949 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
10950 ++cmd;
10951 }
10952
10953 vim_free(buffer);
10954 return cnt;
10955}
10956# endif /* VIM_BACKTICK */
10957
10958/*
10959 * Add a file to a file list. Accepted flags:
10960 * EW_DIR add directories
10961 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010962 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963 * EW_NOTFOUND add even when it doesn't exist
10964 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010965 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966 */
10967 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010968addfile(
10969 garray_T *gap,
10970 char_u *f, /* filename */
10971 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972{
10973 char_u *p;
10974 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020010975 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010977 /* if the file/dir/link doesn't exist, may not add it */
10978 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010010979 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980 return;
10981
10982#ifdef FNAME_ILLEGAL
10983 /* if the file/dir contains illegal characters, don't add it */
10984 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
10985 return;
10986#endif
10987
10988 isdir = mch_isdir(f);
10989 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
10990 return;
10991
Bram Moolenaarb5971142015-03-21 17:32:19 +010010992 /* If the file isn't executable, may not add it. Do accept directories.
10993 * When invoked from expand_shellcmd() do not use $PATH. */
10994 if (!isdir && (flags & EW_EXEC)
10995 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010996 return;
10997
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998 /* Make room for another item in the file list. */
10999 if (ga_grow(gap, 1) == FAIL)
11000 return;
11001
11002 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11003 if (p == NULL)
11004 return;
11005
11006 STRCPY(p, f);
11007#ifdef BACKSLASH_IN_FILENAME
11008 slash_adjust(p);
11009#endif
11010 /*
11011 * Append a slash or backslash after directory names if none is present.
11012 */
11013#ifndef DONT_ADD_PATHSEP_TO_DIR
11014 if (isdir && (flags & EW_ADDSLASH))
11015 add_pathsep(p);
11016#endif
11017 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018}
11019#endif /* !NO_EXPANDPATH */
11020
11021#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11022
11023#ifndef SEEK_SET
11024# define SEEK_SET 0
11025#endif
11026#ifndef SEEK_END
11027# define SEEK_END 2
11028#endif
11029
11030/*
11031 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011032 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11033 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034 * Returns an allocated string, or NULL for error.
11035 */
11036 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011037get_cmd_output(
11038 char_u *cmd,
11039 char_u *infile, /* optional input file name */
11040 int flags, /* can be SHELL_SILENT */
11041 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042{
11043 char_u *tempname;
11044 char_u *command;
11045 char_u *buffer = NULL;
11046 int len;
11047 int i = 0;
11048 FILE *fd;
11049
11050 if (check_restricted() || check_secure())
11051 return NULL;
11052
11053 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011054 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055 {
11056 EMSG(_(e_notmp));
11057 return NULL;
11058 }
11059
11060 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011061 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 if (command == NULL)
11063 goto done;
11064
11065 /*
11066 * Call the shell to execute the command (errors are ignored).
11067 * Don't check timestamps here.
11068 */
11069 ++no_check_timestamps;
11070 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11071 --no_check_timestamps;
11072
11073 vim_free(command);
11074
11075 /*
11076 * read the names from the file into memory
11077 */
11078# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011079 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011080 fd = mch_fopen((char *)tempname, "r");
11081# else
11082 fd = mch_fopen((char *)tempname, READBIN);
11083# endif
11084
11085 if (fd == NULL)
11086 {
11087 EMSG2(_(e_notopen), tempname);
11088 goto done;
11089 }
11090
11091 fseek(fd, 0L, SEEK_END);
11092 len = ftell(fd); /* get size of temp file */
11093 fseek(fd, 0L, SEEK_SET);
11094
11095 buffer = alloc(len + 1);
11096 if (buffer != NULL)
11097 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11098 fclose(fd);
11099 mch_remove(tempname);
11100 if (buffer == NULL)
11101 goto done;
11102#ifdef VMS
11103 len = i; /* VMS doesn't give us what we asked for... */
11104#endif
11105 if (i != len)
11106 {
11107 EMSG2(_(e_notread), tempname);
11108 vim_free(buffer);
11109 buffer = NULL;
11110 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011111 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011112 {
11113 /* Change NUL into SOH, otherwise the string is truncated. */
11114 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011115 if (buffer[i] == NUL)
11116 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011117
Bram Moolenaar162bd912010-07-28 22:29:10 +020011118 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011119 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011120 else
11121 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122
11123done:
11124 vim_free(tempname);
11125 return buffer;
11126}
11127#endif
11128
11129/*
11130 * Free the list of files returned by expand_wildcards() or other expansion
11131 * functions.
11132 */
11133 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011134FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011135{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011136 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137 return;
11138#if defined(__EMX__) && defined(__ALWAYS_HAS_TRAILING_NULL_POINTER) /* XXX */
11139 /*
11140 * Is this still OK for when other functions than expand_wildcards() have
11141 * been used???
11142 */
11143 _fnexplodefree((char **)files);
11144#else
11145 while (count--)
11146 vim_free(files[count]);
11147 vim_free(files);
11148#endif
11149}
11150
11151/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011152 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153 * Don't do this when still processing a command or a mapping.
11154 * Don't do this when inside a ":normal" command.
11155 */
11156 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011157goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158{
11159 return (p_im && stuff_empty() && typebuf_typed());
11160}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011161
11162/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011163 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011164 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11165 * - Remove any argument. E.g., "csh -f" -> "csh".
11166 * But don't allow a space in the path, so that this works:
11167 * "/usr/bin/csh --rcfile ~/.cshrc"
11168 * But don't do that for Windows, it's common to have a space in the path.
11169 */
11170 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011171get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011172{
11173 char_u *p;
11174
11175#ifdef WIN3264
11176 p = gettail(p_sh);
11177 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11178#else
11179 p = skiptowhite(p_sh);
11180 if (*p == NUL)
11181 {
11182 /* No white space, use the tail. */
11183 p = vim_strsave(gettail(p_sh));
11184 }
11185 else
11186 {
11187 char_u *p1, *p2;
11188
11189 /* Find the last path separator before the space. */
11190 p1 = p_sh;
11191 for (p2 = p_sh; p2 < p; mb_ptr_adv(p2))
11192 if (vim_ispathsep(*p2))
11193 p1 = p2 + 1;
11194 p = vim_strnsave(p1, (int)(p - p1));
11195 }
11196#endif
11197 return p;
11198}