blob: c6b8d13b78b018f5b50312850b6fcfb133b92ade [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * misc1.c: functions that didn't seem to fit elsewhere
12 */
13
14#include "vim.h"
15#include "version.h"
16
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010017static char_u *vim_version_dir(char_u *vimdir);
18static char_u *remove_tail(char_u *p, char_u *pend, char_u *name);
Bram Moolenaar06ae70d2013-06-17 19:26:36 +020019#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010020static void init_users(void);
Bram Moolenaar06ae70d2013-06-17 19:26:36 +020021#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static int copy_indent(int size, char_u *src);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023
Bram Moolenaar24305862012-08-15 14:05:05 +020024/* All user names (for ~user completion as done by shell). */
25#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
26static garray_T ga_users;
27#endif
28
Bram Moolenaar071d4272004-06-13 20:20:40 +000029/*
30 * Count the size (in window cells) of the indent in the current line.
31 */
32 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010033get_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000034{
Bram Moolenaar597a4222014-06-25 14:39:50 +020035 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000036}
37
38/*
39 * Count the size (in window cells) of the indent in line "lnum".
40 */
41 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010042get_indent_lnum(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000043{
Bram Moolenaar597a4222014-06-25 14:39:50 +020044 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000045}
46
47#if defined(FEAT_FOLDING) || defined(PROTO)
48/*
49 * Count the size (in window cells) of the indent in line "lnum" of buffer
50 * "buf".
51 */
52 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010053get_indent_buf(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000054{
Bram Moolenaar597a4222014-06-25 14:39:50 +020055 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000056}
57#endif
58
59/*
60 * count the size (in window cells) of the indent in line "ptr", with
61 * 'tabstop' at "ts"
62 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +000063 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010064get_indent_str(
65 char_u *ptr,
66 int ts,
67 int list) /* if TRUE, count only screen size for tabs */
Bram Moolenaar071d4272004-06-13 20:20:40 +000068{
69 int count = 0;
70
71 for ( ; *ptr; ++ptr)
72 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020073 if (*ptr == TAB)
74 {
75 if (!list || lcs_tab1) /* count a tab for what it is worth */
76 count += ts - (count % ts);
77 else
Bram Moolenaare4df1642014-08-29 12:58:44 +020078 /* In list mode, when tab is not set, count screen char width
79 * for Tab, displays: ^I */
Bram Moolenaar597a4222014-06-25 14:39:50 +020080 count += ptr2cells(ptr);
81 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000082 else if (*ptr == ' ')
83 ++count; /* count a space for one */
84 else
85 break;
86 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +000087 return count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000088}
89
90/*
91 * Set the indent of the current line.
92 * Leaves the cursor on the first non-blank in the line.
93 * Caller must take care of undo.
94 * "flags":
95 * SIN_CHANGED: call changed_bytes() if the line was changed.
96 * SIN_INSERT: insert the indent in front of the line.
97 * SIN_UNDO: save line for undo before changing it.
98 * Returns TRUE if the line was changed.
99 */
100 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100101set_indent(
102 int size, /* measured in spaces */
103 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104{
105 char_u *p;
106 char_u *newline;
107 char_u *oldline;
108 char_u *s;
109 int todo;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000110 int ind_len; /* measured in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111 int line_len;
112 int doit = FALSE;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000113 int ind_done = 0; /* measured in spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 int tab_pad;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000115 int retval = FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000116 int orig_char_len = -1; /* number of initial whitespace chars when
Bram Moolenaar5002c292007-07-24 13:26:15 +0000117 'et' and 'pi' are both set */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119 /*
120 * First check if there is anything to do and compute the number of
121 * characters needed for the indent.
122 */
123 todo = size;
124 ind_len = 0;
125 p = oldline = ml_get_curline();
126
127 /* Calculate the buffer size for the new indent, and check to see if it
128 * isn't already set */
129
Bram Moolenaar5002c292007-07-24 13:26:15 +0000130 /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and
131 * 'preserveindent' are set count the number of characters at the
132 * beginning of the line to be copied */
133 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 {
135 /* If 'preserveindent' is set then reuse as much as possible of
136 * the existing indent structure for the new indent */
137 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
138 {
139 ind_done = 0;
140
141 /* count as many characters as we can use */
142 while (todo > 0 && vim_iswhite(*p))
143 {
144 if (*p == TAB)
145 {
146 tab_pad = (int)curbuf->b_p_ts
147 - (ind_done % (int)curbuf->b_p_ts);
148 /* stop if this tab will overshoot the target */
149 if (todo < tab_pad)
150 break;
151 todo -= tab_pad;
152 ++ind_len;
153 ind_done += tab_pad;
154 }
155 else
156 {
157 --todo;
158 ++ind_len;
159 ++ind_done;
160 }
161 ++p;
162 }
163
Bram Moolenaar5002c292007-07-24 13:26:15 +0000164 /* Set initial number of whitespace chars to copy if we are
165 * preserving indent but expandtab is set */
166 if (curbuf->b_p_et)
167 orig_char_len = ind_len;
168
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 /* Fill to next tabstop with a tab, if possible */
170 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000171 if (todo >= tab_pad && orig_char_len == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 {
173 doit = TRUE;
174 todo -= tab_pad;
175 ++ind_len;
176 /* ind_done += tab_pad; */
177 }
178 }
179
180 /* count tabs required for indent */
181 while (todo >= (int)curbuf->b_p_ts)
182 {
183 if (*p != TAB)
184 doit = TRUE;
185 else
186 ++p;
187 todo -= (int)curbuf->b_p_ts;
188 ++ind_len;
189 /* ind_done += (int)curbuf->b_p_ts; */
190 }
191 }
192 /* count spaces required for indent */
193 while (todo > 0)
194 {
195 if (*p != ' ')
196 doit = TRUE;
197 else
198 ++p;
199 --todo;
200 ++ind_len;
201 /* ++ind_done; */
202 }
203
204 /* Return if the indent is OK already. */
205 if (!doit && !vim_iswhite(*p) && !(flags & SIN_INSERT))
206 return FALSE;
207
208 /* Allocate memory for the new line. */
209 if (flags & SIN_INSERT)
210 p = oldline;
211 else
212 p = skipwhite(p);
213 line_len = (int)STRLEN(p) + 1;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000214
215 /* If 'preserveindent' and 'expandtab' are both set keep the original
216 * characters and allocate accordingly. We will fill the rest with spaces
217 * after the if (!curbuf->b_p_et) below. */
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000218 if (orig_char_len != -1)
Bram Moolenaar5002c292007-07-24 13:26:15 +0000219 {
220 newline = alloc(orig_char_len + size - ind_done + line_len);
221 if (newline == NULL)
222 return FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000223 todo = size - ind_done;
224 ind_len = orig_char_len + todo; /* Set total length of indent in
225 * characters, which may have been
226 * undercounted until now */
Bram Moolenaar5002c292007-07-24 13:26:15 +0000227 p = oldline;
228 s = newline;
229 while (orig_char_len > 0)
230 {
231 *s++ = *p++;
232 orig_char_len--;
233 }
Bram Moolenaar913626c2008-01-03 11:43:42 +0000234
Bram Moolenaar5002c292007-07-24 13:26:15 +0000235 /* Skip over any additional white space (useful when newindent is less
236 * than old) */
237 while (vim_iswhite(*p))
Bram Moolenaar913626c2008-01-03 11:43:42 +0000238 ++p;
Bram Moolenaarcc00b952007-08-11 12:32:57 +0000239
Bram Moolenaar5002c292007-07-24 13:26:15 +0000240 }
241 else
242 {
243 todo = size;
244 newline = alloc(ind_len + line_len);
245 if (newline == NULL)
246 return FALSE;
247 s = newline;
248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249
250 /* Put the characters in the new line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 /* if 'expandtab' isn't set: use TABs */
252 if (!curbuf->b_p_et)
253 {
254 /* If 'preserveindent' is set then reuse as much as possible of
255 * the existing indent structure for the new indent */
256 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
257 {
258 p = oldline;
259 ind_done = 0;
260
261 while (todo > 0 && vim_iswhite(*p))
262 {
263 if (*p == TAB)
264 {
265 tab_pad = (int)curbuf->b_p_ts
266 - (ind_done % (int)curbuf->b_p_ts);
267 /* stop if this tab will overshoot the target */
268 if (todo < tab_pad)
269 break;
270 todo -= tab_pad;
271 ind_done += tab_pad;
272 }
273 else
274 {
275 --todo;
276 ++ind_done;
277 }
278 *s++ = *p++;
279 }
280
281 /* Fill to next tabstop with a tab, if possible */
282 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
283 if (todo >= tab_pad)
284 {
285 *s++ = TAB;
286 todo -= tab_pad;
287 }
288
289 p = skipwhite(p);
290 }
291
292 while (todo >= (int)curbuf->b_p_ts)
293 {
294 *s++ = TAB;
295 todo -= (int)curbuf->b_p_ts;
296 }
297 }
298 while (todo > 0)
299 {
300 *s++ = ' ';
301 --todo;
302 }
303 mch_memmove(s, p, (size_t)line_len);
304
305 /* Replace the line (unless undo fails). */
306 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
307 {
308 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
309 if (flags & SIN_CHANGED)
310 changed_bytes(curwin->w_cursor.lnum, 0);
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200311 /* Correct saved cursor position if it is in this line. */
312 if (saved_cursor.lnum == curwin->w_cursor.lnum)
313 {
314 if (saved_cursor.col >= (colnr_T)(p - oldline))
315 /* cursor was after the indent, adjust for the number of
316 * bytes added/removed */
317 saved_cursor.col += ind_len - (colnr_T)(p - oldline);
318 else if (saved_cursor.col >= (colnr_T)(s - newline))
319 /* cursor was in the indent, and is now after it, put it back
320 * at the start of the indent (replacing spaces with TAB) */
321 saved_cursor.col = (colnr_T)(s - newline);
322 }
Bram Moolenaar5409c052005-03-18 20:27:04 +0000323 retval = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 }
325 else
326 vim_free(newline);
327
328 curwin->w_cursor.col = ind_len;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000329 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330}
331
332/*
333 * Copy the indent from ptr to the current line (and fill to size)
334 * Leaves the cursor on the first non-blank in the line.
335 * Returns TRUE if the line was changed.
336 */
337 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100338copy_indent(int size, char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339{
340 char_u *p = NULL;
341 char_u *line = NULL;
342 char_u *s;
343 int todo;
344 int ind_len;
345 int line_len = 0;
346 int tab_pad;
347 int ind_done;
348 int round;
349
350 /* Round 1: compute the number of characters needed for the indent
351 * Round 2: copy the characters. */
352 for (round = 1; round <= 2; ++round)
353 {
354 todo = size;
355 ind_len = 0;
356 ind_done = 0;
357 s = src;
358
359 /* Count/copy the usable portion of the source line */
360 while (todo > 0 && vim_iswhite(*s))
361 {
362 if (*s == TAB)
363 {
364 tab_pad = (int)curbuf->b_p_ts
365 - (ind_done % (int)curbuf->b_p_ts);
366 /* Stop if this tab will overshoot the target */
367 if (todo < tab_pad)
368 break;
369 todo -= tab_pad;
370 ind_done += tab_pad;
371 }
372 else
373 {
374 --todo;
375 ++ind_done;
376 }
377 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000378 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 *p++ = *s;
380 ++s;
381 }
382
383 /* Fill to next tabstop with a tab, if possible */
384 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200385 if (todo >= tab_pad && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 {
387 todo -= tab_pad;
388 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000389 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 *p++ = TAB;
391 }
392
393 /* Add tabs required for indent */
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200394 while (todo >= (int)curbuf->b_p_ts && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 {
396 todo -= (int)curbuf->b_p_ts;
397 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000398 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 *p++ = TAB;
400 }
401
402 /* Count/add spaces required for indent */
403 while (todo > 0)
404 {
405 --todo;
406 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000407 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 *p++ = ' ';
409 }
410
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000411 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 {
413 /* Allocate memory for the result: the copied indent, new indent
414 * and the rest of the line. */
415 line_len = (int)STRLEN(ml_get_curline()) + 1;
416 line = alloc(ind_len + line_len);
417 if (line == NULL)
418 return FALSE;
419 p = line;
420 }
421 }
422
423 /* Append the original line */
424 mch_memmove(p, ml_get_curline(), (size_t)line_len);
425
426 /* Replace the line */
427 ml_replace(curwin->w_cursor.lnum, line, FALSE);
428
429 /* Put the cursor after the indent. */
430 curwin->w_cursor.col = ind_len;
431 return TRUE;
432}
433
434/*
435 * Return the indent of the current line after a number. Return -1 if no
436 * number was found. Used for 'n' in 'formatoptions': numbered list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000437 * Since a pattern is used it can actually handle more than numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 */
439 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100440get_number_indent(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 colnr_T col;
443 pos_T pos;
444
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200445 regmatch_T regmatch;
446 int lead_len = 0; /* length of comment leader */
447
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 if (lnum > curbuf->b_ml.ml_line_count)
449 return -1;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000450 pos.lnum = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200451
452#ifdef FEAT_COMMENTS
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200453 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
454 if ((State & INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200455 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000456#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200457 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
458 if (regmatch.regprog != NULL)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200459 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200460 regmatch.rm_ic = FALSE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200461
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200462 /* vim_regexec() expects a pointer to a line. This lets us
463 * start matching for the flp beyond any comment leader... */
464 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200465 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200466 pos.lnum = lnum;
467 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200468#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200469 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200470#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200471 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200472 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200473 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000474
475 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 getvcol(curwin, &pos, &col, NULL, NULL);
478 return (int)col;
479}
480
Bram Moolenaar597a4222014-06-25 14:39:50 +0200481#if defined(FEAT_LINEBREAK) || defined(PROTO)
482/*
483 * Return appropriate space number for breakindent, taking influencing
484 * parameters into account. Window must be specified, since it is not
485 * necessarily always the current one.
486 */
487 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100488get_breakindent_win(
489 win_T *wp,
490 char_u *line) /* start of the line */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200491{
492 static int prev_indent = 0; /* cached indent value */
493 static long prev_ts = 0L; /* cached tabstop value */
494 static char_u *prev_line = NULL; /* cached pointer to line */
Bram Moolenaar79518e22017-02-17 16:31:35 +0100495 static varnumber_T prev_tick = 0; /* changedtick of cached value */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200496 int bri = 0;
497 /* window width minus window margin space, i.e. what rests for text */
498 const int eff_wwidth = W_WIDTH(wp)
499 - ((wp->w_p_nu || wp->w_p_rnu)
500 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
501 ? number_width(wp) + 1 : 0);
502
503 /* used cached indent, unless pointer or 'tabstop' changed */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200504 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100505 || prev_tick != CHANGEDTICK(wp->w_buffer))
Bram Moolenaar597a4222014-06-25 14:39:50 +0200506 {
507 prev_line = line;
508 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100509 prev_tick = CHANGEDTICK(wp->w_buffer);
Bram Moolenaar597a4222014-06-25 14:39:50 +0200510 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
Bram Moolenaarf58a8472017-03-05 18:03:04 +01001430 * be marks there. But still needed in diff mode. */
1431 if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count
1432#ifdef FEAT_DIFF
1433 || curwin->w_p_diff
1434#endif
1435 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02001436 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 did_append = TRUE;
1438 }
1439#ifdef FEAT_VREPLACE
1440 else
1441 {
1442 /*
1443 * In VREPLACE mode we are starting to replace the next line.
1444 */
1445 curwin->w_cursor.lnum++;
1446 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1447 {
1448 /* In case we NL to a new line, BS to the previous one, and NL
1449 * again, we don't want to save the new line for undo twice.
1450 */
1451 (void)u_save_cursor(); /* errors are ignored! */
1452 vr_lines_changed++;
1453 }
1454 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1455 changed_bytes(curwin->w_cursor.lnum, 0);
1456 curwin->w_cursor.lnum--;
1457 did_append = FALSE;
1458 }
1459#endif
1460
1461 if (newindent
1462#ifdef FEAT_SMARTINDENT
1463 || did_si
1464#endif
1465 )
1466 {
1467 ++curwin->w_cursor.lnum;
1468#ifdef FEAT_SMARTINDENT
1469 if (did_si)
1470 {
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001471 int sw = (int)get_sw_value(curbuf);
Bram Moolenaar14f24742012-08-08 18:01:05 +02001472
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001474 newindent -= newindent % sw;
1475 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 }
1477#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001478 /* Copy the indent */
1479 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 {
1481 (void)copy_indent(newindent, saved_line);
1482
1483 /*
1484 * Set the 'preserveindent' option so that any further screwing
1485 * with the line doesn't entirely destroy our efforts to preserve
1486 * it. It gets restored at the function end.
1487 */
1488 curbuf->b_p_pi = TRUE;
1489 }
1490 else
1491 (void)set_indent(newindent, SIN_INSERT);
1492 less_cols -= curwin->w_cursor.col;
1493
1494 ai_col = curwin->w_cursor.col;
1495
1496 /*
1497 * In REPLACE mode, for each character in the new indent, there must
1498 * be a NUL on the replace stack, for when it is deleted with BS
1499 */
1500 if (REPLACE_NORMAL(State))
1501 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1502 replace_push(NUL);
1503 newcol += curwin->w_cursor.col;
1504#ifdef FEAT_SMARTINDENT
1505 if (no_si)
1506 did_si = FALSE;
1507#endif
1508 }
1509
1510#ifdef FEAT_COMMENTS
1511 /*
1512 * In REPLACE mode, for each character in the extra leader, there must be
1513 * a NUL on the replace stack, for when it is deleted with BS.
1514 */
1515 if (REPLACE_NORMAL(State))
1516 while (lead_len-- > 0)
1517 replace_push(NUL);
1518#endif
1519
1520 curwin->w_cursor = old_cursor;
1521
1522 if (dir == FORWARD)
1523 {
1524 if (trunc_line || (State & INSERT))
1525 {
1526 /* truncate current line at cursor */
1527 saved_line[curwin->w_cursor.col] = NUL;
1528 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1529 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1530 truncate_spaces(saved_line);
1531 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1532 saved_line = NULL;
1533 if (did_append)
1534 {
1535 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1536 curwin->w_cursor.lnum + 1, 1L);
1537 did_append = FALSE;
1538
1539 /* Move marks after the line break to the new line. */
1540 if (flags & OPENLINE_MARKFIX)
1541 mark_col_adjust(curwin->w_cursor.lnum,
1542 curwin->w_cursor.col + less_cols_off,
1543 1L, (long)-less_cols);
1544 }
1545 else
1546 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1547 }
1548
1549 /*
1550 * Put the cursor on the new line. Careful: the scrollup() above may
1551 * have moved w_cursor, we must use old_cursor.
1552 */
1553 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1554 }
1555 if (did_append)
1556 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1557
1558 curwin->w_cursor.col = newcol;
1559#ifdef FEAT_VIRTUALEDIT
1560 curwin->w_cursor.coladd = 0;
1561#endif
1562
1563#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1564 /*
1565 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1566 * fixthisline() from doing it (via change_indent()) by telling it we're in
1567 * normal INSERT mode.
1568 */
1569 if (State & VREPLACE_FLAG)
1570 {
1571 vreplace_mode = State; /* So we know to put things right later */
1572 State = INSERT;
1573 }
1574 else
1575 vreplace_mode = 0;
1576#endif
1577#ifdef FEAT_LISP
1578 /*
1579 * May do lisp indenting.
1580 */
1581 if (!p_paste
1582# ifdef FEAT_COMMENTS
1583 && leader == NULL
1584# endif
1585 && curbuf->b_p_lisp
1586 && curbuf->b_p_ai)
1587 {
1588 fixthisline(get_lisp_indent);
1589 p = ml_get_curline();
1590 ai_col = (colnr_T)(skipwhite(p) - p);
1591 }
1592#endif
1593#ifdef FEAT_CINDENT
1594 /*
1595 * May do indenting after opening a new line.
1596 */
1597 if (!p_paste
1598 && (curbuf->b_p_cin
1599# ifdef FEAT_EVAL
1600 || *curbuf->b_p_inde != NUL
1601# endif
1602 )
1603 && in_cinkeys(dir == FORWARD
1604 ? KEY_OPEN_FORW
1605 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1606 {
1607 do_c_expr_indent();
1608 p = ml_get_curline();
1609 ai_col = (colnr_T)(skipwhite(p) - p);
1610 }
1611#endif
1612#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1613 if (vreplace_mode != 0)
1614 State = vreplace_mode;
1615#endif
1616
1617#ifdef FEAT_VREPLACE
1618 /*
1619 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1620 * original line, and inserts the new stuff char by char, pushing old stuff
1621 * onto the replace stack (via ins_char()).
1622 */
1623 if (State & VREPLACE_FLAG)
1624 {
1625 /* Put new line in p_extra */
1626 p_extra = vim_strsave(ml_get_curline());
1627 if (p_extra == NULL)
1628 goto theend;
1629
1630 /* Put back original line */
1631 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1632
1633 /* Insert new stuff into line again */
1634 curwin->w_cursor.col = 0;
1635#ifdef FEAT_VIRTUALEDIT
1636 curwin->w_cursor.coladd = 0;
1637#endif
1638 ins_bytes(p_extra); /* will call changed_bytes() */
1639 vim_free(p_extra);
1640 next_line = NULL;
1641 }
1642#endif
1643
1644 retval = TRUE; /* success! */
1645theend:
1646 curbuf->b_p_pi = saved_pi;
1647 vim_free(saved_line);
1648 vim_free(next_line);
1649 vim_free(allocated);
1650 return retval;
1651}
1652
1653#if defined(FEAT_COMMENTS) || defined(PROTO)
1654/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +02001655 * get_leader_len() returns the length in bytes of the prefix of the given
1656 * string which introduces a comment. If this string is not a comment then
1657 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 * When "flags" is not NULL, it is set to point to the flags of the recognized
1659 * comment leader.
1660 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001661 * If "include_space" is set, include trailing whitespace while calculating the
1662 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 */
1664 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001665get_leader_len(
1666 char_u *line,
1667 char_u **flags,
1668 int backward,
1669 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670{
1671 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001672 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 int got_com = FALSE;
1674 int found_one;
1675 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1676 char_u *string; /* pointer to comment string */
1677 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001678 int middle_match_len = 0;
1679 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001680 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681
Bram Moolenaar81340392012-06-06 16:12:59 +02001682 result = i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 while (vim_iswhite(line[i])) /* leading white space is ignored */
1684 ++i;
1685
1686 /*
1687 * Repeat to match several nested comment strings.
1688 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001689 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 {
1691 /*
1692 * scan through the 'comments' option for a match
1693 */
1694 found_one = FALSE;
1695 for (list = curbuf->b_p_com; *list; )
1696 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001697 /* Get one option part into part_buf[]. Advance "list" to next
1698 * one. Put "string" at start of string. */
1699 if (!got_com && flags != NULL)
1700 *flags = list; /* remember where flags started */
1701 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1703 string = vim_strchr(part_buf, ':');
1704 if (string == NULL) /* missing ':', ignore this part */
1705 continue;
1706 *string++ = NUL; /* isolate flags from string */
1707
Bram Moolenaara4271d52011-05-10 13:38:27 +02001708 /* If we found a middle match previously, use that match when this
1709 * is not a middle or end. */
1710 if (middle_match_len != 0
1711 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1712 && vim_strchr(part_buf, COM_END) == NULL)
1713 break;
1714
1715 /* When we already found a nested comment, only accept further
1716 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1718 continue;
1719
Bram Moolenaara4271d52011-05-10 13:38:27 +02001720 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1722 continue;
1723
Bram Moolenaara4271d52011-05-10 13:38:27 +02001724 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 * When string starts with white space, must have some white space
1726 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001727 * TABs and spaces). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 if (vim_iswhite(string[0]))
1729 {
1730 if (i == 0 || !vim_iswhite(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001731 continue; /* missing white space */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 while (vim_iswhite(string[0]))
1733 ++string;
1734 }
1735 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1736 ;
1737 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001738 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
Bram Moolenaara4271d52011-05-10 13:38:27 +02001740 /* When 'b' flag used, there must be white space or an
1741 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 if (vim_strchr(part_buf, COM_BLANK) != NULL
1743 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1744 continue;
1745
Bram Moolenaara4271d52011-05-10 13:38:27 +02001746 /* We have found a match, stop searching unless this is a middle
1747 * comment. The middle comment can be a substring of the end
1748 * comment in which case it's better to return the length of the
1749 * end comment and its flags. Thus we keep searching with middle
1750 * and end matches and use an end match if it matches better. */
1751 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1752 {
1753 if (middle_match_len == 0)
1754 {
1755 middle_match_len = j;
1756 saved_flags = prev_list;
1757 }
1758 continue;
1759 }
1760 if (middle_match_len != 0 && j > middle_match_len)
1761 /* Use this match instead of the middle match, since it's a
1762 * longer thus better match. */
1763 middle_match_len = 0;
1764
1765 if (middle_match_len == 0)
1766 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 found_one = TRUE;
1768 break;
1769 }
1770
Bram Moolenaara4271d52011-05-10 13:38:27 +02001771 if (middle_match_len != 0)
1772 {
1773 /* Use the previously found middle match after failing to find a
1774 * match with an end. */
1775 if (!got_com && flags != NULL)
1776 *flags = saved_flags;
1777 i += middle_match_len;
1778 found_one = TRUE;
1779 }
1780
1781 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 if (!found_one)
1783 break;
1784
Bram Moolenaar81340392012-06-06 16:12:59 +02001785 result = i;
1786
Bram Moolenaara4271d52011-05-10 13:38:27 +02001787 /* Include any trailing white space. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 while (vim_iswhite(line[i]))
1789 ++i;
1790
Bram Moolenaar81340392012-06-06 16:12:59 +02001791 if (include_space)
1792 result = i;
1793
Bram Moolenaara4271d52011-05-10 13:38:27 +02001794 /* If this comment doesn't nest, stop here. */
1795 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 if (vim_strchr(part_buf, COM_NEST) == NULL)
1797 break;
1798 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001799 return result;
1800}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001801
Bram Moolenaar81340392012-06-06 16:12:59 +02001802/*
1803 * Return the offset at which the last comment in line starts. If there is no
1804 * comment in the whole line, -1 is returned.
1805 *
1806 * When "flags" is not null, it is set to point to the flags describing the
1807 * recognized comment leader.
1808 */
1809 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001810get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +02001811{
1812 int result = -1;
1813 int i, j;
1814 int lower_check_bound = 0;
1815 char_u *string;
1816 char_u *com_leader;
1817 char_u *com_flags;
1818 char_u *list;
1819 int found_one;
1820 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1821
1822 /*
1823 * Repeat to match several nested comment strings.
1824 */
1825 i = (int)STRLEN(line);
1826 while (--i >= lower_check_bound)
1827 {
1828 /*
1829 * scan through the 'comments' option for a match
1830 */
1831 found_one = FALSE;
1832 for (list = curbuf->b_p_com; *list; )
1833 {
1834 char_u *flags_save = list;
1835
1836 /*
1837 * Get one option part into part_buf[]. Advance list to next one.
1838 * put string at start of string.
1839 */
1840 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1841 string = vim_strchr(part_buf, ':');
1842 if (string == NULL) /* If everything is fine, this cannot actually
1843 * happen. */
1844 {
1845 continue;
1846 }
1847 *string++ = NUL; /* Isolate flags from string. */
1848 com_leader = string;
1849
1850 /*
1851 * Line contents and string must match.
1852 * When string starts with white space, must have some white space
1853 * (but the amount does not need to match, there might be a mix of
1854 * TABs and spaces).
1855 */
1856 if (vim_iswhite(string[0]))
1857 {
1858 if (i == 0 || !vim_iswhite(line[i - 1]))
1859 continue;
1860 while (vim_iswhite(string[0]))
1861 ++string;
1862 }
1863 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1864 /* do nothing */;
1865 if (string[j] != NUL)
1866 continue;
1867
1868 /*
1869 * When 'b' flag used, there must be white space or an
1870 * end-of-line after the string in the line.
1871 */
1872 if (vim_strchr(part_buf, COM_BLANK) != NULL
1873 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1874 {
1875 continue;
1876 }
1877
1878 /*
1879 * We have found a match, stop searching.
1880 */
1881 found_one = TRUE;
1882
1883 if (flags)
1884 *flags = flags_save;
1885 com_flags = flags_save;
1886
1887 break;
1888 }
1889
1890 if (found_one)
1891 {
1892 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
1893 int len1, len2, off;
1894
1895 result = i;
1896 /*
1897 * If this comment nests, continue searching.
1898 */
1899 if (vim_strchr(part_buf, COM_NEST) != NULL)
1900 continue;
1901
1902 lower_check_bound = i;
1903
1904 /* Let's verify whether the comment leader found is a substring
1905 * of other comment leaders. If it is, let's adjust the
1906 * lower_check_bound so that we make sure that we have determined
1907 * the comment leader correctly.
1908 */
1909
1910 while (vim_iswhite(*com_leader))
1911 ++com_leader;
1912 len1 = (int)STRLEN(com_leader);
1913
1914 for (list = curbuf->b_p_com; *list; )
1915 {
1916 char_u *flags_save = list;
1917
1918 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
1919 if (flags_save == com_flags)
1920 continue;
1921 string = vim_strchr(part_buf2, ':');
1922 ++string;
1923 while (vim_iswhite(*string))
1924 ++string;
1925 len2 = (int)STRLEN(string);
1926 if (len2 == 0)
1927 continue;
1928
1929 /* Now we have to verify whether string ends with a substring
1930 * beginning the com_leader. */
1931 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
1932 {
1933 --off;
1934 if (!STRNCMP(string + off, com_leader, len2 - off))
1935 {
1936 if (i - off < lower_check_bound)
1937 lower_check_bound = i - off;
1938 }
1939 }
1940 }
1941 }
1942 }
1943 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944}
1945#endif
1946
1947/*
1948 * Return the number of window lines occupied by buffer line "lnum".
1949 */
1950 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001951plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952{
1953 return plines_win(curwin, lnum, TRUE);
1954}
1955
1956 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001957plines_win(
1958 win_T *wp,
1959 linenr_T lnum,
1960 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961{
1962#if defined(FEAT_DIFF) || defined(PROTO)
1963 /* Check for filler lines above this buffer line. When folded the result
1964 * is one line anyway. */
1965 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
1966}
1967
1968 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001969plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970{
1971 return plines_win_nofill(curwin, lnum, TRUE);
1972}
1973
1974 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001975plines_win_nofill(
1976 win_T *wp,
1977 linenr_T lnum,
1978 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979{
1980#endif
1981 int lines;
1982
1983 if (!wp->w_p_wrap)
1984 return 1;
1985
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001986#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 if (wp->w_width == 0)
1988 return 1;
1989#endif
1990
1991#ifdef FEAT_FOLDING
1992 /* A folded lines is handled just like an empty line. */
1993 /* NOTE: Caller must handle lines that are MAYBE folded. */
1994 if (lineFolded(wp, lnum) == TRUE)
1995 return 1;
1996#endif
1997
1998 lines = plines_win_nofold(wp, lnum);
1999 if (winheight > 0 && lines > wp->w_height)
2000 return (int)wp->w_height;
2001 return lines;
2002}
2003
2004/*
2005 * Return number of window lines physical line "lnum" will occupy in window
2006 * "wp". Does not care about folding, 'wrap' or 'diff'.
2007 */
2008 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002009plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010{
2011 char_u *s;
2012 long col;
2013 int width;
2014
2015 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2016 if (*s == NUL) /* empty line */
2017 return 1;
2018 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
2019
2020 /*
2021 * If list mode is on, then the '$' at the end of the line may take up one
2022 * extra column.
2023 */
2024 if (wp->w_p_list && lcs_eol != NUL)
2025 col += 1;
2026
2027 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002028 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 */
2030 width = W_WIDTH(wp) - win_col_off(wp);
2031 if (width <= 0)
2032 return 32000;
2033 if (col <= width)
2034 return 1;
2035 col -= width;
2036 width += win_col_off2(wp);
2037 return (col + (width - 1)) / width + 1;
2038}
2039
2040/*
2041 * Like plines_win(), but only reports the number of physical screen lines
2042 * used from the start of the line to the given column number.
2043 */
2044 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002045plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046{
2047 long col;
2048 char_u *s;
2049 int lines = 0;
2050 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002051 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052
2053#ifdef FEAT_DIFF
2054 /* Check for filler lines above this buffer line. When folded the result
2055 * is one line anyway. */
2056 lines = diff_check_fill(wp, lnum);
2057#endif
2058
2059 if (!wp->w_p_wrap)
2060 return lines + 1;
2061
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002062#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 if (wp->w_width == 0)
2064 return lines + 1;
2065#endif
2066
Bram Moolenaar597a4222014-06-25 14:39:50 +02002067 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068
2069 col = 0;
2070 while (*s != NUL && --column >= 0)
2071 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02002072 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002073 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 }
2075
2076 /*
2077 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2078 * INSERT mode, then col must be adjusted so that it represents the last
2079 * screen position of the TAB. This only fixes an error when the TAB wraps
2080 * from one screen line to the next (when 'columns' is not a multiple of
2081 * 'ts') -- webb.
2082 */
2083 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02002084 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085
2086 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002087 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 */
2089 width = W_WIDTH(wp) - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002090 if (width <= 0)
2091 return 9999;
2092
2093 lines += 1;
2094 if (col > width)
2095 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2096 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097}
2098
2099 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002100plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101{
2102 int count = 0;
2103
2104 while (first <= last)
2105 {
2106#ifdef FEAT_FOLDING
2107 int x;
2108
2109 /* Check if there are any really folded lines, but also included lines
2110 * that are maybe folded. */
2111 x = foldedCount(wp, first, NULL);
2112 if (x > 0)
2113 {
2114 ++count; /* count 1 for "+-- folded" line */
2115 first += x;
2116 }
2117 else
2118#endif
2119 {
2120#ifdef FEAT_DIFF
2121 if (first == wp->w_topline)
2122 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2123 else
2124#endif
2125 count += plines_win(wp, first, TRUE);
2126 ++first;
2127 }
2128 }
2129 return (count);
2130}
2131
2132#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO)
2133/*
2134 * Insert string "p" at the cursor position. Stops at a NUL byte.
2135 * Handles Replace mode and multi-byte characters.
2136 */
2137 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002138ins_bytes(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139{
2140 ins_bytes_len(p, (int)STRLEN(p));
2141}
2142#endif
2143
2144#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2145 || defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO)
2146/*
2147 * Insert string "p" with length "len" at the cursor position.
2148 * Handles Replace mode and multi-byte characters.
2149 */
2150 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002151ins_bytes_len(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152{
2153 int i;
2154# ifdef FEAT_MBYTE
2155 int n;
2156
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002157 if (has_mbyte)
2158 for (i = 0; i < len; i += n)
2159 {
2160 if (enc_utf8)
2161 /* avoid reading past p[len] */
2162 n = utfc_ptr2len_len(p + i, len - i);
2163 else
2164 n = (*mb_ptr2len)(p + i);
2165 ins_char_bytes(p + i, n);
2166 }
2167 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168# endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002169 for (i = 0; i < len; ++i)
2170 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171}
2172#endif
2173
2174/*
2175 * Insert or replace a single character at the cursor position.
2176 * When in REPLACE or VREPLACE mode, replace any existing character.
2177 * Caller must have prepared for undo.
2178 * For multi-byte characters we get the whole character, the caller must
2179 * convert bytes to a character.
2180 */
2181 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002182ins_char(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002184 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar560379d2017-01-21 22:50:00 +01002185 int n = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186
Bram Moolenaar6a8ede92017-01-22 19:49:12 +01002187#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 n = (*mb_char2bytes)(c, buf);
2189
2190 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2191 * Happens for CTRL-Vu9900. */
2192 if (buf[0] == 0)
2193 buf[0] = '\n';
Bram Moolenaar560379d2017-01-21 22:50:00 +01002194#else
2195 buf[0] = c;
2196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197
2198 ins_char_bytes(buf, n);
2199}
2200
2201 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002202ins_char_bytes(char_u *buf, int charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203{
2204 int c = buf[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 int newlen; /* nr of bytes inserted */
2206 int oldlen; /* nr of bytes deleted (0 when not replacing) */
2207 char_u *p;
2208 char_u *newp;
2209 char_u *oldp;
2210 int linelen; /* length of old line including NUL */
2211 colnr_T col;
2212 linenr_T lnum = curwin->w_cursor.lnum;
2213 int i;
2214
2215#ifdef FEAT_VIRTUALEDIT
2216 /* Break tabs if needed. */
2217 if (virtual_active() && curwin->w_cursor.coladd > 0)
2218 coladvance_force(getviscol());
2219#endif
2220
2221 col = curwin->w_cursor.col;
2222 oldp = ml_get(lnum);
2223 linelen = (int)STRLEN(oldp) + 1;
2224
2225 /* The lengths default to the values for when not replacing. */
2226 oldlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 newlen = charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228
2229 if (State & REPLACE_FLAG)
2230 {
2231#ifdef FEAT_VREPLACE
2232 if (State & VREPLACE_FLAG)
2233 {
2234 colnr_T new_vcol = 0; /* init for GCC */
2235 colnr_T vcol;
2236 int old_list;
2237#ifndef FEAT_MBYTE
2238 char_u buf[2];
2239#endif
2240
2241 /*
2242 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2243 * Returns the old value of list, so when finished,
2244 * curwin->w_p_list should be set back to this.
2245 */
2246 old_list = curwin->w_p_list;
2247 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2248 curwin->w_p_list = FALSE;
2249
2250 /*
2251 * In virtual replace mode each character may replace one or more
2252 * characters (zero if it's a TAB). Count the number of bytes to
2253 * be deleted to make room for the new character, counting screen
2254 * cells. May result in adding spaces to fill a gap.
2255 */
2256 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2257#ifndef FEAT_MBYTE
2258 buf[0] = c;
2259 buf[1] = NUL;
2260#endif
2261 new_vcol = vcol + chartabsize(buf, vcol);
2262 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2263 {
2264 vcol += chartabsize(oldp + col + oldlen, vcol);
2265 /* Don't need to remove a TAB that takes us to the right
2266 * position. */
2267 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2268 break;
2269#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002270 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271#else
2272 ++oldlen;
2273#endif
2274 /* Deleted a bit too much, insert spaces. */
2275 if (vcol > new_vcol)
2276 newlen += vcol - new_vcol;
2277 }
2278 curwin->w_p_list = old_list;
2279 }
2280 else
2281#endif
2282 if (oldp[col] != NUL)
2283 {
2284 /* normal replace */
2285#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002286 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287#else
2288 oldlen = 1;
2289#endif
2290 }
2291
2292
2293 /* Push the replaced bytes onto the replace stack, so that they can be
2294 * put back when BS is used. The bytes of a multi-byte character are
2295 * done the other way around, so that the first byte is popped off
2296 * first (it tells the byte length of the character). */
2297 replace_push(NUL);
2298 for (i = 0; i < oldlen; ++i)
2299 {
2300#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002301 if (has_mbyte)
2302 i += replace_push_mb(oldp + col + i) - 1;
2303 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002305 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 }
2307 }
2308
2309 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2310 if (newp == NULL)
2311 return;
2312
2313 /* Copy bytes before the cursor. */
2314 if (col > 0)
2315 mch_memmove(newp, oldp, (size_t)col);
2316
2317 /* Copy bytes after the changed character(s). */
2318 p = newp + col;
2319 mch_memmove(p + newlen, oldp + col + oldlen,
2320 (size_t)(linelen - col - oldlen));
2321
2322 /* Insert or overwrite the new character. */
2323#ifdef FEAT_MBYTE
2324 mch_memmove(p, buf, charlen);
2325 i = charlen;
2326#else
2327 *p = c;
2328 i = 1;
2329#endif
2330
2331 /* Fill with spaces when necessary. */
2332 while (i < newlen)
2333 p[i++] = ' ';
2334
2335 /* Replace the line in the buffer. */
2336 ml_replace(lnum, newp, FALSE);
2337
2338 /* mark the buffer as changed and prepare for displaying */
2339 changed_bytes(lnum, col);
2340
2341 /*
2342 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2343 * show the match for right parens and braces.
2344 */
2345 if (p_sm && (State & INSERT)
2346 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002347#ifdef FEAT_INS_EXPAND
2348 && !ins_compl_active()
2349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002351 {
2352#ifdef FEAT_MBYTE
2353 if (has_mbyte)
2354 showmatch(mb_ptr2char(buf));
2355 else
2356#endif
2357 showmatch(c);
2358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359
2360#ifdef FEAT_RIGHTLEFT
2361 if (!p_ri || (State & REPLACE_FLAG))
2362#endif
2363 {
2364 /* Normal insert: move cursor right */
2365#ifdef FEAT_MBYTE
2366 curwin->w_cursor.col += charlen;
2367#else
2368 ++curwin->w_cursor.col;
2369#endif
2370 }
2371 /*
2372 * TODO: should try to update w_row here, to avoid recomputing it later.
2373 */
2374}
2375
2376/*
2377 * Insert a string at the cursor position.
2378 * Note: Does NOT handle Replace mode.
2379 * Caller must have prepared for undo.
2380 */
2381 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002382ins_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383{
2384 char_u *oldp, *newp;
2385 int newlen = (int)STRLEN(s);
2386 int oldlen;
2387 colnr_T col;
2388 linenr_T lnum = curwin->w_cursor.lnum;
2389
2390#ifdef FEAT_VIRTUALEDIT
2391 if (virtual_active() && curwin->w_cursor.coladd > 0)
2392 coladvance_force(getviscol());
2393#endif
2394
2395 col = curwin->w_cursor.col;
2396 oldp = ml_get(lnum);
2397 oldlen = (int)STRLEN(oldp);
2398
2399 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2400 if (newp == NULL)
2401 return;
2402 if (col > 0)
2403 mch_memmove(newp, oldp, (size_t)col);
2404 mch_memmove(newp + col, s, (size_t)newlen);
2405 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2406 ml_replace(lnum, newp, FALSE);
2407 changed_bytes(lnum, col);
2408 curwin->w_cursor.col += newlen;
2409}
2410
2411/*
2412 * Delete one character under the cursor.
2413 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2414 * Caller must have prepared for undo.
2415 *
2416 * return FAIL for failure, OK otherwise
2417 */
2418 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002419del_char(int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420{
2421#ifdef FEAT_MBYTE
2422 if (has_mbyte)
2423 {
2424 /* Make sure the cursor is at the start of a character. */
2425 mb_adjust_cursor();
2426 if (*ml_get_cursor() == NUL)
2427 return FAIL;
2428 return del_chars(1L, fixpos);
2429 }
2430#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002431 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432}
2433
2434#if defined(FEAT_MBYTE) || defined(PROTO)
2435/*
2436 * Like del_bytes(), but delete characters instead of bytes.
2437 */
2438 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002439del_chars(long count, int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440{
2441 long bytes = 0;
2442 long i;
2443 char_u *p;
2444 int l;
2445
2446 p = ml_get_cursor();
2447 for (i = 0; i < count && *p != NUL; ++i)
2448 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002449 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 bytes += l;
2451 p += l;
2452 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002453 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454}
2455#endif
2456
2457/*
2458 * Delete "count" bytes under the cursor.
2459 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2460 * Caller must have prepared for undo.
2461 *
2462 * return FAIL for failure, OK otherwise
2463 */
2464 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002465del_bytes(
2466 long count,
2467 int fixpos_arg,
2468 int use_delcombine UNUSED) /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469{
2470 char_u *oldp, *newp;
2471 colnr_T oldlen;
2472 linenr_T lnum = curwin->w_cursor.lnum;
2473 colnr_T col = curwin->w_cursor.col;
2474 int was_alloced;
2475 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002476 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477
2478 oldp = ml_get(lnum);
2479 oldlen = (int)STRLEN(oldp);
2480
2481 /*
2482 * Can't do anything when the cursor is on the NUL after the line.
2483 */
2484 if (col >= oldlen)
2485 return FAIL;
2486
2487#ifdef FEAT_MBYTE
2488 /* If 'delcombine' is set and deleting (less than) one character, only
2489 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002490 if (p_deco && use_delcombine && enc_utf8
2491 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002493 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 int n;
2495
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002496 (void)utfc_ptr2char(oldp + col, cc);
2497 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 {
2499 /* Find the last composing char, there can be several. */
2500 n = col;
2501 do
2502 {
2503 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002504 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 n += count;
2506 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2507 fixpos = 0;
2508 }
2509 }
2510#endif
2511
2512 /*
2513 * When count is too big, reduce it.
2514 */
2515 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2516 if (movelen <= 1)
2517 {
2518 /*
2519 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002520 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2521 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002523 if (col > 0 && fixpos && restart_edit == 0
2524#ifdef FEAT_VIRTUALEDIT
2525 && (ve_flags & VE_ONEMORE) == 0
2526#endif
2527 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 {
2529 --curwin->w_cursor.col;
2530#ifdef FEAT_VIRTUALEDIT
2531 curwin->w_cursor.coladd = 0;
2532#endif
2533#ifdef FEAT_MBYTE
2534 if (has_mbyte)
2535 curwin->w_cursor.col -=
2536 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2537#endif
2538 }
2539 count = oldlen - col;
2540 movelen = 1;
2541 }
2542
2543 /*
2544 * If the old line has been allocated the deletion can be done in the
2545 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002546 * Can't do this when using Netbeans, because we would need to invoke
2547 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002548 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002551 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002552 was_alloced = FALSE;
2553 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002555 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 if (was_alloced)
2557 newp = oldp; /* use same allocated memory */
2558 else
2559 { /* need to allocate a new line */
2560 newp = alloc((unsigned)(oldlen + 1 - count));
2561 if (newp == NULL)
2562 return FAIL;
2563 mch_memmove(newp, oldp, (size_t)col);
2564 }
2565 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2566 if (!was_alloced)
2567 ml_replace(lnum, newp, FALSE);
2568
2569 /* mark the buffer as changed and prepare for displaying */
2570 changed_bytes(lnum, curwin->w_cursor.col);
2571
2572 return OK;
2573}
2574
2575/*
2576 * Delete from cursor to end of line.
2577 * Caller must have prepared for undo.
2578 *
2579 * return FAIL for failure, OK otherwise
2580 */
2581 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002582truncate_line(
2583 int fixpos) /* if TRUE fix the cursor position when done */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584{
2585 char_u *newp;
2586 linenr_T lnum = curwin->w_cursor.lnum;
2587 colnr_T col = curwin->w_cursor.col;
2588
2589 if (col == 0)
2590 newp = vim_strsave((char_u *)"");
2591 else
2592 newp = vim_strnsave(ml_get(lnum), col);
2593
2594 if (newp == NULL)
2595 return FAIL;
2596
2597 ml_replace(lnum, newp, FALSE);
2598
2599 /* mark the buffer as changed and prepare for displaying */
2600 changed_bytes(lnum, curwin->w_cursor.col);
2601
2602 /*
2603 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2604 */
2605 if (fixpos && curwin->w_cursor.col > 0)
2606 --curwin->w_cursor.col;
2607
2608 return OK;
2609}
2610
2611/*
2612 * Delete "nlines" lines at the cursor.
2613 * Saves the lines for undo first if "undo" is TRUE.
2614 */
2615 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002616del_lines(
2617 long nlines, /* number of lines to delete */
2618 int undo) /* if TRUE, prepare for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619{
2620 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002621 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622
2623 if (nlines <= 0)
2624 return;
2625
2626 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002627 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 return;
2629
2630 for (n = 0; n < nlines; )
2631 {
2632 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2633 break;
2634
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002635 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 ++n;
2637
2638 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002639 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 break;
2641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002643 /* Correct the cursor position before calling deleted_lines_mark(), it may
2644 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 curwin->w_cursor.col = 0;
2646 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002647
2648 /* adjust marks, mark the buffer as changed and prepare for displaying */
2649 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650}
2651
2652 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002653gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654{
2655 char_u *ptr = ml_get_pos(pos);
2656
2657#ifdef FEAT_MBYTE
2658 if (has_mbyte)
2659 return (*mb_ptr2char)(ptr);
2660#endif
2661 return (int)*ptr;
2662}
2663
2664 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002665gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666{
2667#ifdef FEAT_MBYTE
2668 if (has_mbyte)
2669 return (*mb_ptr2char)(ml_get_cursor());
2670#endif
2671 return (int)*ml_get_cursor();
2672}
2673
2674/*
2675 * Write a character at the current cursor position.
2676 * It is directly written into the block.
2677 */
2678 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002679pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680{
2681 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2682 + curwin->w_cursor.col) = c;
2683}
2684
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685/*
2686 * When extra == 0: Return TRUE if the cursor is before or on the first
2687 * non-blank in the line.
2688 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2689 * the line.
2690 */
2691 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002692inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693{
2694 char_u *ptr;
2695 colnr_T col;
2696
2697 for (col = 0, ptr = ml_get_curline(); vim_iswhite(*ptr); ++col)
2698 ++ptr;
2699 if (col >= curwin->w_cursor.col + extra)
2700 return TRUE;
2701 else
2702 return FALSE;
2703}
2704
2705/*
2706 * Skip to next part of an option argument: Skip space and comma.
2707 */
2708 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002709skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710{
2711 if (*p == ',')
2712 ++p;
2713 while (*p == ' ')
2714 ++p;
2715 return p;
2716}
2717
2718/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002719 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 *
2721 * Most often called through changed_bytes() and changed_lines(), which also
2722 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002723 *
2724 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 */
2726 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002727changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728{
2729#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2730 /* The text of the preediting area is inserted, but this doesn't
2731 * mean a change of the buffer yet. That is delayed until the
2732 * text is committed. (this means preedit becomes empty) */
2733 if (im_is_preediting() && !xim_changed_while_preediting)
2734 return;
2735 xim_changed_while_preediting = FALSE;
2736#endif
2737
2738 if (!curbuf->b_changed)
2739 {
2740 int save_msg_scroll = msg_scroll;
2741
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002742 /* Give a warning about changing a read-only file. This may also
2743 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002745
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 /* Create a swap file if that is wanted.
2747 * Don't do this for "nofile" and "nowrite" buffer types. */
2748 if (curbuf->b_may_swap
2749#ifdef FEAT_QUICKFIX
2750 && !bt_dontwrite(curbuf)
2751#endif
2752 )
2753 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002754 int save_need_wait_return = need_wait_return;
2755
2756 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 ml_open_file(curbuf);
2758
2759 /* The ml_open_file() can cause an ATTENTION message.
2760 * Wait two seconds, to make sure the user reads this unexpected
2761 * message. Since we could be anywhere, call wait_return() now,
2762 * and don't let the emsg() set msg_scroll. */
2763 if (need_wait_return && emsg_silent == 0)
2764 {
2765 out_flush();
2766 ui_delay(2000L, TRUE);
2767 wait_return(TRUE);
2768 msg_scroll = save_msg_scroll;
2769 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002770 else
2771 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002773 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002775 ++CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776}
2777
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002778/*
2779 * Internal part of changed(), no user interaction.
2780 */
2781 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002782changed_int(void)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002783{
2784 curbuf->b_changed = TRUE;
2785 ml_setflags(curbuf);
2786#ifdef FEAT_WINDOWS
2787 check_status(curbuf);
2788 redraw_tabline = TRUE;
2789#endif
2790#ifdef FEAT_TITLE
2791 need_maketitle = TRUE; /* set window title later */
2792#endif
2793}
2794
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002795static void changedOneline(buf_T *buf, linenr_T lnum);
2796static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
2797static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798
2799/*
2800 * Changed bytes within a single line for the current buffer.
2801 * - marks the windows on this buffer to be redisplayed
2802 * - marks the buffer changed by calling changed()
2803 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002804 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 */
2806 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002807changed_bytes(linenr_T lnum, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002809 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002811
2812#ifdef FEAT_DIFF
2813 /* Diff highlighting in other diff windows may need to be updated too. */
2814 if (curwin->w_p_diff)
2815 {
2816 win_T *wp;
2817 linenr_T wlnum;
2818
Bram Moolenaar29323592016-07-24 22:04:11 +02002819 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002820 if (wp->w_p_diff && wp != curwin)
2821 {
2822 redraw_win_later(wp, VALID);
2823 wlnum = diff_lnum_win(lnum, wp);
2824 if (wlnum > 0)
2825 changedOneline(wp->w_buffer, wlnum);
2826 }
2827 }
2828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829}
2830
2831 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002832changedOneline(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002834 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 {
2836 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002837 if (lnum < buf->b_mod_top)
2838 buf->b_mod_top = lnum;
2839 else if (lnum >= buf->b_mod_bot)
2840 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 }
2842 else
2843 {
2844 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002845 buf->b_mod_set = TRUE;
2846 buf->b_mod_top = lnum;
2847 buf->b_mod_bot = lnum + 1;
2848 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 }
2850}
2851
2852/*
2853 * Appended "count" lines below line "lnum" in the current buffer.
2854 * Must be called AFTER the change and after mark_adjust().
2855 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2856 */
2857 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002858appended_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859{
2860 changed_lines(lnum + 1, 0, lnum + 1, count);
2861}
2862
2863/*
2864 * Like appended_lines(), but adjust marks first.
2865 */
2866 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002867appended_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868{
Bram Moolenaar82faa252016-06-04 20:14:07 +02002869 /* Skip mark_adjust when adding a line after the last one, there can't
Bram Moolenaarf58a8472017-03-05 18:03:04 +01002870 * be marks there. But it's still needed in diff mode. */
2871 if (lnum + count < curbuf->b_ml.ml_line_count
2872#ifdef FEAT_DIFF
2873 || curwin->w_p_diff
2874#endif
2875 )
Bram Moolenaar82faa252016-06-04 20:14:07 +02002876 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 changed_lines(lnum + 1, 0, lnum + 1, count);
2878}
2879
2880/*
2881 * Deleted "count" lines at line "lnum" in the current buffer.
2882 * Must be called AFTER the change and after mark_adjust().
2883 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2884 */
2885 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002886deleted_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887{
2888 changed_lines(lnum, 0, lnum + count, -count);
2889}
2890
2891/*
2892 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002893 * Make sure the cursor is on a valid line before calling, a GUI callback may
2894 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 */
2896 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002897deleted_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898{
2899 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
2900 changed_lines(lnum, 0, lnum + count, -count);
2901}
2902
2903/*
2904 * Changed lines for the current buffer.
2905 * Must be called AFTER the change and after mark_adjust().
2906 * - mark the buffer changed by calling changed()
2907 * - mark the windows on this buffer to be redisplayed
2908 * - invalidate cached values
2909 * "lnum" is the first line that needs displaying, "lnume" the first line
2910 * below the changed lines (BEFORE the change).
2911 * When only inserting lines, "lnum" and "lnume" are equal.
2912 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002913 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 */
2915 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002916changed_lines(
2917 linenr_T lnum, /* first line with change */
2918 colnr_T col, /* column in first line with change */
2919 linenr_T lnume, /* line below last changed line */
2920 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002922 changed_lines_buf(curbuf, lnum, lnume, xtra);
2923
2924#ifdef FEAT_DIFF
2925 if (xtra == 0 && curwin->w_p_diff)
2926 {
2927 /* When the number of lines doesn't change then mark_adjust() isn't
2928 * called and other diff buffers still need to be marked for
2929 * displaying. */
2930 win_T *wp;
2931 linenr_T wlnum;
2932
Bram Moolenaar29323592016-07-24 22:04:11 +02002933 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002934 if (wp->w_p_diff && wp != curwin)
2935 {
2936 redraw_win_later(wp, VALID);
2937 wlnum = diff_lnum_win(lnum, wp);
2938 if (wlnum > 0)
2939 changed_lines_buf(wp->w_buffer, wlnum,
2940 lnume - lnum + wlnum, 0L);
2941 }
2942 }
2943#endif
2944
2945 changed_common(lnum, col, lnume, xtra);
2946}
2947
2948 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002949changed_lines_buf(
2950 buf_T *buf,
2951 linenr_T lnum, /* first line with change */
2952 linenr_T lnume, /* line below last changed line */
2953 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002954{
2955 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 {
2957 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002958 if (lnum < buf->b_mod_top)
2959 buf->b_mod_top = lnum;
2960 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 {
2962 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002963 buf->b_mod_bot += xtra;
2964 if (buf->b_mod_bot < lnum)
2965 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00002967 if (lnume + xtra > buf->b_mod_bot)
2968 buf->b_mod_bot = lnume + xtra;
2969 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 }
2971 else
2972 {
2973 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002974 buf->b_mod_set = TRUE;
2975 buf->b_mod_top = lnum;
2976 buf->b_mod_bot = lnume + xtra;
2977 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979}
2980
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002981/*
2982 * Common code for when a change is was made.
2983 * See changed_lines() for the arguments.
2984 * Careful: may trigger autocommands that reload the buffer.
2985 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002987changed_common(
2988 linenr_T lnum,
2989 colnr_T col,
2990 linenr_T lnume,
2991 long xtra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992{
2993 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00002994#ifdef FEAT_WINDOWS
2995 tabpage_T *tp;
2996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 int i;
2998#ifdef FEAT_JUMPLIST
2999 int cols;
3000 pos_T *p;
3001 int add;
3002#endif
3003
3004 /* mark the buffer as modified */
3005 changed();
3006
3007 /* set the '. mark */
3008 if (!cmdmod.keepjumps)
3009 {
3010 curbuf->b_last_change.lnum = lnum;
3011 curbuf->b_last_change.col = col;
3012
3013#ifdef FEAT_JUMPLIST
3014 /* Create a new entry if a new undo-able change was started or we
3015 * don't have an entry yet. */
3016 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3017 {
3018 if (curbuf->b_changelistlen == 0)
3019 add = TRUE;
3020 else
3021 {
3022 /* Don't create a new entry when the line number is the same
3023 * as the last one and the column is not too far away. Avoids
3024 * creating many entries for typing "xxxxx". */
3025 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3026 if (p->lnum != lnum)
3027 add = TRUE;
3028 else
3029 {
3030 cols = comp_textwidth(FALSE);
3031 if (cols == 0)
3032 cols = 79;
3033 add = (p->col + cols < col || col + cols < p->col);
3034 }
3035 }
3036 if (add)
3037 {
3038 /* This is the first of a new sequence of undo-able changes
3039 * and it's at some distance of the last change. Use a new
3040 * position in the changelist. */
3041 curbuf->b_new_change = FALSE;
3042
3043 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3044 {
3045 /* changelist is full: remove oldest entry */
3046 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3047 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3048 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003049 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 {
3051 /* Correct position in changelist for other windows on
3052 * this buffer. */
3053 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3054 --wp->w_changelistidx;
3055 }
3056 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003057 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 {
3059 /* For other windows, if the position in the changelist is
3060 * at the end it stays at the end. */
3061 if (wp->w_buffer == curbuf
3062 && wp->w_changelistidx == curbuf->b_changelistlen)
3063 ++wp->w_changelistidx;
3064 }
3065 ++curbuf->b_changelistlen;
3066 }
3067 }
3068 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3069 curbuf->b_last_change;
3070 /* The current window is always after the last change, so that "g,"
3071 * takes you back to it. */
3072 curwin->w_changelistidx = curbuf->b_changelistlen;
3073#endif
3074 }
3075
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003076 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 {
3078 if (wp->w_buffer == curbuf)
3079 {
3080 /* Mark this window to be redrawn later. */
3081 if (wp->w_redr_type < VALID)
3082 wp->w_redr_type = VALID;
3083
3084 /* Check if a change in the buffer has invalidated the cached
3085 * values for the cursor. */
3086#ifdef FEAT_FOLDING
3087 /*
3088 * Update the folds for this window. Can't postpone this, because
3089 * a following operator might work on the whole fold: ">>dd".
3090 */
3091 foldUpdate(wp, lnum, lnume + xtra - 1);
3092
3093 /* The change may cause lines above or below the change to become
3094 * included in a fold. Set lnum/lnume to the first/last line that
3095 * might be displayed differently.
3096 * Set w_cline_folded here as an efficient way to update it when
3097 * inserting lines just above a closed fold. */
3098 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3099 if (wp->w_cursor.lnum == lnum)
3100 wp->w_cline_folded = i;
3101 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3102 if (wp->w_cursor.lnum == lnume)
3103 wp->w_cline_folded = i;
3104
3105 /* If the changed line is in a range of previously folded lines,
3106 * compare with the first line in that range. */
3107 if (wp->w_cursor.lnum <= lnum)
3108 {
3109 i = find_wl_entry(wp, lnum);
3110 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3111 changed_line_abv_curs_win(wp);
3112 }
3113#endif
3114
3115 if (wp->w_cursor.lnum > lnum)
3116 changed_line_abv_curs_win(wp);
3117 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3118 changed_cline_bef_curs_win(wp);
3119 if (wp->w_botline >= lnum)
3120 {
3121 /* Assume that botline doesn't change (inserted lines make
3122 * other lines scroll down below botline). */
3123 approximate_botline_win(wp);
3124 }
3125
3126 /* Check if any w_lines[] entries have become invalid.
3127 * For entries below the change: Correct the lnums for
3128 * inserted/deleted lines. Makes it possible to stop displaying
3129 * after the change. */
3130 for (i = 0; i < wp->w_lines_valid; ++i)
3131 if (wp->w_lines[i].wl_valid)
3132 {
3133 if (wp->w_lines[i].wl_lnum >= lnum)
3134 {
3135 if (wp->w_lines[i].wl_lnum < lnume)
3136 {
3137 /* line included in change */
3138 wp->w_lines[i].wl_valid = FALSE;
3139 }
3140 else if (xtra != 0)
3141 {
3142 /* line below change */
3143 wp->w_lines[i].wl_lnum += xtra;
3144#ifdef FEAT_FOLDING
3145 wp->w_lines[i].wl_lastlnum += xtra;
3146#endif
3147 }
3148 }
3149#ifdef FEAT_FOLDING
3150 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3151 {
3152 /* change somewhere inside this range of folded lines,
3153 * may need to be redrawn */
3154 wp->w_lines[i].wl_valid = FALSE;
3155 }
3156#endif
3157 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003158
3159#ifdef FEAT_FOLDING
3160 /* Take care of side effects for setting w_topline when folds have
3161 * changed. Esp. when the buffer was changed in another window. */
3162 if (hasAnyFolding(wp))
3163 set_topline(wp, wp->w_topline);
3164#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003165 /* relative numbering may require updating more */
3166 if (wp->w_p_rnu)
3167 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 }
3169 }
3170
3171 /* Call update_screen() later, which checks out what needs to be redrawn,
3172 * since it notices b_mod_set and then uses b_mod_*. */
3173 if (must_redraw < VALID)
3174 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003175
3176#ifdef FEAT_AUTOCMD
3177 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003178 if (lnum <= curwin->w_cursor.lnum
3179 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003180 last_cursormoved.lnum = 0;
3181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182}
3183
3184/*
3185 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3186 */
3187 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003188unchanged(
3189 buf_T *buf,
3190 int ff) /* also reset 'fileformat' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003192 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 {
3194 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003195 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 if (ff)
3197 save_file_ff(buf);
3198#ifdef FEAT_WINDOWS
3199 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003200 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201#endif
3202#ifdef FEAT_TITLE
3203 need_maketitle = TRUE; /* set window title later */
3204#endif
3205 }
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003206 ++CHANGEDTICK(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207#ifdef FEAT_NETBEANS_INTG
3208 netbeans_unmodified(buf);
3209#endif
3210}
3211
3212#if defined(FEAT_WINDOWS) || defined(PROTO)
3213/*
3214 * check_status: called when the status bars for the buffer 'buf'
3215 * need to be updated
3216 */
3217 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003218check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219{
3220 win_T *wp;
3221
Bram Moolenaar29323592016-07-24 22:04:11 +02003222 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 if (wp->w_buffer == buf && wp->w_status_height)
3224 {
3225 wp->w_redr_status = TRUE;
3226 if (must_redraw < VALID)
3227 must_redraw = VALID;
3228 }
3229}
3230#endif
3231
3232/*
3233 * If the file is readonly, give a warning message with the first change.
3234 * Don't do this for autocommands.
3235 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003236 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003238 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 */
3240 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003241change_warning(
3242 int col) /* column for message; non-zero when in insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 mode and 'showmode' is on */
3244{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003245 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3246
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 if (curbuf->b_did_warn == FALSE
3248 && curbufIsChanged() == 0
3249#ifdef FEAT_AUTOCMD
3250 && !autocmd_busy
3251#endif
3252 && curbuf->b_p_ro)
3253 {
3254#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003255 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003257 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 if (!curbuf->b_p_ro)
3259 return;
3260#endif
3261 /*
3262 * Do what msg() does, but with a column offset if the warning should
3263 * be after the mode message.
3264 */
3265 msg_start();
3266 if (msg_row == Rows - 1)
3267 msg_col = col;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003268 msg_source(hl_attr(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003269 MSG_PUTS_ATTR(_(w_readonly), hl_attr(HLF_W) | MSG_HIST);
3270#ifdef FEAT_EVAL
3271 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 msg_clr_eos();
3274 (void)msg_end();
Bram Moolenaare5f2a072017-02-01 22:31:49 +01003275 if (msg_silent == 0 && !silent_mode
3276#ifdef FEAT_EVAL
3277 && time_for_testing != 1
3278#endif
3279 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 {
3281 out_flush();
3282 ui_delay(1000L, TRUE); /* give the user time to think about it */
3283 }
3284 curbuf->b_did_warn = TRUE;
3285 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3286 if (msg_row < Rows - 1)
3287 showmode();
3288 }
3289}
3290
3291/*
3292 * Ask for a reply from the user, a 'y' or a 'n'.
3293 * No other characters are accepted, the message is repeated until a valid
3294 * reply is entered or CTRL-C is hit.
3295 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3296 * from any buffers but directly from the user.
3297 *
3298 * return the 'y' or 'n'
3299 */
3300 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003301ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302{
3303 int r = ' ';
3304 int save_State = State;
3305
3306 if (exiting) /* put terminal in raw mode for this question */
3307 settmode(TMODE_RAW);
3308 ++no_wait_return;
3309#ifdef USE_ON_FLY_SCROLL
3310 dont_scroll = TRUE; /* disallow scrolling here */
3311#endif
3312 State = CONFIRM; /* mouse behaves like with :confirm */
3313#ifdef FEAT_MOUSE
3314 setmouse(); /* disables mouse for xterm */
3315#endif
3316 ++no_mapping;
3317 ++allow_keys; /* no mapping here, but recognize keys */
3318
3319 while (r != 'y' && r != 'n')
3320 {
3321 /* same highlighting as for wait_return */
3322 smsg_attr(hl_attr(HLF_R), (char_u *)"%s (y/n)?", str);
3323 if (direct)
3324 r = get_keystroke();
3325 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003326 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 if (r == Ctrl_C || r == ESC)
3328 r = 'n';
3329 msg_putchar(r); /* show what you typed */
3330 out_flush();
3331 }
3332 --no_wait_return;
3333 State = save_State;
3334#ifdef FEAT_MOUSE
3335 setmouse();
3336#endif
3337 --no_mapping;
3338 --allow_keys;
3339
3340 return r;
3341}
3342
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003343#if defined(FEAT_MOUSE) || defined(PROTO)
3344/*
3345 * Return TRUE if "c" is a mouse key.
3346 */
3347 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003348is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003349{
3350 return c == K_LEFTMOUSE
3351 || c == K_LEFTMOUSE_NM
3352 || c == K_LEFTDRAG
3353 || c == K_LEFTRELEASE
3354 || c == K_LEFTRELEASE_NM
3355 || c == K_MIDDLEMOUSE
3356 || c == K_MIDDLEDRAG
3357 || c == K_MIDDLERELEASE
3358 || c == K_RIGHTMOUSE
3359 || c == K_RIGHTDRAG
3360 || c == K_RIGHTRELEASE
3361 || c == K_MOUSEDOWN
3362 || c == K_MOUSEUP
3363 || c == K_MOUSELEFT
3364 || c == K_MOUSERIGHT
3365 || c == K_X1MOUSE
3366 || c == K_X1DRAG
3367 || c == K_X1RELEASE
3368 || c == K_X2MOUSE
3369 || c == K_X2DRAG
3370 || c == K_X2RELEASE;
3371}
3372#endif
3373
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374/*
3375 * Get a key stroke directly from the user.
3376 * Ignores mouse clicks and scrollbar events, except a click for the left
3377 * button (used at the more prompt).
3378 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3379 * Disadvantage: typeahead is ignored.
3380 * Translates the interrupt character for unix to ESC.
3381 */
3382 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003383get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003385 char_u *buf = NULL;
3386 int buflen = 150;
3387 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 int len = 0;
3389 int n;
3390 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003391 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392
3393 mapped_ctrl_c = FALSE; /* mappings are not used here */
3394 for (;;)
3395 {
3396 cursor_on();
3397 out_flush();
3398
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003399 /* Leave some room for check_termcode() to insert a key code into (max
3400 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3401 * bytes. */
3402 maxlen = (buflen - 6 - len) / 3;
3403 if (buf == NULL)
3404 buf = alloc(buflen);
3405 else if (maxlen < 10)
3406 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003407 char_u *t_buf = buf;
3408
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003409 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003410 * escape sequence. */
3411 buflen += 100;
3412 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003413 if (buf == NULL)
3414 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003415 maxlen = (buflen - 6 - len) / 3;
3416 }
3417 if (buf == NULL)
3418 {
3419 do_outofmem_msg((long_u)buflen);
3420 return ESC; /* panic! */
3421 }
3422
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003424 * terminal code to complete. */
3425 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 if (n > 0)
3427 {
3428 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003429 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003431 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003433 else if (len > 0)
3434 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435
Bram Moolenaar4395a712006-09-05 18:57:57 +00003436 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003437 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003438 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003440
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003441 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003442 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003443 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003444 {
3445 /* Redrawing was postponed, do it now. */
3446 update_screen(0);
3447 setcursor(); /* put cursor back where it belongs */
3448 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003449 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003450 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003451 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003453 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 continue;
3455
3456 /* Handle modifier and/or special key code. */
3457 n = buf[0];
3458 if (n == K_SPECIAL)
3459 {
3460 n = TO_SPECIAL(buf[1], buf[2]);
3461 if (buf[1] == KS_MODIFIER
3462 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003463#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003464 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003465#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003466#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 || n == K_VER_SCROLLBAR
3468 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469#endif
3470 )
3471 {
3472 if (buf[1] == KS_MODIFIER)
3473 mod_mask = buf[2];
3474 len -= 3;
3475 if (len > 0)
3476 mch_memmove(buf, buf + 3, (size_t)len);
3477 continue;
3478 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003479 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 }
3481#ifdef FEAT_MBYTE
3482 if (has_mbyte)
3483 {
3484 if (MB_BYTE2LEN(n) > len)
3485 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003486 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 n = (*mb_ptr2char)(buf);
3488 }
3489#endif
3490#ifdef UNIX
3491 if (n == intr_char)
3492 n = ESC;
3493#endif
3494 break;
3495 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003496 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497
3498 mapped_ctrl_c = save_mapped_ctrl_c;
3499 return n;
3500}
3501
3502/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003503 * Get a number from the user.
3504 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 */
3506 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003507get_number(
3508 int colon, /* allow colon to abort */
3509 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510{
3511 int n = 0;
3512 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003513 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003515 if (mouse_used != NULL)
3516 *mouse_used = FALSE;
3517
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 /* When not printing messages, the user won't know what to type, return a
3519 * zero (as if CR was hit). */
3520 if (msg_silent != 0)
3521 return 0;
3522
3523#ifdef USE_ON_FLY_SCROLL
3524 dont_scroll = TRUE; /* disallow scrolling here */
3525#endif
3526 ++no_mapping;
3527 ++allow_keys; /* no mapping here, but recognize keys */
3528 for (;;)
3529 {
3530 windgoto(msg_row, msg_col);
3531 c = safe_vgetc();
3532 if (VIM_ISDIGIT(c))
3533 {
3534 n = n * 10 + c - '0';
3535 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003536 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 }
3538 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3539 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003540 if (typed > 0)
3541 {
3542 MSG_PUTS("\b \b");
3543 --typed;
3544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003547#ifdef FEAT_MOUSE
3548 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3549 {
3550 *mouse_used = TRUE;
3551 n = mouse_row + 1;
3552 break;
3553 }
3554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 else if (n == 0 && c == ':' && colon)
3556 {
3557 stuffcharReadbuff(':');
3558 if (!exmode_active)
3559 cmdline_row = msg_row;
3560 skip_redraw = TRUE; /* skip redraw once */
3561 do_redraw = FALSE;
3562 break;
3563 }
3564 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3565 break;
3566 }
3567 --no_mapping;
3568 --allow_keys;
3569 return n;
3570}
3571
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003572/*
3573 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003574 * When "mouse_used" is not NULL allow using the mouse and in that case return
3575 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003576 */
3577 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003578prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003579{
3580 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003581 int save_cmdline_row;
3582 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003583
3584 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003585 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003586 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003587 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003588 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003589
Bram Moolenaar203335e2006-09-03 14:35:42 +00003590 /* Set the state such that text can be selected/copied/pasted and we still
3591 * get mouse events. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003592 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003593 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003594 save_State = State;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003595 State = CMDLINE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003596
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003597 i = get_number(TRUE, mouse_used);
3598 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003599 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003600 /* don't call wait_return() now */
3601 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003602 cmdline_row = msg_row - 1;
3603 need_wait_return = FALSE;
3604 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003605 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003606 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003607 else
3608 cmdline_row = save_cmdline_row;
3609 State = save_State;
3610
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003611 return i;
3612}
3613
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003615msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616{
3617 long pn;
3618
3619 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3621 return;
3622
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003623 /* We don't want to overwrite another important message, but do overwrite
3624 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3625 * then "put" reports the last action. */
3626 if (keep_msg != NULL && !keep_msg_more)
3627 return;
3628
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 if (n > 0)
3630 pn = n;
3631 else
3632 pn = -n;
3633
3634 if (pn > p_report)
3635 {
3636 if (pn == 1)
3637 {
3638 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003639 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3640 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003642 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3643 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 }
3645 else
3646 {
3647 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003648 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3649 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003651 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3652 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 }
3654 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003655 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 if (msg(msg_buf))
3657 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003658 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003659 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 }
3661 }
3662}
3663
3664/*
3665 * flush map and typeahead buffers and give a warning for an error
3666 */
3667 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003668beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669{
3670 if (emsg_silent == 0)
3671 {
3672 flush_buffers(FALSE);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003673 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 }
3675}
3676
3677/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003678 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 */
3680 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003681vim_beep(
3682 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683{
3684 if (emsg_silent == 0)
3685 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003686 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3687 {
3688 if (p_vb
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689#ifdef FEAT_GUI
Bram Moolenaar165bc692015-07-21 17:53:25 +02003690 /* While the GUI is starting up the termcap is set for the
3691 * GUI but the output still goes to a terminal. */
3692 && !(gui.in_use && gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693#endif
Bram Moolenaar165bc692015-07-21 17:53:25 +02003694 )
Bram Moolenaar165bc692015-07-21 17:53:25 +02003695 out_str(T_VB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02003697 out_char(BELL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003699
3700 /* When 'verbose' is set and we are sourcing a script or executing a
3701 * function give the user a hint where the beep comes from. */
3702 if (vim_strchr(p_debug, 'e') != NULL)
3703 {
3704 msg_source(hl_attr(HLF_W));
3705 msg_attr((char_u *)_("Beep!"), hl_attr(HLF_W));
3706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 }
3708}
3709
3710/*
3711 * To get the "real" home directory:
3712 * - get value of $HOME
3713 * For Unix:
3714 * - go to that directory
3715 * - do mch_dirname() to get the real name of that directory.
3716 * This also works with mounts and links.
3717 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3718 */
3719static char_u *homedir = NULL;
3720
3721 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003722init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723{
3724 char_u *var;
3725
Bram Moolenaar05159a02005-02-26 23:04:13 +00003726 /* In case we are called a second time (when 'encoding' changes). */
3727 vim_free(homedir);
3728 homedir = NULL;
3729
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730#ifdef VMS
3731 var = mch_getenv((char_u *)"SYS$LOGIN");
3732#else
3733 var = mch_getenv((char_u *)"HOME");
3734#endif
3735
3736 if (var != NULL && *var == NUL) /* empty is same as not set */
3737 var = NULL;
3738
3739#ifdef WIN3264
3740 /*
3741 * Weird but true: $HOME may contain an indirect reference to another
3742 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3743 * when $HOME is being set.
3744 */
3745 if (var != NULL && *var == '%')
3746 {
3747 char_u *p;
3748 char_u *exp;
3749
3750 p = vim_strchr(var + 1, '%');
3751 if (p != NULL)
3752 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003753 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 exp = mch_getenv(NameBuff);
3755 if (exp != NULL && *exp != NUL
3756 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3757 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003758 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 var = NameBuff;
3760 /* Also set $HOME, it's needed for _viminfo. */
3761 vim_setenv((char_u *)"HOME", NameBuff);
3762 }
3763 }
3764 }
3765
3766 /*
3767 * Typically, $HOME is not defined on Windows, unless the user has
3768 * specifically defined it for Vim's sake. However, on Windows NT
3769 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3770 * each user. Try constructing $HOME from these.
3771 */
3772 if (var == NULL)
3773 {
3774 char_u *homedrive, *homepath;
3775
3776 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3777 homepath = mch_getenv((char_u *)"HOMEPATH");
Bram Moolenaar6f977012010-01-06 17:53:38 +01003778 if (homepath == NULL || *homepath == NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003779 homepath = (char_u *)"\\";
Bram Moolenaar6f977012010-01-06 17:53:38 +01003780 if (homedrive != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3782 {
3783 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3784 if (NameBuff[0] != NUL)
3785 {
3786 var = NameBuff;
3787 /* Also set $HOME, it's needed for _viminfo. */
3788 vim_setenv((char_u *)"HOME", NameBuff);
3789 }
3790 }
3791 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003792
3793# if defined(FEAT_MBYTE)
3794 if (enc_utf8 && var != NULL)
3795 {
3796 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003797 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003798
3799 /* Convert from active codepage to UTF-8. Other conversions are
3800 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003801 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003802 if (pp != NULL)
3803 {
3804 homedir = pp;
3805 return;
3806 }
3807 }
3808# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809#endif
3810
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003811#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 /*
3813 * Default home dir is C:/
3814 * Best assumption we can make in such a situation.
3815 */
3816 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003817 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818#endif
3819 if (var != NULL)
3820 {
3821#ifdef UNIX
3822 /*
3823 * Change to the directory and get the actual path. This resolves
3824 * links. Don't do it when we can't return.
3825 */
3826 if (mch_dirname(NameBuff, MAXPATHL) == OK
3827 && mch_chdir((char *)NameBuff) == 0)
3828 {
3829 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3830 var = IObuff;
3831 if (mch_chdir((char *)NameBuff) != 0)
3832 EMSG(_(e_prev_dir));
3833 }
3834#endif
3835 homedir = vim_strsave(var);
3836 }
3837}
3838
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003839#if defined(EXITFREE) || defined(PROTO)
3840 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003841free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003842{
3843 vim_free(homedir);
3844}
Bram Moolenaar24305862012-08-15 14:05:05 +02003845
3846# ifdef FEAT_CMDL_COMPL
3847 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003848free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02003849{
3850 ga_clear_strings(&ga_users);
3851}
3852# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003853#endif
3854
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003856 * Call expand_env() and store the result in an allocated string.
3857 * This is not very memory efficient, this expects the result to be freed
3858 * again soon.
3859 */
3860 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003861expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003862{
3863 return expand_env_save_opt(src, FALSE);
3864}
3865
3866/*
3867 * Idem, but when "one" is TRUE handle the string as one file name, only
3868 * expand "~" at the start.
3869 */
3870 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003871expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003872{
3873 char_u *p;
3874
3875 p = alloc(MAXPATHL);
3876 if (p != NULL)
3877 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3878 return p;
3879}
3880
3881/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 * Expand environment variable with path name.
3883 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003884 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 * If anything fails no expansion is done and dst equals src.
3886 */
3887 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003888expand_env(
3889 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
3890 char_u *dst, /* where to put the result */
3891 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003893 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894}
3895
3896 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003897expand_env_esc(
3898 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
3899 char_u *dst, /* where to put the result */
3900 int dstlen, /* maximum length of the result */
3901 int esc, /* escape spaces in expanded variables */
3902 int one, /* "srcp" is one file name */
3903 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003905 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 char_u *tail;
3907 int c;
3908 char_u *var;
3909 int copy_char;
3910 int mustfree; /* var was allocated, need to free it later */
3911 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003912 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003914 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003915 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003916
3917 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 --dstlen; /* leave one char space for "\," */
3919 while (*src && dstlen > 0)
3920 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003921#ifdef FEAT_EVAL
3922 /* Skip over `=expr`. */
3923 if (src[0] == '`' && src[1] == '=')
3924 {
3925 size_t len;
3926
3927 var = src;
3928 src += 2;
3929 (void)skip_expr(&src);
3930 if (*src == '`')
3931 ++src;
3932 len = src - var;
3933 if (len > (size_t)dstlen)
3934 len = dstlen;
3935 vim_strncpy(dst, var, len);
3936 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02003937 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003938 continue;
3939 }
3940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003942 if ((*src == '$'
3943#ifdef VMS
3944 && at_start
3945#endif
3946 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003947#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 || *src == '%'
3949#endif
3950 || (*src == '~' && at_start))
3951 {
3952 mustfree = FALSE;
3953
3954 /*
3955 * The variable name is copied into dst temporarily, because it may
3956 * be a string in read-only memory and a NUL needs to be appended.
3957 */
3958 if (*src != '~') /* environment var */
3959 {
3960 tail = src + 1;
3961 var = dst;
3962 c = dstlen - 1;
3963
3964#ifdef UNIX
3965 /* Unix has ${var-name} type environment vars */
3966 if (*tail == '{' && !vim_isIDc('{'))
3967 {
3968 tail++; /* ignore '{' */
3969 while (c-- > 0 && *tail && *tail != '}')
3970 *var++ = *tail++;
3971 }
3972 else
3973#endif
3974 {
3975 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003976#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 || (*src == '%' && *tail != '%')
3978#endif
3979 ))
3980 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 }
3983 }
3984
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003985#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986# ifdef UNIX
3987 if (src[1] == '{' && *tail != '}')
3988# else
3989 if (*src == '%' && *tail != '%')
3990# endif
3991 var = NULL;
3992 else
3993 {
3994# ifdef UNIX
3995 if (src[1] == '{')
3996# else
3997 if (*src == '%')
3998#endif
3999 ++tail;
4000#endif
4001 *var = NUL;
4002 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004003#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 }
4005#endif
4006 }
4007 /* home directory */
4008 else if ( src[1] == NUL
4009 || vim_ispathsep(src[1])
4010 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4011 {
4012 var = homedir;
4013 tail = src + 1;
4014 }
4015 else /* user directory */
4016 {
4017#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4018 /*
4019 * Copy ~user to dst[], so we can put a NUL after it.
4020 */
4021 tail = src;
4022 var = dst;
4023 c = dstlen - 1;
4024 while ( c-- > 0
4025 && *tail
4026 && vim_isfilec(*tail)
4027 && !vim_ispathsep(*tail))
4028 *var++ = *tail++;
4029 *var = NUL;
4030# ifdef UNIX
4031 /*
4032 * If the system supports getpwnam(), use it.
4033 * Otherwise, or if getpwnam() fails, the shell is used to
4034 * expand ~user. This is slower and may fail if the shell
4035 * does not support ~user (old versions of /bin/sh).
4036 */
4037# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4038 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004039 /* Note: memory allocated by getpwnam() is never freed.
4040 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar187a4f22017-02-23 17:07:14 +01004041 struct passwd *pw = (*dst == NUL)
4042 ? NULL : getpwnam((char *)dst + 1);
4043
4044 var = (pw == NULL) ? NULL : (char_u *)pw->pw_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 }
4046 if (var == NULL)
4047# endif
4048 {
4049 expand_T xpc;
4050
4051 ExpandInit(&xpc);
4052 xpc.xp_context = EXPAND_FILES;
4053 var = ExpandOne(&xpc, dst, NULL,
4054 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 mustfree = TRUE;
4056 }
4057
4058# else /* !UNIX, thus VMS */
4059 /*
4060 * USER_HOME is a comma-separated list of
4061 * directories to search for the user account in.
4062 */
4063 {
4064 char_u test[MAXPATHL], paths[MAXPATHL];
4065 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004066 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067
4068 STRCPY(paths, USER_HOME);
4069 next_path = paths;
4070 while (*next_path)
4071 {
4072 for (path = next_path; *next_path && *next_path != ',';
4073 next_path++);
4074 if (*next_path)
4075 *next_path++ = NUL;
4076 STRCPY(test, path);
4077 STRCAT(test, "/");
4078 STRCAT(test, dst + 1);
4079 if (mch_stat(test, &st) == 0)
4080 {
4081 var = alloc(STRLEN(test) + 1);
4082 STRCPY(var, test);
4083 mustfree = TRUE;
4084 break;
4085 }
4086 }
4087 }
4088# endif /* UNIX */
4089#else
4090 /* cannot expand user's home directory, so don't try */
4091 var = NULL;
4092 tail = (char_u *)""; /* for gcc */
4093#endif /* UNIX || VMS */
4094 }
4095
4096#ifdef BACKSLASH_IN_FILENAME
4097 /* If 'shellslash' is set change backslashes to forward slashes.
4098 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4099 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4100 {
4101 char_u *p = vim_strsave(var);
4102
4103 if (p != NULL)
4104 {
4105 if (mustfree)
4106 vim_free(var);
4107 var = p;
4108 mustfree = TRUE;
4109 forward_slash(var);
4110 }
4111 }
4112#endif
4113
4114 /* If "var" contains white space, escape it with a backslash.
4115 * Required for ":e ~/tt" when $HOME includes a space. */
4116 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4117 {
4118 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4119
4120 if (p != NULL)
4121 {
4122 if (mustfree)
4123 vim_free(var);
4124 var = p;
4125 mustfree = TRUE;
4126 }
4127 }
4128
4129 if (var != NULL && *var != NUL
4130 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4131 {
4132 STRCPY(dst, var);
4133 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004134 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 /* if var[] ends in a path separator and tail[] starts
4136 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004137 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4139 && dst[-1] != ':'
4140#endif
4141 && vim_ispathsep(*tail))
4142 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004143 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 src = tail;
4145 copy_char = FALSE;
4146 }
4147 if (mustfree)
4148 vim_free(var);
4149 }
4150
4151 if (copy_char) /* copy at least one char */
4152 {
4153 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004154 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004155 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4156 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 */
4158 at_start = FALSE;
4159 if (src[0] == '\\' && src[1] != NUL)
4160 {
4161 *dst++ = *src++;
4162 --dstlen;
4163 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004164 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 at_start = TRUE;
4166 *dst++ = *src++;
4167 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004168
4169 if (startstr != NULL && src - startstr_len >= srcp
4170 && STRNCMP(src - startstr_len, startstr, startstr_len) == 0)
4171 at_start = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 }
4173 }
4174 *dst = NUL;
4175}
4176
4177/*
4178 * Vim's version of getenv().
4179 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004180 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004181 * "mustfree" is set to TRUE when returned is allocated, it must be
4182 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 */
4184 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004185vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186{
4187 char_u *p;
4188 char_u *pend;
4189 int vimruntime;
4190
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004191#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 /* use "C:/" when $HOME is not set */
4193 if (STRCMP(name, "HOME") == 0)
4194 return homedir;
4195#endif
4196
4197 p = mch_getenv(name);
4198 if (p != NULL && *p == NUL) /* empty is the same as not set */
4199 p = NULL;
4200
4201 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004202 {
4203#if defined(FEAT_MBYTE) && defined(WIN3264)
4204 if (enc_utf8)
4205 {
4206 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004207 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004208
4209 /* Convert from active codepage to UTF-8. Other conversions are
4210 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004211 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004212 if (pp != NULL)
4213 {
4214 p = pp;
4215 *mustfree = TRUE;
4216 }
4217 }
4218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221
4222 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4223 if (!vimruntime && STRCMP(name, "VIM") != 0)
4224 return NULL;
4225
4226 /*
4227 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4228 * Don't do this when default_vimruntime_dir is non-empty.
4229 */
4230 if (vimruntime
4231#ifdef HAVE_PATHDEF
4232 && *default_vimruntime_dir == NUL
4233#endif
4234 )
4235 {
4236 p = mch_getenv((char_u *)"VIM");
4237 if (p != NULL && *p == NUL) /* empty is the same as not set */
4238 p = NULL;
4239 if (p != NULL)
4240 {
4241 p = vim_version_dir(p);
4242 if (p != NULL)
4243 *mustfree = TRUE;
4244 else
4245 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004246
4247#if defined(FEAT_MBYTE) && defined(WIN3264)
4248 if (enc_utf8)
4249 {
4250 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004251 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004252
4253 /* Convert from active codepage to UTF-8. Other conversions
4254 * are not done, because they would fail for non-ASCII
4255 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004256 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004257 if (pp != NULL)
4258 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004259 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004260 vim_free(p);
4261 p = pp;
4262 *mustfree = TRUE;
4263 }
4264 }
4265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 }
4267 }
4268
4269 /*
4270 * When expanding $VIM or $VIMRUNTIME fails, try using:
4271 * - the directory name from 'helpfile' (unless it contains '$')
4272 * - the executable name from argv[0]
4273 */
4274 if (p == NULL)
4275 {
4276 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4277 p = p_hf;
4278#ifdef USE_EXE_NAME
4279 /*
4280 * Use the name of the executable, obtained from argv[0].
4281 */
4282 else
4283 p = exe_name;
4284#endif
4285 if (p != NULL)
4286 {
4287 /* remove the file name */
4288 pend = gettail(p);
4289
4290 /* remove "doc/" from 'helpfile', if present */
4291 if (p == p_hf)
4292 pend = remove_tail(p, pend, (char_u *)"doc");
4293
4294#ifdef USE_EXE_NAME
4295# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004296 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 if (p == exe_name)
4298 {
4299 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004300 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004302 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4303 if (pend1 != pend)
4304 {
4305 pnew = alloc((unsigned)(pend1 - p) + 15);
4306 if (pnew != NULL)
4307 {
4308 STRNCPY(pnew, p, (pend1 - p));
4309 STRCPY(pnew + (pend1 - p), "Resources/vim");
4310 p = pnew;
4311 pend = p + STRLEN(p);
4312 }
4313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 }
4315# endif
4316 /* remove "src/" from exe_name, if present */
4317 if (p == exe_name)
4318 pend = remove_tail(p, pend, (char_u *)"src");
4319#endif
4320
4321 /* for $VIM, remove "runtime/" or "vim54/", if present */
4322 if (!vimruntime)
4323 {
4324 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4325 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4326 }
4327
4328 /* remove trailing path separator */
4329#ifndef MACOS_CLASSIC
4330 /* With MacOS path (with colons) the final colon is required */
Bram Moolenaare21877a2008-02-13 09:58:14 +00004331 /* to avoid confusion between absolute and relative path */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004332 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 --pend;
4334#endif
4335
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004336#ifdef MACOS_X
4337 if (p == exe_name || p == p_hf)
4338#endif
4339 /* check that the result is a directory name */
4340 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341
4342 if (p != NULL && !mch_isdir(p))
4343 {
4344 vim_free(p);
4345 p = NULL;
4346 }
4347 else
4348 {
4349#ifdef USE_EXE_NAME
4350 /* may add "/vim54" or "/runtime" if it exists */
4351 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4352 {
4353 vim_free(p);
4354 p = pend;
4355 }
4356#endif
4357 *mustfree = TRUE;
4358 }
4359 }
4360 }
4361
4362#ifdef HAVE_PATHDEF
4363 /* When there is a pathdef.c file we can use default_vim_dir and
4364 * default_vimruntime_dir */
4365 if (p == NULL)
4366 {
4367 /* Only use default_vimruntime_dir when it is not empty */
4368 if (vimruntime && *default_vimruntime_dir != NUL)
4369 {
4370 p = default_vimruntime_dir;
4371 *mustfree = FALSE;
4372 }
4373 else if (*default_vim_dir != NUL)
4374 {
4375 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4376 *mustfree = TRUE;
4377 else
4378 {
4379 p = default_vim_dir;
4380 *mustfree = FALSE;
4381 }
4382 }
4383 }
4384#endif
4385
4386 /*
4387 * Set the environment variable, so that the new value can be found fast
4388 * next time, and others can also use it (e.g. Perl).
4389 */
4390 if (p != NULL)
4391 {
4392 if (vimruntime)
4393 {
4394 vim_setenv((char_u *)"VIMRUNTIME", p);
4395 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 }
4397 else
4398 {
4399 vim_setenv((char_u *)"VIM", p);
4400 didset_vim = TRUE;
4401 }
4402 }
4403 return p;
4404}
4405
4406/*
4407 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4408 * Return NULL if not, return its name in allocated memory otherwise.
4409 */
4410 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004411vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412{
4413 char_u *p;
4414
4415 if (vimdir == NULL || *vimdir == NUL)
4416 return NULL;
4417 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4418 if (p != NULL && mch_isdir(p))
4419 return p;
4420 vim_free(p);
4421 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4422 if (p != NULL && mch_isdir(p))
4423 return p;
4424 vim_free(p);
4425 return NULL;
4426}
4427
4428/*
4429 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4430 * the length of "name/". Otherwise return "pend".
4431 */
4432 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004433remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434{
4435 int len = (int)STRLEN(name) + 1;
4436 char_u *newend = pend - len;
4437
4438 if (newend >= p
4439 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004440 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 return newend;
4442 return pend;
4443}
4444
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 * Our portable version of setenv.
4447 */
4448 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004449vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450{
4451#ifdef HAVE_SETENV
4452 mch_setenv((char *)name, (char *)val, 1);
4453#else
4454 char_u *envbuf;
4455
4456 /*
4457 * Putenv does not copy the string, it has to remain
4458 * valid. The allocated memory will never be freed.
4459 */
4460 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4461 if (envbuf != NULL)
4462 {
4463 sprintf((char *)envbuf, "%s=%s", name, val);
4464 putenv((char *)envbuf);
4465 }
4466#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004467#ifdef FEAT_GETTEXT
4468 /*
4469 * When setting $VIMRUNTIME adjust the directory to find message
4470 * translations to $VIMRUNTIME/lang.
4471 */
4472 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4473 {
4474 char_u *buf = concat_str(val, (char_u *)"/lang");
4475
4476 if (buf != NULL)
4477 {
4478 bindtextdomain(VIMPACKAGE, (char *)buf);
4479 vim_free(buf);
4480 }
4481 }
4482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483}
4484
4485#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4486/*
4487 * Function given to ExpandGeneric() to obtain an environment variable name.
4488 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004490get_env_name(
4491 expand_T *xp UNUSED,
4492 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493{
4494# if defined(AMIGA) || defined(__MRC__) || defined(__SC__)
4495 /*
4496 * No environ[] on the Amiga and on the Mac (using MPW).
4497 */
4498 return NULL;
4499# else
4500# ifndef __WIN32__
4501 /* Borland C++ 5.2 has this in a header file. */
4502 extern char **environ;
4503# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004504# define ENVNAMELEN 100
4505 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 char_u *str;
4507 int n;
4508
4509 str = (char_u *)environ[idx];
4510 if (str == NULL)
4511 return NULL;
4512
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004513 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 {
4515 if (str[n] == '=' || str[n] == NUL)
4516 break;
4517 name[n] = str[n];
4518 }
4519 name[n] = NUL;
4520 return name;
4521# endif
4522}
Bram Moolenaar24305862012-08-15 14:05:05 +02004523
4524/*
4525 * Find all user names for user completion.
4526 * Done only once and then cached.
4527 */
4528 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004529init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004530{
Bram Moolenaar24305862012-08-15 14:05:05 +02004531 static int lazy_init_done = FALSE;
4532
4533 if (lazy_init_done)
4534 return;
4535
4536 lazy_init_done = TRUE;
4537 ga_init2(&ga_users, sizeof(char_u *), 20);
4538
4539# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4540 {
4541 char_u* user;
4542 struct passwd* pw;
4543
4544 setpwent();
4545 while ((pw = getpwent()) != NULL)
4546 /* pw->pw_name shouldn't be NULL but just in case... */
4547 if (pw->pw_name != NULL)
4548 {
4549 if (ga_grow(&ga_users, 1) == FAIL)
4550 break;
4551 user = vim_strsave((char_u*)pw->pw_name);
4552 if (user == NULL)
4553 break;
4554 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4555 }
4556 endpwent();
4557 }
4558# endif
4559}
4560
4561/*
4562 * Function given to ExpandGeneric() to obtain an user names.
4563 */
4564 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004565get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004566{
4567 init_users();
4568 if (idx < ga_users.ga_len)
4569 return ((char_u **)ga_users.ga_data)[idx];
4570 return NULL;
4571}
4572
4573/*
4574 * Check whether name matches a user name. Return:
4575 * 0 if name does not match any user name.
4576 * 1 if name partially matches the beginning of a user name.
4577 * 2 is name fully matches a user name.
4578 */
Bram Moolenaar9b578142016-01-30 19:39:49 +01004579int match_user(char_u* name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004580{
4581 int i;
4582 int n = (int)STRLEN(name);
4583 int result = 0;
4584
4585 init_users();
4586 for (i = 0; i < ga_users.ga_len; i++)
4587 {
4588 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4589 return 2; /* full match */
4590 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4591 result = 1; /* partial match */
4592 }
4593 return result;
4594}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595#endif
4596
4597/*
4598 * Replace home directory by "~" in each space or comma separated file name in
4599 * 'src'.
4600 * If anything fails (except when out of space) dst equals src.
4601 */
4602 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004603home_replace(
4604 buf_T *buf, /* when not NULL, check for help files */
4605 char_u *src, /* input file name */
4606 char_u *dst, /* where to put the result */
4607 int dstlen, /* maximum length of the result */
4608 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 spaces and commas in the file name. */
4610{
4611 size_t dirlen = 0, envlen = 0;
4612 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004613 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 char_u *p;
4615
4616 if (src == NULL)
4617 {
4618 *dst = NUL;
4619 return;
4620 }
4621
4622 /*
4623 * If the file is a help file, remove the path completely.
4624 */
4625 if (buf != NULL && buf->b_help)
4626 {
4627 STRCPY(dst, gettail(src));
4628 return;
4629 }
4630
4631 /*
4632 * We check both the value of the $HOME environment variable and the
4633 * "real" home directory.
4634 */
4635 if (homedir != NULL)
4636 dirlen = STRLEN(homedir);
4637
4638#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004639 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004641 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4642#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004643 /* Empty is the same as not set. */
4644 if (homedir_env != NULL && *homedir_env == NUL)
4645 homedir_env = NULL;
4646
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004647#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004648 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004649 {
4650 int usedlen = 0;
4651 int flen;
4652 char_u *fbuf = NULL;
4653
4654 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004655 (void)modify_fname((char_u *)":p", &usedlen,
4656 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004657 flen = (int)STRLEN(homedir_env);
4658 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4659 /* Remove the trailing / that is added to a directory. */
4660 homedir_env[flen - 1] = NUL;
4661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662#endif
4663
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 if (homedir_env != NULL)
4665 envlen = STRLEN(homedir_env);
4666
4667 if (!one)
4668 src = skipwhite(src);
4669 while (*src && dstlen > 0)
4670 {
4671 /*
4672 * Here we are at the beginning of a file name.
4673 * First, check to see if the beginning of the file name matches
4674 * $HOME or the "real" home directory. Check that there is a '/'
4675 * after the match (so that if e.g. the file is "/home/pieter/bla",
4676 * and the home directory is "/home/piet", the file does not end up
4677 * as "~er/bla" (which would seem to indicate the file "bla" in user
4678 * er's home directory)).
4679 */
4680 p = homedir;
4681 len = dirlen;
4682 for (;;)
4683 {
4684 if ( len
4685 && fnamencmp(src, p, len) == 0
4686 && (vim_ispathsep(src[len])
4687 || (!one && (src[len] == ',' || src[len] == ' '))
4688 || src[len] == NUL))
4689 {
4690 src += len;
4691 if (--dstlen > 0)
4692 *dst++ = '~';
4693
4694 /*
4695 * If it's just the home directory, add "/".
4696 */
4697 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4698 *dst++ = '/';
4699 break;
4700 }
4701 if (p == homedir_env)
4702 break;
4703 p = homedir_env;
4704 len = envlen;
4705 }
4706
4707 /* if (!one) skip to separator: space or comma */
4708 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4709 *dst++ = *src++;
4710 /* skip separator */
4711 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4712 *dst++ = *src++;
4713 }
4714 /* if (dstlen == 0) out of space, what to do??? */
4715
4716 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004717
4718 if (homedir_env != homedir_env_orig)
4719 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720}
4721
4722/*
4723 * Like home_replace, store the replaced string in allocated memory.
4724 * When something fails, NULL is returned.
4725 */
4726 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004727home_replace_save(
4728 buf_T *buf, /* when not NULL, check for help files */
4729 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730{
4731 char_u *dst;
4732 unsigned len;
4733
4734 len = 3; /* space for "~/" and trailing NUL */
4735 if (src != NULL) /* just in case */
4736 len += (unsigned)STRLEN(src);
4737 dst = alloc(len);
4738 if (dst != NULL)
4739 home_replace(buf, src, dst, len, TRUE);
4740 return dst;
4741}
4742
4743/*
4744 * Compare two file names and return:
4745 * FPC_SAME if they both exist and are the same file.
4746 * FPC_SAMEX if they both don't exist and have the same file name.
4747 * FPC_DIFF if they both exist and are different files.
4748 * FPC_NOTX if they both don't exist.
4749 * FPC_DIFFX if one of them doesn't exist.
4750 * For the first name environment variables are expanded
4751 */
4752 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004753fullpathcmp(
4754 char_u *s1,
4755 char_u *s2,
4756 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757{
4758#ifdef UNIX
4759 char_u exp1[MAXPATHL];
4760 char_u full1[MAXPATHL];
4761 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02004762 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 int r1, r2;
4764
4765 expand_env(s1, exp1, MAXPATHL);
4766 r1 = mch_stat((char *)exp1, &st1);
4767 r2 = mch_stat((char *)s2, &st2);
4768 if (r1 != 0 && r2 != 0)
4769 {
4770 /* if mch_stat() doesn't work, may compare the names */
4771 if (checkname)
4772 {
4773 if (fnamecmp(exp1, s2) == 0)
4774 return FPC_SAMEX;
4775 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4776 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4777 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4778 return FPC_SAMEX;
4779 }
4780 return FPC_NOTX;
4781 }
4782 if (r1 != 0 || r2 != 0)
4783 return FPC_DIFFX;
4784 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4785 return FPC_SAME;
4786 return FPC_DIFF;
4787#else
4788 char_u *exp1; /* expanded s1 */
4789 char_u *full1; /* full path of s1 */
4790 char_u *full2; /* full path of s2 */
4791 int retval = FPC_DIFF;
4792 int r1, r2;
4793
4794 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4795 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4796 {
4797 full1 = exp1 + MAXPATHL;
4798 full2 = full1 + MAXPATHL;
4799
4800 expand_env(s1, exp1, MAXPATHL);
4801 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4802 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4803
4804 /* If vim_FullName() fails, the file probably doesn't exist. */
4805 if (r1 != OK && r2 != OK)
4806 {
4807 if (checkname && fnamecmp(exp1, s2) == 0)
4808 retval = FPC_SAMEX;
4809 else
4810 retval = FPC_NOTX;
4811 }
4812 else if (r1 != OK || r2 != OK)
4813 retval = FPC_DIFFX;
4814 else if (fnamecmp(full1, full2))
4815 retval = FPC_DIFF;
4816 else
4817 retval = FPC_SAME;
4818 vim_free(exp1);
4819 }
4820 return retval;
4821#endif
4822}
4823
4824/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004825 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004826 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004827 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 */
4829 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004830gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831{
4832 char_u *p1, *p2;
4833
4834 if (fname == NULL)
4835 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01004836 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01004838 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 p1 = p2 + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004840 mb_ptr_adv(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 }
4842 return p1;
4843}
4844
Bram Moolenaar31710262010-08-13 13:36:15 +02004845#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004846static char_u *gettail_dir(char_u *fname);
Bram Moolenaar31710262010-08-13 13:36:15 +02004847
4848/*
4849 * Return the end of the directory name, on the first path
4850 * separator:
4851 * "/path/file", "/path/dir/", "/path//dir", "/file"
4852 * ^ ^ ^ ^
4853 */
4854 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004855gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02004856{
4857 char_u *dir_end = fname;
4858 char_u *next_dir_end = fname;
4859 int look_for_sep = TRUE;
4860 char_u *p;
4861
4862 for (p = fname; *p != NUL; )
4863 {
4864 if (vim_ispathsep(*p))
4865 {
4866 if (look_for_sep)
4867 {
4868 next_dir_end = p;
4869 look_for_sep = FALSE;
4870 }
4871 }
4872 else
4873 {
4874 if (!look_for_sep)
4875 dir_end = next_dir_end;
4876 look_for_sep = TRUE;
4877 }
4878 mb_ptr_adv(p);
4879 }
4880 return dir_end;
4881}
4882#endif
4883
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004885 * Get pointer to tail of "fname", including path separators. Putting a NUL
4886 * here leaves the directory name. Takes care of "c:/" and "//".
4887 * Always returns a valid pointer.
4888 */
4889 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004890gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004891{
4892 char_u *p;
4893 char_u *t;
4894
4895 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4896 t = gettail(fname);
4897 while (t > p && after_pathsep(fname, t))
4898 --t;
4899#ifdef VMS
4900 /* path separator is part of the path */
4901 ++t;
4902#endif
4903 return t;
4904}
4905
4906/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 * get the next path component (just after the next path separator).
4908 */
4909 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004910getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911{
4912 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004913 mb_ptr_adv(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 if (*fname)
4915 ++fname;
4916 return fname;
4917}
4918
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919/*
4920 * Get a pointer to one character past the head of a path name.
4921 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4922 * If there is no head, path is returned.
4923 */
4924 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004925get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926{
4927 char_u *retval;
4928
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004929#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 /* may skip "c:" */
4931 if (isalpha(path[0]) && path[1] == ':')
4932 retval = path + 2;
4933 else
4934 retval = path;
4935#else
4936# if defined(AMIGA)
4937 /* may skip "label:" */
4938 retval = vim_strchr(path, ':');
4939 if (retval == NULL)
4940 retval = path;
4941# else /* Unix */
4942 retval = path;
4943# endif
4944#endif
4945
4946 while (vim_ispathsep(*retval))
4947 ++retval;
4948
4949 return retval;
4950}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951
4952/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01004953 * Return TRUE if 'c' is a path separator.
4954 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 */
4956 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004957vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958{
Bram Moolenaare60acc12011-05-10 16:41:25 +02004959#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02004961#else
4962# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004964# else
4965# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
4967 return (c == ':' || c == '[' || c == ']' || c == '/'
4968 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02004969# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004971# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02004973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974}
4975
Bram Moolenaar69c35002013-11-04 02:54:12 +01004976/*
4977 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
4978 */
4979 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004980vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01004981{
4982 return vim_ispathsep(c)
4983#ifdef BACKSLASH_IN_FILENAME
4984 && c != ':'
4985#endif
4986 ;
4987}
4988
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989#if defined(FEAT_SEARCHPATH) || defined(PROTO)
4990/*
4991 * return TRUE if 'c' is a path list separator.
4992 */
4993 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004994vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995{
4996#ifdef UNIX
4997 return (c == ':');
4998#else
Bram Moolenaar25394022007-05-10 19:06:20 +00004999 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000#endif
5001}
5002#endif
5003
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005004#if defined(FEAT_GUI_TABLINE) || defined(FEAT_WINDOWS) \
5005 || defined(FEAT_EVAL) || defined(PROTO)
5006/*
5007 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5008 * It's done in-place.
5009 */
5010 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005011shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005012{
5013 char_u *tail, *s, *d;
5014 int skip = FALSE;
5015
5016 tail = gettail(str);
5017 d = str;
5018 for (s = str; ; ++s)
5019 {
5020 if (s >= tail) /* copy the whole tail */
5021 {
5022 *d++ = *s;
5023 if (*s == NUL)
5024 break;
5025 }
5026 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5027 {
5028 *d++ = *s;
5029 skip = FALSE;
5030 }
5031 else if (!skip)
5032 {
5033 *d++ = *s; /* copy next char */
5034 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5035 skip = TRUE;
5036# ifdef FEAT_MBYTE
5037 if (has_mbyte)
5038 {
5039 int l = mb_ptr2len(s);
5040
5041 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005042 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005043 }
5044# endif
5045 }
5046 }
5047}
5048#endif
5049
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005050/*
5051 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5052 * Also returns TRUE if there is no directory name.
5053 * "fname" must be writable!.
5054 */
5055 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005056dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005057{
5058 char_u *p;
5059 int c;
5060 int retval;
5061
5062 p = gettail_sep(fname);
5063 if (p == fname)
5064 return TRUE;
5065 c = *p;
5066 *p = NUL;
5067 retval = mch_isdir(fname);
5068 *p = c;
5069 return retval;
5070}
5071
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005073 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5074 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 */
5076 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005077vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005079#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005081#else
5082 if (p_fic)
5083 return MB_STRICMP(x, y);
5084 return STRCMP(x, y);
5085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086}
5087
5088 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005089vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005091#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005092 char_u *px = x;
5093 char_u *py = y;
5094 int cx = NUL;
5095 int cy = NUL;
5096
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005097 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005099 cx = PTR2CHAR(px);
5100 cy = PTR2CHAR(py);
5101 if (cx == NUL || cy == NUL
5102 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5103 && !(cx == '/' && cy == '\\')
5104 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005106 len -= MB_PTR2LEN(px);
5107 px += MB_PTR2LEN(px);
5108 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 }
5110 if (len == 0)
5111 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005112 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005113#else
5114 if (p_fic)
5115 return MB_STRNICMP(x, y, len);
5116 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005118}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119
5120/*
5121 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005122 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 */
5124 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005125concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126{
5127 char_u *dest;
5128
5129 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5130 if (dest != NULL)
5131 {
5132 STRCPY(dest, fname1);
5133 if (sep)
5134 add_pathsep(dest);
5135 STRCAT(dest, fname2);
5136 }
5137 return dest;
5138}
5139
Bram Moolenaard6754642005-01-17 22:18:45 +00005140/*
5141 * Concatenate two strings and return the result in allocated memory.
5142 * Returns NULL when out of memory.
5143 */
5144 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005145concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005146{
5147 char_u *dest;
5148 size_t l = STRLEN(str1);
5149
5150 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5151 if (dest != NULL)
5152 {
5153 STRCPY(dest, str1);
5154 STRCPY(dest + l, str2);
5155 }
5156 return dest;
5157}
Bram Moolenaard6754642005-01-17 22:18:45 +00005158
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159/*
5160 * Add a path separator to a file name, unless it already ends in a path
5161 * separator.
5162 */
5163 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005164add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005166 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 STRCAT(p, PATHSEPSTR);
5168}
5169
5170/*
5171 * FullName_save - Make an allocated copy of a full file name.
5172 * Returns NULL when out of memory.
5173 */
5174 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005175FullName_save(
5176 char_u *fname,
5177 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005178 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179{
5180 char_u *buf;
5181 char_u *new_fname = NULL;
5182
5183 if (fname == NULL)
5184 return NULL;
5185
5186 buf = alloc((unsigned)MAXPATHL);
5187 if (buf != NULL)
5188 {
5189 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5190 new_fname = vim_strsave(buf);
5191 else
5192 new_fname = vim_strsave(fname);
5193 vim_free(buf);
5194 }
5195 return new_fname;
5196}
5197
5198#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5199
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005200static char_u *skip_string(char_u *p);
5201static pos_T *ind_find_start_comment(void);
5202static pos_T *ind_find_start_CORS(void);
5203static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204
5205/*
5206 * Find the start of a comment, not knowing if we are in a comment right now.
5207 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005208 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005210 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005211ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005212{
5213 return find_start_comment(curbuf->b_ind_maxcomment);
5214}
5215
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005217find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218{
5219 pos_T *pos;
5220 char_u *line;
5221 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005222 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005224 for (;;)
5225 {
5226 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5227 if (pos == NULL)
5228 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005230 /*
5231 * Check if the comment start we found is inside a string.
5232 * If it is then restrict the search to below this line and try again.
5233 */
5234 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005235 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005236 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005237 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005238 break;
5239 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5240 if (cur_maxcomment <= 0)
5241 {
5242 pos = NULL;
5243 break;
5244 }
5245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 return pos;
5247}
5248
5249/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005250 * Find the start of a comment or raw string, not knowing if we are in a
5251 * comment or raw string right now.
5252 * Search starts at w_cursor.lnum and goes backwards.
5253 * Return NULL when not inside a comment or raw string.
5254 * "CORS" -> Comment Or Raw String
5255 */
5256 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005257ind_find_start_CORS(void) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005258{
Bram Moolenaar089af182015-10-07 11:41:49 +02005259 static pos_T comment_pos_copy;
5260 pos_T *comment_pos;
5261 pos_T *rs_pos;
5262
5263 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5264 if (comment_pos != NULL)
5265 {
5266 /* Need to make a copy of the static pos in findmatchlimit(),
5267 * calling find_start_rawstring() may change it. */
5268 comment_pos_copy = *comment_pos;
5269 comment_pos = &comment_pos_copy;
5270 }
5271 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005272
5273 /* If comment_pos is before rs_pos the raw string is inside the comment.
5274 * If rs_pos is before comment_pos the comment is inside the raw string. */
5275 if (comment_pos == NULL || (rs_pos != NULL && lt(*rs_pos, *comment_pos)))
5276 return rs_pos;
5277 return comment_pos;
5278}
5279
5280/*
5281 * Find the start of a raw string, not knowing if we are in one right now.
5282 * Search starts at w_cursor.lnum and goes backwards.
5283 * Return NULL when not inside a raw string.
5284 */
5285 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005286find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005287{
5288 pos_T *pos;
5289 char_u *line;
5290 char_u *p;
5291 int cur_maxcomment = ind_maxcomment;
5292
5293 for (;;)
5294 {
5295 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5296 if (pos == NULL)
5297 break;
5298
5299 /*
5300 * Check if the raw string start we found is inside a string.
5301 * If it is then restrict the search to below this line and try again.
5302 */
5303 line = ml_get(pos->lnum);
5304 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5305 p = skip_string(p);
5306 if ((colnr_T)(p - line) <= pos->col)
5307 break;
5308 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5309 if (cur_maxcomment <= 0)
5310 {
5311 pos = NULL;
5312 break;
5313 }
5314 }
5315 return pos;
5316}
5317
5318/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 * Skip to the end of a "string" and a 'c' character.
5320 * If there is no string or character, return argument unmodified.
5321 */
5322 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005323skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324{
5325 int i;
5326
5327 /*
5328 * We loop, because strings may be concatenated: "date""time".
5329 */
5330 for ( ; ; ++p)
5331 {
5332 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5333 {
5334 if (!p[1]) /* ' at end of line */
5335 break;
5336 i = 2;
5337 if (p[1] == '\\') /* '\n' or '\000' */
5338 {
5339 ++i;
5340 while (vim_isdigit(p[i - 1])) /* '\000' */
5341 ++i;
5342 }
5343 if (p[i] == '\'') /* check for trailing ' */
5344 {
5345 p += i;
5346 continue;
5347 }
5348 }
5349 else if (p[0] == '"') /* start of string */
5350 {
5351 for (++p; p[0]; ++p)
5352 {
5353 if (p[0] == '\\' && p[1] != NUL)
5354 ++p;
5355 else if (p[0] == '"') /* end of string */
5356 break;
5357 }
5358 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005359 continue; /* continue for another string */
5360 }
5361 else if (p[0] == 'R' && p[1] == '"')
5362 {
5363 /* Raw string: R"[delim](...)[delim]" */
5364 char_u *delim = p + 2;
5365 char_u *paren = vim_strchr(delim, '(');
5366
5367 if (paren != NULL)
5368 {
5369 size_t delim_len = paren - delim;
5370
5371 for (p += 3; *p; ++p)
5372 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5373 && p[delim_len + 1] == '"')
5374 {
5375 p += delim_len + 1;
5376 break;
5377 }
5378 if (p[0] == '"')
5379 continue; /* continue for another string */
5380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 }
5382 break; /* no string found */
5383 }
5384 if (!*p)
5385 --p; /* backup from NUL */
5386 return p;
5387}
5388#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5389
5390#if defined(FEAT_CINDENT) || defined(PROTO)
5391
5392/*
5393 * Do C or expression indenting on the current line.
5394 */
5395 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005396do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397{
5398# ifdef FEAT_EVAL
5399 if (*curbuf->b_p_inde != NUL)
5400 fixthisline(get_expr_indent);
5401 else
5402# endif
5403 fixthisline(get_c_indent);
5404}
5405
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005406/* Find result cache for cpp_baseclass */
5407typedef struct {
5408 int found;
5409 lpos_T lpos;
5410} cpp_baseclass_cache_T;
5411
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412/*
5413 * Functions for C-indenting.
5414 * Most of this originally comes from Eric Fischer.
5415 */
5416/*
5417 * Below "XXX" means that this function may unlock the current line.
5418 */
5419
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005420static char_u *cin_skipcomment(char_u *);
5421static int cin_nocode(char_u *);
5422static pos_T *find_line_comment(void);
5423static int cin_has_js_key(char_u *text);
5424static int cin_islabel_skip(char_u **);
5425static int cin_isdefault(char_u *);
5426static char_u *after_label(char_u *l);
5427static int get_indent_nolabel(linenr_T lnum);
5428static int skip_label(linenr_T, char_u **pp);
5429static int cin_first_id_amount(void);
5430static int cin_get_equal_amount(linenr_T lnum);
5431static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005432static int cin_iscomment(char_u *);
5433static int cin_islinecomment(char_u *);
5434static int cin_isterminated(char_u *, int, int);
5435static int cin_isinit(void);
5436static int cin_isfuncdecl(char_u **, linenr_T, linenr_T);
5437static int cin_isif(char_u *);
5438static int cin_iselse(char_u *);
5439static int cin_isdo(char_u *);
5440static int cin_iswhileofdo(char_u *, linenr_T);
5441static int cin_is_if_for_while_before_offset(char_u *line, int *poffset);
5442static int cin_iswhileofdo_end(int terminated);
5443static int cin_isbreak(char_u *);
5444static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached);
5445static int get_baseclass_amount(int col);
5446static int cin_ends_in(char_u *, char_u *, char_u *);
5447static int cin_starts_with(char_u *s, char *word);
5448static int cin_skip2pos(pos_T *trypos);
5449static pos_T *find_start_brace(void);
5450static pos_T *find_match_paren(int);
5451static pos_T *find_match_char(int c, int ind_maxparen);
5452static int corr_ind_maxparen(pos_T *startpos);
5453static int find_last_paren(char_u *l, int start, int end);
5454static int find_match(int lookfor, linenr_T ourscope);
5455static int cin_is_cpp_namespace(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456
5457/*
5458 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005459 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 */
5461 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005462cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463{
5464 while (*s)
5465 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005466 char_u *prev_s = s;
5467
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005469
5470 /* Perl/shell # comment comment continues until eol. Require a space
5471 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005472 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005473 {
5474 s += STRLEN(s);
5475 break;
5476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 if (*s != '/')
5478 break;
5479 ++s;
5480 if (*s == '/') /* slash-slash comment continues till eol */
5481 {
5482 s += STRLEN(s);
5483 break;
5484 }
5485 if (*s != '*')
5486 break;
5487 for (++s; *s; ++s) /* skip slash-star comment */
5488 if (s[0] == '*' && s[1] == '/')
5489 {
5490 s += 2;
5491 break;
5492 }
5493 }
5494 return s;
5495}
5496
5497/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005498 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 * not considered code.
5500 */
5501 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005502cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503{
5504 return *cin_skipcomment(s) == NUL;
5505}
5506
5507/*
5508 * Check previous lines for a "//" line comment, skipping over blank lines.
5509 */
5510 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005511find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512{
5513 static pos_T pos;
5514 char_u *line;
5515 char_u *p;
5516
5517 pos = curwin->w_cursor;
5518 while (--pos.lnum > 0)
5519 {
5520 line = ml_get(pos.lnum);
5521 p = skipwhite(line);
5522 if (cin_islinecomment(p))
5523 {
5524 pos.col = (int)(p - line);
5525 return &pos;
5526 }
5527 if (*p != NUL)
5528 break;
5529 }
5530 return NULL;
5531}
5532
5533/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005534 * Return TRUE if "text" starts with "key:".
5535 */
5536 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005537cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005538{
5539 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005540 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005541
5542 if (*s == '\'' || *s == '"')
5543 {
5544 /* can be 'key': or "key": */
5545 quote = *s;
5546 ++s;
5547 }
5548 if (!vim_isIDc(*s)) /* need at least one ID character */
5549 return FALSE;
5550
5551 while (vim_isIDc(*s))
5552 ++s;
5553 if (*s == quote)
5554 ++s;
5555
5556 s = cin_skipcomment(s);
5557
5558 /* "::" is not a label, it's C++ */
5559 return (*s == ':' && s[1] != ':');
5560}
5561
5562/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005564 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 */
5566 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005567cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568{
5569 if (!vim_isIDc(**s)) /* need at least one ID character */
5570 return FALSE;
5571
5572 while (vim_isIDc(**s))
5573 (*s)++;
5574
5575 *s = cin_skipcomment(*s);
5576
5577 /* "::" is not a label, it's C++ */
5578 return (**s == ':' && *++*s != ':');
5579}
5580
5581/*
5582 * Recognize a label: "label:".
5583 * Note: curwin->w_cursor must be where we are looking for the label.
5584 */
5585 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005586cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587{
5588 char_u *s;
5589
5590 s = cin_skipcomment(ml_get_curline());
5591
5592 /*
5593 * Exclude "default" from labels, since it should be indented
5594 * like a switch label. Same for C++ scope declarations.
5595 */
5596 if (cin_isdefault(s))
5597 return FALSE;
5598 if (cin_isscopedecl(s))
5599 return FALSE;
5600
5601 if (cin_islabel_skip(&s))
5602 {
5603 /*
5604 * Only accept a label if the previous line is terminated or is a case
5605 * label.
5606 */
5607 pos_T cursor_save;
5608 pos_T *trypos;
5609 char_u *line;
5610
5611 cursor_save = curwin->w_cursor;
5612 while (curwin->w_cursor.lnum > 1)
5613 {
5614 --curwin->w_cursor.lnum;
5615
5616 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005617 * If we're in a comment or raw string now, skip to the start of
5618 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 */
5620 curwin->w_cursor.col = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005621 if ((trypos = ind_find_start_CORS()) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 curwin->w_cursor = *trypos;
5623
5624 line = ml_get_curline();
5625 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5626 continue;
5627 if (*(line = cin_skipcomment(line)) == NUL)
5628 continue;
5629
5630 curwin->w_cursor = cursor_save;
5631 if (cin_isterminated(line, TRUE, FALSE)
5632 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005633 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 || (cin_islabel_skip(&line) && cin_nocode(line)))
5635 return TRUE;
5636 return FALSE;
5637 }
5638 curwin->w_cursor = cursor_save;
5639 return TRUE; /* label at start of file??? */
5640 }
5641 return FALSE;
5642}
5643
5644/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005645 * Recognize structure initialization and enumerations:
5646 * "[typedef] [static|public|protected|private] enum"
5647 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 */
5649 static int
5650cin_isinit(void)
5651{
5652 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005653 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654
5655 s = cin_skipcomment(ml_get_curline());
5656
Bram Moolenaar75342212013-03-07 13:13:52 +01005657 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 s = cin_skipcomment(s + 7);
5659
Bram Moolenaar75342212013-03-07 13:13:52 +01005660 for (;;)
5661 {
5662 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005663
Bram Moolenaar75342212013-03-07 13:13:52 +01005664 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5665 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005666 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005667 if (cin_starts_with(s, skip[i]))
5668 {
5669 s = cin_skipcomment(s + l);
5670 l = 0;
5671 break;
5672 }
5673 }
5674 if (l != 0)
5675 break;
5676 }
5677
5678 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 return TRUE;
5680
5681 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5682 return TRUE;
5683
5684 return FALSE;
5685}
5686
5687/*
5688 * Recognize a switch label: "case .*:" or "default:".
5689 */
5690 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005691cin_iscase(
5692 char_u *s,
5693 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694{
5695 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005696 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 {
5698 for (s += 4; *s; ++s)
5699 {
5700 s = cin_skipcomment(s);
5701 if (*s == ':')
5702 {
5703 if (s[1] == ':') /* skip over "::" for C++ */
5704 ++s;
5705 else
5706 return TRUE;
5707 }
5708 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005709 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5711 return FALSE; /* stop at comment */
5712 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005713 {
5714 /* JS etc. */
5715 if (strict)
5716 return FALSE; /* stop at string */
5717 else
5718 return TRUE;
5719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 }
5721 return FALSE;
5722 }
5723
5724 if (cin_isdefault(s))
5725 return TRUE;
5726 return FALSE;
5727}
5728
5729/*
5730 * Recognize a "default" switch label.
5731 */
5732 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005733cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734{
5735 return (STRNCMP(s, "default", 7) == 0
5736 && *(s = cin_skipcomment(s + 7)) == ':'
5737 && s[1] != ':');
5738}
5739
5740/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005741 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 */
5743 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005744cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745{
5746 int i;
5747
5748 s = cin_skipcomment(s);
5749 if (STRNCMP(s, "public", 6) == 0)
5750 i = 6;
5751 else if (STRNCMP(s, "protected", 9) == 0)
5752 i = 9;
5753 else if (STRNCMP(s, "private", 7) == 0)
5754 i = 7;
5755 else
5756 return FALSE;
5757 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5758}
5759
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005760/* Maximum number of lines to search back for a "namespace" line. */
5761#define FIND_NAMESPACE_LIM 20
5762
5763/*
5764 * Recognize a "namespace" scope declaration.
5765 */
5766 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005767cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005768{
5769 char_u *p;
5770 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005771 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005772
5773 s = cin_skipcomment(s);
5774 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5775 {
5776 p = cin_skipcomment(skipwhite(s + 9));
5777 while (*p != NUL)
5778 {
5779 if (vim_iswhite(*p))
5780 {
5781 has_name = TRUE; /* found end of a name */
5782 p = cin_skipcomment(skipwhite(p));
5783 }
5784 else if (*p == '{')
5785 {
5786 break;
5787 }
5788 else if (vim_iswordc(*p))
5789 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005790 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005791 if (has_name)
5792 return FALSE; /* word character after skipping past name */
5793 ++p;
5794 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005795 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
5796 {
5797 if (!has_name_start || has_name)
5798 return FALSE;
5799 /* C++ 17 nested namespace */
5800 p += 3;
5801 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005802 else
5803 {
5804 return FALSE;
5805 }
5806 }
5807 return TRUE;
5808 }
5809 return FALSE;
5810}
5811
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812/*
Bram Moolenaar7720ba82017-03-08 22:19:26 +01005813 * Recognize a `extern "C"` or `extern "C++"` linkage specifications.
5814 */
5815 static int
5816cin_is_cpp_extern_c(char_u *s)
5817{
5818 char_u *p;
5819 int has_string_literal = FALSE;
5820
5821 s = cin_skipcomment(s);
5822 if (STRNCMP(s, "extern", 6) == 0 && (s[6] == NUL || !vim_iswordc(s[6])))
5823 {
5824 p = cin_skipcomment(skipwhite(s + 6));
5825 while (*p != NUL)
5826 {
5827 if (vim_iswhite(*p))
5828 {
5829 p = cin_skipcomment(skipwhite(p));
5830 }
5831 else if (*p == '{')
5832 {
5833 break;
5834 }
5835 else if (p[0] == '"' && p[1] == 'C' && p[2] == '"')
5836 {
5837 if (has_string_literal)
5838 return FALSE;
5839 has_string_literal = TRUE;
5840 p += 3;
5841 }
5842 else if (p[0] == '"' && p[1] == 'C' && p[2] == '+' && p[3] == '+'
5843 && p[4] == '"')
5844 {
5845 if (has_string_literal)
5846 return FALSE;
5847 has_string_literal = TRUE;
5848 p += 5;
5849 }
5850 else
5851 {
5852 return FALSE;
5853 }
5854 }
5855 return has_string_literal ? TRUE : FALSE;
5856 }
5857 return FALSE;
5858}
5859
5860/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 * Return a pointer to the first non-empty non-comment character after a ':'.
5862 * Return NULL if not found.
5863 * case 234: a = b;
5864 * ^
5865 */
5866 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005867after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868{
5869 for ( ; *l; ++l)
5870 {
5871 if (*l == ':')
5872 {
5873 if (l[1] == ':') /* skip over "::" for C++ */
5874 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005875 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 break;
5877 }
5878 else if (*l == '\'' && l[1] && l[2] == '\'')
5879 l += 2; /* skip over 'x' */
5880 }
5881 if (*l == NUL)
5882 return NULL;
5883 l = cin_skipcomment(l + 1);
5884 if (*l == NUL)
5885 return NULL;
5886 return l;
5887}
5888
5889/*
5890 * Get indent of line "lnum", skipping a label.
5891 * Return 0 if there is nothing after the label.
5892 */
5893 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005894get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895{
5896 char_u *l;
5897 pos_T fp;
5898 colnr_T col;
5899 char_u *p;
5900
5901 l = ml_get(lnum);
5902 p = after_label(l);
5903 if (p == NULL)
5904 return 0;
5905
5906 fp.col = (colnr_T)(p - l);
5907 fp.lnum = lnum;
5908 getvcol(curwin, &fp, &col, NULL, NULL);
5909 return (int)col;
5910}
5911
5912/*
5913 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005914 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 * label: if (asdf && asdfasdf)
5916 * ^
5917 */
5918 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005919skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920{
5921 char_u *l;
5922 int amount;
5923 pos_T cursor_save;
5924
5925 cursor_save = curwin->w_cursor;
5926 curwin->w_cursor.lnum = lnum;
5927 l = ml_get_curline();
5928 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005929 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 {
5931 amount = get_indent_nolabel(lnum);
5932 l = after_label(ml_get_curline());
5933 if (l == NULL) /* just in case */
5934 l = ml_get_curline();
5935 }
5936 else
5937 {
5938 amount = get_indent();
5939 l = ml_get_curline();
5940 }
5941 *pp = l;
5942
5943 curwin->w_cursor = cursor_save;
5944 return amount;
5945}
5946
5947/*
5948 * Return the indent of the first variable name after a type in a declaration.
5949 * int a, indent of "a"
5950 * static struct foo b, indent of "b"
5951 * enum bla c, indent of "c"
5952 * Returns zero when it doesn't look like a declaration.
5953 */
5954 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005955cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956{
5957 char_u *line, *p, *s;
5958 int len;
5959 pos_T fp;
5960 colnr_T col;
5961
5962 line = ml_get_curline();
5963 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005964 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 if (len == 6 && STRNCMP(p, "static", 6) == 0)
5966 {
5967 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005968 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 }
5970 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
5971 p = skipwhite(p + 6);
5972 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
5973 p = skipwhite(p + 4);
5974 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
5975 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
5976 {
5977 s = skipwhite(p + len);
5978 if ((STRNCMP(s, "int", 3) == 0 && vim_iswhite(s[3]))
5979 || (STRNCMP(s, "long", 4) == 0 && vim_iswhite(s[4]))
5980 || (STRNCMP(s, "short", 5) == 0 && vim_iswhite(s[5]))
5981 || (STRNCMP(s, "char", 4) == 0 && vim_iswhite(s[4])))
5982 p = s;
5983 }
5984 for (len = 0; vim_isIDc(p[len]); ++len)
5985 ;
5986 if (len == 0 || !vim_iswhite(p[len]) || cin_nocode(p))
5987 return 0;
5988
5989 p = skipwhite(p + len);
5990 fp.lnum = curwin->w_cursor.lnum;
5991 fp.col = (colnr_T)(p - line);
5992 getvcol(curwin, &fp, &col, NULL, NULL);
5993 return (int)col;
5994}
5995
5996/*
5997 * Return the indent of the first non-blank after an equal sign.
5998 * char *foo = "here";
5999 * Return zero if no (useful) equal sign found.
6000 * Return -1 if the line above "lnum" ends in a backslash.
6001 * foo = "asdf\
6002 * asdf\
6003 * here";
6004 */
6005 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006006cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007{
6008 char_u *line;
6009 char_u *s;
6010 colnr_T col;
6011 pos_T fp;
6012
6013 if (lnum > 1)
6014 {
6015 line = ml_get(lnum - 1);
6016 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6017 return -1;
6018 }
6019
6020 line = s = ml_get(lnum);
6021 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
6022 {
6023 if (cin_iscomment(s)) /* ignore comments */
6024 s = cin_skipcomment(s);
6025 else
6026 ++s;
6027 }
6028 if (*s != '=')
6029 return 0;
6030
6031 s = skipwhite(s + 1);
6032 if (cin_nocode(s))
6033 return 0;
6034
6035 if (*s == '"') /* nice alignment for continued strings */
6036 ++s;
6037
6038 fp.lnum = lnum;
6039 fp.col = (colnr_T)(s - line);
6040 getvcol(curwin, &fp, &col, NULL, NULL);
6041 return (int)col;
6042}
6043
6044/*
6045 * Recognize a preprocessor statement: Any line that starts with '#'.
6046 */
6047 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006048cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006050 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 return TRUE;
6052 return FALSE;
6053}
6054
6055/*
6056 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6057 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6058 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006059 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 */
6061 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006062cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063{
6064 char_u *line = *pp;
6065 linenr_T lnum = *lnump;
6066 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006067 int candidate_amount = *amount;
6068
6069 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6070 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006072 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 {
6074 if (cin_ispreproc(line))
6075 {
6076 retval = TRUE;
6077 *lnump = lnum;
6078 break;
6079 }
6080 if (lnum == 1)
6081 break;
6082 line = ml_get(--lnum);
6083 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6084 break;
6085 }
6086
6087 if (lnum != *lnump)
6088 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006089 if (retval)
6090 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 return retval;
6092}
6093
6094/*
6095 * Recognize the start of a C or C++ comment.
6096 */
6097 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006098cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099{
6100 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6101}
6102
6103/*
6104 * Recognize the start of a "//" comment.
6105 */
6106 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006107cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108{
6109 return (p[0] == '/' && p[1] == '/');
6110}
6111
6112/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006113 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6114 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006116 * If a line begins with an "else", only consider it terminated if no unmatched
6117 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 * Return the character terminating the line (ending char's have precedence if
6119 * both apply in order to determine initializations).
6120 */
6121 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006122cin_isterminated(
6123 char_u *s,
6124 int incl_open, /* include '{' at the end as terminator */
6125 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006127 char_u found_start = 0;
6128 unsigned n_open = 0;
6129 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130
6131 s = cin_skipcomment(s);
6132
6133 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6134 found_start = *s;
6135
Bram Moolenaar496f9512011-05-19 16:35:09 +02006136 if (!found_start)
6137 is_else = cin_iselse(s);
6138
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 while (*s)
6140 {
6141 /* skip over comments, "" strings and 'c'haracters */
6142 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006143 if (*s == '}' && n_open > 0)
6144 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006145 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006146 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 && cin_nocode(s + 1))
6148 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006149 else if (*s == '{')
6150 {
6151 if (incl_open && cin_nocode(s + 1))
6152 return *s;
6153 else
6154 ++n_open;
6155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156
6157 if (*s)
6158 s++;
6159 }
6160 return found_start;
6161}
6162
6163/*
6164 * Recognize the basic picture of a function declaration -- it needs to
6165 * have an open paren somewhere and a close paren at the end of the line and
6166 * no semicolons anywhere.
6167 * When a line ends in a comma we continue looking in the next line.
6168 * "sp" points to a string with the line. When looking at other lines it must
6169 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006170 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006171 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 */
6173 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006174cin_isfuncdecl(
6175 char_u **sp,
6176 linenr_T first_lnum,
6177 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178{
6179 char_u *s;
6180 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006181 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006183 pos_T *trypos;
6184 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185
6186 if (sp == NULL)
6187 s = ml_get(lnum);
6188 else
6189 s = *sp;
6190
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006191 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006192 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006193 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006194 {
6195 lnum = trypos->lnum;
6196 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006197 {
6198 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006199 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006200 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006201
6202 s = ml_get(lnum);
6203 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006204 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006205
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006206 /* Ignore line starting with #. */
6207 if (cin_ispreproc(s))
6208 return FALSE;
6209
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6211 {
6212 if (cin_iscomment(s)) /* ignore comments */
6213 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006214 else if (*s == ':')
6215 {
6216 if (*(s + 1) == ':')
6217 s += 2;
6218 else
6219 /* To avoid a mistake in the following situation:
6220 * A::A(int a, int b)
6221 * : a(0) // <--not a function decl
6222 * , b(0)
6223 * {...
6224 */
6225 return FALSE;
6226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 else
6228 ++s;
6229 }
6230 if (*s != '(')
6231 return FALSE; /* ';', ' or " before any () or no '(' */
6232
6233 while (*s && *s != ';' && *s != '\'' && *s != '"')
6234 {
6235 if (*s == ')' && cin_nocode(s + 1))
6236 {
6237 /* ')' at the end: may have found a match
6238 * Check for he previous line not to end in a backslash:
6239 * #if defined(x) && \
6240 * defined(y)
6241 */
6242 lnum = first_lnum - 1;
6243 s = ml_get(lnum);
6244 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6245 retval = TRUE;
6246 goto done;
6247 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006248 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006250 int comma = (*s == ',');
6251
6252 /* ',' at the end: continue looking in the next line.
6253 * At the end: check for ',' in the next line, for this style:
6254 * func(arg1
6255 * , arg2) */
6256 for (;;)
6257 {
6258 if (lnum >= curbuf->b_ml.ml_line_count)
6259 break;
6260 s = ml_get(++lnum);
6261 if (!cin_ispreproc(s))
6262 break;
6263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 if (lnum >= curbuf->b_ml.ml_line_count)
6265 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006266 /* Require a comma at end of the line or a comma or ')' at the
6267 * start of next line. */
6268 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006269 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006270 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006271 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 }
6273 else if (cin_iscomment(s)) /* ignore comments */
6274 s = cin_skipcomment(s);
6275 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006276 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006278 just_started = FALSE;
6279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 }
6281
6282done:
6283 if (lnum != first_lnum && sp != NULL)
6284 *sp = ml_get(first_lnum);
6285
6286 return retval;
6287}
6288
6289 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006290cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006292 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293}
6294
6295 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006296cin_iselse(
6297 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298{
6299 if (*p == '}') /* accept "} else" */
6300 p = cin_skipcomment(p + 1);
6301 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6302}
6303
6304 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006305cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306{
6307 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6308}
6309
6310/*
6311 * Check if this is a "while" that should have a matching "do".
6312 * We only accept a "while (condition) ;", with only white space between the
6313 * ')' and ';'. The condition may be spread over several lines.
6314 */
6315 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006316cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317{
6318 pos_T cursor_save;
6319 pos_T *trypos;
6320 int retval = FALSE;
6321
6322 p = cin_skipcomment(p);
6323 if (*p == '}') /* accept "} while (cond);" */
6324 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006325 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 {
6327 cursor_save = curwin->w_cursor;
6328 curwin->w_cursor.lnum = lnum;
6329 curwin->w_cursor.col = 0;
6330 p = ml_get_curline();
6331 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6332 {
6333 ++p;
6334 ++curwin->w_cursor.col;
6335 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006336 if ((trypos = findmatchlimit(NULL, 0, 0,
6337 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6339 retval = TRUE;
6340 curwin->w_cursor = cursor_save;
6341 }
6342 return retval;
6343}
6344
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006345/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006346 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006347 * Return 0 if there is none.
6348 * Otherwise return !0 and update "*poffset" to point to the place where the
6349 * string was found.
6350 */
6351 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006352cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006353{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006354 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006355
6356 if (offset-- < 2)
6357 return 0;
6358 while (offset > 2 && vim_iswhite(line[offset]))
6359 --offset;
6360
6361 offset -= 1;
6362 if (!STRNCMP(line + offset, "if", 2))
6363 goto probablyFound;
6364
6365 if (offset >= 1)
6366 {
6367 offset -= 1;
6368 if (!STRNCMP(line + offset, "for", 3))
6369 goto probablyFound;
6370
6371 if (offset >= 2)
6372 {
6373 offset -= 2;
6374 if (!STRNCMP(line + offset, "while", 5))
6375 goto probablyFound;
6376 }
6377 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006378 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006379
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006380probablyFound:
6381 if (!offset || !vim_isIDc(line[offset - 1]))
6382 {
6383 *poffset = offset;
6384 return 1;
6385 }
6386 return 0;
6387}
6388
6389/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006390 * Return TRUE if we are at the end of a do-while.
6391 * do
6392 * nothing;
6393 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006394 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006395 * Adjust the cursor to the line with "while".
6396 */
6397 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006398cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006399{
6400 char_u *line;
6401 char_u *p;
6402 char_u *s;
6403 pos_T *trypos;
6404 int i;
6405
6406 if (terminated != ';') /* there must be a ';' at the end */
6407 return FALSE;
6408
6409 p = line = ml_get_curline();
6410 while (*p != NUL)
6411 {
6412 p = cin_skipcomment(p);
6413 if (*p == ')')
6414 {
6415 s = skipwhite(p + 1);
6416 if (*s == ';' && cin_nocode(s + 1))
6417 {
6418 /* Found ");" at end of the line, now check there is "while"
6419 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006420 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006421 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006422 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006423 if (trypos != NULL)
6424 {
6425 s = cin_skipcomment(ml_get(trypos->lnum));
6426 if (*s == '}') /* accept "} while (cond);" */
6427 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006428 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006429 {
6430 curwin->w_cursor.lnum = trypos->lnum;
6431 return TRUE;
6432 }
6433 }
6434
6435 /* Searching may have made "line" invalid, get it again. */
6436 line = ml_get_curline();
6437 p = line + i;
6438 }
6439 }
6440 if (*p != NUL)
6441 ++p;
6442 }
6443 return FALSE;
6444}
6445
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006447cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448{
6449 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6450}
6451
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006452/*
6453 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 * constructor-initialization. eg:
6455 *
6456 * class MyClass :
6457 * baseClass <-- here
6458 * class MyClass : public baseClass,
6459 * anotherBaseClass <-- here (should probably lineup ??)
6460 * MyClass::MyClass(...) :
6461 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006462 *
6463 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 */
6465 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006466cin_is_cpp_baseclass(
6467 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006469 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 char_u *s;
6471 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006472 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006473 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006475 if (pos->lnum <= lnum)
6476 return cached->found; /* Use the cached result */
6477
6478 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006480 s = skipwhite(line);
6481 if (*s == '#') /* skip #define FOO x ? (x) : x */
6482 return FALSE;
6483 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 if (*s == NUL)
6485 return FALSE;
6486
6487 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6488
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006489 /* Search for a line starting with '#', empty, ending in ';' or containing
6490 * '{' or '}' and start below it. This handles the following situations:
6491 * a = cond ?
6492 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006493 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006494 * func::foo()
6495 * : something
6496 * {}
6497 * Foo::Foo (int one, int two)
6498 * : something(4),
6499 * somethingelse(3)
6500 * {}
6501 */
6502 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006504 line = ml_get(lnum - 1);
6505 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006506 if (*s == '#' || *s == NUL)
6507 break;
6508 while (*s != NUL)
6509 {
6510 s = cin_skipcomment(s);
6511 if (*s == '{' || *s == '}'
6512 || (*s == ';' && cin_nocode(s + 1)))
6513 break;
6514 if (*s != NUL)
6515 ++s;
6516 }
6517 if (*s != NUL)
6518 break;
6519 --lnum;
6520 }
6521
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006522 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006523 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006524 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006525 for (;;)
6526 {
6527 if (*s == NUL)
6528 {
6529 if (lnum == curwin->w_cursor.lnum)
6530 break;
6531 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006532 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006533 s = line;
6534 }
6535 if (s == line)
6536 {
6537 /* don't recognize "case (foo):" as a baseclass */
6538 if (cin_iscase(s, FALSE))
6539 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006540 s = cin_skipcomment(line);
6541 if (*s == NUL)
6542 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006543 }
6544
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006545 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006546 s = skip_string(s) + 1;
6547 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 {
6549 if (s[1] == ':')
6550 {
6551 /* skip double colon. It can't be a constructor
6552 * initialization any more */
6553 lookfor_ctor_init = FALSE;
6554 s = cin_skipcomment(s + 2);
6555 }
6556 else if (lookfor_ctor_init || class_or_struct)
6557 {
6558 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006559 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 cpp_base_class = TRUE;
6561 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006562 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 s = cin_skipcomment(s + 1);
6564 }
6565 else
6566 s = cin_skipcomment(s + 1);
6567 }
6568 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6569 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6570 {
6571 class_or_struct = TRUE;
6572 lookfor_ctor_init = FALSE;
6573
6574 if (*s == 'c')
6575 s = cin_skipcomment(s + 5);
6576 else
6577 s = cin_skipcomment(s + 6);
6578 }
6579 else
6580 {
6581 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6582 {
6583 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6584 }
6585 else if (s[0] == ')')
6586 {
6587 /* Constructor-initialization is assumed if we come across
6588 * something like "):" */
6589 class_or_struct = FALSE;
6590 lookfor_ctor_init = TRUE;
6591 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006592 else if (s[0] == '?')
6593 {
6594 /* Avoid seeing '() :' after '?' as constructor init. */
6595 return FALSE;
6596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 else if (!vim_isIDc(s[0]))
6598 {
6599 /* if it is not an identifier, we are wrong */
6600 class_or_struct = FALSE;
6601 lookfor_ctor_init = FALSE;
6602 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006603 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 {
6605 /* it can't be a constructor-initialization any more */
6606 lookfor_ctor_init = FALSE;
6607
6608 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006609 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006610 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 }
6612
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006613 /* When the line ends in a comma don't align with it. */
6614 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006615 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006616
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 s = cin_skipcomment(s + 1);
6618 }
6619 }
6620
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006621 cached->found = cpp_base_class;
6622 if (cpp_base_class)
6623 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 return cpp_base_class;
6625}
6626
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006627 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006628get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006629{
6630 int amount;
6631 colnr_T vcol;
6632 pos_T *trypos;
6633
6634 if (col == 0)
6635 {
6636 amount = get_indent();
6637 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006638 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006639 amount = get_indent_lnum(trypos->lnum); /* XXX */
6640 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006641 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006642 }
6643 else
6644 {
6645 curwin->w_cursor.col = col;
6646 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6647 amount = (int)vcol;
6648 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006649 if (amount < curbuf->b_ind_cpp_baseclass)
6650 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006651 return amount;
6652}
6653
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654/*
6655 * Return TRUE if string "s" ends with the string "find", possibly followed by
6656 * white space and comments. Skip strings and comments.
6657 * Ignore "ignore" after "find" if it's not NULL.
6658 */
6659 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006660cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661{
6662 char_u *p = s;
6663 char_u *r;
6664 int len = (int)STRLEN(find);
6665
6666 while (*p != NUL)
6667 {
6668 p = cin_skipcomment(p);
6669 if (STRNCMP(p, find, len) == 0)
6670 {
6671 r = skipwhite(p + len);
6672 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6673 r = skipwhite(r + STRLEN(ignore));
6674 if (cin_nocode(r))
6675 return TRUE;
6676 }
6677 if (*p != NUL)
6678 ++p;
6679 }
6680 return FALSE;
6681}
6682
6683/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006684 * Return TRUE when "s" starts with "word" and then a non-ID character.
6685 */
6686 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006687cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006688{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006689 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006690
6691 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6692}
6693
6694/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 * Skip strings, chars and comments until at or past "trypos".
6696 * Return the column found.
6697 */
6698 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006699cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700{
6701 char_u *line;
6702 char_u *p;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006703 char_u *new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704
6705 p = line = ml_get(trypos->lnum);
6706 while (*p && (colnr_T)(p - line) < trypos->col)
6707 {
6708 if (cin_iscomment(p))
6709 p = cin_skipcomment(p);
6710 else
6711 {
Bram Moolenaar7720ba82017-03-08 22:19:26 +01006712 new_p = skip_string(p);
6713 if (new_p == p)
6714 ++p;
6715 else
6716 p = new_p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 }
6718 }
6719 return (int)(p - line);
6720}
6721
6722/*
6723 * Find the '{' at the start of the block we are in.
6724 * Return NULL if no match found.
6725 * Ignore a '{' that is in a comment, makes indenting the next three lines
6726 * work. */
6727/* foo() */
6728/* { */
6729/* } */
6730
6731 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006732find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733{
6734 pos_T cursor_save;
6735 pos_T *trypos;
6736 pos_T *pos;
6737 static pos_T pos_copy;
6738
6739 cursor_save = curwin->w_cursor;
6740 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6741 {
6742 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6743 trypos = &pos_copy;
6744 curwin->w_cursor = *trypos;
6745 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006746 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006748 && (pos = ind_find_start_CORS()) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 break;
6750 if (pos != NULL)
6751 curwin->w_cursor.lnum = pos->lnum;
6752 }
6753 curwin->w_cursor = cursor_save;
6754 return trypos;
6755}
6756
6757/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006758 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006759 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 */
6761 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006762find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006764 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006765}
6766
6767 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006768find_match_char (int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006769{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 pos_T cursor_save;
6771 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006772 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006773 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774
6775 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006776 ind_maxp_wk = ind_maxparen;
6777retry:
6778 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 {
6780 /* check if the ( is in a // comment */
6781 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01006782 {
6783 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
6784 if (ind_maxp_wk > 0)
6785 {
6786 curwin->w_cursor = *trypos;
6787 curwin->w_cursor.col = 0; /* XXX */
6788 goto retry;
6789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 else
6793 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01006794 pos_T *trypos_wk;
6795
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6797 trypos = &pos_copy;
6798 curwin->w_cursor = *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006799 if ((trypos_wk = ind_find_start_CORS()) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01006800 {
6801 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
6802 - trypos_wk->lnum);
6803 if (ind_maxp_wk > 0)
6804 {
6805 curwin->w_cursor = *trypos_wk;
6806 goto retry;
6807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 }
6811 }
6812 curwin->w_cursor = cursor_save;
6813 return trypos;
6814}
6815
6816/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006817 * Find the matching '(', ignoring it if it is in a comment or before an
6818 * unmatched {.
6819 * Return NULL if no match found.
6820 */
6821 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006822find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02006823{
6824 pos_T *trypos = find_match_paren(ind_maxparen);
6825
6826 if (trypos != NULL)
6827 {
6828 pos_T *tryposBrace = find_start_brace();
6829
6830 /* If both an unmatched '(' and '{' is found. Ignore the '('
6831 * position if the '{' is further down. */
6832 if (tryposBrace != NULL
6833 && (trypos->lnum != tryposBrace->lnum
6834 ? trypos->lnum < tryposBrace->lnum
6835 : trypos->col < tryposBrace->col))
6836 trypos = NULL;
6837 }
6838 return trypos;
6839}
6840
6841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 * Return ind_maxparen corrected for the difference in line number between the
6843 * cursor position and "startpos". This makes sure that searching for a
6844 * matching paren above the cursor line doesn't find a match because of
6845 * looking a few lines further.
6846 */
6847 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006848corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849{
6850 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6851
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006852 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
6853 return curbuf->b_ind_maxparen - (int)n;
6854 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855}
6856
6857/*
6858 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006859 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 */
6861 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006862find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863{
6864 int i;
6865 int retval = FALSE;
6866 int open_count = 0;
6867
6868 curwin->w_cursor.col = 0; /* default is start of line */
6869
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006870 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 {
6872 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6873 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6874 if (l[i] == start)
6875 ++open_count;
6876 else if (l[i] == end)
6877 {
6878 if (open_count > 0)
6879 --open_count;
6880 else
6881 {
6882 curwin->w_cursor.col = i;
6883 retval = TRUE;
6884 }
6885 }
6886 }
6887 return retval;
6888}
6889
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006890/*
6891 * Parse 'cinoptions' and set the values in "curbuf".
6892 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
6893 */
6894 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006895parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006896{
6897 char_u *p;
6898 char_u *l;
6899 char_u *digits;
6900 int n;
6901 int divider;
6902 int fraction = 0;
6903 int sw = (int)get_sw_value(buf);
6904
6905 /*
6906 * Set the default values.
6907 */
6908 /* Spaces from a block's opening brace the prevailing indent for that
6909 * block should be. */
6910 buf->b_ind_level = sw;
6911
6912 /* Spaces from the edge of the line an open brace that's at the end of a
6913 * line is imagined to be. */
6914 buf->b_ind_open_imag = 0;
6915
6916 /* Spaces from the prevailing indent for a line that is not preceded by
6917 * an opening brace. */
6918 buf->b_ind_no_brace = 0;
6919
6920 /* Column where the first { of a function should be located }. */
6921 buf->b_ind_first_open = 0;
6922
6923 /* Spaces from the prevailing indent a leftmost open brace should be
6924 * located. */
6925 buf->b_ind_open_extra = 0;
6926
6927 /* Spaces from the matching open brace (real location for one at the left
6928 * edge; imaginary location from one that ends a line) the matching close
6929 * brace should be located. */
6930 buf->b_ind_close_extra = 0;
6931
6932 /* Spaces from the edge of the line an open brace sitting in the leftmost
6933 * column is imagined to be. */
6934 buf->b_ind_open_left_imag = 0;
6935
6936 /* Spaces jump labels should be shifted to the left if N is non-negative,
6937 * otherwise the jump label will be put to column 1. */
6938 buf->b_ind_jump_label = -1;
6939
6940 /* Spaces from the switch() indent a "case xx" label should be located. */
6941 buf->b_ind_case = sw;
6942
6943 /* Spaces from the "case xx:" code after a switch() should be located. */
6944 buf->b_ind_case_code = sw;
6945
6946 /* Lineup break at end of case in switch() with case label. */
6947 buf->b_ind_case_break = 0;
6948
6949 /* Spaces from the class declaration indent a scope declaration label
6950 * should be located. */
6951 buf->b_ind_scopedecl = sw;
6952
6953 /* Spaces from the scope declaration label code should be located. */
6954 buf->b_ind_scopedecl_code = sw;
6955
6956 /* Amount K&R-style parameters should be indented. */
6957 buf->b_ind_param = sw;
6958
6959 /* Amount a function type spec should be indented. */
6960 buf->b_ind_func_type = sw;
6961
6962 /* Amount a cpp base class declaration or constructor initialization
6963 * should be indented. */
6964 buf->b_ind_cpp_baseclass = sw;
6965
6966 /* additional spaces beyond the prevailing indent a continuation line
6967 * should be located. */
6968 buf->b_ind_continuation = sw;
6969
6970 /* Spaces from the indent of the line with an unclosed parentheses. */
6971 buf->b_ind_unclosed = sw * 2;
6972
6973 /* Spaces from the indent of the line with an unclosed parentheses, which
6974 * itself is also unclosed. */
6975 buf->b_ind_unclosed2 = sw;
6976
6977 /* Suppress ignoring spaces from the indent of a line starting with an
6978 * unclosed parentheses. */
6979 buf->b_ind_unclosed_noignore = 0;
6980
6981 /* If the opening paren is the last nonwhite character on the line, and
6982 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
6983 * context (for very long lines). */
6984 buf->b_ind_unclosed_wrapped = 0;
6985
6986 /* Suppress ignoring white space when lining up with the character after
6987 * an unclosed parentheses. */
6988 buf->b_ind_unclosed_whiteok = 0;
6989
6990 /* Indent a closing parentheses under the line start of the matching
6991 * opening parentheses. */
6992 buf->b_ind_matching_paren = 0;
6993
6994 /* Indent a closing parentheses under the previous line. */
6995 buf->b_ind_paren_prev = 0;
6996
6997 /* Extra indent for comments. */
6998 buf->b_ind_comment = 0;
6999
7000 /* Spaces from the comment opener when there is nothing after it. */
7001 buf->b_ind_in_comment = 3;
7002
7003 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
7004 * after the comment opener. */
7005 buf->b_ind_in_comment2 = 0;
7006
7007 /* Max lines to search for an open paren. */
7008 buf->b_ind_maxparen = 20;
7009
7010 /* Max lines to search for an open comment. */
7011 buf->b_ind_maxcomment = 70;
7012
7013 /* Handle braces for java code. */
7014 buf->b_ind_java = 0;
7015
7016 /* Not to confuse JS object properties with labels. */
7017 buf->b_ind_js = 0;
7018
7019 /* Handle blocked cases correctly. */
7020 buf->b_ind_keep_case_label = 0;
7021
7022 /* Handle C++ namespace. */
7023 buf->b_ind_cpp_namespace = 0;
7024
7025 /* Handle continuation lines containing conditions of if(), for() and
7026 * while(). */
7027 buf->b_ind_if_for_while = 0;
7028
Bram Moolenaar6b643942017-03-05 19:44:06 +01007029 /* indentation for # comments */
7030 buf->b_ind_hash_comment = 0;
7031
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007032 /* Handle C++ extern "C" or "C++" */
7033 buf->b_ind_cpp_extern_c = 0;
7034
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007035 for (p = buf->b_p_cino; *p; )
7036 {
7037 l = p++;
7038 if (*p == '-')
7039 ++p;
7040 digits = p; /* remember where the digits start */
7041 n = getdigits(&p);
7042 divider = 0;
7043 if (*p == '.') /* ".5s" means a fraction */
7044 {
7045 fraction = atol((char *)++p);
7046 while (VIM_ISDIGIT(*p))
7047 {
7048 ++p;
7049 if (divider)
7050 divider *= 10;
7051 else
7052 divider = 10;
7053 }
7054 }
7055 if (*p == 's') /* "2s" means two times 'shiftwidth' */
7056 {
7057 if (p == digits)
7058 n = sw; /* just "s" is one 'shiftwidth' */
7059 else
7060 {
7061 n *= sw;
7062 if (divider)
7063 n += (sw * fraction + divider / 2) / divider;
7064 }
7065 ++p;
7066 }
7067 if (l[1] == '-')
7068 n = -n;
7069
7070 /* When adding an entry here, also update the default 'cinoptions' in
7071 * doc/indent.txt, and add explanation for it! */
7072 switch (*l)
7073 {
7074 case '>': buf->b_ind_level = n; break;
7075 case 'e': buf->b_ind_open_imag = n; break;
7076 case 'n': buf->b_ind_no_brace = n; break;
7077 case 'f': buf->b_ind_first_open = n; break;
7078 case '{': buf->b_ind_open_extra = n; break;
7079 case '}': buf->b_ind_close_extra = n; break;
7080 case '^': buf->b_ind_open_left_imag = n; break;
7081 case 'L': buf->b_ind_jump_label = n; break;
7082 case ':': buf->b_ind_case = n; break;
7083 case '=': buf->b_ind_case_code = n; break;
7084 case 'b': buf->b_ind_case_break = n; break;
7085 case 'p': buf->b_ind_param = n; break;
7086 case 't': buf->b_ind_func_type = n; break;
7087 case '/': buf->b_ind_comment = n; break;
7088 case 'c': buf->b_ind_in_comment = n; break;
7089 case 'C': buf->b_ind_in_comment2 = n; break;
7090 case 'i': buf->b_ind_cpp_baseclass = n; break;
7091 case '+': buf->b_ind_continuation = n; break;
7092 case '(': buf->b_ind_unclosed = n; break;
7093 case 'u': buf->b_ind_unclosed2 = n; break;
7094 case 'U': buf->b_ind_unclosed_noignore = n; break;
7095 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7096 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7097 case 'm': buf->b_ind_matching_paren = n; break;
7098 case 'M': buf->b_ind_paren_prev = n; break;
7099 case ')': buf->b_ind_maxparen = n; break;
7100 case '*': buf->b_ind_maxcomment = n; break;
7101 case 'g': buf->b_ind_scopedecl = n; break;
7102 case 'h': buf->b_ind_scopedecl_code = n; break;
7103 case 'j': buf->b_ind_java = n; break;
7104 case 'J': buf->b_ind_js = n; break;
7105 case 'l': buf->b_ind_keep_case_label = n; break;
7106 case '#': buf->b_ind_hash_comment = n; break;
7107 case 'N': buf->b_ind_cpp_namespace = n; break;
7108 case 'k': buf->b_ind_if_for_while = n; break;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007109 case 'E': buf->b_ind_cpp_extern_c = n; break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007110 }
7111 if (*p == ',')
7112 ++p;
7113 }
7114}
7115
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007116/*
7117 * Return the desired indent for C code.
7118 * Return -1 if the indent should be left alone (inside a raw string).
7119 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007121get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 pos_T cur_curpos;
7124 int amount;
7125 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007126 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 colnr_T col;
7128 char_u *theline;
7129 char_u *linecopy;
7130 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007131 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007133 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 pos_T our_paren_pos;
7135 char_u *start;
7136 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007137#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138#define BRACE_AT_START 2 /* '{' is at start of line */
7139#define BRACE_AT_END 3 /* '{' is at end of line */
7140 linenr_T ourscope;
7141 char_u *l;
7142 char_u *look;
7143 char_u terminated;
7144 int lookfor;
7145#define LOOKFOR_INITIAL 0
7146#define LOOKFOR_IF 1
7147#define LOOKFOR_DO 2
7148#define LOOKFOR_CASE 3
7149#define LOOKFOR_ANY 4
7150#define LOOKFOR_TERM 5
7151#define LOOKFOR_UNTERM 6
7152#define LOOKFOR_SCOPEDECL 7
7153#define LOOKFOR_NOBREAK 8
7154#define LOOKFOR_CPP_BASECLASS 9
7155#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007156#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007157#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158
7159 int whilelevel;
7160 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 int n;
7162 int iscase;
7163 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007164 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007166 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007167 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007168 int js_cur_has_key = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007169 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007171 /* make a copy, value is changed below */
7172 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173
7174 /* remember where the cursor was when we started */
7175 cur_curpos = curwin->w_cursor;
7176
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007177 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007178 if (cur_curpos.lnum == 1)
7179 return 0;
7180
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 /* Get a copy of the current contents of the line.
7182 * This is required, because only the most recent line obtained with
7183 * ml_get is valid! */
7184 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7185 if (linecopy == NULL)
7186 return 0;
7187
7188 /*
7189 * In insert mode and the cursor is on a ')' truncate the line at the
7190 * cursor position. We don't want to line up with the matching '(' when
7191 * inserting new stuff.
7192 * For unknown reasons the cursor might be past the end of the line, thus
7193 * check for that.
7194 */
7195 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007196 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 && linecopy[curwin->w_cursor.col] == ')')
7198 linecopy[curwin->w_cursor.col] = NUL;
7199
7200 theline = skipwhite(linecopy);
7201
7202 /* move the cursor to the start of the line */
7203
7204 curwin->w_cursor.col = 0;
7205
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007206 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007207
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007209 * If we are inside a raw string don't change the indent.
7210 * Ignore a raw string inside a comment.
7211 */
7212 comment_pos = ind_find_start_comment();
7213 if (comment_pos != NULL)
7214 {
7215 /* findmatchlimit() static pos is overwritten, make a copy */
7216 tryposCopy = *comment_pos;
7217 comment_pos = &tryposCopy;
7218 }
7219 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
7220 if (trypos != NULL && (comment_pos == NULL || lt(*trypos, *comment_pos)))
7221 {
7222 amount = -1;
7223 goto laterend;
7224 }
7225
7226 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 * #defines and so on always go at the left when included in 'cinkeys'.
7228 */
7229 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007230 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007231 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007232 goto theend;
7233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234
7235 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007236 * Is it a non-case label? Then that goes at the left margin too unless:
7237 * - JS flag is set.
7238 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007240 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007241 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 {
7243 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007244 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 }
7246
7247 /*
7248 * If we're inside a "//" comment and there is a "//" comment in a
7249 * previous line, lineup with that one.
7250 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007251 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 && (trypos = find_line_comment()) != NULL) /* XXX */
7253 {
7254 /* find how indented the line beginning the comment is */
7255 getvcol(curwin, trypos, &col, NULL, NULL);
7256 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007257 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 }
7259
7260 /*
7261 * If we're inside a comment and not looking at the start of the
7262 * comment, try using the 'comments' option.
7263 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007264 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 {
7266 int lead_start_len = 2;
7267 int lead_middle_len = 1;
7268 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7269 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7270 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7271 char_u *p;
7272 int start_align = 0;
7273 int start_off = 0;
7274 int done = FALSE;
7275
7276 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007277 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007279 *lead_start = NUL;
7280 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281
7282 p = curbuf->b_p_com;
7283 while (*p != NUL)
7284 {
7285 int align = 0;
7286 int off = 0;
7287 int what = 0;
7288
7289 while (*p != NUL && *p != ':')
7290 {
7291 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7292 what = *p++;
7293 else if (*p == COM_LEFT || *p == COM_RIGHT)
7294 align = *p++;
7295 else if (VIM_ISDIGIT(*p) || *p == '-')
7296 off = getdigits(&p);
7297 else
7298 ++p;
7299 }
7300
7301 if (*p == ':')
7302 ++p;
7303 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7304 if (what == COM_START)
7305 {
7306 STRCPY(lead_start, lead_end);
7307 lead_start_len = (int)STRLEN(lead_start);
7308 start_off = off;
7309 start_align = align;
7310 }
7311 else if (what == COM_MIDDLE)
7312 {
7313 STRCPY(lead_middle, lead_end);
7314 lead_middle_len = (int)STRLEN(lead_middle);
7315 }
7316 else if (what == COM_END)
7317 {
7318 /* If our line starts with the middle comment string, line it
7319 * up with the comment opener per the 'comments' option. */
7320 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7321 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7322 {
7323 done = TRUE;
7324 if (curwin->w_cursor.lnum > 1)
7325 {
7326 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007327 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 * the middle comment string matches in the previous
7329 * line, use the indent of that line. XXX */
7330 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7331 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7332 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7333 else if (STRNCMP(look, lead_middle,
7334 lead_middle_len) == 0)
7335 {
7336 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7337 break;
7338 }
7339 /* If the start comment string doesn't match with the
7340 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007341 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 lead_start, lead_start_len) != 0)
7343 continue;
7344 }
7345 if (start_off != 0)
7346 amount += start_off;
7347 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007348 amount += vim_strsize(lead_start)
7349 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 break;
7351 }
7352
7353 /* If our line starts with the end comment string, line it up
7354 * with the middle comment */
7355 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7356 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7357 {
7358 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7359 /* XXX */
7360 if (off != 0)
7361 amount += off;
7362 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007363 amount += vim_strsize(lead_start)
7364 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 done = TRUE;
7366 break;
7367 }
7368 }
7369 }
7370
7371 /* If our line starts with an asterisk, line up with the
7372 * asterisk in the comment opener; otherwise, line up
7373 * with the first character of the comment text.
7374 */
7375 if (done)
7376 ;
7377 else if (theline[0] == '*')
7378 amount += 1;
7379 else
7380 {
7381 /*
7382 * If we are more than one line away from the comment opener, take
7383 * the indent of the previous non-empty line. If 'cino' has "CO"
7384 * and we are just below the comment opener and there are any
7385 * white characters after it line up with the text after it;
7386 * otherwise, add the amount specified by "c" in 'cino'
7387 */
7388 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007389 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 {
7391 if (linewhite(lnum)) /* skip blank lines */
7392 continue;
7393 amount = get_indent_lnum(lnum); /* XXX */
7394 break;
7395 }
7396 if (amount == -1) /* use the comment opener */
7397 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007398 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007400 start = ml_get(comment_pos->lnum);
7401 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007403 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007405 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007407 if (curbuf->b_ind_in_comment2 || *look == NUL)
7408 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 }
7410 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007411 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 }
7413
7414 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007415 * Are we looking at a ']' that has a match?
7416 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007417 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007418 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7419 {
7420 /* align with the line containing the '['. */
7421 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007422 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007423 }
7424
7425 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 * Are we inside parentheses or braces?
7427 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007428 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007429 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007430 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 || trypos != NULL)
7432 {
7433 if (trypos != NULL && tryposBrace != NULL)
7434 {
7435 /* Both an unmatched '(' and '{' is found. Use the one which is
7436 * closer to the current cursor position, set the other to NULL. */
7437 if (trypos->lnum != tryposBrace->lnum
7438 ? trypos->lnum < tryposBrace->lnum
7439 : trypos->col < tryposBrace->col)
7440 trypos = NULL;
7441 else
7442 tryposBrace = NULL;
7443 }
7444
7445 if (trypos != NULL)
7446 {
7447 /*
7448 * If the matching paren is more than one line away, use the indent of
7449 * a previous non-empty line that matches the same paren.
7450 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007451 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007453 /* Line up with the start of the matching paren line. */
7454 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7455 }
7456 else
7457 {
7458 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007459 our_paren_pos = *trypos;
7460 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007462 l = skipwhite(ml_get(lnum));
7463 if (cin_nocode(l)) /* skip comment lines */
7464 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007465 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007466 continue; /* ignore #define, #if, etc. */
7467 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007469 /* Skip a comment or raw string. XXX */
7470 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007471 {
7472 lnum = trypos->lnum + 1;
7473 continue;
7474 }
7475
7476 /* XXX */
7477 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007478 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007479 && trypos->lnum == our_paren_pos.lnum
7480 && trypos->col == our_paren_pos.col)
7481 {
7482 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007484 if (theline[0] == ')')
7485 {
7486 if (our_paren_pos.lnum != lnum
7487 && cur_amount > amount)
7488 cur_amount = amount;
7489 amount = -1;
7490 }
7491 break;
7492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 }
7494 }
7495
7496 /*
7497 * Line up with line where the matching paren is. XXX
7498 * If the line starts with a '(' or the indent for unclosed
7499 * parentheses is zero, line up with the unclosed parentheses.
7500 */
7501 if (amount == -1)
7502 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007503 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007504 int is_if_for_while = 0;
7505
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007506 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007507 {
7508 /* Look for the outermost opening parenthesis on this line
7509 * and check whether it belongs to an "if", "for" or "while". */
7510
7511 pos_T cursor_save = curwin->w_cursor;
7512 pos_T outermost;
7513 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007514
7515 trypos = &our_paren_pos;
7516 do {
7517 outermost = *trypos;
7518 curwin->w_cursor.lnum = outermost.lnum;
7519 curwin->w_cursor.col = outermost.col;
7520
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007521 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007522 } while (trypos && trypos->lnum == outermost.lnum);
7523
7524 curwin->w_cursor = cursor_save;
7525
7526 line = ml_get(outermost.lnum);
7527
7528 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007529 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007530 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007531
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007532 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007533 look = skipwhite(look);
7534 if (*look == '(')
7535 {
7536 linenr_T save_lnum = curwin->w_cursor.lnum;
7537 char_u *line;
7538 int look_col;
7539
7540 /* Ignore a '(' in front of the line that has a match before
7541 * our matching '('. */
7542 curwin->w_cursor.lnum = our_paren_pos.lnum;
7543 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007544 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007545 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007546 if ((trypos = findmatchlimit(NULL, ')', 0,
7547 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007548 != NULL
7549 && trypos->lnum == our_paren_pos.lnum
7550 && trypos->col < our_paren_pos.col)
7551 ignore_paren_col = trypos->col + 1;
7552
7553 curwin->w_cursor.lnum = save_lnum;
7554 look = ml_get(our_paren_pos.lnum) + look_col;
7555 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007556 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7557 && is_if_for_while == 0)
7558 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007559 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 {
7561 /*
7562 * If we're looking at a close paren, line up right there;
7563 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007564 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 * the last nonwhite character of the line, use either the
7566 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007567 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 * lines).
7569 */
7570 if (theline[0] != ')')
7571 {
7572 cur_amount = MAXCOL;
7573 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007574 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 && cin_ends_in(l, (char_u *)"(", NULL))
7576 {
7577 /* look for opening unmatched paren, indent one level
7578 * for each additional level */
7579 n = 1;
7580 for (col = 0; col < our_paren_pos.col; ++col)
7581 {
7582 switch (l[col])
7583 {
7584 case '(':
7585 case '{': ++n;
7586 break;
7587
7588 case ')':
7589 case '}': if (n > 1)
7590 --n;
7591 break;
7592 }
7593 }
7594
7595 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007596 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007598 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 our_paren_pos.col++;
7600 else
7601 {
7602 col = our_paren_pos.col + 1;
7603 while (vim_iswhite(l[col]))
7604 col++;
7605 if (l[col] != NUL) /* In case of trailing space */
7606 our_paren_pos.col = col;
7607 else
7608 our_paren_pos.col++;
7609 }
7610 }
7611
7612 /*
7613 * Find how indented the paren is, or the character after it
7614 * if we did the above "if".
7615 */
7616 if (our_paren_pos.col > 0)
7617 {
7618 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7619 if (cur_amount > (int)col)
7620 cur_amount = col;
7621 }
7622 }
7623
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007624 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 {
7626 /* Line up with the start of the matching paren line. */
7627 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007628 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7629 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007630 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 {
7632 if (cur_amount != MAXCOL)
7633 amount = cur_amount;
7634 }
7635 else
7636 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007637 /* Add b_ind_unclosed2 for each '(' before our matching one,
7638 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007640 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 {
7642 --our_paren_pos.col;
7643 switch (*ml_get_pos(&our_paren_pos))
7644 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007645 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 col = our_paren_pos.col;
7647 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007648 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 col = MAXCOL;
7650 break;
7651 }
7652 }
7653
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007654 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 * braces */
7656 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007657 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 else
7659 {
7660 curwin->w_cursor.lnum = our_paren_pos.lnum;
7661 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007662 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7663 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007664 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007666 {
7667 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007668 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007669 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007670 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 }
7673 /*
7674 * For a line starting with ')' use the minimum of the two
7675 * positions, to avoid giving it more indent than the previous
7676 * lines:
7677 * func_long_name( if (x
7678 * arg && yy
7679 * ) ^ not here ) ^ not here
7680 */
7681 if (cur_amount < amount)
7682 amount = cur_amount;
7683 }
7684 }
7685
7686 /* add extra indent for a comment */
7687 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007688 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 else
7691 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007692 /*
7693 * We are inside braces, there is a { before this line at the position
7694 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007695 * Make a copy of tryposBrace, it may point to pos_copy inside
7696 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007697 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007698 tryposCopy = *tryposBrace;
7699 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 ourscope = trypos->lnum;
7702 start = ml_get(ourscope);
7703
7704 /*
7705 * Now figure out how indented the line is in general.
7706 * If the brace was at the start of the line, we use that;
7707 * otherwise, check out the indentation of the line as
7708 * a whole and then add the "imaginary indent" to that.
7709 */
7710 look = skipwhite(start);
7711 if (*look == '{')
7712 {
7713 getvcol(curwin, trypos, &col, NULL, NULL);
7714 amount = col;
7715 if (*start == '{')
7716 start_brace = BRACE_IN_COL0;
7717 else
7718 start_brace = BRACE_AT_START;
7719 }
7720 else
7721 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007722 /* That opening brace might have been on a continuation
7723 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 curwin->w_cursor.lnum = ourscope;
7725
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007726 /* Position the cursor over the rightmost paren, so that
7727 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 lnum = ourscope;
7729 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007730 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7731 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 lnum = trypos->lnum;
7733
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007734 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 * case 1: if (asdf &&
7736 * ldfd) {
7737 * }
7738 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007739 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7740 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007742 else if (curbuf->b_ind_js)
7743 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007745 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746
7747 start_brace = BRACE_AT_END;
7748 }
7749
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007750 /* For Javascript check if the line starts with "key:". */
7751 if (curbuf->b_ind_js)
7752 js_cur_has_key = cin_has_js_key(theline);
7753
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007755 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 * we want to be. otherwise, add the amount of room
7757 * that an indent is supposed to be.
7758 */
7759 if (theline[0] == '}')
7760 {
7761 /*
7762 * they may want closing braces to line up with something
7763 * other than the open brace. indulge them, if so.
7764 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007765 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 }
7767 else
7768 {
7769 /*
7770 * If we're looking at an "else", try to find an "if"
7771 * to match it with.
7772 * If we're looking at a "while", try to find a "do"
7773 * to match it with.
7774 */
7775 lookfor = LOOKFOR_INITIAL;
7776 if (cin_iselse(theline))
7777 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007778 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 lookfor = LOOKFOR_DO;
7780 if (lookfor != LOOKFOR_INITIAL)
7781 {
7782 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007783 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 {
7785 amount = get_indent(); /* XXX */
7786 goto theend;
7787 }
7788 }
7789
7790 /*
7791 * We get here if we are not on an "while-of-do" or "else" (or
7792 * failed to find a matching "if").
7793 * Search backwards for something to line up with.
7794 * First set amount for when we don't find anything.
7795 */
7796
7797 /*
7798 * if the '{' is _really_ at the left margin, use the imaginary
7799 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007800 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 */
7802
7803 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7804 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007805 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007806 lookfor_cpp_namespace = TRUE;
7807 }
7808 else if (start_brace == BRACE_AT_START &&
7809 lookfor_cpp_namespace) /* '{' is at start */
7810 {
7811
7812 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 }
7814 else
7815 {
7816 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007817 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007818 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007819
7820 l = skipwhite(ml_get_curline());
7821 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007822 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaar7720ba82017-03-08 22:19:26 +01007823 else if (cin_is_cpp_extern_c(l))
7824 amount += curbuf->b_ind_cpp_extern_c;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 else
7827 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007828 /* Compensate for adding b_ind_open_extra later. */
7829 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 if (amount < 0)
7831 amount = 0;
7832 }
7833 }
7834
7835 lookfor_break = FALSE;
7836
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007837 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 {
7839 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007840 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 }
7842 else if (cin_isscopedecl(theline)) /* private:, ... */
7843 {
7844 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007845 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 }
7847 else
7848 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007849 if (curbuf->b_ind_case_break && cin_isbreak(theline))
7850 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 lookfor_break = TRUE;
7852
7853 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007854 /* b_ind_level from start of block */
7855 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 }
7857 scope_amount = amount;
7858 whilelevel = 0;
7859
7860 /*
7861 * Search backwards. If we find something we recognize, line up
7862 * with that.
7863 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007864 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 * the usual amount relative to the conditional
7866 * that opens the block.
7867 */
7868 curwin->w_cursor = cur_curpos;
7869 for (;;)
7870 {
7871 curwin->w_cursor.lnum--;
7872 curwin->w_cursor.col = 0;
7873
7874 /*
7875 * If we went all the way back to the start of our scope, line
7876 * up with it.
7877 */
7878 if (curwin->w_cursor.lnum <= ourscope)
7879 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007880 /* We reached end of scope:
7881 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007883 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 * don't add ind_continuation, otherwise it is a variable
7885 * declaration:
7886 * int x,
7887 * here; <-- add ind_continuation
7888 */
7889 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7890 {
7891 if (curwin->w_cursor.lnum == 0
7892 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007893 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007895 /* nothing found (abuse curbuf->b_ind_maxparen as
7896 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 * initialization) */
7898 if (cont_amount > 0)
7899 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007900 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 amount += ind_continuation;
7902 break;
7903 }
7904
7905 l = ml_get_curline();
7906
7907 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007908 * If we're in a comment or raw string now, skip to
7909 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007911 trypos = ind_find_start_CORS();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 if (trypos != NULL)
7913 {
7914 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007915 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 continue;
7917 }
7918
7919 /*
7920 * Skip preprocessor directives and blank lines.
7921 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007922 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
7923 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 continue;
7925
7926 if (cin_nocode(l))
7927 continue;
7928
7929 terminated = cin_isterminated(l, FALSE, TRUE);
7930
7931 /*
7932 * If we are at top level and the line looks like a
7933 * function declaration, we are done
7934 * (it's a variable declaration).
7935 */
7936 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007937 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 {
7939 /* if the line is terminated with another ','
7940 * it is a continued variable initialization.
7941 * don't add extra indent.
7942 * TODO: does not work, if a function
7943 * declaration is split over multiple lines:
7944 * cin_isfuncdecl returns FALSE then.
7945 */
7946 if (terminated == ',')
7947 break;
7948
7949 /* if it es a enum declaration or an assignment,
7950 * we are done.
7951 */
7952 if (terminated != ';' && cin_isinit())
7953 break;
7954
7955 /* nothing useful found */
7956 if (terminated == 0 || terminated == '{')
7957 continue;
7958 }
7959
7960 if (terminated != ';')
7961 {
7962 /* Skip parens and braces. Position the cursor
7963 * over the rightmost paren, so that matching it
7964 * will take us back to the start of the line.
7965 */ /* XXX */
7966 trypos = NULL;
7967 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007968 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007969 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970
7971 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007972 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973
7974 if (trypos != NULL)
7975 {
7976 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007977 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 continue;
7979 }
7980 }
7981
7982 /* it's a variable declaration, add indentation
7983 * like in
7984 * int a,
7985 * b;
7986 */
7987 if (cont_amount > 0)
7988 amount = cont_amount;
7989 else
7990 amount += ind_continuation;
7991 }
7992 else if (lookfor == LOOKFOR_UNTERM)
7993 {
7994 if (cont_amount > 0)
7995 amount = cont_amount;
7996 else
7997 amount += ind_continuation;
7998 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02007999 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008000 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02008001 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008002 && lookfor != LOOKFOR_CPP_BASECLASS
8003 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02008004 {
8005 amount = scope_amount;
8006 if (theline[0] == '{')
8007 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008008 amount += curbuf->b_ind_open_extra;
8009 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008010 }
8011 }
8012
8013 if (lookfor_cpp_namespace)
8014 {
8015 /*
8016 * Looking for C++ namespace, need to look further
8017 * back.
8018 */
8019 if (curwin->w_cursor.lnum == ourscope)
8020 continue;
8021
8022 if (curwin->w_cursor.lnum == 0
8023 || curwin->w_cursor.lnum
8024 < ourscope - FIND_NAMESPACE_LIM)
8025 break;
8026
8027 l = ml_get_curline();
8028
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008029 /* If we're in a comment or raw string now, skip
8030 * to the start of it. */
8031 trypos = ind_find_start_CORS();
Bram Moolenaare79d1532011-10-04 18:03:47 +02008032 if (trypos != NULL)
8033 {
8034 curwin->w_cursor.lnum = trypos->lnum + 1;
8035 curwin->w_cursor.col = 0;
8036 continue;
8037 }
8038
8039 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008040 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
8041 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02008042 continue;
8043
8044 /* Finally the actual check for "namespace". */
8045 if (cin_is_cpp_namespace(l))
8046 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008047 amount += curbuf->b_ind_cpp_namespace
8048 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008049 break;
8050 }
Bram Moolenaar7720ba82017-03-08 22:19:26 +01008051 else if (cin_is_cpp_extern_c(l))
8052 {
8053 amount += curbuf->b_ind_cpp_extern_c
8054 - added_to_amount;
8055 break;
8056 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008057
8058 if (cin_nocode(l))
8059 continue;
8060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 }
8062 break;
8063 }
8064
8065 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008066 * If we're in a comment or raw string now, skip to the start
8067 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008069 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 {
8071 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008072 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 continue;
8074 }
8075
8076 l = ml_get_curline();
8077
8078 /*
8079 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008080 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008082 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 if (iscase || cin_isscopedecl(l))
8084 {
8085 /* we are only looking for cpp base class
8086 * declaration/initialization any longer */
8087 if (lookfor == LOOKFOR_CPP_BASECLASS)
8088 break;
8089
8090 /* When looking for a "do" we are not interested in
8091 * labels. */
8092 if (whilelevel > 0)
8093 continue;
8094
8095 /*
8096 * case xx:
8097 * c = 99 + <- this indent plus continuation
8098 *-> here;
8099 */
8100 if (lookfor == LOOKFOR_UNTERM
8101 || lookfor == LOOKFOR_ENUM_OR_INIT)
8102 {
8103 if (cont_amount > 0)
8104 amount = cont_amount;
8105 else
8106 amount += ind_continuation;
8107 break;
8108 }
8109
8110 /*
8111 * case xx: <- line up with this case
8112 * x = 333;
8113 * case yy:
8114 */
8115 if ( (iscase && lookfor == LOOKFOR_CASE)
8116 || (iscase && lookfor_break)
8117 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8118 {
8119 /*
8120 * Check that this case label is not for another
8121 * switch()
8122 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008123 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008124 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 {
8126 amount = get_indent(); /* XXX */
8127 break;
8128 }
8129 continue;
8130 }
8131
8132 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8133
8134 /*
8135 * case xx: if (cond) <- line up with this if
8136 * y = y + 1;
8137 * -> s = 99;
8138 *
8139 * case xx:
8140 * if (cond) <- line up with this line
8141 * y = y + 1;
8142 * -> s = 99;
8143 */
8144 if (lookfor == LOOKFOR_TERM)
8145 {
8146 if (n)
8147 amount = n;
8148
8149 if (!lookfor_break)
8150 break;
8151 }
8152
8153 /*
8154 * case xx: x = x + 1; <- line up with this x
8155 * -> y = y + 1;
8156 *
8157 * case xx: if (cond) <- line up with this if
8158 * -> y = y + 1;
8159 */
8160 if (n)
8161 {
8162 amount = n;
8163 l = after_label(ml_get_curline());
8164 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008165 {
8166 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008167 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008168 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008169 amount += curbuf->b_ind_level
8170 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 break;
8173 }
8174
8175 /*
8176 * Try to get the indent of a statement before the switch
8177 * label. If nothing is found, line up relative to the
8178 * switch label.
8179 * break; <- may line up with this line
8180 * case xx:
8181 * -> y = 1;
8182 */
8183 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008184 ? curbuf->b_ind_case_code
8185 : curbuf->b_ind_scopedecl_code);
8186 lookfor = curbuf->b_ind_case_break
8187 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 continue;
8189 }
8190
8191 /*
8192 * Looking for a switch() label or C++ scope declaration,
8193 * ignore other lines, skip {}-blocks.
8194 */
8195 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8196 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008197 if (find_last_paren(l, '{', '}')
8198 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008199 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008201 curwin->w_cursor.col = 0;
8202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 continue;
8204 }
8205
8206 /*
8207 * Ignore jump labels with nothing after them.
8208 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008209 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 {
8211 l = after_label(ml_get_curline());
8212 if (l == NULL || cin_nocode(l))
8213 continue;
8214 }
8215
8216 /*
8217 * Ignore #defines, #if, etc.
8218 * Ignore comment and empty lines.
8219 * (need to get the line again, cin_islabel() may have
8220 * unlocked it)
8221 */
8222 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008223 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 || cin_nocode(l))
8225 continue;
8226
8227 /*
8228 * Are we at the start of a cpp base class declaration or
8229 * constructor initialization?
8230 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008231 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008232 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008233 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008234 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008235 l = ml_get_curline();
8236 }
8237 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 {
8239 if (lookfor == LOOKFOR_UNTERM)
8240 {
8241 if (cont_amount > 0)
8242 amount = cont_amount;
8243 else
8244 amount += ind_continuation;
8245 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008246 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008248 /* Need to find start of the declaration. */
8249 lookfor = LOOKFOR_UNTERM;
8250 ind_continuation = 0;
8251 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 }
8253 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008254 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008255 amount = get_baseclass_amount(
8256 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 break;
8258 }
8259 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8260 {
8261 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008262 * declaration or initialization before the opening brace.
8263 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 if (cin_isterminated(l, TRUE, FALSE))
8265 break;
8266 else
8267 continue;
8268 }
8269
8270 /*
8271 * What happens next depends on the line being terminated.
8272 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008273 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 * 123,
8275 * sizeof
8276 * here
8277 * Otherwise check whether it is a enumeration or structure
8278 * initialisation (not indented) or a variable declaration
8279 * (indented).
8280 */
8281 terminated = cin_isterminated(l, FALSE, TRUE);
8282
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008283 if (js_cur_has_key)
8284 {
8285 js_cur_has_key = 0; /* only check the first line */
8286 if (curbuf->b_ind_js && terminated == ',')
8287 {
8288 /* For Javascript we might be inside an object:
8289 * key: something, <- align with this
8290 * key: something
8291 * or:
8292 * key: something + <- align with this
8293 * something,
8294 * key: something
8295 */
8296 lookfor = LOOKFOR_JS_KEY;
8297 }
8298 }
8299 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8300 {
8301 amount = get_indent();
8302 break;
8303 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008304 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008305 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008306 if (tryposBrace != NULL && tryposBrace->lnum
8307 >= curwin->w_cursor.lnum)
8308 break;
8309 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008310 /* line below current line is the one that starts a
8311 * (possibly broken) line ending in a comma. */
8312 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008313 else
8314 {
8315 amount = get_indent();
8316 if (curwin->w_cursor.lnum - 1 == ourscope)
8317 /* line above is start of the scope, thus current
8318 * line is the one that stars a (possibly broken)
8319 * line ending in a comma. */
8320 break;
8321 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008322 }
8323
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8325 && terminated == ','))
8326 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008327 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8328 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008329 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 /*
8331 * if we're in the middle of a paren thing,
8332 * go back to the line that starts it so
8333 * we can get the right prevailing indent
8334 * if ( foo &&
8335 * bar )
8336 */
8337 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008338 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008340 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 */
8342 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008343 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008344 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8345 || (trypos->lnum == tryposBrace->lnum
8346 && trypos->col < tryposBrace->col)))
8347 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348
8349 /*
8350 * If we are looking for ',', we also look for matching
8351 * braces.
8352 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008353 if (trypos == NULL && terminated == ','
8354 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008355 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356
8357 if (trypos != NULL)
8358 {
8359 /*
8360 * Check if we are on a case label now. This is
8361 * handled above.
8362 * case xx: if ( asdf &&
8363 * asdf)
8364 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008365 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008367 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 {
8369 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008370 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 continue;
8372 }
8373 }
8374
8375 /*
8376 * Skip over continuation lines to find the one to get the
8377 * indent from
8378 * char *usethis = "bla\
8379 * bla",
8380 * here;
8381 */
8382 if (terminated == ',')
8383 {
8384 while (curwin->w_cursor.lnum > 1)
8385 {
8386 l = ml_get(curwin->w_cursor.lnum - 1);
8387 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8388 break;
8389 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008390 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 }
8392 }
8393
8394 /*
8395 * Get indent and pointer to text for current line,
8396 * ignoring any jump label. XXX
8397 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008398 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008399 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008400 else
8401 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 /*
8403 * If this is just above the line we are indenting, and it
8404 * starts with a '{', line it up with this line.
8405 * while (not)
8406 * -> {
8407 * }
8408 */
8409 if (terminated != ',' && lookfor != LOOKFOR_TERM
8410 && theline[0] == '{')
8411 {
8412 amount = cur_amount;
8413 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008414 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 * doesn't start with a '{', which must have a match
8416 * in the same line (scope is the same). Probably:
8417 * { 1, 2 },
8418 * -> { 3, 4 }
8419 */
8420 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008421 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008423 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 {
8425 /* have to look back, whether it is a cpp base
8426 * class declaration or initialization */
8427 lookfor = LOOKFOR_CPP_BASECLASS;
8428 continue;
8429 }
8430 break;
8431 }
8432
8433 /*
8434 * Check if we are after an "if", "while", etc.
8435 * Also allow " } else".
8436 */
8437 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8438 {
8439 /*
8440 * Found an unterminated line after an if (), line up
8441 * with the last one.
8442 * if (cond)
8443 * 100 +
8444 * -> here;
8445 */
8446 if (lookfor == LOOKFOR_UNTERM
8447 || lookfor == LOOKFOR_ENUM_OR_INIT)
8448 {
8449 if (cont_amount > 0)
8450 amount = cont_amount;
8451 else
8452 amount += ind_continuation;
8453 break;
8454 }
8455
8456 /*
8457 * If this is just above the line we are indenting, we
8458 * are finished.
8459 * while (not)
8460 * -> here;
8461 * Otherwise this indent can be used when the line
8462 * before this is terminated.
8463 * yyy;
8464 * if (stat)
8465 * while (not)
8466 * xxx;
8467 * -> here;
8468 */
8469 amount = cur_amount;
8470 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008471 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 if (lookfor != LOOKFOR_TERM)
8473 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008474 amount += curbuf->b_ind_level
8475 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 break;
8477 }
8478
8479 /*
8480 * Special trick: when expecting the while () after a
8481 * do, line up with the while()
8482 * do
8483 * x = 1;
8484 * -> here
8485 */
8486 l = skipwhite(ml_get_curline());
8487 if (cin_isdo(l))
8488 {
8489 if (whilelevel == 0)
8490 break;
8491 --whilelevel;
8492 }
8493
8494 /*
8495 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008496 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 * Need to use the scope of this "else". XXX
8498 * If whilelevel != 0 continue looking for a "do {".
8499 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008500 if (cin_iselse(l) && whilelevel == 0)
8501 {
8502 /* If we're looking at "} else", let's make sure we
8503 * find the opening brace of the enclosing scope,
8504 * not the one from "if () {". */
8505 if (*l == '}')
8506 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008507 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008508
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008509 if ((trypos = find_start_brace()) == NULL
8510 || find_match(LOOKFOR_IF, trypos->lnum)
8511 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008512 break;
8513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 }
8515
8516 /*
8517 * If we're below an unterminated line that is not an
8518 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008519 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 * the line before this one.
8521 */
8522 else
8523 {
8524 /*
8525 * Found two unterminated lines on a row, line up with
8526 * the last one.
8527 * c = 99 +
8528 * 100 +
8529 * -> here;
8530 */
8531 if (lookfor == LOOKFOR_UNTERM)
8532 {
8533 /* When line ends in a comma add extra indent */
8534 if (terminated == ',')
8535 amount += ind_continuation;
8536 break;
8537 }
8538
8539 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8540 {
8541 /* Found two lines ending in ',', lineup with the
8542 * lowest one, but check for cpp base class
8543 * declaration/initialization, if it is an
8544 * opening brace or we are looking just for
8545 * enumerations/initializations. */
8546 if (terminated == ',')
8547 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008548 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 break;
8550
8551 lookfor = LOOKFOR_CPP_BASECLASS;
8552 continue;
8553 }
8554
8555 /* Ignore unterminated lines in between, but
8556 * reduce indent. */
8557 if (amount > cur_amount)
8558 amount = cur_amount;
8559 }
8560 else
8561 {
8562 /*
8563 * Found first unterminated line on a row, may
8564 * line up with this line, remember its indent
8565 * 100 +
8566 * -> here;
8567 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008568 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008570
8571 n = (int)STRLEN(l);
8572 if (terminated == ',' && (*skipwhite(l) == ']'
8573 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008574 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575
8576 /*
8577 * If previous line ends in ',', check whether we
8578 * are in an initialization or enum
8579 * struct xxx =
8580 * {
8581 * sizeof a,
8582 * 124 };
8583 * or a normal possible continuation line.
8584 * but only, of no other statement has been found
8585 * yet.
8586 */
8587 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8588 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008589 if (curbuf->b_ind_js)
8590 {
8591 /* Search for a line ending in a comma
8592 * and line up with the line below it
8593 * (could be the current line).
8594 * some = [
8595 * 1, <- line up here
8596 * 2,
8597 * some = [
8598 * 3 + <- line up here
8599 * 4 *
8600 * 5,
8601 * 6,
8602 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008603 if (cin_iscomment(skipwhite(l)))
8604 break;
8605 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008606 trypos = find_match_char('[',
8607 curbuf->b_ind_maxparen);
8608 if (trypos != NULL)
8609 {
8610 if (trypos->lnum
8611 == curwin->w_cursor.lnum - 1)
8612 {
8613 /* Current line is first inside
8614 * [], line up with it. */
8615 break;
8616 }
8617 ourscope = trypos->lnum;
8618 }
8619 }
8620 else
8621 {
8622 lookfor = LOOKFOR_ENUM_OR_INIT;
8623 cont_amount = cin_first_id_amount();
8624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 }
8626 else
8627 {
8628 if (lookfor == LOOKFOR_INITIAL
8629 && *l != NUL
8630 && l[STRLEN(l) - 1] == '\\')
8631 /* XXX */
8632 cont_amount = cin_get_equal_amount(
8633 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008634 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008635 && lookfor != LOOKFOR_JS_KEY
8636 && lookfor != LOOKFOR_COMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 lookfor = LOOKFOR_UNTERM;
8638 }
8639 }
8640 }
8641 }
8642
8643 /*
8644 * Check if we are after a while (cond);
8645 * If so: Ignore until the matching "do".
8646 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008647 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 {
8649 /*
8650 * Found an unterminated line after a while ();, line up
8651 * with the last one.
8652 * while (cond);
8653 * 100 + <- line up with this one
8654 * -> here;
8655 */
8656 if (lookfor == LOOKFOR_UNTERM
8657 || lookfor == LOOKFOR_ENUM_OR_INIT)
8658 {
8659 if (cont_amount > 0)
8660 amount = cont_amount;
8661 else
8662 amount += ind_continuation;
8663 break;
8664 }
8665
8666 if (whilelevel == 0)
8667 {
8668 lookfor = LOOKFOR_TERM;
8669 amount = get_indent(); /* XXX */
8670 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008671 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 }
8673 ++whilelevel;
8674 }
8675
8676 /*
8677 * We are after a "normal" statement.
8678 * If we had another statement we can stop now and use the
8679 * indent of that other statement.
8680 * Otherwise the indent of the current statement may be used,
8681 * search backwards for the next "normal" statement.
8682 */
8683 else
8684 {
8685 /*
8686 * Skip single break line, if before a switch label. It
8687 * may be lined up with the case label.
8688 */
8689 if (lookfor == LOOKFOR_NOBREAK
8690 && cin_isbreak(skipwhite(ml_get_curline())))
8691 {
8692 lookfor = LOOKFOR_ANY;
8693 continue;
8694 }
8695
8696 /*
8697 * Handle "do {" line.
8698 */
8699 if (whilelevel > 0)
8700 {
8701 l = cin_skipcomment(ml_get_curline());
8702 if (cin_isdo(l))
8703 {
8704 amount = get_indent(); /* XXX */
8705 --whilelevel;
8706 continue;
8707 }
8708 }
8709
8710 /*
8711 * Found a terminated line above an unterminated line. Add
8712 * the amount for a continuation line.
8713 * x = 1;
8714 * y = foo +
8715 * -> here;
8716 * or
8717 * int x = 1;
8718 * int foo,
8719 * -> here;
8720 */
8721 if (lookfor == LOOKFOR_UNTERM
8722 || lookfor == LOOKFOR_ENUM_OR_INIT)
8723 {
8724 if (cont_amount > 0)
8725 amount = cont_amount;
8726 else
8727 amount += ind_continuation;
8728 break;
8729 }
8730
8731 /*
8732 * Found a terminated line above a terminated line or "if"
8733 * etc. line. Use the amount of the line below us.
8734 * x = 1; x = 1;
8735 * if (asdf) y = 2;
8736 * while (asdf) ->here;
8737 * here;
8738 * ->foo;
8739 */
8740 if (lookfor == LOOKFOR_TERM)
8741 {
8742 if (!lookfor_break && whilelevel == 0)
8743 break;
8744 }
8745
8746 /*
8747 * First line above the one we're indenting is terminated.
8748 * To know what needs to be done look further backward for
8749 * a terminated line.
8750 */
8751 else
8752 {
8753 /*
8754 * position the cursor over the rightmost paren, so
8755 * that matching it will take us back to the start of
8756 * the line. Helps for:
8757 * func(asdr,
8758 * asdfasdf);
8759 * here;
8760 */
8761term_again:
8762 l = ml_get_curline();
8763 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008764 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008765 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 {
8767 /*
8768 * Check if we are on a case label now. This is
8769 * handled above.
8770 * case xx: if ( asdf &&
8771 * asdf)
8772 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008773 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008775 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 {
8777 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008778 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 continue;
8780 }
8781 }
8782
8783 /* When aligning with the case statement, don't align
8784 * with a statement after it.
8785 * case 1: { <-- don't use this { position
8786 * stat;
8787 * }
8788 * case 2:
8789 * stat;
8790 * }
8791 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008792 iscase = (curbuf->b_ind_keep_case_label
8793 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794
8795 /*
8796 * Get indent and pointer to text for current line,
8797 * ignoring any jump label.
8798 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008799 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800
8801 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008802 amount += curbuf->b_ind_open_extra;
8803 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008804 l = skipwhite(l);
8805 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008806 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8808
8809 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008810 * When a terminated line starts with "else" skip to
8811 * the matching "if":
8812 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008813 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008814 * Need to use the scope of this "else". XXX
8815 * If whilelevel != 0 continue looking for a "do {".
8816 */
8817 if (lookfor == LOOKFOR_TERM
8818 && *l != '}'
8819 && cin_iselse(l)
8820 && whilelevel == 0)
8821 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008822 if ((trypos = find_start_brace()) == NULL
8823 || find_match(LOOKFOR_IF, trypos->lnum)
8824 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008825 break;
8826 continue;
8827 }
8828
8829 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 * If we're at the end of a block, skip to the start of
8831 * that block.
8832 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008833 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008834 if (find_last_paren(l, '{', '}') /* XXX */
8835 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008837 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 /* if not "else {" check for terminated again */
8839 /* but skip block for "} else {" */
8840 l = cin_skipcomment(ml_get_curline());
8841 if (*l == '}' || !cin_iselse(l))
8842 goto term_again;
8843 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008844 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 }
8846 }
8847 }
8848 }
8849 }
8850 }
8851
8852 /* add extra indent for a comment */
8853 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008854 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008855
8856 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008857 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
8858 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008859
8860 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008862
8863 /*
8864 * ok -- we're not inside any sort of structure at all!
8865 *
8866 * This means we're at the top level, and everything should
8867 * basically just match where the previous line is, except
8868 * for the lines immediately following a function declaration,
8869 * which are K&R-style parameters and need to be indented.
8870 *
8871 * if our line starts with an open brace, forget about any
8872 * prevailing indent and make sure it looks like the start
8873 * of a function
8874 */
8875
8876 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008878 amount = curbuf->b_ind_first_open;
8879 goto theend;
8880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008882 /*
8883 * If the NEXT line is a function declaration, the current
8884 * line needs to be indented as a function type spec.
8885 * Don't do this if the current line looks like a comment or if the
8886 * current line is terminated, ie. ends in ';', or if the current line
8887 * contains { or }: "void f() {\n if (1)"
8888 */
8889 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8890 && !cin_nocode(theline)
8891 && vim_strchr(theline, '{') == NULL
8892 && vim_strchr(theline, '}') == NULL
8893 && !cin_ends_in(theline, (char_u *)":", NULL)
8894 && !cin_ends_in(theline, (char_u *)",", NULL)
8895 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
8896 cur_curpos.lnum + 1)
8897 && !cin_isterminated(theline, FALSE, TRUE))
8898 {
8899 amount = curbuf->b_ind_func_type;
8900 goto theend;
8901 }
8902
8903 /* search backwards until we find something we recognize */
8904 amount = 0;
8905 curwin->w_cursor = cur_curpos;
8906 while (curwin->w_cursor.lnum > 1)
8907 {
8908 curwin->w_cursor.lnum--;
8909 curwin->w_cursor.col = 0;
8910
8911 l = ml_get_curline();
8912
8913 /*
8914 * If we're in a comment or raw string now, skip to the start
8915 * of it.
8916 */ /* XXX */
8917 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008919 curwin->w_cursor.lnum = trypos->lnum + 1;
8920 curwin->w_cursor.col = 0;
8921 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 }
8923
8924 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008925 * Are we at the start of a cpp base class declaration or
8926 * constructor initialization?
8927 */ /* XXX */
8928 n = FALSE;
8929 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008931 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
8932 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008934 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008936 /* XXX */
8937 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
8938 break;
8939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008941 /*
8942 * Skip preprocessor directives and blank lines.
8943 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008944 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008945 continue;
8946
8947 if (cin_nocode(l))
8948 continue;
8949
8950 /*
8951 * If the previous line ends in ',', use one level of
8952 * indentation:
8953 * int foo,
8954 * bar;
8955 * do this before checking for '}' in case of eg.
8956 * enum foobar
8957 * {
8958 * ...
8959 * } foo,
8960 * bar;
8961 */
8962 n = 0;
8963 if (cin_ends_in(l, (char_u *)",", NULL)
8964 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
8965 {
8966 /* take us back to opening paren */
8967 if (find_last_paren(l, '(', ')')
8968 && (trypos = find_match_paren(
8969 curbuf->b_ind_maxparen)) != NULL)
8970 curwin->w_cursor = *trypos;
8971
8972 /* For a line ending in ',' that is a continuation line go
8973 * back to the first line with a backslash:
8974 * char *foo = "bla\
8975 * bla",
8976 * here;
8977 */
8978 while (n == 0 && curwin->w_cursor.lnum > 1)
8979 {
8980 l = ml_get(curwin->w_cursor.lnum - 1);
8981 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8982 break;
8983 --curwin->w_cursor.lnum;
8984 curwin->w_cursor.col = 0;
8985 }
8986
8987 amount = get_indent(); /* XXX */
8988
8989 if (amount == 0)
8990 amount = cin_first_id_amount();
8991 if (amount == 0)
8992 amount = ind_continuation;
8993 break;
8994 }
8995
8996 /*
8997 * If the line looks like a function declaration, and we're
8998 * not in a comment, put it the left margin.
8999 */
9000 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
9001 break;
9002 l = ml_get_curline();
9003
9004 /*
9005 * Finding the closing '}' of a previous function. Put
9006 * current line at the left margin. For when 'cino' has "fs".
9007 */
9008 if (*skipwhite(l) == '}')
9009 break;
9010
9011 /* (matching {)
9012 * If the previous line ends on '};' (maybe followed by
9013 * comments) align at column 0. For example:
9014 * char *string_array[] = { "foo",
9015 * / * x * / "b};ar" }; / * foobar * /
9016 */
9017 if (cin_ends_in(l, (char_u *)"};", NULL))
9018 break;
9019
9020 /*
9021 * If the previous line ends on '[' we are probably in an
9022 * array constant:
9023 * something = [
9024 * 234, <- extra indent
9025 */
9026 if (cin_ends_in(l, (char_u *)"[", NULL))
9027 {
9028 amount = get_indent() + ind_continuation;
9029 break;
9030 }
9031
9032 /*
9033 * Find a line only has a semicolon that belongs to a previous
9034 * line ending in '}', e.g. before an #endif. Don't increase
9035 * indent then.
9036 */
9037 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
9038 {
9039 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040
9041 while (curwin->w_cursor.lnum > 1)
9042 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009043 look = ml_get(--curwin->w_cursor.lnum);
9044 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01009045 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009047 }
9048 if (curwin->w_cursor.lnum > 0
9049 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009052 curwin->w_cursor = curpos_save;
9053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009055 /*
9056 * If the PREVIOUS line is a function declaration, the current
9057 * line (and the ones that follow) needs to be indented as
9058 * parameters.
9059 */
9060 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
9061 {
9062 amount = curbuf->b_ind_param;
9063 break;
9064 }
9065
9066 /*
9067 * If the previous line ends in ';' and the line before the
9068 * previous line ends in ',' or '\', ident to column zero:
9069 * int foo,
9070 * bar;
9071 * indent_to_0 here;
9072 */
9073 if (cin_ends_in(l, (char_u *)";", NULL))
9074 {
9075 l = ml_get(curwin->w_cursor.lnum - 1);
9076 if (cin_ends_in(l, (char_u *)",", NULL)
9077 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9078 break;
9079 l = ml_get_curline();
9080 }
9081
9082 /*
9083 * Doesn't look like anything interesting -- so just
9084 * use the indent of this line.
9085 *
9086 * Position the cursor over the rightmost paren, so that
9087 * matching it will take us back to the start of the line.
9088 */
9089 find_last_paren(l, '(', ')');
9090
9091 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9092 curwin->w_cursor = *trypos;
9093 amount = get_indent(); /* XXX */
9094 break;
9095 }
9096
9097 /* add extra indent for a comment */
9098 if (cin_iscomment(theline))
9099 amount += curbuf->b_ind_comment;
9100
9101 /* add extra indent if the previous line ended in a backslash:
9102 * "asdfasdf\
9103 * here";
9104 * char *foo = "asdf\
9105 * here";
9106 */
9107 if (cur_curpos.lnum > 1)
9108 {
9109 l = ml_get(cur_curpos.lnum - 1);
9110 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9111 {
9112 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9113 if (cur_amount > 0)
9114 amount = cur_amount;
9115 else if (cur_amount == 0)
9116 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 }
9118 }
9119
9120theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009121 if (amount < 0)
9122 amount = 0;
9123
9124laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 /* put the cursor back where it belongs */
9126 curwin->w_cursor = cur_curpos;
9127
9128 vim_free(linecopy);
9129
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 return amount;
9131}
9132
9133 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009134find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135{
9136 char_u *look;
9137 pos_T *theirscope;
9138 char_u *mightbeif;
9139 int elselevel;
9140 int whilelevel;
9141
9142 if (lookfor == LOOKFOR_IF)
9143 {
9144 elselevel = 1;
9145 whilelevel = 0;
9146 }
9147 else
9148 {
9149 elselevel = 0;
9150 whilelevel = 1;
9151 }
9152
9153 curwin->w_cursor.col = 0;
9154
9155 while (curwin->w_cursor.lnum > ourscope + 1)
9156 {
9157 curwin->w_cursor.lnum--;
9158 curwin->w_cursor.col = 0;
9159
9160 look = cin_skipcomment(ml_get_curline());
9161 if (cin_iselse(look)
9162 || cin_isif(look)
9163 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009164 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 {
9166 /*
9167 * if we've gone outside the braces entirely,
9168 * we must be out of scope...
9169 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009170 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 if (theirscope == NULL)
9172 break;
9173
9174 /*
9175 * and if the brace enclosing this is further
9176 * back than the one enclosing the else, we're
9177 * out of luck too.
9178 */
9179 if (theirscope->lnum < ourscope)
9180 break;
9181
9182 /*
9183 * and if they're enclosed in a *deeper* brace,
9184 * then we can ignore it because it's in a
9185 * different scope...
9186 */
9187 if (theirscope->lnum > ourscope)
9188 continue;
9189
9190 /*
9191 * if it was an "else" (that's not an "else if")
9192 * then we need to go back to another if, so
9193 * increment elselevel
9194 */
9195 look = cin_skipcomment(ml_get_curline());
9196 if (cin_iselse(look))
9197 {
9198 mightbeif = cin_skipcomment(look + 4);
9199 if (!cin_isif(mightbeif))
9200 ++elselevel;
9201 continue;
9202 }
9203
9204 /*
9205 * if it was a "while" then we need to go back to
9206 * another "do", so increment whilelevel. XXX
9207 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009208 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 {
9210 ++whilelevel;
9211 continue;
9212 }
9213
9214 /* If it's an "if" decrement elselevel */
9215 look = cin_skipcomment(ml_get_curline());
9216 if (cin_isif(look))
9217 {
9218 elselevel--;
9219 /*
9220 * When looking for an "if" ignore "while"s that
9221 * get in the way.
9222 */
9223 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9224 whilelevel = 0;
9225 }
9226
9227 /* If it's a "do" decrement whilelevel */
9228 if (cin_isdo(look))
9229 whilelevel--;
9230
9231 /*
9232 * if we've used up all the elses, then
9233 * this must be the if that we want!
9234 * match the indent level of that if.
9235 */
9236 if (elselevel <= 0 && whilelevel <= 0)
9237 {
9238 return OK;
9239 }
9240 }
9241 }
9242 return FAIL;
9243}
9244
9245# if defined(FEAT_EVAL) || defined(PROTO)
9246/*
9247 * Get indent level from 'indentexpr'.
9248 */
9249 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009250get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251{
9252 int indent;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009253 pos_T save_pos;
9254 colnr_T save_curswant;
9255 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009257 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9258 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009260 /* Save and restore cursor position and curswant, in case it was changed
9261 * via :normal commands */
9262 save_pos = curwin->w_cursor;
9263 save_curswant = curwin->w_curswant;
9264 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009266 if (use_sandbox)
9267 ++sandbox;
9268 ++textlock;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02009269 indent = (int)eval_to_number(curbuf->b_p_inde);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009270 if (use_sandbox)
9271 --sandbox;
9272 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273
9274 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9275 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9276 * command. */
9277 save_State = State;
9278 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009279 curwin->w_cursor = save_pos;
9280 curwin->w_curswant = save_curswant;
9281 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282 check_cursor();
9283 State = save_State;
9284
9285 /* If there is an error, just keep the current indent. */
9286 if (indent < 0)
9287 indent = get_indent();
9288
9289 return indent;
9290}
9291# endif
9292
9293#endif /* FEAT_CINDENT */
9294
9295#if defined(FEAT_LISP) || defined(PROTO)
9296
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009297static int lisp_match(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298
9299 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009300lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301{
9302 char_u buf[LSIZE];
9303 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009304 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305
9306 while (*word != NUL)
9307 {
9308 (void)copy_option_part(&word, buf, LSIZE, ",");
9309 len = (int)STRLEN(buf);
9310 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9311 return TRUE;
9312 }
9313 return FALSE;
9314}
9315
9316/*
9317 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9318 * The incompatible newer method is quite a bit better at indenting
9319 * code in lisp-like languages than the traditional one; it's still
9320 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9321 *
9322 * TODO:
9323 * Findmatch() should be adapted for lisp, also to make showmatch
9324 * work correctly: now (v5.3) it seems all C/C++ oriented:
9325 * - it does not recognize the #\( and #\) notations as character literals
9326 * - it doesn't know about comments starting with a semicolon
9327 * - it incorrectly interprets '(' as a character literal
9328 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009329 * Update from Sergey Khorev:
9330 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331 */
9332 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009333get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009335 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 int amount;
9337 char_u *that;
9338 colnr_T col;
9339 colnr_T firsttry;
9340 int parencount, quotecount;
9341 int vi_lisp;
9342
9343 /* Set vi_lisp to use the vi-compatible method */
9344 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9345
9346 realpos = curwin->w_cursor;
9347 curwin->w_cursor.col = 0;
9348
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009349 if ((pos = findmatch(NULL, '(')) == NULL)
9350 pos = findmatch(NULL, '[');
9351 else
9352 {
9353 paren = *pos;
9354 pos = findmatch(NULL, '[');
9355 if (pos == NULL || ltp(pos, &paren))
9356 pos = &paren;
9357 }
9358 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 {
9360 /* Extra trick: Take the indent of the first previous non-white
9361 * line that is at the same () level. */
9362 amount = -1;
9363 parencount = 0;
9364
9365 while (--curwin->w_cursor.lnum >= pos->lnum)
9366 {
9367 if (linewhite(curwin->w_cursor.lnum))
9368 continue;
9369 for (that = ml_get_curline(); *that != NUL; ++that)
9370 {
9371 if (*that == ';')
9372 {
9373 while (*(that + 1) != NUL)
9374 ++that;
9375 continue;
9376 }
9377 if (*that == '\\')
9378 {
9379 if (*(that + 1) != NUL)
9380 ++that;
9381 continue;
9382 }
9383 if (*that == '"' && *(that + 1) != NUL)
9384 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009385 while (*++that && *that != '"')
9386 {
9387 /* skipping escaped characters in the string */
9388 if (*that == '\\')
9389 {
9390 if (*++that == NUL)
9391 break;
9392 if (that[1] == NUL)
9393 {
9394 ++that;
9395 break;
9396 }
9397 }
9398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009400 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009402 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 --parencount;
9404 }
9405 if (parencount == 0)
9406 {
9407 amount = get_indent();
9408 break;
9409 }
9410 }
9411
9412 if (amount == -1)
9413 {
9414 curwin->w_cursor.lnum = pos->lnum;
9415 curwin->w_cursor.col = pos->col;
9416 col = pos->col;
9417
9418 that = ml_get_curline();
9419
9420 if (vi_lisp && get_indent() == 0)
9421 amount = 2;
9422 else
9423 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009424 char_u *line = that;
9425
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 amount = 0;
9427 while (*that && col)
9428 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009429 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 col--;
9431 }
9432
9433 /*
9434 * Some keywords require "body" indenting rules (the
9435 * non-standard-lisp ones are Scheme special forms):
9436 *
9437 * (let ((a 1)) instead (let ((a 1))
9438 * (...)) of (...))
9439 */
9440
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009441 if (!vi_lisp && (*that == '(' || *that == '[')
9442 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 amount += 2;
9444 else
9445 {
9446 that++;
9447 amount++;
9448 firsttry = amount;
9449
9450 while (vim_iswhite(*that))
9451 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009452 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 ++that;
9454 }
9455
9456 if (*that && *that != ';') /* not a comment line */
9457 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009458 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009459 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009460 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 firsttry++;
9462
9463 parencount = 0;
9464 quotecount = 0;
9465
9466 if (vi_lisp
9467 || (*that != '"'
9468 && *that != '\''
9469 && *that != '#'
9470 && (*that < '0' || *that > '9')))
9471 {
9472 while (*that
9473 && (!vim_iswhite(*that)
9474 || quotecount
9475 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009476 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 && !quotecount
9478 && !parencount
9479 && vi_lisp)))
9480 {
9481 if (*that == '"')
9482 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009483 if ((*that == '(' || *that == '[')
9484 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009486 if ((*that == ')' || *that == ']')
9487 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 --parencount;
9489 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009490 amount += lbr_chartabsize_adv(
9491 line, &that, (colnr_T)amount);
9492 amount += lbr_chartabsize_adv(
9493 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 }
9495 }
9496 while (vim_iswhite(*that))
9497 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009498 amount += lbr_chartabsize(
9499 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 that++;
9501 }
9502 if (!*that || *that == ';')
9503 amount = firsttry;
9504 }
9505 }
9506 }
9507 }
9508 }
9509 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009510 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511
9512 curwin->w_cursor = realpos;
9513
9514 return amount;
9515}
9516#endif /* FEAT_LISP */
9517
9518 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009519prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009521#if defined(SIGHUP) && defined(SIG_IGN)
9522 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9523 * makes Vim exit and then handling SIGHUP causes various reentrance
9524 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009525 signal(SIGHUP, SIG_IGN);
9526#endif
9527
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528#ifdef FEAT_GUI
9529 if (gui.in_use)
9530 {
9531 gui.dying = TRUE;
9532 out_trash(); /* trash any pending output */
9533 }
9534 else
9535#endif
9536 {
9537 windgoto((int)Rows - 1, 0);
9538
9539 /*
9540 * Switch terminal mode back now, so messages end up on the "normal"
9541 * screen (if there are two screens).
9542 */
9543 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009544 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545 out_flush();
9546 }
9547}
9548
9549/*
9550 * Preserve files and exit.
9551 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009552 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9553 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 */
9555 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009556preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557{
9558 buf_T *buf;
9559
9560 prepare_to_exit();
9561
Bram Moolenaar4770d092006-01-12 23:22:24 +00009562 /* Setting this will prevent free() calls. That avoids calling free()
9563 * recursively when free() was invoked with a bad pointer. */
9564 really_exiting = TRUE;
9565
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566 out_str(IObuff);
9567 screen_start(); /* don't know where cursor is now */
9568 out_flush();
9569
9570 ml_close_notmod(); /* close all not-modified buffers */
9571
Bram Moolenaar29323592016-07-24 22:04:11 +02009572 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 {
9574 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9575 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009576 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 screen_start(); /* don't know where cursor is now */
9578 out_flush();
9579 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9580 break;
9581 }
9582 }
9583
9584 ml_close_all(FALSE); /* close all memfiles, without deleting */
9585
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009586 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587
9588 getout(1);
9589}
9590
9591/*
9592 * return TRUE if "fname" exists.
9593 */
9594 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009595vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009597 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598
9599 if (mch_stat((char *)fname, &st))
9600 return FALSE;
9601 return TRUE;
9602}
9603
9604/*
9605 * Check for CTRL-C pressed, but only once in a while.
9606 * Should be used instead of ui_breakcheck() for functions that check for
9607 * each line in the file. Calling ui_breakcheck() each time takes too much
9608 * time, because it can be a system call.
9609 */
9610
9611#ifndef BREAKCHECK_SKIP
9612# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9613# define BREAKCHECK_SKIP 200
9614# else
9615# define BREAKCHECK_SKIP 32
9616# endif
9617#endif
9618
9619static int breakcheck_count = 0;
9620
9621 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009622line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623{
9624 if (++breakcheck_count >= BREAKCHECK_SKIP)
9625 {
9626 breakcheck_count = 0;
9627 ui_breakcheck();
9628 }
9629}
9630
9631/*
9632 * Like line_breakcheck() but check 10 times less often.
9633 */
9634 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009635fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636{
9637 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9638 {
9639 breakcheck_count = 0;
9640 ui_breakcheck();
9641 }
9642}
9643
9644/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009645 * Invoke expand_wildcards() for one pattern.
9646 * Expand items like "%:h" before the expansion.
9647 * Returns OK or FAIL.
9648 */
9649 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009650expand_wildcards_eval(
9651 char_u **pat, /* pointer to input pattern */
9652 int *num_file, /* resulting number of files */
9653 char_u ***file, /* array of resulting files */
9654 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009655{
9656 int ret = FAIL;
9657 char_u *eval_pat = NULL;
9658 char_u *exp_pat = *pat;
9659 char_u *ignored_msg;
9660 int usedlen;
9661
9662 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9663 {
9664 ++emsg_off;
9665 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9666 NULL, &ignored_msg, NULL);
9667 --emsg_off;
9668 if (eval_pat != NULL)
9669 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9670 }
9671
9672 if (exp_pat != NULL)
9673 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9674
9675 if (eval_pat != NULL)
9676 {
9677 vim_free(exp_pat);
9678 vim_free(eval_pat);
9679 }
9680
9681 return ret;
9682}
9683
9684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9686 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009687 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 */
9689 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009690expand_wildcards(
9691 int num_pat, /* number of input patterns */
9692 char_u **pat, /* array of input patterns */
9693 int *num_files, /* resulting number of files */
9694 char_u ***files, /* array of resulting files */
9695 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696{
9697 int retval;
9698 int i, j;
9699 char_u *p;
9700 int non_suf_match; /* number without matching suffix */
9701
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009702 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703
9704 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009705 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 return retval;
9707
9708#ifdef FEAT_WILDIGN
9709 /*
9710 * Remove names that match 'wildignore'.
9711 */
9712 if (*p_wig)
9713 {
9714 char_u *ffname;
9715
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009716 /* check all files in (*files)[] */
9717 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009719 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 if (ffname == NULL) /* out of memory */
9721 break;
9722# ifdef VMS
9723 vms_remove_version(ffname);
9724# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009725 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +01009727 /* remove this matching file from the list */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009728 vim_free((*files)[i]);
9729 for (j = i; j + 1 < *num_files; ++j)
9730 (*files)[j] = (*files)[j + 1];
9731 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 --i;
9733 }
9734 vim_free(ffname);
9735 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009736
9737 /* If the number of matches is now zero, we fail. */
9738 if (*num_files == 0)
9739 {
9740 vim_free(*files);
9741 *files = NULL;
9742 return FAIL;
9743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 }
9745#endif
9746
9747 /*
9748 * Move the names where 'suffixes' match to the end.
9749 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009750 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 {
9752 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009753 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009755 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 {
9757 /*
9758 * Move the name without matching suffix to the front
9759 * of the list.
9760 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009761 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009763 (*files)[j] = (*files)[j - 1];
9764 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 }
9766 }
9767 }
9768
9769 return retval;
9770}
9771
9772/*
9773 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9774 */
9775 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009776match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777{
9778 int fnamelen, setsuflen;
9779 char_u *setsuf;
9780#define MAXSUFLEN 30 /* maximum length of a file suffix */
9781 char_u suf_buf[MAXSUFLEN];
9782
9783 fnamelen = (int)STRLEN(fname);
9784 setsuflen = 0;
9785 for (setsuf = p_su; *setsuf; )
9786 {
9787 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009788 if (setsuflen == 0)
9789 {
9790 char_u *tail = gettail(fname);
9791
9792 /* empty entry: match name without a '.' */
9793 if (vim_strchr(tail, '.') == NULL)
9794 {
9795 setsuflen = 1;
9796 break;
9797 }
9798 }
9799 else
9800 {
9801 if (fnamelen >= setsuflen
9802 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9803 (size_t)setsuflen) == 0)
9804 break;
9805 setsuflen = 0;
9806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 }
9808 return (setsuflen != 0);
9809}
9810
9811#if !defined(NO_EXPANDPATH) || defined(PROTO)
9812
9813# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009814static int vim_backtick(char_u *p);
9815static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816# endif
9817
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009818# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819/*
9820 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9821 * it's shared between these systems.
9822 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009823# if defined(PROTO)
9824# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825# else
9826# ifdef __BORLANDC__
9827# define _cdecl _RTLENTRYF
9828# endif
9829# endif
9830
9831/*
9832 * comparison function for qsort in dos_expandpath()
9833 */
9834 static int _cdecl
9835pstrcmp(const void *a, const void *b)
9836{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009837 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838}
9839
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009841 * Recursively expand one path component into all matching files and/or
9842 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 * Return the number of matches found.
9844 * "path" has backslashes before chars that are not to be expanded, starting
9845 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009846 * Return the number of matches found.
9847 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 */
9849 static int
9850dos_expandpath(
9851 garray_T *gap,
9852 char_u *path,
9853 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009854 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009855 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009857 char_u *buf;
9858 char_u *path_end;
9859 char_u *p, *s, *e;
9860 int start_len = gap->ga_len;
9861 char_u *pat;
9862 regmatch_T regmatch;
9863 int starts_with_dot;
9864 int matches;
9865 int len;
9866 int starstar = FALSE;
9867 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 WIN32_FIND_DATA fb;
9869 HANDLE hFind = (HANDLE)0;
9870# ifdef FEAT_MBYTE
9871 WIN32_FIND_DATAW wfb;
9872 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9873# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009875 int ok;
9876
9877 /* Expanding "**" may take a long time, check for CTRL-C. */
9878 if (stardepth > 0)
9879 {
9880 ui_breakcheck();
9881 if (got_int)
9882 return 0;
9883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884
Bram Moolenaar7314efd2015-10-31 15:32:52 +01009885 /* Make room for file name. When doing encoding conversion the actual
9886 * length may be quite a bit longer, thus use the maximum possible length. */
9887 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 if (buf == NULL)
9889 return 0;
9890
9891 /*
9892 * Find the first part in the path name that contains a wildcard or a ~1.
9893 * Copy it into buf, including the preceding characters.
9894 */
9895 p = buf;
9896 s = buf;
9897 e = NULL;
9898 path_end = path;
9899 while (*path_end != NUL)
9900 {
9901 /* May ignore a wildcard that has a backslash before it; it will
9902 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9903 if (path_end >= path + wildoff && rem_backslash(path_end))
9904 *p++ = *path_end++;
9905 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9906 {
9907 if (e != NULL)
9908 break;
9909 s = p + 1;
9910 }
9911 else if (path_end >= path + wildoff
9912 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9913 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009914# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915 if (has_mbyte)
9916 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009917 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 STRNCPY(p, path_end, len);
9919 p += len;
9920 path_end += len;
9921 }
9922 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009923# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 *p++ = *path_end++;
9925 }
9926 e = p;
9927 *e = NUL;
9928
9929 /* now we have one wildcard component between s and e */
9930 /* Remove backslashes between "wildoff" and the start of the wildcard
9931 * component. */
9932 for (p = buf + wildoff; p < s; ++p)
9933 if (rem_backslash(p))
9934 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009935 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 --e;
9937 --s;
9938 }
9939
Bram Moolenaar231334e2005-07-25 20:46:57 +00009940 /* Check for "**" between "s" and "e". */
9941 for (p = s; p < e; ++p)
9942 if (p[0] == '*' && p[1] == '*')
9943 starstar = TRUE;
9944
Bram Moolenaard82103e2016-01-17 17:04:05 +01009945 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9947 if (pat == NULL)
9948 {
9949 vim_free(buf);
9950 return 0;
9951 }
9952
9953 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009954 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009955 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956 regmatch.rm_ic = TRUE; /* Always ignore case */
9957 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009958 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009959 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 vim_free(pat);
9961
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009962 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963 {
9964 vim_free(buf);
9965 return 0;
9966 }
9967
9968 /* remember the pattern or file name being looked for */
9969 matchname = vim_strsave(s);
9970
Bram Moolenaar231334e2005-07-25 20:46:57 +00009971 /* If "**" is by itself, this is the first time we encounter it and more
9972 * is following then find matches without any directory. */
9973 if (!didstar && stardepth < 100 && starstar && e - s == 2
9974 && *path_end == '/')
9975 {
9976 STRCPY(s, path_end + 1);
9977 ++stardepth;
9978 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9979 --stardepth;
9980 }
9981
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 /* Scan all files in the directory with "dir/ *.*" */
9983 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984# ifdef FEAT_MBYTE
9985 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
9986 {
9987 /* The active codepage differs from 'encoding'. Attempt using the
9988 * wide function. If it fails because it is not implemented fall back
9989 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009990 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991 if (wn != NULL)
9992 {
9993 hFind = FindFirstFileW(wn, &wfb);
9994 if (hFind == INVALID_HANDLE_VALUE
9995 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
9996 {
9997 vim_free(wn);
9998 wn = NULL;
9999 }
10000 }
10001 }
10002
10003 if (wn == NULL)
10004# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010005 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007
10008 while (ok)
10009 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010# ifdef FEAT_MBYTE
10011 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010012 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013 else
10014# endif
10015 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 /* Ignore entries starting with a dot, unless when asked for. Accept
10017 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010018 if ((p[0] != '.' || starts_with_dot
10019 || ((flags & EW_DODOT)
10020 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010022 || (regmatch.regprog != NULL
10023 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010024 || ((flags & EW_NOTWILD)
10025 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010029
10030 if (starstar && stardepth < 100)
10031 {
10032 /* For "**" in the pattern first go deeper in the tree to
10033 * find matches. */
10034 STRCPY(buf + len, "/**");
10035 STRCPY(buf + len + 3, path_end);
10036 ++stardepth;
10037 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
10038 --stardepth;
10039 }
10040
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041 STRCPY(buf + len, path_end);
10042 if (mch_has_exp_wildcard(path_end))
10043 {
10044 /* need to expand another component of the path */
10045 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010046 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047 }
10048 else
10049 {
10050 /* no more wildcards, check if there is a match */
10051 /* remove backslashes for the remaining components only */
10052 if (*path_end != 0)
10053 backslash_halve(buf + len + 1);
10054 if (mch_getperm(buf) >= 0) /* add existing file */
10055 addfile(gap, buf, flags);
10056 }
10057 }
10058
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059# ifdef FEAT_MBYTE
10060 if (wn != NULL)
10061 {
10062 vim_free(p);
10063 ok = FindNextFileW(hFind, &wfb);
10064 }
10065 else
10066# endif
10067 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068
10069 /* If no more matches and no match was used, try expanding the name
10070 * itself. Finds the long name of a short filename. */
10071 if (!ok && matchname != NULL && gap->ga_len == start_len)
10072 {
10073 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 FindClose(hFind);
10075# ifdef FEAT_MBYTE
10076 if (wn != NULL)
10077 {
10078 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010079 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080 if (wn != NULL)
10081 hFind = FindFirstFileW(wn, &wfb);
10082 }
10083 if (wn == NULL)
10084# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010085 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 vim_free(matchname);
10088 matchname = NULL;
10089 }
10090 }
10091
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 FindClose(hFind);
10093# ifdef FEAT_MBYTE
10094 vim_free(wn);
10095# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010097 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 vim_free(matchname);
10099
10100 matches = gap->ga_len - start_len;
10101 if (matches > 0)
10102 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10103 sizeof(char_u *), pstrcmp);
10104 return matches;
10105}
10106
10107 int
10108mch_expandpath(
10109 garray_T *gap,
10110 char_u *path,
10111 int flags) /* EW_* flags */
10112{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010113 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010115# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116
Bram Moolenaar231334e2005-07-25 20:46:57 +000010117#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10118 || defined(PROTO)
10119/*
10120 * Unix style wildcard expansion code.
10121 * It's here because it's used both for Unix and Mac.
10122 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010123static int pstrcmp(const void *, const void *);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010124
10125 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010126pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010127{
10128 return (pathcmp(*(char **)a, *(char **)b, -1));
10129}
10130
10131/*
10132 * Recursively expand one path component into all matching files and/or
10133 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10134 * "path" has backslashes before chars that are not to be expanded, starting
10135 * at "path + wildoff".
10136 * Return the number of matches found.
10137 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10138 */
10139 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010140unix_expandpath(
10141 garray_T *gap,
10142 char_u *path,
10143 int wildoff,
10144 int flags, /* EW_* flags */
10145 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010146{
10147 char_u *buf;
10148 char_u *path_end;
10149 char_u *p, *s, *e;
10150 int start_len = gap->ga_len;
10151 char_u *pat;
10152 regmatch_T regmatch;
10153 int starts_with_dot;
10154 int matches;
10155 int len;
10156 int starstar = FALSE;
10157 static int stardepth = 0; /* depth for "**" expansion */
10158
10159 DIR *dirp;
10160 struct dirent *dp;
10161
10162 /* Expanding "**" may take a long time, check for CTRL-C. */
10163 if (stardepth > 0)
10164 {
10165 ui_breakcheck();
10166 if (got_int)
10167 return 0;
10168 }
10169
10170 /* make room for file name */
10171 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10172 if (buf == NULL)
10173 return 0;
10174
10175 /*
10176 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010177 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010178 * Copy it into "buf", including the preceding characters.
10179 */
10180 p = buf;
10181 s = buf;
10182 e = NULL;
10183 path_end = path;
10184 while (*path_end != NUL)
10185 {
10186 /* May ignore a wildcard that has a backslash before it; it will
10187 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10188 if (path_end >= path + wildoff && rem_backslash(path_end))
10189 *p++ = *path_end++;
10190 else if (*path_end == '/')
10191 {
10192 if (e != NULL)
10193 break;
10194 s = p + 1;
10195 }
10196 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010197 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010198 || (!p_fic && (flags & EW_ICASE)
10199 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010200 e = p;
10201#ifdef FEAT_MBYTE
10202 if (has_mbyte)
10203 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010204 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010205 STRNCPY(p, path_end, len);
10206 p += len;
10207 path_end += len;
10208 }
10209 else
10210#endif
10211 *p++ = *path_end++;
10212 }
10213 e = p;
10214 *e = NUL;
10215
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010216 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010217 /* Remove backslashes between "wildoff" and the start of the wildcard
10218 * component. */
10219 for (p = buf + wildoff; p < s; ++p)
10220 if (rem_backslash(p))
10221 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010222 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010223 --e;
10224 --s;
10225 }
10226
10227 /* Check for "**" between "s" and "e". */
10228 for (p = s; p < e; ++p)
10229 if (p[0] == '*' && p[1] == '*')
10230 starstar = TRUE;
10231
10232 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010233 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010234 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10235 if (pat == NULL)
10236 {
10237 vim_free(buf);
10238 return 0;
10239 }
10240
10241 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010242 if (flags & EW_ICASE)
10243 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10244 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010245 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010246 if (flags & (EW_NOERROR | EW_NOTWILD))
10247 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010248 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010249 if (flags & (EW_NOERROR | EW_NOTWILD))
10250 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010251 vim_free(pat);
10252
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010253 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010254 {
10255 vim_free(buf);
10256 return 0;
10257 }
10258
10259 /* If "**" is by itself, this is the first time we encounter it and more
10260 * is following then find matches without any directory. */
10261 if (!didstar && stardepth < 100 && starstar && e - s == 2
10262 && *path_end == '/')
10263 {
10264 STRCPY(s, path_end + 1);
10265 ++stardepth;
10266 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10267 --stardepth;
10268 }
10269
10270 /* open the directory for scanning */
10271 *s = NUL;
10272 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10273
10274 /* Find all matching entries */
10275 if (dirp != NULL)
10276 {
10277 for (;;)
10278 {
10279 dp = readdir(dirp);
10280 if (dp == NULL)
10281 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010282 if ((dp->d_name[0] != '.' || starts_with_dot
10283 || ((flags & EW_DODOT)
10284 && dp->d_name[1] != NUL
10285 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010286 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10287 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010288 || ((flags & EW_NOTWILD)
10289 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010290 {
10291 STRCPY(s, dp->d_name);
10292 len = STRLEN(buf);
10293
10294 if (starstar && stardepth < 100)
10295 {
10296 /* For "**" in the pattern first go deeper in the tree to
10297 * find matches. */
10298 STRCPY(buf + len, "/**");
10299 STRCPY(buf + len + 3, path_end);
10300 ++stardepth;
10301 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10302 --stardepth;
10303 }
10304
10305 STRCPY(buf + len, path_end);
10306 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10307 {
10308 /* need to expand another component of the path */
10309 /* remove backslashes for the remaining components only */
10310 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10311 }
10312 else
10313 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010314 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010315
Bram Moolenaar231334e2005-07-25 20:46:57 +000010316 /* no more wildcards, check if there is a match */
10317 /* remove backslashes for the remaining components only */
10318 if (*path_end != NUL)
10319 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010320 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010321 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010322 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010323 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010324#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010325 size_t precomp_len = STRLEN(buf)+1;
10326 char_u *precomp_buf =
10327 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010328
Bram Moolenaar231334e2005-07-25 20:46:57 +000010329 if (precomp_buf)
10330 {
10331 mch_memmove(buf, precomp_buf, precomp_len);
10332 vim_free(precomp_buf);
10333 }
10334#endif
10335 addfile(gap, buf, flags);
10336 }
10337 }
10338 }
10339 }
10340
10341 closedir(dirp);
10342 }
10343
10344 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010345 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010346
10347 matches = gap->ga_len - start_len;
10348 if (matches > 0)
10349 qsort(((char_u **)gap->ga_data) + start_len, matches,
10350 sizeof(char_u *), pstrcmp);
10351 return matches;
10352}
10353#endif
10354
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010355#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010356static int find_previous_pathsep(char_u *path, char_u **psep);
10357static int is_unique(char_u *maybe_unique, garray_T *gap, int i);
10358static void expand_path_option(char_u *curdir, garray_T *gap);
10359static char_u *get_path_cutoff(char_u *fname, garray_T *gap);
10360static void uniquefy_paths(garray_T *gap, char_u *pattern);
10361static int expand_in_path(garray_T *gap, char_u *pattern, int flags);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010362
10363/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010364 * Moves "*psep" back to the previous path separator in "path".
10365 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010366 */
10367 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010368find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010369{
10370 /* skip the current separator */
10371 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010372 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010373
10374 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010375 while (*psep > path)
10376 {
10377 if (vim_ispathsep(**psep))
10378 return OK;
10379 mb_ptr_back(path, *psep);
10380 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010381
10382 return FAIL;
10383}
10384
10385/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010386 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10387 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010388 */
10389 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010390is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010391{
10392 int j;
10393 int candidate_len;
10394 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010395 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010396 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010397
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010398 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010399 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010400 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010401 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010402
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010403 candidate_len = (int)STRLEN(maybe_unique);
10404 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010405 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010406 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010407
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010408 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010409 if (fnamecmp(maybe_unique, rival) == 0
10410 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010411 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010412 }
10413
Bram Moolenaar162bd912010-07-28 22:29:10 +020010414 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010415}
10416
10417/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010418 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010419 * paths are expanded to their equivalent fullpath. This includes the "."
10420 * (relative to current buffer directory) and empty path (relative to current
10421 * directory) notations.
10422 *
10423 * TODO: handle upward search (;) and path limiter (**N) notations by
10424 * expanding each into their equivalent path(s).
10425 */
10426 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010427expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010428{
10429 char_u *path_option = *curbuf->b_p_path == NUL
10430 ? p_path : curbuf->b_p_path;
10431 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010432 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010433 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010434
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010435 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010436 return;
10437
10438 while (*path_option != NUL)
10439 {
10440 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10441
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010442 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010443 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010444 /* Relative to current buffer:
10445 * "/path/file" + "." -> "/path/"
10446 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010447 if (curbuf->b_ffname == NULL)
10448 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010449 p = gettail(curbuf->b_ffname);
10450 len = (int)(p - curbuf->b_ffname);
10451 if (len + (int)STRLEN(buf) >= MAXPATHL)
10452 continue;
10453 if (buf[1] == NUL)
10454 buf[len] = NUL;
10455 else
10456 STRMOVE(buf + len, buf + 2);
10457 mch_memmove(buf, curbuf->b_ffname, len);
10458 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010459 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010460 else if (buf[0] == NUL)
10461 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010462 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010463 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010464 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010465 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010466 else if (!mch_isFullName(buf))
10467 {
10468 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010469 len = (int)STRLEN(curdir);
10470 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010471 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010472 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010473 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010474 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010475 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010476 }
10477
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010478 if (ga_grow(gap, 1) == FAIL)
10479 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010480
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010481# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010482 /* Avoid the path ending in a backslash, it fails when a comma is
10483 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010484 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010485 if (buf[len - 1] == '\\')
10486 buf[len - 1] = '/';
10487# endif
10488
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010489 p = vim_strsave(buf);
10490 if (p == NULL)
10491 break;
10492 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010493 }
10494
10495 vim_free(buf);
10496}
10497
10498/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010499 * Returns a pointer to the file or directory name in "fname" that matches the
10500 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010501 *
10502 * path: /foo/bar/baz
10503 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010504 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010505 */
10506 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010507get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010508{
10509 int i;
10510 int maxlen = 0;
10511 char_u **path_part = (char_u **)gap->ga_data;
10512 char_u *cutoff = NULL;
10513
10514 for (i = 0; i < gap->ga_len; i++)
10515 {
10516 int j = 0;
10517
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010518 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010519# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010520 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10521#endif
10522 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010523 j++;
10524 if (j > maxlen)
10525 {
10526 maxlen = j;
10527 cutoff = &fname[j];
10528 }
10529 }
10530
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010531 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010532 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010533 while (vim_ispathsep(*cutoff))
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010534 mb_ptr_adv(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010535
10536 return cutoff;
10537}
10538
10539/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010540 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10541 * that they are unique with respect to each other while conserving the part
10542 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010543 */
10544 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010545uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010546{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010547 int i;
10548 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010549 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010550 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010551 char_u *pat;
10552 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010553 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010554 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010555 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010556 char_u **in_curdir = NULL;
10557 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010558
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010559 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010560 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010561
10562 /*
10563 * We need to prepend a '*' at the beginning of file_pattern so that the
10564 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010565 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010566 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010567 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010568 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010569 if (file_pattern == NULL)
10570 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010571 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010572 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010573 STRCAT(file_pattern, pattern);
10574 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10575 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010576 if (pat == NULL)
10577 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010578
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010579 regmatch.rm_ic = TRUE; /* always ignore case */
10580 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10581 vim_free(pat);
10582 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010583 return;
10584
Bram Moolenaar162bd912010-07-28 22:29:10 +020010585 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010586 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010587 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010588 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010589
10590 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010591 if (in_curdir == NULL)
10592 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010593
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010594 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010595 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010596 char_u *path = fnames[i];
10597 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010598 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010599 char_u *pathsep_p;
10600 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010601
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010602 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010603 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010604 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010605 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010606 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010607
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010608 /* Shorten the filename while maintaining its uniqueness */
10609 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010610
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010611 /* Don't assume all files can be reached without path when search
10612 * pattern starts with star star slash, so only remove path_cutoff
10613 * when possible. */
10614 if (pattern[0] == '*' && pattern[1] == '*'
10615 && vim_ispathsep_nocolon(pattern[2])
10616 && path_cutoff != NULL
10617 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10618 && is_unique(path_cutoff, gap, i))
10619 {
10620 sort_again = TRUE;
10621 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10622 }
10623 else
10624 {
10625 /* Here all files can be reached without path, so get shortest
10626 * unique path. We start at the end of the path. */
10627 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010628
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010629 while (find_previous_pathsep(path, &pathsep_p))
10630 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10631 && is_unique(pathsep_p + 1, gap, i)
10632 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10633 {
10634 sort_again = TRUE;
10635 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10636 break;
10637 }
10638 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010639
10640 if (mch_isFullName(path))
10641 {
10642 /*
10643 * Last resort: shorten relative to curdir if possible.
10644 * 'possible' means:
10645 * 1. It is under the current directory.
10646 * 2. The result is actually shorter than the original.
10647 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010648 * Before curdir After
10649 * /foo/bar/file.txt /foo/bar ./file.txt
10650 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10651 * /file.txt / /file.txt
10652 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010653 */
10654 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010655 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010656#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010657 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010658 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010659 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010660 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010661 && !vim_ispathsep(*short_name)
10662#endif
10663 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010664 {
10665 STRCPY(path, ".");
10666 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010667 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010668 }
10669 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010670 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010671 }
10672
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010673 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010674 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010675 {
10676 char_u *rel_path;
10677 char_u *path = in_curdir[i];
10678
10679 if (path == NULL)
10680 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010681
10682 /* If the {filename} is not unique, change it to ./{filename}.
10683 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010684 short_name = shorten_fname(path, curdir);
10685 if (short_name == NULL)
10686 short_name = path;
10687 if (is_unique(short_name, gap, i))
10688 {
10689 STRCPY(fnames[i], short_name);
10690 continue;
10691 }
10692
10693 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10694 if (rel_path == NULL)
10695 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010696 STRCPY(rel_path, ".");
10697 add_pathsep(rel_path);
10698 STRCAT(rel_path, short_name);
10699
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010700 vim_free(fnames[i]);
10701 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010702 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010703 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010704 }
10705
Bram Moolenaar162bd912010-07-28 22:29:10 +020010706theend:
10707 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010708 if (in_curdir != NULL)
10709 {
10710 for (i = 0; i < gap->ga_len; i++)
10711 vim_free(in_curdir[i]);
10712 vim_free(in_curdir);
10713 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010714 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010715 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010716
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010717 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010718 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010719}
10720
10721/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010722 * Calls globpath() with 'path' values for the given pattern and stores the
10723 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010724 * Returns the total number of matches.
10725 */
10726 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010727expand_in_path(
10728 garray_T *gap,
10729 char_u *pattern,
10730 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010731{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010732 char_u *curdir;
10733 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010734 char_u *paths = NULL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010735
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010736 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010737 return 0;
10738 mch_dirname(curdir, MAXPATHL);
10739
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010740 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010741 expand_path_option(curdir, &path_ga);
10742 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010743 if (path_ga.ga_len == 0)
10744 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010745
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010746 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010747 ga_clear_strings(&path_ga);
10748 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010749 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010750
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010751 globpath(paths, pattern, gap, (flags & EW_ICASE) ? WILD_ICASE : 0);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010752 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010753
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010754 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010755}
10756#endif
10757
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010758#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10759/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010760 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10761 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010762 */
10763 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010764remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010765{
10766 int i;
10767 int j;
10768 char_u **fnames = (char_u **)gap->ga_data;
10769
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010770 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010771 for (i = gap->ga_len - 1; i > 0; --i)
10772 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10773 {
10774 vim_free(fnames[i]);
10775 for (j = i + 1; j < gap->ga_len; ++j)
10776 fnames[j - 1] = fnames[j];
10777 --gap->ga_len;
10778 }
10779}
10780#endif
10781
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010782static int has_env_var(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010783
10784/*
10785 * Return TRUE if "p" contains what looks like an environment variable.
10786 * Allowing for escaping.
10787 */
10788 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010789has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010790{
10791 for ( ; *p; mb_ptr_adv(p))
10792 {
10793 if (*p == '\\' && p[1] != NUL)
10794 ++p;
10795 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010796#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010797 "$%"
10798#else
10799 "$"
10800#endif
10801 , *p) != NULL)
10802 return TRUE;
10803 }
10804 return FALSE;
10805}
10806
10807#ifdef SPECIAL_WILDCHAR
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010808static int has_special_wildchar(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010809
10810/*
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010811 * Return TRUE if "p" contains a special wildcard character, one that Vim
10812 * cannot expand, requires using a shell.
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010813 */
10814 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010815has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010816{
10817 for ( ; *p; mb_ptr_adv(p))
10818 {
Bram Moolenaar187a4f22017-02-23 17:07:14 +010010819 /* Allow for escaping. */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010820 if (*p == '\\' && p[1] != NUL)
10821 ++p;
10822 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10823 return TRUE;
10824 }
10825 return FALSE;
10826}
10827#endif
10828
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829/*
10830 * Generic wildcard expansion code.
10831 *
10832 * Characters in "pat" that should not be expanded must be preceded with a
10833 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10834 *
10835 * Return FAIL when no single file was found. In this case "num_file" is not
10836 * set, and "file" may contain an error message.
10837 * Return OK when some files found. "num_file" is set to the number of
10838 * matches, "file" to the array of matches. Call FreeWild() later.
10839 */
10840 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010841gen_expand_wildcards(
10842 int num_pat, /* number of input patterns */
10843 char_u **pat, /* array of input patterns */
10844 int *num_file, /* resulting number of files */
10845 char_u ***file, /* array of resulting files */
10846 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847{
10848 int i;
10849 garray_T ga;
10850 char_u *p;
10851 static int recursive = FALSE;
10852 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020010853 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010854#if defined(FEAT_SEARCHPATH)
10855 int did_expand_in_path = FALSE;
10856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857
10858 /*
10859 * expand_env() is called to expand things like "~user". If this fails,
10860 * it calls ExpandOne(), which brings us back here. In this case, always
10861 * call the machine specific expansion function, if possible. Otherwise,
10862 * return FAIL.
10863 */
10864 if (recursive)
10865#ifdef SPECIAL_WILDCHAR
10866 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10867#else
10868 return FAIL;
10869#endif
10870
10871#ifdef SPECIAL_WILDCHAR
10872 /*
10873 * If there are any special wildcard characters which we cannot handle
10874 * here, call machine specific function for all the expansion. This
10875 * avoids starting the shell for each argument separately.
10876 * For `=expr` do use the internal function.
10877 */
10878 for (i = 0; i < num_pat; i++)
10879 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010880 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881# ifdef VIM_BACKTICK
10882 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10883# endif
10884 )
10885 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10886 }
10887#endif
10888
10889 recursive = TRUE;
10890
10891 /*
10892 * The matching file names are stored in a growarray. Init it empty.
10893 */
10894 ga_init2(&ga, (int)sizeof(char_u *), 30);
10895
10896 for (i = 0; i < num_pat; ++i)
10897 {
10898 add_pat = -1;
10899 p = pat[i];
10900
10901#ifdef VIM_BACKTICK
10902 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020010903 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020010905 if (add_pat == -1)
10906 retval = FAIL;
10907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908 else
10909#endif
10910 {
10911 /*
10912 * First expand environment variables, "~/" and "~user/".
10913 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010914 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010916 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917 if (p == NULL)
10918 p = pat[i];
10919#ifdef UNIX
10920 /*
10921 * On Unix, if expand_env() can't expand an environment
10922 * variable, use the shell to do that. Discard previously
10923 * found file names and start all over again.
10924 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010925 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926 {
10927 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010928 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020010930 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931 recursive = FALSE;
10932 return i;
10933 }
10934#endif
10935 }
10936
10937 /*
10938 * If there are wildcards: Expand file names and add each match to
10939 * the list. If there is no match, and EW_NOTFOUND is given, add
10940 * the pattern.
10941 * If there are no wildcards: Add the file name if it exists or
10942 * when EW_NOTFOUND is given.
10943 */
10944 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010945 {
10946#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010947 if ((flags & EW_PATH)
10948 && !mch_isFullName(p)
10949 && !(p[0] == '.'
10950 && (vim_ispathsep(p[1])
10951 || (p[1] == '.' && vim_ispathsep(p[2]))))
10952 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010953 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010954 /* :find completion where 'path' is used.
10955 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010956 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010957 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010958 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010959 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010960 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010961 else
10962#endif
10963 add_pat = mch_expandpath(&ga, p, flags);
10964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 }
10966
10967 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
10968 {
10969 char_u *t = backslash_halve_save(p);
10970
10971#if defined(MACOS_CLASSIC)
10972 slash_to_colon(t);
10973#endif
10974 /* When EW_NOTFOUND is used, always add files and dirs. Makes
10975 * "vim c:/" work. */
10976 if (flags & EW_NOTFOUND)
10977 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020010978 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979 addfile(&ga, t, flags);
10980 vim_free(t);
10981 }
10982
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010983#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010984 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010985 uniquefy_paths(&ga, p);
10986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987 if (p != pat[i])
10988 vim_free(p);
10989 }
10990
10991 *num_file = ga.ga_len;
10992 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
10993
10994 recursive = FALSE;
10995
Bram Moolenaar336bd622016-01-17 18:23:58 +010010996 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997}
10998
10999# ifdef VIM_BACKTICK
11000
11001/*
11002 * Return TRUE if we can expand this backtick thing here.
11003 */
11004 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011005vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006{
11007 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
11008}
11009
11010/*
11011 * Expand an item in `backticks` by executing it as a command.
11012 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020011013 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014 */
11015 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011016expand_backtick(
11017 garray_T *gap,
11018 char_u *pat,
11019 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020{
11021 char_u *p;
11022 char_u *cmd;
11023 char_u *buffer;
11024 int cnt = 0;
11025 int i;
11026
11027 /* Create the command: lop off the backticks. */
11028 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
11029 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011030 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031
11032#ifdef FEAT_EVAL
11033 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011034 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035 else
11036#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011037 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011038 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039 vim_free(cmd);
11040 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011041 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042
11043 cmd = buffer;
11044 while (*cmd != NUL)
11045 {
11046 cmd = skipwhite(cmd); /* skip over white space */
11047 p = cmd;
11048 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
11049 ++p;
11050 /* add an entry if it is not empty */
11051 if (p > cmd)
11052 {
11053 i = *p;
11054 *p = NUL;
11055 addfile(gap, cmd, flags);
11056 *p = i;
11057 ++cnt;
11058 }
11059 cmd = p;
11060 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
11061 ++cmd;
11062 }
11063
11064 vim_free(buffer);
11065 return cnt;
11066}
11067# endif /* VIM_BACKTICK */
11068
11069/*
11070 * Add a file to a file list. Accepted flags:
11071 * EW_DIR add directories
11072 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011073 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074 * EW_NOTFOUND add even when it doesn't exist
11075 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011076 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077 */
11078 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011079addfile(
11080 garray_T *gap,
11081 char_u *f, /* filename */
11082 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083{
11084 char_u *p;
11085 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011086 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011087
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011088 /* if the file/dir/link doesn't exist, may not add it */
11089 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011090 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091 return;
11092
11093#ifdef FNAME_ILLEGAL
11094 /* if the file/dir contains illegal characters, don't add it */
11095 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11096 return;
11097#endif
11098
11099 isdir = mch_isdir(f);
11100 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11101 return;
11102
Bram Moolenaarb5971142015-03-21 17:32:19 +010011103 /* If the file isn't executable, may not add it. Do accept directories.
11104 * When invoked from expand_shellcmd() do not use $PATH. */
11105 if (!isdir && (flags & EW_EXEC)
11106 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011107 return;
11108
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109 /* Make room for another item in the file list. */
11110 if (ga_grow(gap, 1) == FAIL)
11111 return;
11112
11113 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11114 if (p == NULL)
11115 return;
11116
11117 STRCPY(p, f);
11118#ifdef BACKSLASH_IN_FILENAME
11119 slash_adjust(p);
11120#endif
11121 /*
11122 * Append a slash or backslash after directory names if none is present.
11123 */
11124#ifndef DONT_ADD_PATHSEP_TO_DIR
11125 if (isdir && (flags & EW_ADDSLASH))
11126 add_pathsep(p);
11127#endif
11128 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129}
11130#endif /* !NO_EXPANDPATH */
11131
11132#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11133
11134#ifndef SEEK_SET
11135# define SEEK_SET 0
11136#endif
11137#ifndef SEEK_END
11138# define SEEK_END 2
11139#endif
11140
11141/*
11142 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011143 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11144 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145 * Returns an allocated string, or NULL for error.
11146 */
11147 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011148get_cmd_output(
11149 char_u *cmd,
11150 char_u *infile, /* optional input file name */
11151 int flags, /* can be SHELL_SILENT */
11152 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153{
11154 char_u *tempname;
11155 char_u *command;
11156 char_u *buffer = NULL;
11157 int len;
11158 int i = 0;
11159 FILE *fd;
11160
11161 if (check_restricted() || check_secure())
11162 return NULL;
11163
11164 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011165 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166 {
11167 EMSG(_(e_notmp));
11168 return NULL;
11169 }
11170
11171 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011172 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173 if (command == NULL)
11174 goto done;
11175
11176 /*
11177 * Call the shell to execute the command (errors are ignored).
11178 * Don't check timestamps here.
11179 */
11180 ++no_check_timestamps;
11181 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11182 --no_check_timestamps;
11183
11184 vim_free(command);
11185
11186 /*
11187 * read the names from the file into memory
11188 */
11189# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011190 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011191 fd = mch_fopen((char *)tempname, "r");
11192# else
11193 fd = mch_fopen((char *)tempname, READBIN);
11194# endif
11195
11196 if (fd == NULL)
11197 {
11198 EMSG2(_(e_notopen), tempname);
11199 goto done;
11200 }
11201
11202 fseek(fd, 0L, SEEK_END);
11203 len = ftell(fd); /* get size of temp file */
11204 fseek(fd, 0L, SEEK_SET);
11205
11206 buffer = alloc(len + 1);
11207 if (buffer != NULL)
11208 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11209 fclose(fd);
11210 mch_remove(tempname);
11211 if (buffer == NULL)
11212 goto done;
11213#ifdef VMS
11214 len = i; /* VMS doesn't give us what we asked for... */
11215#endif
11216 if (i != len)
11217 {
11218 EMSG2(_(e_notread), tempname);
11219 vim_free(buffer);
11220 buffer = NULL;
11221 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011222 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011223 {
11224 /* Change NUL into SOH, otherwise the string is truncated. */
11225 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011226 if (buffer[i] == NUL)
11227 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011228
Bram Moolenaar162bd912010-07-28 22:29:10 +020011229 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011230 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011231 else
11232 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233
11234done:
11235 vim_free(tempname);
11236 return buffer;
11237}
11238#endif
11239
11240/*
11241 * Free the list of files returned by expand_wildcards() or other expansion
11242 * functions.
11243 */
11244 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011245FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011247 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249 while (count--)
11250 vim_free(files[count]);
11251 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011252}
11253
11254/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011255 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256 * Don't do this when still processing a command or a mapping.
11257 * Don't do this when inside a ":normal" command.
11258 */
11259 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011260goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261{
11262 return (p_im && stuff_empty() && typebuf_typed());
11263}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011264
11265/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011266 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011267 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11268 * - Remove any argument. E.g., "csh -f" -> "csh".
11269 * But don't allow a space in the path, so that this works:
11270 * "/usr/bin/csh --rcfile ~/.cshrc"
11271 * But don't do that for Windows, it's common to have a space in the path.
11272 */
11273 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011274get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011275{
11276 char_u *p;
11277
11278#ifdef WIN3264
11279 p = gettail(p_sh);
11280 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11281#else
11282 p = skiptowhite(p_sh);
11283 if (*p == NUL)
11284 {
11285 /* No white space, use the tail. */
11286 p = vim_strsave(gettail(p_sh));
11287 }
11288 else
11289 {
11290 char_u *p1, *p2;
11291
11292 /* Find the last path separator before the space. */
11293 p1 = p_sh;
11294 for (p2 = p_sh; p2 < p; mb_ptr_adv(p2))
11295 if (vim_ispathsep(*p2))
11296 p1 = p2 + 1;
11297 p = vim_strnsave(p1, (int)(p - p1));
11298 }
11299#endif
11300 return p;
11301}