blob: f95c3fe477da1d38b16b06e58a4e4bf7b08cf949 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * misc1.c: functions that didn't seem to fit elsewhere
12 */
13
14#include "vim.h"
15#include "version.h"
16
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010017static char_u *vim_version_dir(char_u *vimdir);
18static char_u *remove_tail(char_u *p, char_u *pend, char_u *name);
Bram Moolenaar06ae70d2013-06-17 19:26:36 +020019#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010020static void init_users(void);
Bram Moolenaar06ae70d2013-06-17 19:26:36 +020021#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static int copy_indent(int size, char_u *src);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023
Bram Moolenaar24305862012-08-15 14:05:05 +020024/* All user names (for ~user completion as done by shell). */
25#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
26static garray_T ga_users;
27#endif
28
Bram Moolenaar071d4272004-06-13 20:20:40 +000029/*
30 * Count the size (in window cells) of the indent in the current line.
31 */
32 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010033get_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000034{
Bram Moolenaar597a4222014-06-25 14:39:50 +020035 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000036}
37
38/*
39 * Count the size (in window cells) of the indent in line "lnum".
40 */
41 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010042get_indent_lnum(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000043{
Bram Moolenaar597a4222014-06-25 14:39:50 +020044 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000045}
46
47#if defined(FEAT_FOLDING) || defined(PROTO)
48/*
49 * Count the size (in window cells) of the indent in line "lnum" of buffer
50 * "buf".
51 */
52 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010053get_indent_buf(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000054{
Bram Moolenaar597a4222014-06-25 14:39:50 +020055 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000056}
57#endif
58
59/*
60 * count the size (in window cells) of the indent in line "ptr", with
61 * 'tabstop' at "ts"
62 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +000063 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010064get_indent_str(
65 char_u *ptr,
66 int ts,
67 int list) /* if TRUE, count only screen size for tabs */
Bram Moolenaar071d4272004-06-13 20:20:40 +000068{
69 int count = 0;
70
71 for ( ; *ptr; ++ptr)
72 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020073 if (*ptr == TAB)
74 {
75 if (!list || lcs_tab1) /* count a tab for what it is worth */
76 count += ts - (count % ts);
77 else
Bram Moolenaare4df1642014-08-29 12:58:44 +020078 /* In list mode, when tab is not set, count screen char width
79 * for Tab, displays: ^I */
Bram Moolenaar597a4222014-06-25 14:39:50 +020080 count += ptr2cells(ptr);
81 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000082 else if (*ptr == ' ')
83 ++count; /* count a space for one */
84 else
85 break;
86 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +000087 return count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000088}
89
90/*
91 * Set the indent of the current line.
92 * Leaves the cursor on the first non-blank in the line.
93 * Caller must take care of undo.
94 * "flags":
95 * SIN_CHANGED: call changed_bytes() if the line was changed.
96 * SIN_INSERT: insert the indent in front of the line.
97 * SIN_UNDO: save line for undo before changing it.
98 * Returns TRUE if the line was changed.
99 */
100 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100101set_indent(
102 int size, /* measured in spaces */
103 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104{
105 char_u *p;
106 char_u *newline;
107 char_u *oldline;
108 char_u *s;
109 int todo;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000110 int ind_len; /* measured in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111 int line_len;
112 int doit = FALSE;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000113 int ind_done = 0; /* measured in spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 int tab_pad;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000115 int retval = FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000116 int orig_char_len = -1; /* number of initial whitespace chars when
Bram Moolenaar5002c292007-07-24 13:26:15 +0000117 'et' and 'pi' are both set */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119 /*
120 * First check if there is anything to do and compute the number of
121 * characters needed for the indent.
122 */
123 todo = size;
124 ind_len = 0;
125 p = oldline = ml_get_curline();
126
127 /* Calculate the buffer size for the new indent, and check to see if it
128 * isn't already set */
129
Bram Moolenaar5002c292007-07-24 13:26:15 +0000130 /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and
131 * 'preserveindent' are set count the number of characters at the
132 * beginning of the line to be copied */
133 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 {
135 /* If 'preserveindent' is set then reuse as much as possible of
136 * the existing indent structure for the new indent */
137 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
138 {
139 ind_done = 0;
140
141 /* count as many characters as we can use */
142 while (todo > 0 && vim_iswhite(*p))
143 {
144 if (*p == TAB)
145 {
146 tab_pad = (int)curbuf->b_p_ts
147 - (ind_done % (int)curbuf->b_p_ts);
148 /* stop if this tab will overshoot the target */
149 if (todo < tab_pad)
150 break;
151 todo -= tab_pad;
152 ++ind_len;
153 ind_done += tab_pad;
154 }
155 else
156 {
157 --todo;
158 ++ind_len;
159 ++ind_done;
160 }
161 ++p;
162 }
163
Bram Moolenaar5002c292007-07-24 13:26:15 +0000164 /* Set initial number of whitespace chars to copy if we are
165 * preserving indent but expandtab is set */
166 if (curbuf->b_p_et)
167 orig_char_len = ind_len;
168
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 /* Fill to next tabstop with a tab, if possible */
170 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000171 if (todo >= tab_pad && orig_char_len == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 {
173 doit = TRUE;
174 todo -= tab_pad;
175 ++ind_len;
176 /* ind_done += tab_pad; */
177 }
178 }
179
180 /* count tabs required for indent */
181 while (todo >= (int)curbuf->b_p_ts)
182 {
183 if (*p != TAB)
184 doit = TRUE;
185 else
186 ++p;
187 todo -= (int)curbuf->b_p_ts;
188 ++ind_len;
189 /* ind_done += (int)curbuf->b_p_ts; */
190 }
191 }
192 /* count spaces required for indent */
193 while (todo > 0)
194 {
195 if (*p != ' ')
196 doit = TRUE;
197 else
198 ++p;
199 --todo;
200 ++ind_len;
201 /* ++ind_done; */
202 }
203
204 /* Return if the indent is OK already. */
205 if (!doit && !vim_iswhite(*p) && !(flags & SIN_INSERT))
206 return FALSE;
207
208 /* Allocate memory for the new line. */
209 if (flags & SIN_INSERT)
210 p = oldline;
211 else
212 p = skipwhite(p);
213 line_len = (int)STRLEN(p) + 1;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000214
215 /* If 'preserveindent' and 'expandtab' are both set keep the original
216 * characters and allocate accordingly. We will fill the rest with spaces
217 * after the if (!curbuf->b_p_et) below. */
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000218 if (orig_char_len != -1)
Bram Moolenaar5002c292007-07-24 13:26:15 +0000219 {
220 newline = alloc(orig_char_len + size - ind_done + line_len);
221 if (newline == NULL)
222 return FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000223 todo = size - ind_done;
224 ind_len = orig_char_len + todo; /* Set total length of indent in
225 * characters, which may have been
226 * undercounted until now */
Bram Moolenaar5002c292007-07-24 13:26:15 +0000227 p = oldline;
228 s = newline;
229 while (orig_char_len > 0)
230 {
231 *s++ = *p++;
232 orig_char_len--;
233 }
Bram Moolenaar913626c2008-01-03 11:43:42 +0000234
Bram Moolenaar5002c292007-07-24 13:26:15 +0000235 /* Skip over any additional white space (useful when newindent is less
236 * than old) */
237 while (vim_iswhite(*p))
Bram Moolenaar913626c2008-01-03 11:43:42 +0000238 ++p;
Bram Moolenaarcc00b952007-08-11 12:32:57 +0000239
Bram Moolenaar5002c292007-07-24 13:26:15 +0000240 }
241 else
242 {
243 todo = size;
244 newline = alloc(ind_len + line_len);
245 if (newline == NULL)
246 return FALSE;
247 s = newline;
248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249
250 /* Put the characters in the new line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 /* if 'expandtab' isn't set: use TABs */
252 if (!curbuf->b_p_et)
253 {
254 /* If 'preserveindent' is set then reuse as much as possible of
255 * the existing indent structure for the new indent */
256 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
257 {
258 p = oldline;
259 ind_done = 0;
260
261 while (todo > 0 && vim_iswhite(*p))
262 {
263 if (*p == TAB)
264 {
265 tab_pad = (int)curbuf->b_p_ts
266 - (ind_done % (int)curbuf->b_p_ts);
267 /* stop if this tab will overshoot the target */
268 if (todo < tab_pad)
269 break;
270 todo -= tab_pad;
271 ind_done += tab_pad;
272 }
273 else
274 {
275 --todo;
276 ++ind_done;
277 }
278 *s++ = *p++;
279 }
280
281 /* Fill to next tabstop with a tab, if possible */
282 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
283 if (todo >= tab_pad)
284 {
285 *s++ = TAB;
286 todo -= tab_pad;
287 }
288
289 p = skipwhite(p);
290 }
291
292 while (todo >= (int)curbuf->b_p_ts)
293 {
294 *s++ = TAB;
295 todo -= (int)curbuf->b_p_ts;
296 }
297 }
298 while (todo > 0)
299 {
300 *s++ = ' ';
301 --todo;
302 }
303 mch_memmove(s, p, (size_t)line_len);
304
305 /* Replace the line (unless undo fails). */
306 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
307 {
308 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
309 if (flags & SIN_CHANGED)
310 changed_bytes(curwin->w_cursor.lnum, 0);
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200311 /* Correct saved cursor position if it is in this line. */
312 if (saved_cursor.lnum == curwin->w_cursor.lnum)
313 {
314 if (saved_cursor.col >= (colnr_T)(p - oldline))
315 /* cursor was after the indent, adjust for the number of
316 * bytes added/removed */
317 saved_cursor.col += ind_len - (colnr_T)(p - oldline);
318 else if (saved_cursor.col >= (colnr_T)(s - newline))
319 /* cursor was in the indent, and is now after it, put it back
320 * at the start of the indent (replacing spaces with TAB) */
321 saved_cursor.col = (colnr_T)(s - newline);
322 }
Bram Moolenaar5409c052005-03-18 20:27:04 +0000323 retval = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 }
325 else
326 vim_free(newline);
327
328 curwin->w_cursor.col = ind_len;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000329 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330}
331
332/*
333 * Copy the indent from ptr to the current line (and fill to size)
334 * Leaves the cursor on the first non-blank in the line.
335 * Returns TRUE if the line was changed.
336 */
337 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100338copy_indent(int size, char_u *src)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339{
340 char_u *p = NULL;
341 char_u *line = NULL;
342 char_u *s;
343 int todo;
344 int ind_len;
345 int line_len = 0;
346 int tab_pad;
347 int ind_done;
348 int round;
349
350 /* Round 1: compute the number of characters needed for the indent
351 * Round 2: copy the characters. */
352 for (round = 1; round <= 2; ++round)
353 {
354 todo = size;
355 ind_len = 0;
356 ind_done = 0;
357 s = src;
358
359 /* Count/copy the usable portion of the source line */
360 while (todo > 0 && vim_iswhite(*s))
361 {
362 if (*s == TAB)
363 {
364 tab_pad = (int)curbuf->b_p_ts
365 - (ind_done % (int)curbuf->b_p_ts);
366 /* Stop if this tab will overshoot the target */
367 if (todo < tab_pad)
368 break;
369 todo -= tab_pad;
370 ind_done += tab_pad;
371 }
372 else
373 {
374 --todo;
375 ++ind_done;
376 }
377 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000378 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 *p++ = *s;
380 ++s;
381 }
382
383 /* Fill to next tabstop with a tab, if possible */
384 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200385 if (todo >= tab_pad && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 {
387 todo -= tab_pad;
388 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000389 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 *p++ = TAB;
391 }
392
393 /* Add tabs required for indent */
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200394 while (todo >= (int)curbuf->b_p_ts && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 {
396 todo -= (int)curbuf->b_p_ts;
397 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000398 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 *p++ = TAB;
400 }
401
402 /* Count/add spaces required for indent */
403 while (todo > 0)
404 {
405 --todo;
406 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000407 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 *p++ = ' ';
409 }
410
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000411 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 {
413 /* Allocate memory for the result: the copied indent, new indent
414 * and the rest of the line. */
415 line_len = (int)STRLEN(ml_get_curline()) + 1;
416 line = alloc(ind_len + line_len);
417 if (line == NULL)
418 return FALSE;
419 p = line;
420 }
421 }
422
423 /* Append the original line */
424 mch_memmove(p, ml_get_curline(), (size_t)line_len);
425
426 /* Replace the line */
427 ml_replace(curwin->w_cursor.lnum, line, FALSE);
428
429 /* Put the cursor after the indent. */
430 curwin->w_cursor.col = ind_len;
431 return TRUE;
432}
433
434/*
435 * Return the indent of the current line after a number. Return -1 if no
436 * number was found. Used for 'n' in 'formatoptions': numbered list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000437 * Since a pattern is used it can actually handle more than numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 */
439 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100440get_number_indent(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 colnr_T col;
443 pos_T pos;
444
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200445 regmatch_T regmatch;
446 int lead_len = 0; /* length of comment leader */
447
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 if (lnum > curbuf->b_ml.ml_line_count)
449 return -1;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000450 pos.lnum = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200451
452#ifdef FEAT_COMMENTS
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200453 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
454 if ((State & INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200455 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000456#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200457 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
458 if (regmatch.regprog != NULL)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200459 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200460 regmatch.rm_ic = FALSE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200461
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200462 /* vim_regexec() expects a pointer to a line. This lets us
463 * start matching for the flp beyond any comment leader... */
464 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200465 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200466 pos.lnum = lnum;
467 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200468#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200469 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200470#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200471 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200472 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200473 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000474
475 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 getvcol(curwin, &pos, &col, NULL, NULL);
478 return (int)col;
479}
480
Bram Moolenaar597a4222014-06-25 14:39:50 +0200481#if defined(FEAT_LINEBREAK) || defined(PROTO)
482/*
483 * Return appropriate space number for breakindent, taking influencing
484 * parameters into account. Window must be specified, since it is not
485 * necessarily always the current one.
486 */
487 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100488get_breakindent_win(
489 win_T *wp,
490 char_u *line) /* start of the line */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200491{
492 static int prev_indent = 0; /* cached indent value */
493 static long prev_ts = 0L; /* cached tabstop value */
494 static char_u *prev_line = NULL; /* cached pointer to line */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200495 static int prev_tick = 0; /* changedtick of cached value */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200496 int bri = 0;
497 /* window width minus window margin space, i.e. what rests for text */
498 const int eff_wwidth = W_WIDTH(wp)
499 - ((wp->w_p_nu || wp->w_p_rnu)
500 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
501 ? number_width(wp) + 1 : 0);
502
503 /* used cached indent, unless pointer or 'tabstop' changed */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200504 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
505 || prev_tick != wp->w_buffer->b_changedtick)
Bram Moolenaar597a4222014-06-25 14:39:50 +0200506 {
507 prev_line = line;
508 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaara40aa762014-06-25 22:55:38 +0200509 prev_tick = wp->w_buffer->b_changedtick;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200510 prev_indent = get_indent_str(line,
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200511 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
Bram Moolenaar597a4222014-06-25 14:39:50 +0200512 }
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200513 bri = prev_indent + wp->w_p_brishift;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200514
515 /* indent minus the length of the showbreak string */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200516 if (wp->w_p_brisbr)
517 bri -= vim_strsize(p_sbr);
518
519 /* Add offset for number column, if 'n' is in 'cpoptions' */
520 bri += win_col_off2(wp);
521
522 /* never indent past left window margin */
523 if (bri < 0)
524 bri = 0;
525 /* always leave at least bri_min characters on the left,
526 * if text width is sufficient */
527 else if (bri > eff_wwidth - wp->w_p_brimin)
528 bri = (eff_wwidth - wp->w_p_brimin < 0)
529 ? 0 : eff_wwidth - wp->w_p_brimin;
530
531 return bri;
532}
533#endif
534
535
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
537
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100538static int cin_is_cinword(char_u *line);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539
540/*
541 * Return TRUE if the string "line" starts with a word from 'cinwords'.
542 */
543 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100544cin_is_cinword(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545{
546 char_u *cinw;
547 char_u *cinw_buf;
548 int cinw_len;
549 int retval = FALSE;
550 int len;
551
552 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1;
553 cinw_buf = alloc((unsigned)cinw_len);
554 if (cinw_buf != NULL)
555 {
556 line = skipwhite(line);
557 for (cinw = curbuf->b_p_cinw; *cinw; )
558 {
559 len = copy_option_part(&cinw, cinw_buf, cinw_len, ",");
560 if (STRNCMP(line, cinw_buf, len) == 0
561 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1])))
562 {
563 retval = TRUE;
564 break;
565 }
566 }
567 vim_free(cinw_buf);
568 }
569 return retval;
570}
571#endif
572
573/*
574 * open_line: Add a new line below or above the current line.
575 *
576 * For VREPLACE mode, we only add a new line when we get to the end of the
577 * file, otherwise we just start replacing the next line.
578 *
579 * Caller must take care of undo. Since VREPLACE may affect any number of
580 * lines however, it may call u_save_cursor() again when starting to change a
581 * new line.
582 * "flags": OPENLINE_DELSPACES delete spaces after cursor
583 * OPENLINE_DO_COM format comments
584 * OPENLINE_KEEPTRAIL keep trailing spaces
585 * OPENLINE_MARKFIX adjust mark positions after the line break
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200586 * OPENLINE_COM_LIST format comments with list or 2nd line indent
587 *
588 * "second_line_indent": indent for after ^^D in Insert mode or if flag
589 * OPENLINE_COM_LIST
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 *
591 * Return TRUE for success, FALSE for failure
592 */
593 int
Bram Moolenaar9b578142016-01-30 19:39:49 +0100594open_line(
595 int dir, /* FORWARD or BACKWARD */
596 int flags,
597 int second_line_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598{
599 char_u *saved_line; /* copy of the original line */
600 char_u *next_line = NULL; /* copy of the next line */
601 char_u *p_extra = NULL; /* what goes to next line */
602 int less_cols = 0; /* less columns for mark in new line */
603 int less_cols_off = 0; /* columns to skip for mark adjust */
604 pos_T old_cursor; /* old cursor position */
605 int newcol = 0; /* new cursor column */
606 int newindent = 0; /* auto-indent of the new line */
607 int n;
608 int trunc_line = FALSE; /* truncate current line afterwards */
609 int retval = FALSE; /* return value, default is FAIL */
610#ifdef FEAT_COMMENTS
611 int extra_len = 0; /* length of p_extra string */
612 int lead_len; /* length of comment leader */
613 char_u *lead_flags; /* position in 'comments' for comment leader */
614 char_u *leader = NULL; /* copy of comment leader */
615#endif
616 char_u *allocated = NULL; /* allocated memory */
617#if defined(FEAT_SMARTINDENT) || defined(FEAT_VREPLACE) || defined(FEAT_LISP) \
618 || defined(FEAT_CINDENT) || defined(FEAT_COMMENTS)
619 char_u *p;
620#endif
621 int saved_char = NUL; /* init for GCC */
622#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
623 pos_T *pos;
624#endif
625#ifdef FEAT_SMARTINDENT
626 int do_si = (!p_paste && curbuf->b_p_si
627# ifdef FEAT_CINDENT
628 && !curbuf->b_p_cin
629# endif
630 );
631 int no_si = FALSE; /* reset did_si afterwards */
632 int first_char = NUL; /* init for GCC */
633#endif
634#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
635 int vreplace_mode;
636#endif
637 int did_append; /* appended a new line */
638 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */
639
640 /*
641 * make a copy of the current line so we can mess with it
642 */
643 saved_line = vim_strsave(ml_get_curline());
644 if (saved_line == NULL) /* out of memory! */
645 return FALSE;
646
647#ifdef FEAT_VREPLACE
648 if (State & VREPLACE_FLAG)
649 {
650 /*
651 * With VREPLACE we make a copy of the next line, which we will be
652 * starting to replace. First make the new line empty and let vim play
653 * with the indenting and comment leader to its heart's content. Then
654 * we grab what it ended up putting on the new line, put back the
655 * original line, and call ins_char() to put each new character onto
656 * the line, replacing what was there before and pushing the right
657 * stuff onto the replace stack. -- webb.
658 */
659 if (curwin->w_cursor.lnum < orig_line_count)
660 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
661 else
662 next_line = vim_strsave((char_u *)"");
663 if (next_line == NULL) /* out of memory! */
664 goto theend;
665
666 /*
667 * In VREPLACE mode, a NL replaces the rest of the line, and starts
668 * replacing the next line, so push all of the characters left on the
669 * line onto the replace stack. We'll push any other characters that
670 * might be replaced at the start of the next line (due to autoindent
671 * etc) a bit later.
672 */
673 replace_push(NUL); /* Call twice because BS over NL expects it */
674 replace_push(NUL);
675 p = saved_line + curwin->w_cursor.col;
676 while (*p != NUL)
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000677 {
678#ifdef FEAT_MBYTE
679 if (has_mbyte)
680 p += replace_push_mb(p);
681 else
682#endif
683 replace_push(*p++);
684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 saved_line[curwin->w_cursor.col] = NUL;
686 }
687#endif
688
689 if ((State & INSERT)
690#ifdef FEAT_VREPLACE
691 && !(State & VREPLACE_FLAG)
692#endif
693 )
694 {
695 p_extra = saved_line + curwin->w_cursor.col;
696#ifdef FEAT_SMARTINDENT
697 if (do_si) /* need first char after new line break */
698 {
699 p = skipwhite(p_extra);
700 first_char = *p;
701 }
702#endif
703#ifdef FEAT_COMMENTS
704 extra_len = (int)STRLEN(p_extra);
705#endif
706 saved_char = *p_extra;
707 *p_extra = NUL;
708 }
709
710 u_clearline(); /* cannot do "U" command when adding lines */
711#ifdef FEAT_SMARTINDENT
712 did_si = FALSE;
713#endif
714 ai_col = 0;
715
716 /*
717 * If we just did an auto-indent, then we didn't type anything on
718 * the prior line, and it should be truncated. Do this even if 'ai' is not
719 * set because automatically inserting a comment leader also sets did_ai.
720 */
721 if (dir == FORWARD && did_ai)
722 trunc_line = TRUE;
723
724 /*
725 * If 'autoindent' and/or 'smartindent' is set, try to figure out what
726 * indent to use for the new line.
727 */
728 if (curbuf->b_p_ai
729#ifdef FEAT_SMARTINDENT
730 || do_si
731#endif
732 )
733 {
734 /*
735 * count white space on current line
736 */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200737 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200738 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
739 newindent = second_line_indent; /* for ^^D command in insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740
741#ifdef FEAT_SMARTINDENT
742 /*
743 * Do smart indenting.
744 * In insert/replace mode (only when dir == FORWARD)
745 * we may move some text to the next line. If it starts with '{'
746 * don't add an indent. Fixes inserting a NL before '{' in line
747 * "if (condition) {"
748 */
749 if (!trunc_line && do_si && *saved_line != NUL
750 && (p_extra == NULL || first_char != '{'))
751 {
752 char_u *ptr;
753 char_u last_char;
754
755 old_cursor = curwin->w_cursor;
756 ptr = saved_line;
757# ifdef FEAT_COMMENTS
758 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200759 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 else
761 lead_len = 0;
762# endif
763 if (dir == FORWARD)
764 {
765 /*
766 * Skip preprocessor directives, unless they are
767 * recognised as comments.
768 */
769 if (
770# ifdef FEAT_COMMENTS
771 lead_len == 0 &&
772# endif
773 ptr[0] == '#')
774 {
775 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
776 ptr = ml_get(--curwin->w_cursor.lnum);
777 newindent = get_indent();
778 }
779# ifdef FEAT_COMMENTS
780 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200781 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 else
783 lead_len = 0;
784 if (lead_len > 0)
785 {
786 /*
787 * This case gets the following right:
788 * \*
789 * * A comment (read '\' as '/').
790 * *\
791 * #define IN_THE_WAY
792 * This should line up here;
793 */
794 p = skipwhite(ptr);
795 if (p[0] == '/' && p[1] == '*')
796 p++;
797 if (p[0] == '*')
798 {
799 for (p++; *p; p++)
800 {
801 if (p[0] == '/' && p[-1] == '*')
802 {
803 /*
804 * End of C comment, indent should line up
805 * with the line containing the start of
806 * the comment
807 */
808 curwin->w_cursor.col = (colnr_T)(p - ptr);
809 if ((pos = findmatch(NULL, NUL)) != NULL)
810 {
811 curwin->w_cursor.lnum = pos->lnum;
812 newindent = get_indent();
813 }
814 }
815 }
816 }
817 }
818 else /* Not a comment line */
819# endif
820 {
821 /* Find last non-blank in line */
822 p = ptr + STRLEN(ptr) - 1;
823 while (p > ptr && vim_iswhite(*p))
824 --p;
825 last_char = *p;
826
827 /*
828 * find the character just before the '{' or ';'
829 */
830 if (last_char == '{' || last_char == ';')
831 {
832 if (p > ptr)
833 --p;
834 while (p > ptr && vim_iswhite(*p))
835 --p;
836 }
837 /*
838 * Try to catch lines that are split over multiple
839 * lines. eg:
840 * if (condition &&
841 * condition) {
842 * Should line up here!
843 * }
844 */
845 if (*p == ')')
846 {
847 curwin->w_cursor.col = (colnr_T)(p - ptr);
848 if ((pos = findmatch(NULL, '(')) != NULL)
849 {
850 curwin->w_cursor.lnum = pos->lnum;
851 newindent = get_indent();
852 ptr = ml_get_curline();
853 }
854 }
855 /*
856 * If last character is '{' do indent, without
857 * checking for "if" and the like.
858 */
859 if (last_char == '{')
860 {
861 did_si = TRUE; /* do indent */
862 no_si = TRUE; /* don't delete it when '{' typed */
863 }
864 /*
865 * Look for "if" and the like, use 'cinwords'.
866 * Don't do this if the previous line ended in ';' or
867 * '}'.
868 */
869 else if (last_char != ';' && last_char != '}'
870 && cin_is_cinword(ptr))
871 did_si = TRUE;
872 }
873 }
874 else /* dir == BACKWARD */
875 {
876 /*
877 * Skip preprocessor directives, unless they are
878 * recognised as comments.
879 */
880 if (
881# ifdef FEAT_COMMENTS
882 lead_len == 0 &&
883# endif
884 ptr[0] == '#')
885 {
886 int was_backslashed = FALSE;
887
888 while ((ptr[0] == '#' || was_backslashed) &&
889 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
890 {
891 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
892 was_backslashed = TRUE;
893 else
894 was_backslashed = FALSE;
895 ptr = ml_get(++curwin->w_cursor.lnum);
896 }
897 if (was_backslashed)
898 newindent = 0; /* Got to end of file */
899 else
900 newindent = get_indent();
901 }
902 p = skipwhite(ptr);
903 if (*p == '}') /* if line starts with '}': do indent */
904 did_si = TRUE;
905 else /* can delete indent when '{' typed */
906 can_si_back = TRUE;
907 }
908 curwin->w_cursor = old_cursor;
909 }
910 if (do_si)
911 can_si = TRUE;
912#endif /* FEAT_SMARTINDENT */
913
914 did_ai = TRUE;
915 }
916
917#ifdef FEAT_COMMENTS
918 /*
919 * Find out if the current line starts with a comment leader.
920 * This may then be inserted in front of the new line.
921 */
922 end_comment_pending = NUL;
923 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200924 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 else
926 lead_len = 0;
927 if (lead_len > 0)
928 {
929 char_u *lead_repl = NULL; /* replaces comment leader */
930 int lead_repl_len = 0; /* length of *lead_repl */
931 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
932 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
933 char_u *comment_end = NULL; /* where lead_end has been found */
934 int extra_space = FALSE; /* append extra space */
935 int current_flag;
936 int require_blank = FALSE; /* requires blank after middle */
937 char_u *p2;
938
939 /*
940 * If the comment leader has the start, middle or end flag, it may not
941 * be used or may be replaced with the middle leader.
942 */
943 for (p = lead_flags; *p && *p != ':'; ++p)
944 {
945 if (*p == COM_BLANK)
946 {
947 require_blank = TRUE;
948 continue;
949 }
950 if (*p == COM_START || *p == COM_MIDDLE)
951 {
952 current_flag = *p;
953 if (*p == COM_START)
954 {
955 /*
956 * Doing "O" on a start of comment does not insert leader.
957 */
958 if (dir == BACKWARD)
959 {
960 lead_len = 0;
961 break;
962 }
963
964 /* find start of middle part */
965 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
966 require_blank = FALSE;
967 }
968
969 /*
970 * Isolate the strings of the middle and end leader.
971 */
972 while (*p && p[-1] != ':') /* find end of middle flags */
973 {
974 if (*p == COM_BLANK)
975 require_blank = TRUE;
976 ++p;
977 }
978 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
979
980 while (*p && p[-1] != ':') /* find end of end flags */
981 {
982 /* Check whether we allow automatic ending of comments */
983 if (*p == COM_AUTO_END)
984 end_comment_pending = -1; /* means we want to set it */
985 ++p;
986 }
987 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
988
989 if (end_comment_pending == -1) /* we can set it now */
990 end_comment_pending = lead_end[n - 1];
991
992 /*
993 * If the end of the comment is in the same line, don't use
994 * the comment leader.
995 */
996 if (dir == FORWARD)
997 {
998 for (p = saved_line + lead_len; *p; ++p)
999 if (STRNCMP(p, lead_end, n) == 0)
1000 {
1001 comment_end = p;
1002 lead_len = 0;
1003 break;
1004 }
1005 }
1006
1007 /*
1008 * Doing "o" on a start of comment inserts the middle leader.
1009 */
1010 if (lead_len > 0)
1011 {
1012 if (current_flag == COM_START)
1013 {
1014 lead_repl = lead_middle;
1015 lead_repl_len = (int)STRLEN(lead_middle);
1016 }
1017
1018 /*
1019 * If we have hit RETURN immediately after the start
1020 * comment leader, then put a space after the middle
1021 * comment leader on the next line.
1022 */
1023 if (!vim_iswhite(saved_line[lead_len - 1])
1024 && ((p_extra != NULL
1025 && (int)curwin->w_cursor.col == lead_len)
1026 || (p_extra == NULL
1027 && saved_line[lead_len] == NUL)
1028 || require_blank))
1029 extra_space = TRUE;
1030 }
1031 break;
1032 }
1033 if (*p == COM_END)
1034 {
1035 /*
1036 * Doing "o" on the end of a comment does not insert leader.
1037 * Remember where the end is, might want to use it to find the
1038 * start (for C-comments).
1039 */
1040 if (dir == FORWARD)
1041 {
1042 comment_end = skipwhite(saved_line);
1043 lead_len = 0;
1044 break;
1045 }
1046
1047 /*
1048 * Doing "O" on the end of a comment inserts the middle leader.
1049 * Find the string for the middle leader, searching backwards.
1050 */
1051 while (p > curbuf->b_p_com && *p != ',')
1052 --p;
1053 for (lead_repl = p; lead_repl > curbuf->b_p_com
1054 && lead_repl[-1] != ':'; --lead_repl)
1055 ;
1056 lead_repl_len = (int)(p - lead_repl);
1057
1058 /* We can probably always add an extra space when doing "O" on
1059 * the comment-end */
1060 extra_space = TRUE;
1061
1062 /* Check whether we allow automatic ending of comments */
1063 for (p2 = p; *p2 && *p2 != ':'; p2++)
1064 {
1065 if (*p2 == COM_AUTO_END)
1066 end_comment_pending = -1; /* means we want to set it */
1067 }
1068 if (end_comment_pending == -1)
1069 {
1070 /* Find last character in end-comment string */
1071 while (*p2 && *p2 != ',')
1072 p2++;
1073 end_comment_pending = p2[-1];
1074 }
1075 break;
1076 }
1077 if (*p == COM_FIRST)
1078 {
1079 /*
1080 * Comment leader for first line only: Don't repeat leader
1081 * when using "O", blank out leader when using "o".
1082 */
1083 if (dir == BACKWARD)
1084 lead_len = 0;
1085 else
1086 {
1087 lead_repl = (char_u *)"";
1088 lead_repl_len = 0;
1089 }
1090 break;
1091 }
1092 }
1093 if (lead_len)
1094 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001095 /* allocate buffer (may concatenate p_extra later) */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001096 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001097 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 allocated = leader; /* remember to free it later */
1099
1100 if (leader == NULL)
1101 lead_len = 0;
1102 else
1103 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001104 vim_strncpy(leader, saved_line, lead_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105
1106 /*
1107 * Replace leader with lead_repl, right or left adjusted
1108 */
1109 if (lead_repl != NULL)
1110 {
1111 int c = 0;
1112 int off = 0;
1113
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001114 for (p = lead_flags; *p != NUL && *p != ':'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 {
1116 if (*p == COM_RIGHT || *p == COM_LEFT)
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001117 c = *p++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 else if (VIM_ISDIGIT(*p) || *p == '-')
1119 off = getdigits(&p);
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001120 else
1121 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 }
1123 if (c == COM_RIGHT) /* right adjusted leader */
1124 {
1125 /* find last non-white in the leader to line up with */
1126 for (p = leader + lead_len - 1; p > leader
1127 && vim_iswhite(*p); --p)
1128 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 ++p;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001130
1131#ifdef FEAT_MBYTE
1132 /* Compute the length of the replaced characters in
1133 * screen characters, not bytes. */
1134 {
1135 int repl_size = vim_strnsize(lead_repl,
1136 lead_repl_len);
1137 int old_size = 0;
1138 char_u *endp = p;
1139 int l;
1140
1141 while (old_size < repl_size && p > leader)
1142 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001143 mb_ptr_back(leader, p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001144 old_size += ptr2cells(p);
1145 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001146 l = lead_repl_len - (int)(endp - p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001147 if (l != 0)
1148 mch_memmove(endp + l, endp,
1149 (size_t)((leader + lead_len) - endp));
1150 lead_len += l;
1151 }
1152#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 if (p < leader + lead_repl_len)
1154 p = leader;
1155 else
1156 p -= lead_repl_len;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1159 if (p + lead_repl_len > leader + lead_len)
1160 p[lead_repl_len] = NUL;
1161
1162 /* blank-out any other chars from the old leader. */
1163 while (--p >= leader)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001164 {
1165#ifdef FEAT_MBYTE
1166 int l = mb_head_off(leader, p);
1167
1168 if (l > 1)
1169 {
1170 p -= l;
1171 if (ptr2cells(p) > 1)
1172 {
1173 p[1] = ' ';
1174 --l;
1175 }
1176 mch_memmove(p + 1, p + l + 1,
1177 (size_t)((leader + lead_len) - (p + l + 1)));
1178 lead_len -= l;
1179 *p = ' ';
1180 }
1181 else
1182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 if (!vim_iswhite(*p))
1184 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 }
1187 else /* left adjusted leader */
1188 {
1189 p = skipwhite(leader);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001190#ifdef FEAT_MBYTE
1191 /* Compute the length of the replaced characters in
1192 * screen characters, not bytes. Move the part that is
1193 * not to be overwritten. */
1194 {
1195 int repl_size = vim_strnsize(lead_repl,
1196 lead_repl_len);
1197 int i;
1198 int l;
1199
Bram Moolenaardc633cf2016-04-23 14:33:19 +02001200 for (i = 0; i < lead_len && p[i] != NUL; i += l)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001201 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001202 l = (*mb_ptr2len)(p + i);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001203 if (vim_strnsize(p, i + l) > repl_size)
1204 break;
1205 }
1206 if (i != lead_repl_len)
1207 {
1208 mch_memmove(p + lead_repl_len, p + i,
Bram Moolenaar2d7ff052009-11-17 15:08:26 +00001209 (size_t)(lead_len - i - (p - leader)));
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001210 lead_len += lead_repl_len - i;
1211 }
1212 }
1213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1215
1216 /* Replace any remaining non-white chars in the old
1217 * leader by spaces. Keep Tabs, the indent must
1218 * remain the same. */
1219 for (p += lead_repl_len; p < leader + lead_len; ++p)
1220 if (!vim_iswhite(*p))
1221 {
1222 /* Don't put a space before a TAB. */
1223 if (p + 1 < leader + lead_len && p[1] == TAB)
1224 {
1225 --lead_len;
1226 mch_memmove(p, p + 1,
1227 (leader + lead_len) - p);
1228 }
1229 else
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001230 {
1231#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001232 int l = (*mb_ptr2len)(p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001233
1234 if (l > 1)
1235 {
1236 if (ptr2cells(p) > 1)
1237 {
1238 /* Replace a double-wide char with
1239 * two spaces */
1240 --l;
1241 *p++ = ' ';
1242 }
1243 mch_memmove(p + 1, p + l,
1244 (leader + lead_len) - p);
1245 lead_len -= l - 1;
1246 }
1247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 }
1251 *p = NUL;
1252 }
1253
1254 /* Recompute the indent, it may have changed. */
1255 if (curbuf->b_p_ai
1256#ifdef FEAT_SMARTINDENT
1257 || do_si
1258#endif
1259 )
Bram Moolenaar597a4222014-06-25 14:39:50 +02001260 newindent = get_indent_str(leader, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261
1262 /* Add the indent offset */
1263 if (newindent + off < 0)
1264 {
1265 off = -newindent;
1266 newindent = 0;
1267 }
1268 else
1269 newindent += off;
1270
1271 /* Correct trailing spaces for the shift, so that
1272 * alignment remains equal. */
1273 while (off > 0 && lead_len > 0
1274 && leader[lead_len - 1] == ' ')
1275 {
1276 /* Don't do it when there is a tab before the space */
1277 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1278 break;
1279 --lead_len;
1280 --off;
1281 }
1282
1283 /* If the leader ends in white space, don't add an
1284 * extra space */
1285 if (lead_len > 0 && vim_iswhite(leader[lead_len - 1]))
1286 extra_space = FALSE;
1287 leader[lead_len] = NUL;
1288 }
1289
1290 if (extra_space)
1291 {
1292 leader[lead_len++] = ' ';
1293 leader[lead_len] = NUL;
1294 }
1295
1296 newcol = lead_len;
1297
1298 /*
1299 * if a new indent will be set below, remove the indent that
1300 * is in the comment leader
1301 */
1302 if (newindent
1303#ifdef FEAT_SMARTINDENT
1304 || did_si
1305#endif
1306 )
1307 {
1308 while (lead_len && vim_iswhite(*leader))
1309 {
1310 --lead_len;
1311 --newcol;
1312 ++leader;
1313 }
1314 }
1315
1316 }
1317#ifdef FEAT_SMARTINDENT
1318 did_si = can_si = FALSE;
1319#endif
1320 }
1321 else if (comment_end != NULL)
1322 {
1323 /*
1324 * We have finished a comment, so we don't use the leader.
1325 * If this was a C-comment and 'ai' or 'si' is set do a normal
1326 * indent to align with the line containing the start of the
1327 * comment.
1328 */
1329 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1330 (curbuf->b_p_ai
1331#ifdef FEAT_SMARTINDENT
1332 || do_si
1333#endif
1334 ))
1335 {
1336 old_cursor = curwin->w_cursor;
1337 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1338 if ((pos = findmatch(NULL, NUL)) != NULL)
1339 {
1340 curwin->w_cursor.lnum = pos->lnum;
1341 newindent = get_indent();
1342 }
1343 curwin->w_cursor = old_cursor;
1344 }
1345 }
1346 }
1347#endif
1348
1349 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */
1350 if (p_extra != NULL)
1351 {
1352 *p_extra = saved_char; /* restore char that NUL replaced */
1353
1354 /*
1355 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
1356 * non-blank.
1357 *
1358 * When in REPLACE mode, put the deleted blanks on the replace stack,
1359 * preceded by a NUL, so they can be put back when a BS is entered.
1360 */
1361 if (REPLACE_NORMAL(State))
1362 replace_push(NUL); /* end of extra blanks */
1363 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
1364 {
1365 while ((*p_extra == ' ' || *p_extra == '\t')
1366#ifdef FEAT_MBYTE
1367 && (!enc_utf8
1368 || !utf_iscomposing(utf_ptr2char(p_extra + 1)))
1369#endif
1370 )
1371 {
1372 if (REPLACE_NORMAL(State))
1373 replace_push(*p_extra);
1374 ++p_extra;
1375 ++less_cols_off;
1376 }
1377 }
1378 if (*p_extra != NUL)
1379 did_ai = FALSE; /* append some text, don't truncate now */
1380
1381 /* columns for marks adjusted for removed columns */
1382 less_cols = (int)(p_extra - saved_line);
1383 }
1384
1385 if (p_extra == NULL)
1386 p_extra = (char_u *)""; /* append empty line */
1387
1388#ifdef FEAT_COMMENTS
1389 /* concatenate leader and p_extra, if there is a leader */
1390 if (lead_len)
1391 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001392 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
1393 {
1394 int i;
Bram Moolenaar36105782012-06-14 20:59:25 +02001395 int padding = second_line_indent
1396 - (newindent + (int)STRLEN(leader));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001397
1398 /* Here whitespace is inserted after the comment char.
1399 * Below, set_indent(newindent, SIN_INSERT) will insert the
1400 * whitespace needed before the comment char. */
1401 for (i = 0; i < padding; i++)
1402 {
1403 STRCAT(leader, " ");
Bram Moolenaar5fb9ec52012-07-25 16:10:03 +02001404 less_cols--;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001405 newcol++;
1406 }
1407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 STRCAT(leader, p_extra);
1409 p_extra = leader;
1410 did_ai = TRUE; /* So truncating blanks works with comments */
1411 less_cols -= lead_len;
1412 }
1413 else
1414 end_comment_pending = NUL; /* turns out there was no leader */
1415#endif
1416
1417 old_cursor = curwin->w_cursor;
1418 if (dir == BACKWARD)
1419 --curwin->w_cursor.lnum;
1420#ifdef FEAT_VREPLACE
1421 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
1422#endif
1423 {
1424 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
1425 == FAIL)
1426 goto theend;
1427 /* Postpone calling changed_lines(), because it would mess up folding
Bram Moolenaar82faa252016-06-04 20:14:07 +02001428 * with markers.
1429 * Skip mark_adjust when adding a line after the last one, there can't
1430 * be marks there. */
1431 if (curwin->w_cursor.lnum + 1 < curbuf->b_ml.ml_line_count)
1432 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 did_append = TRUE;
1434 }
1435#ifdef FEAT_VREPLACE
1436 else
1437 {
1438 /*
1439 * In VREPLACE mode we are starting to replace the next line.
1440 */
1441 curwin->w_cursor.lnum++;
1442 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1443 {
1444 /* In case we NL to a new line, BS to the previous one, and NL
1445 * again, we don't want to save the new line for undo twice.
1446 */
1447 (void)u_save_cursor(); /* errors are ignored! */
1448 vr_lines_changed++;
1449 }
1450 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1451 changed_bytes(curwin->w_cursor.lnum, 0);
1452 curwin->w_cursor.lnum--;
1453 did_append = FALSE;
1454 }
1455#endif
1456
1457 if (newindent
1458#ifdef FEAT_SMARTINDENT
1459 || did_si
1460#endif
1461 )
1462 {
1463 ++curwin->w_cursor.lnum;
1464#ifdef FEAT_SMARTINDENT
1465 if (did_si)
1466 {
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001467 int sw = (int)get_sw_value(curbuf);
Bram Moolenaar14f24742012-08-08 18:01:05 +02001468
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001470 newindent -= newindent % sw;
1471 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 }
1473#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001474 /* Copy the indent */
1475 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 {
1477 (void)copy_indent(newindent, saved_line);
1478
1479 /*
1480 * Set the 'preserveindent' option so that any further screwing
1481 * with the line doesn't entirely destroy our efforts to preserve
1482 * it. It gets restored at the function end.
1483 */
1484 curbuf->b_p_pi = TRUE;
1485 }
1486 else
1487 (void)set_indent(newindent, SIN_INSERT);
1488 less_cols -= curwin->w_cursor.col;
1489
1490 ai_col = curwin->w_cursor.col;
1491
1492 /*
1493 * In REPLACE mode, for each character in the new indent, there must
1494 * be a NUL on the replace stack, for when it is deleted with BS
1495 */
1496 if (REPLACE_NORMAL(State))
1497 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1498 replace_push(NUL);
1499 newcol += curwin->w_cursor.col;
1500#ifdef FEAT_SMARTINDENT
1501 if (no_si)
1502 did_si = FALSE;
1503#endif
1504 }
1505
1506#ifdef FEAT_COMMENTS
1507 /*
1508 * In REPLACE mode, for each character in the extra leader, there must be
1509 * a NUL on the replace stack, for when it is deleted with BS.
1510 */
1511 if (REPLACE_NORMAL(State))
1512 while (lead_len-- > 0)
1513 replace_push(NUL);
1514#endif
1515
1516 curwin->w_cursor = old_cursor;
1517
1518 if (dir == FORWARD)
1519 {
1520 if (trunc_line || (State & INSERT))
1521 {
1522 /* truncate current line at cursor */
1523 saved_line[curwin->w_cursor.col] = NUL;
1524 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1525 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1526 truncate_spaces(saved_line);
1527 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1528 saved_line = NULL;
1529 if (did_append)
1530 {
1531 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1532 curwin->w_cursor.lnum + 1, 1L);
1533 did_append = FALSE;
1534
1535 /* Move marks after the line break to the new line. */
1536 if (flags & OPENLINE_MARKFIX)
1537 mark_col_adjust(curwin->w_cursor.lnum,
1538 curwin->w_cursor.col + less_cols_off,
1539 1L, (long)-less_cols);
1540 }
1541 else
1542 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1543 }
1544
1545 /*
1546 * Put the cursor on the new line. Careful: the scrollup() above may
1547 * have moved w_cursor, we must use old_cursor.
1548 */
1549 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1550 }
1551 if (did_append)
1552 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1553
1554 curwin->w_cursor.col = newcol;
1555#ifdef FEAT_VIRTUALEDIT
1556 curwin->w_cursor.coladd = 0;
1557#endif
1558
1559#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1560 /*
1561 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1562 * fixthisline() from doing it (via change_indent()) by telling it we're in
1563 * normal INSERT mode.
1564 */
1565 if (State & VREPLACE_FLAG)
1566 {
1567 vreplace_mode = State; /* So we know to put things right later */
1568 State = INSERT;
1569 }
1570 else
1571 vreplace_mode = 0;
1572#endif
1573#ifdef FEAT_LISP
1574 /*
1575 * May do lisp indenting.
1576 */
1577 if (!p_paste
1578# ifdef FEAT_COMMENTS
1579 && leader == NULL
1580# endif
1581 && curbuf->b_p_lisp
1582 && curbuf->b_p_ai)
1583 {
1584 fixthisline(get_lisp_indent);
1585 p = ml_get_curline();
1586 ai_col = (colnr_T)(skipwhite(p) - p);
1587 }
1588#endif
1589#ifdef FEAT_CINDENT
1590 /*
1591 * May do indenting after opening a new line.
1592 */
1593 if (!p_paste
1594 && (curbuf->b_p_cin
1595# ifdef FEAT_EVAL
1596 || *curbuf->b_p_inde != NUL
1597# endif
1598 )
1599 && in_cinkeys(dir == FORWARD
1600 ? KEY_OPEN_FORW
1601 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1602 {
1603 do_c_expr_indent();
1604 p = ml_get_curline();
1605 ai_col = (colnr_T)(skipwhite(p) - p);
1606 }
1607#endif
1608#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1609 if (vreplace_mode != 0)
1610 State = vreplace_mode;
1611#endif
1612
1613#ifdef FEAT_VREPLACE
1614 /*
1615 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1616 * original line, and inserts the new stuff char by char, pushing old stuff
1617 * onto the replace stack (via ins_char()).
1618 */
1619 if (State & VREPLACE_FLAG)
1620 {
1621 /* Put new line in p_extra */
1622 p_extra = vim_strsave(ml_get_curline());
1623 if (p_extra == NULL)
1624 goto theend;
1625
1626 /* Put back original line */
1627 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1628
1629 /* Insert new stuff into line again */
1630 curwin->w_cursor.col = 0;
1631#ifdef FEAT_VIRTUALEDIT
1632 curwin->w_cursor.coladd = 0;
1633#endif
1634 ins_bytes(p_extra); /* will call changed_bytes() */
1635 vim_free(p_extra);
1636 next_line = NULL;
1637 }
1638#endif
1639
1640 retval = TRUE; /* success! */
1641theend:
1642 curbuf->b_p_pi = saved_pi;
1643 vim_free(saved_line);
1644 vim_free(next_line);
1645 vim_free(allocated);
1646 return retval;
1647}
1648
1649#if defined(FEAT_COMMENTS) || defined(PROTO)
1650/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +02001651 * get_leader_len() returns the length in bytes of the prefix of the given
1652 * string which introduces a comment. If this string is not a comment then
1653 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 * When "flags" is not NULL, it is set to point to the flags of the recognized
1655 * comment leader.
1656 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001657 * If "include_space" is set, include trailing whitespace while calculating the
1658 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 */
1660 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001661get_leader_len(
1662 char_u *line,
1663 char_u **flags,
1664 int backward,
1665 int include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666{
1667 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001668 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 int got_com = FALSE;
1670 int found_one;
1671 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1672 char_u *string; /* pointer to comment string */
1673 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001674 int middle_match_len = 0;
1675 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001676 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
Bram Moolenaar81340392012-06-06 16:12:59 +02001678 result = i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 while (vim_iswhite(line[i])) /* leading white space is ignored */
1680 ++i;
1681
1682 /*
1683 * Repeat to match several nested comment strings.
1684 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001685 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 {
1687 /*
1688 * scan through the 'comments' option for a match
1689 */
1690 found_one = FALSE;
1691 for (list = curbuf->b_p_com; *list; )
1692 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001693 /* Get one option part into part_buf[]. Advance "list" to next
1694 * one. Put "string" at start of string. */
1695 if (!got_com && flags != NULL)
1696 *flags = list; /* remember where flags started */
1697 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1699 string = vim_strchr(part_buf, ':');
1700 if (string == NULL) /* missing ':', ignore this part */
1701 continue;
1702 *string++ = NUL; /* isolate flags from string */
1703
Bram Moolenaara4271d52011-05-10 13:38:27 +02001704 /* If we found a middle match previously, use that match when this
1705 * is not a middle or end. */
1706 if (middle_match_len != 0
1707 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1708 && vim_strchr(part_buf, COM_END) == NULL)
1709 break;
1710
1711 /* When we already found a nested comment, only accept further
1712 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1714 continue;
1715
Bram Moolenaara4271d52011-05-10 13:38:27 +02001716 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1718 continue;
1719
Bram Moolenaara4271d52011-05-10 13:38:27 +02001720 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 * When string starts with white space, must have some white space
1722 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001723 * TABs and spaces). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 if (vim_iswhite(string[0]))
1725 {
1726 if (i == 0 || !vim_iswhite(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001727 continue; /* missing white space */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 while (vim_iswhite(string[0]))
1729 ++string;
1730 }
1731 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1732 ;
1733 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001734 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735
Bram Moolenaara4271d52011-05-10 13:38:27 +02001736 /* When 'b' flag used, there must be white space or an
1737 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 if (vim_strchr(part_buf, COM_BLANK) != NULL
1739 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1740 continue;
1741
Bram Moolenaara4271d52011-05-10 13:38:27 +02001742 /* We have found a match, stop searching unless this is a middle
1743 * comment. The middle comment can be a substring of the end
1744 * comment in which case it's better to return the length of the
1745 * end comment and its flags. Thus we keep searching with middle
1746 * and end matches and use an end match if it matches better. */
1747 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1748 {
1749 if (middle_match_len == 0)
1750 {
1751 middle_match_len = j;
1752 saved_flags = prev_list;
1753 }
1754 continue;
1755 }
1756 if (middle_match_len != 0 && j > middle_match_len)
1757 /* Use this match instead of the middle match, since it's a
1758 * longer thus better match. */
1759 middle_match_len = 0;
1760
1761 if (middle_match_len == 0)
1762 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 found_one = TRUE;
1764 break;
1765 }
1766
Bram Moolenaara4271d52011-05-10 13:38:27 +02001767 if (middle_match_len != 0)
1768 {
1769 /* Use the previously found middle match after failing to find a
1770 * match with an end. */
1771 if (!got_com && flags != NULL)
1772 *flags = saved_flags;
1773 i += middle_match_len;
1774 found_one = TRUE;
1775 }
1776
1777 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 if (!found_one)
1779 break;
1780
Bram Moolenaar81340392012-06-06 16:12:59 +02001781 result = i;
1782
Bram Moolenaara4271d52011-05-10 13:38:27 +02001783 /* Include any trailing white space. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 while (vim_iswhite(line[i]))
1785 ++i;
1786
Bram Moolenaar81340392012-06-06 16:12:59 +02001787 if (include_space)
1788 result = i;
1789
Bram Moolenaara4271d52011-05-10 13:38:27 +02001790 /* If this comment doesn't nest, stop here. */
1791 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 if (vim_strchr(part_buf, COM_NEST) == NULL)
1793 break;
1794 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001795 return result;
1796}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001797
Bram Moolenaar81340392012-06-06 16:12:59 +02001798/*
1799 * Return the offset at which the last comment in line starts. If there is no
1800 * comment in the whole line, -1 is returned.
1801 *
1802 * When "flags" is not null, it is set to point to the flags describing the
1803 * recognized comment leader.
1804 */
1805 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001806get_last_leader_offset(char_u *line, char_u **flags)
Bram Moolenaar81340392012-06-06 16:12:59 +02001807{
1808 int result = -1;
1809 int i, j;
1810 int lower_check_bound = 0;
1811 char_u *string;
1812 char_u *com_leader;
1813 char_u *com_flags;
1814 char_u *list;
1815 int found_one;
1816 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1817
1818 /*
1819 * Repeat to match several nested comment strings.
1820 */
1821 i = (int)STRLEN(line);
1822 while (--i >= lower_check_bound)
1823 {
1824 /*
1825 * scan through the 'comments' option for a match
1826 */
1827 found_one = FALSE;
1828 for (list = curbuf->b_p_com; *list; )
1829 {
1830 char_u *flags_save = list;
1831
1832 /*
1833 * Get one option part into part_buf[]. Advance list to next one.
1834 * put string at start of string.
1835 */
1836 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1837 string = vim_strchr(part_buf, ':');
1838 if (string == NULL) /* If everything is fine, this cannot actually
1839 * happen. */
1840 {
1841 continue;
1842 }
1843 *string++ = NUL; /* Isolate flags from string. */
1844 com_leader = string;
1845
1846 /*
1847 * Line contents and string must match.
1848 * When string starts with white space, must have some white space
1849 * (but the amount does not need to match, there might be a mix of
1850 * TABs and spaces).
1851 */
1852 if (vim_iswhite(string[0]))
1853 {
1854 if (i == 0 || !vim_iswhite(line[i - 1]))
1855 continue;
1856 while (vim_iswhite(string[0]))
1857 ++string;
1858 }
1859 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1860 /* do nothing */;
1861 if (string[j] != NUL)
1862 continue;
1863
1864 /*
1865 * When 'b' flag used, there must be white space or an
1866 * end-of-line after the string in the line.
1867 */
1868 if (vim_strchr(part_buf, COM_BLANK) != NULL
1869 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1870 {
1871 continue;
1872 }
1873
1874 /*
1875 * We have found a match, stop searching.
1876 */
1877 found_one = TRUE;
1878
1879 if (flags)
1880 *flags = flags_save;
1881 com_flags = flags_save;
1882
1883 break;
1884 }
1885
1886 if (found_one)
1887 {
1888 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
1889 int len1, len2, off;
1890
1891 result = i;
1892 /*
1893 * If this comment nests, continue searching.
1894 */
1895 if (vim_strchr(part_buf, COM_NEST) != NULL)
1896 continue;
1897
1898 lower_check_bound = i;
1899
1900 /* Let's verify whether the comment leader found is a substring
1901 * of other comment leaders. If it is, let's adjust the
1902 * lower_check_bound so that we make sure that we have determined
1903 * the comment leader correctly.
1904 */
1905
1906 while (vim_iswhite(*com_leader))
1907 ++com_leader;
1908 len1 = (int)STRLEN(com_leader);
1909
1910 for (list = curbuf->b_p_com; *list; )
1911 {
1912 char_u *flags_save = list;
1913
1914 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
1915 if (flags_save == com_flags)
1916 continue;
1917 string = vim_strchr(part_buf2, ':');
1918 ++string;
1919 while (vim_iswhite(*string))
1920 ++string;
1921 len2 = (int)STRLEN(string);
1922 if (len2 == 0)
1923 continue;
1924
1925 /* Now we have to verify whether string ends with a substring
1926 * beginning the com_leader. */
1927 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
1928 {
1929 --off;
1930 if (!STRNCMP(string + off, com_leader, len2 - off))
1931 {
1932 if (i - off < lower_check_bound)
1933 lower_check_bound = i - off;
1934 }
1935 }
1936 }
1937 }
1938 }
1939 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940}
1941#endif
1942
1943/*
1944 * Return the number of window lines occupied by buffer line "lnum".
1945 */
1946 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001947plines(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948{
1949 return plines_win(curwin, lnum, TRUE);
1950}
1951
1952 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001953plines_win(
1954 win_T *wp,
1955 linenr_T lnum,
1956 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957{
1958#if defined(FEAT_DIFF) || defined(PROTO)
1959 /* Check for filler lines above this buffer line. When folded the result
1960 * is one line anyway. */
1961 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
1962}
1963
1964 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001965plines_nofill(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966{
1967 return plines_win_nofill(curwin, lnum, TRUE);
1968}
1969
1970 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01001971plines_win_nofill(
1972 win_T *wp,
1973 linenr_T lnum,
1974 int winheight) /* when TRUE limit to window height */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975{
1976#endif
1977 int lines;
1978
1979 if (!wp->w_p_wrap)
1980 return 1;
1981
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001982#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 if (wp->w_width == 0)
1984 return 1;
1985#endif
1986
1987#ifdef FEAT_FOLDING
1988 /* A folded lines is handled just like an empty line. */
1989 /* NOTE: Caller must handle lines that are MAYBE folded. */
1990 if (lineFolded(wp, lnum) == TRUE)
1991 return 1;
1992#endif
1993
1994 lines = plines_win_nofold(wp, lnum);
1995 if (winheight > 0 && lines > wp->w_height)
1996 return (int)wp->w_height;
1997 return lines;
1998}
1999
2000/*
2001 * Return number of window lines physical line "lnum" will occupy in window
2002 * "wp". Does not care about folding, 'wrap' or 'diff'.
2003 */
2004 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002005plines_win_nofold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006{
2007 char_u *s;
2008 long col;
2009 int width;
2010
2011 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2012 if (*s == NUL) /* empty line */
2013 return 1;
2014 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
2015
2016 /*
2017 * If list mode is on, then the '$' at the end of the line may take up one
2018 * extra column.
2019 */
2020 if (wp->w_p_list && lcs_eol != NUL)
2021 col += 1;
2022
2023 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002024 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 */
2026 width = W_WIDTH(wp) - win_col_off(wp);
2027 if (width <= 0)
2028 return 32000;
2029 if (col <= width)
2030 return 1;
2031 col -= width;
2032 width += win_col_off2(wp);
2033 return (col + (width - 1)) / width + 1;
2034}
2035
2036/*
2037 * Like plines_win(), but only reports the number of physical screen lines
2038 * used from the start of the line to the given column number.
2039 */
2040 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002041plines_win_col(win_T *wp, linenr_T lnum, long column)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042{
2043 long col;
2044 char_u *s;
2045 int lines = 0;
2046 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002047 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048
2049#ifdef FEAT_DIFF
2050 /* Check for filler lines above this buffer line. When folded the result
2051 * is one line anyway. */
2052 lines = diff_check_fill(wp, lnum);
2053#endif
2054
2055 if (!wp->w_p_wrap)
2056 return lines + 1;
2057
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002058#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 if (wp->w_width == 0)
2060 return lines + 1;
2061#endif
2062
Bram Moolenaar597a4222014-06-25 14:39:50 +02002063 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064
2065 col = 0;
2066 while (*s != NUL && --column >= 0)
2067 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02002068 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002069 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 }
2071
2072 /*
2073 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2074 * INSERT mode, then col must be adjusted so that it represents the last
2075 * screen position of the TAB. This only fixes an error when the TAB wraps
2076 * from one screen line to the next (when 'columns' is not a multiple of
2077 * 'ts') -- webb.
2078 */
2079 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02002080 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081
2082 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002083 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 */
2085 width = W_WIDTH(wp) - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002086 if (width <= 0)
2087 return 9999;
2088
2089 lines += 1;
2090 if (col > width)
2091 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2092 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093}
2094
2095 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002096plines_m_win(win_T *wp, linenr_T first, linenr_T last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097{
2098 int count = 0;
2099
2100 while (first <= last)
2101 {
2102#ifdef FEAT_FOLDING
2103 int x;
2104
2105 /* Check if there are any really folded lines, but also included lines
2106 * that are maybe folded. */
2107 x = foldedCount(wp, first, NULL);
2108 if (x > 0)
2109 {
2110 ++count; /* count 1 for "+-- folded" line */
2111 first += x;
2112 }
2113 else
2114#endif
2115 {
2116#ifdef FEAT_DIFF
2117 if (first == wp->w_topline)
2118 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2119 else
2120#endif
2121 count += plines_win(wp, first, TRUE);
2122 ++first;
2123 }
2124 }
2125 return (count);
2126}
2127
2128#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO)
2129/*
2130 * Insert string "p" at the cursor position. Stops at a NUL byte.
2131 * Handles Replace mode and multi-byte characters.
2132 */
2133 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002134ins_bytes(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135{
2136 ins_bytes_len(p, (int)STRLEN(p));
2137}
2138#endif
2139
2140#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2141 || defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO)
2142/*
2143 * Insert string "p" with length "len" at the cursor position.
2144 * Handles Replace mode and multi-byte characters.
2145 */
2146 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002147ins_bytes_len(char_u *p, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148{
2149 int i;
2150# ifdef FEAT_MBYTE
2151 int n;
2152
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002153 if (has_mbyte)
2154 for (i = 0; i < len; i += n)
2155 {
2156 if (enc_utf8)
2157 /* avoid reading past p[len] */
2158 n = utfc_ptr2len_len(p + i, len - i);
2159 else
2160 n = (*mb_ptr2len)(p + i);
2161 ins_char_bytes(p + i, n);
2162 }
2163 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164# endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002165 for (i = 0; i < len; ++i)
2166 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167}
2168#endif
2169
2170/*
2171 * Insert or replace a single character at the cursor position.
2172 * When in REPLACE or VREPLACE mode, replace any existing character.
2173 * Caller must have prepared for undo.
2174 * For multi-byte characters we get the whole character, the caller must
2175 * convert bytes to a character.
2176 */
2177 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002178ins_char(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002180 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar560379d2017-01-21 22:50:00 +01002181 int n = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182
Bram Moolenaar6a8ede92017-01-22 19:49:12 +01002183#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 n = (*mb_char2bytes)(c, buf);
2185
2186 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2187 * Happens for CTRL-Vu9900. */
2188 if (buf[0] == 0)
2189 buf[0] = '\n';
Bram Moolenaar560379d2017-01-21 22:50:00 +01002190#else
2191 buf[0] = c;
2192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193
2194 ins_char_bytes(buf, n);
2195}
2196
2197 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002198ins_char_bytes(char_u *buf, int charlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199{
2200 int c = buf[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 int newlen; /* nr of bytes inserted */
2202 int oldlen; /* nr of bytes deleted (0 when not replacing) */
2203 char_u *p;
2204 char_u *newp;
2205 char_u *oldp;
2206 int linelen; /* length of old line including NUL */
2207 colnr_T col;
2208 linenr_T lnum = curwin->w_cursor.lnum;
2209 int i;
2210
2211#ifdef FEAT_VIRTUALEDIT
2212 /* Break tabs if needed. */
2213 if (virtual_active() && curwin->w_cursor.coladd > 0)
2214 coladvance_force(getviscol());
2215#endif
2216
2217 col = curwin->w_cursor.col;
2218 oldp = ml_get(lnum);
2219 linelen = (int)STRLEN(oldp) + 1;
2220
2221 /* The lengths default to the values for when not replacing. */
2222 oldlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 newlen = charlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224
2225 if (State & REPLACE_FLAG)
2226 {
2227#ifdef FEAT_VREPLACE
2228 if (State & VREPLACE_FLAG)
2229 {
2230 colnr_T new_vcol = 0; /* init for GCC */
2231 colnr_T vcol;
2232 int old_list;
2233#ifndef FEAT_MBYTE
2234 char_u buf[2];
2235#endif
2236
2237 /*
2238 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2239 * Returns the old value of list, so when finished,
2240 * curwin->w_p_list should be set back to this.
2241 */
2242 old_list = curwin->w_p_list;
2243 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2244 curwin->w_p_list = FALSE;
2245
2246 /*
2247 * In virtual replace mode each character may replace one or more
2248 * characters (zero if it's a TAB). Count the number of bytes to
2249 * be deleted to make room for the new character, counting screen
2250 * cells. May result in adding spaces to fill a gap.
2251 */
2252 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2253#ifndef FEAT_MBYTE
2254 buf[0] = c;
2255 buf[1] = NUL;
2256#endif
2257 new_vcol = vcol + chartabsize(buf, vcol);
2258 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2259 {
2260 vcol += chartabsize(oldp + col + oldlen, vcol);
2261 /* Don't need to remove a TAB that takes us to the right
2262 * position. */
2263 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2264 break;
2265#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002266 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267#else
2268 ++oldlen;
2269#endif
2270 /* Deleted a bit too much, insert spaces. */
2271 if (vcol > new_vcol)
2272 newlen += vcol - new_vcol;
2273 }
2274 curwin->w_p_list = old_list;
2275 }
2276 else
2277#endif
2278 if (oldp[col] != NUL)
2279 {
2280 /* normal replace */
2281#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002282 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283#else
2284 oldlen = 1;
2285#endif
2286 }
2287
2288
2289 /* Push the replaced bytes onto the replace stack, so that they can be
2290 * put back when BS is used. The bytes of a multi-byte character are
2291 * done the other way around, so that the first byte is popped off
2292 * first (it tells the byte length of the character). */
2293 replace_push(NUL);
2294 for (i = 0; i < oldlen; ++i)
2295 {
2296#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002297 if (has_mbyte)
2298 i += replace_push_mb(oldp + col + i) - 1;
2299 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002301 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 }
2303 }
2304
2305 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2306 if (newp == NULL)
2307 return;
2308
2309 /* Copy bytes before the cursor. */
2310 if (col > 0)
2311 mch_memmove(newp, oldp, (size_t)col);
2312
2313 /* Copy bytes after the changed character(s). */
2314 p = newp + col;
2315 mch_memmove(p + newlen, oldp + col + oldlen,
2316 (size_t)(linelen - col - oldlen));
2317
2318 /* Insert or overwrite the new character. */
2319#ifdef FEAT_MBYTE
2320 mch_memmove(p, buf, charlen);
2321 i = charlen;
2322#else
2323 *p = c;
2324 i = 1;
2325#endif
2326
2327 /* Fill with spaces when necessary. */
2328 while (i < newlen)
2329 p[i++] = ' ';
2330
2331 /* Replace the line in the buffer. */
2332 ml_replace(lnum, newp, FALSE);
2333
2334 /* mark the buffer as changed and prepare for displaying */
2335 changed_bytes(lnum, col);
2336
2337 /*
2338 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2339 * show the match for right parens and braces.
2340 */
2341 if (p_sm && (State & INSERT)
2342 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002343#ifdef FEAT_INS_EXPAND
2344 && !ins_compl_active()
2345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002347 {
2348#ifdef FEAT_MBYTE
2349 if (has_mbyte)
2350 showmatch(mb_ptr2char(buf));
2351 else
2352#endif
2353 showmatch(c);
2354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355
2356#ifdef FEAT_RIGHTLEFT
2357 if (!p_ri || (State & REPLACE_FLAG))
2358#endif
2359 {
2360 /* Normal insert: move cursor right */
2361#ifdef FEAT_MBYTE
2362 curwin->w_cursor.col += charlen;
2363#else
2364 ++curwin->w_cursor.col;
2365#endif
2366 }
2367 /*
2368 * TODO: should try to update w_row here, to avoid recomputing it later.
2369 */
2370}
2371
2372/*
2373 * Insert a string at the cursor position.
2374 * Note: Does NOT handle Replace mode.
2375 * Caller must have prepared for undo.
2376 */
2377 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002378ins_str(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379{
2380 char_u *oldp, *newp;
2381 int newlen = (int)STRLEN(s);
2382 int oldlen;
2383 colnr_T col;
2384 linenr_T lnum = curwin->w_cursor.lnum;
2385
2386#ifdef FEAT_VIRTUALEDIT
2387 if (virtual_active() && curwin->w_cursor.coladd > 0)
2388 coladvance_force(getviscol());
2389#endif
2390
2391 col = curwin->w_cursor.col;
2392 oldp = ml_get(lnum);
2393 oldlen = (int)STRLEN(oldp);
2394
2395 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2396 if (newp == NULL)
2397 return;
2398 if (col > 0)
2399 mch_memmove(newp, oldp, (size_t)col);
2400 mch_memmove(newp + col, s, (size_t)newlen);
2401 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2402 ml_replace(lnum, newp, FALSE);
2403 changed_bytes(lnum, col);
2404 curwin->w_cursor.col += newlen;
2405}
2406
2407/*
2408 * Delete one character under the cursor.
2409 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2410 * Caller must have prepared for undo.
2411 *
2412 * return FAIL for failure, OK otherwise
2413 */
2414 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002415del_char(int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416{
2417#ifdef FEAT_MBYTE
2418 if (has_mbyte)
2419 {
2420 /* Make sure the cursor is at the start of a character. */
2421 mb_adjust_cursor();
2422 if (*ml_get_cursor() == NUL)
2423 return FAIL;
2424 return del_chars(1L, fixpos);
2425 }
2426#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002427 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428}
2429
2430#if defined(FEAT_MBYTE) || defined(PROTO)
2431/*
2432 * Like del_bytes(), but delete characters instead of bytes.
2433 */
2434 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002435del_chars(long count, int fixpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436{
2437 long bytes = 0;
2438 long i;
2439 char_u *p;
2440 int l;
2441
2442 p = ml_get_cursor();
2443 for (i = 0; i < count && *p != NUL; ++i)
2444 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002445 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 bytes += l;
2447 p += l;
2448 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002449 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450}
2451#endif
2452
2453/*
2454 * Delete "count" bytes under the cursor.
2455 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2456 * Caller must have prepared for undo.
2457 *
2458 * return FAIL for failure, OK otherwise
2459 */
2460 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002461del_bytes(
2462 long count,
2463 int fixpos_arg,
2464 int use_delcombine UNUSED) /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465{
2466 char_u *oldp, *newp;
2467 colnr_T oldlen;
2468 linenr_T lnum = curwin->w_cursor.lnum;
2469 colnr_T col = curwin->w_cursor.col;
2470 int was_alloced;
2471 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002472 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473
2474 oldp = ml_get(lnum);
2475 oldlen = (int)STRLEN(oldp);
2476
2477 /*
2478 * Can't do anything when the cursor is on the NUL after the line.
2479 */
2480 if (col >= oldlen)
2481 return FAIL;
2482
2483#ifdef FEAT_MBYTE
2484 /* If 'delcombine' is set and deleting (less than) one character, only
2485 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002486 if (p_deco && use_delcombine && enc_utf8
2487 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002489 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 int n;
2491
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002492 (void)utfc_ptr2char(oldp + col, cc);
2493 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 {
2495 /* Find the last composing char, there can be several. */
2496 n = col;
2497 do
2498 {
2499 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002500 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 n += count;
2502 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2503 fixpos = 0;
2504 }
2505 }
2506#endif
2507
2508 /*
2509 * When count is too big, reduce it.
2510 */
2511 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2512 if (movelen <= 1)
2513 {
2514 /*
2515 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002516 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2517 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002519 if (col > 0 && fixpos && restart_edit == 0
2520#ifdef FEAT_VIRTUALEDIT
2521 && (ve_flags & VE_ONEMORE) == 0
2522#endif
2523 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 {
2525 --curwin->w_cursor.col;
2526#ifdef FEAT_VIRTUALEDIT
2527 curwin->w_cursor.coladd = 0;
2528#endif
2529#ifdef FEAT_MBYTE
2530 if (has_mbyte)
2531 curwin->w_cursor.col -=
2532 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2533#endif
2534 }
2535 count = oldlen - col;
2536 movelen = 1;
2537 }
2538
2539 /*
2540 * If the old line has been allocated the deletion can be done in the
2541 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002542 * Can't do this when using Netbeans, because we would need to invoke
2543 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002544 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002547 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002548 was_alloced = FALSE;
2549 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002551 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 if (was_alloced)
2553 newp = oldp; /* use same allocated memory */
2554 else
2555 { /* need to allocate a new line */
2556 newp = alloc((unsigned)(oldlen + 1 - count));
2557 if (newp == NULL)
2558 return FAIL;
2559 mch_memmove(newp, oldp, (size_t)col);
2560 }
2561 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2562 if (!was_alloced)
2563 ml_replace(lnum, newp, FALSE);
2564
2565 /* mark the buffer as changed and prepare for displaying */
2566 changed_bytes(lnum, curwin->w_cursor.col);
2567
2568 return OK;
2569}
2570
2571/*
2572 * Delete from cursor to end of line.
2573 * Caller must have prepared for undo.
2574 *
2575 * return FAIL for failure, OK otherwise
2576 */
2577 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002578truncate_line(
2579 int fixpos) /* if TRUE fix the cursor position when done */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580{
2581 char_u *newp;
2582 linenr_T lnum = curwin->w_cursor.lnum;
2583 colnr_T col = curwin->w_cursor.col;
2584
2585 if (col == 0)
2586 newp = vim_strsave((char_u *)"");
2587 else
2588 newp = vim_strnsave(ml_get(lnum), col);
2589
2590 if (newp == NULL)
2591 return FAIL;
2592
2593 ml_replace(lnum, newp, FALSE);
2594
2595 /* mark the buffer as changed and prepare for displaying */
2596 changed_bytes(lnum, curwin->w_cursor.col);
2597
2598 /*
2599 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2600 */
2601 if (fixpos && curwin->w_cursor.col > 0)
2602 --curwin->w_cursor.col;
2603
2604 return OK;
2605}
2606
2607/*
2608 * Delete "nlines" lines at the cursor.
2609 * Saves the lines for undo first if "undo" is TRUE.
2610 */
2611 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002612del_lines(
2613 long nlines, /* number of lines to delete */
2614 int undo) /* if TRUE, prepare for undo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615{
2616 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002617 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618
2619 if (nlines <= 0)
2620 return;
2621
2622 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002623 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 return;
2625
2626 for (n = 0; n < nlines; )
2627 {
2628 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2629 break;
2630
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002631 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 ++n;
2633
2634 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002635 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 break;
2637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002639 /* Correct the cursor position before calling deleted_lines_mark(), it may
2640 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 curwin->w_cursor.col = 0;
2642 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002643
2644 /* adjust marks, mark the buffer as changed and prepare for displaying */
2645 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646}
2647
2648 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002649gchar_pos(pos_T *pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650{
2651 char_u *ptr = ml_get_pos(pos);
2652
2653#ifdef FEAT_MBYTE
2654 if (has_mbyte)
2655 return (*mb_ptr2char)(ptr);
2656#endif
2657 return (int)*ptr;
2658}
2659
2660 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002661gchar_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662{
2663#ifdef FEAT_MBYTE
2664 if (has_mbyte)
2665 return (*mb_ptr2char)(ml_get_cursor());
2666#endif
2667 return (int)*ml_get_cursor();
2668}
2669
2670/*
2671 * Write a character at the current cursor position.
2672 * It is directly written into the block.
2673 */
2674 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002675pchar_cursor(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676{
2677 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2678 + curwin->w_cursor.col) = c;
2679}
2680
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681/*
2682 * When extra == 0: Return TRUE if the cursor is before or on the first
2683 * non-blank in the line.
2684 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2685 * the line.
2686 */
2687 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01002688inindent(int extra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689{
2690 char_u *ptr;
2691 colnr_T col;
2692
2693 for (col = 0, ptr = ml_get_curline(); vim_iswhite(*ptr); ++col)
2694 ++ptr;
2695 if (col >= curwin->w_cursor.col + extra)
2696 return TRUE;
2697 else
2698 return FALSE;
2699}
2700
2701/*
2702 * Skip to next part of an option argument: Skip space and comma.
2703 */
2704 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01002705skip_to_option_part(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706{
2707 if (*p == ',')
2708 ++p;
2709 while (*p == ' ')
2710 ++p;
2711 return p;
2712}
2713
2714/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002715 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 *
2717 * Most often called through changed_bytes() and changed_lines(), which also
2718 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002719 *
2720 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 */
2722 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002723changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724{
2725#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2726 /* The text of the preediting area is inserted, but this doesn't
2727 * mean a change of the buffer yet. That is delayed until the
2728 * text is committed. (this means preedit becomes empty) */
2729 if (im_is_preediting() && !xim_changed_while_preediting)
2730 return;
2731 xim_changed_while_preediting = FALSE;
2732#endif
2733
2734 if (!curbuf->b_changed)
2735 {
2736 int save_msg_scroll = msg_scroll;
2737
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002738 /* Give a warning about changing a read-only file. This may also
2739 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002741
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 /* Create a swap file if that is wanted.
2743 * Don't do this for "nofile" and "nowrite" buffer types. */
2744 if (curbuf->b_may_swap
2745#ifdef FEAT_QUICKFIX
2746 && !bt_dontwrite(curbuf)
2747#endif
2748 )
2749 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002750 int save_need_wait_return = need_wait_return;
2751
2752 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 ml_open_file(curbuf);
2754
2755 /* The ml_open_file() can cause an ATTENTION message.
2756 * Wait two seconds, to make sure the user reads this unexpected
2757 * message. Since we could be anywhere, call wait_return() now,
2758 * and don't let the emsg() set msg_scroll. */
2759 if (need_wait_return && emsg_silent == 0)
2760 {
2761 out_flush();
2762 ui_delay(2000L, TRUE);
2763 wait_return(TRUE);
2764 msg_scroll = save_msg_scroll;
2765 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002766 else
2767 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002769 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 }
2771 ++curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772}
2773
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002774/*
2775 * Internal part of changed(), no user interaction.
2776 */
2777 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002778changed_int(void)
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002779{
2780 curbuf->b_changed = TRUE;
2781 ml_setflags(curbuf);
2782#ifdef FEAT_WINDOWS
2783 check_status(curbuf);
2784 redraw_tabline = TRUE;
2785#endif
2786#ifdef FEAT_TITLE
2787 need_maketitle = TRUE; /* set window title later */
2788#endif
2789}
2790
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002791static void changedOneline(buf_T *buf, linenr_T lnum);
2792static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
2793static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794
2795/*
2796 * Changed bytes within a single line for the current buffer.
2797 * - marks the windows on this buffer to be redisplayed
2798 * - marks the buffer changed by calling changed()
2799 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002800 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 */
2802 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002803changed_bytes(linenr_T lnum, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002805 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002807
2808#ifdef FEAT_DIFF
2809 /* Diff highlighting in other diff windows may need to be updated too. */
2810 if (curwin->w_p_diff)
2811 {
2812 win_T *wp;
2813 linenr_T wlnum;
2814
Bram Moolenaar29323592016-07-24 22:04:11 +02002815 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002816 if (wp->w_p_diff && wp != curwin)
2817 {
2818 redraw_win_later(wp, VALID);
2819 wlnum = diff_lnum_win(lnum, wp);
2820 if (wlnum > 0)
2821 changedOneline(wp->w_buffer, wlnum);
2822 }
2823 }
2824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825}
2826
2827 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002828changedOneline(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002830 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 {
2832 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002833 if (lnum < buf->b_mod_top)
2834 buf->b_mod_top = lnum;
2835 else if (lnum >= buf->b_mod_bot)
2836 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 }
2838 else
2839 {
2840 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002841 buf->b_mod_set = TRUE;
2842 buf->b_mod_top = lnum;
2843 buf->b_mod_bot = lnum + 1;
2844 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 }
2846}
2847
2848/*
2849 * Appended "count" lines below line "lnum" in the current buffer.
2850 * Must be called AFTER the change and after mark_adjust().
2851 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2852 */
2853 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002854appended_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855{
2856 changed_lines(lnum + 1, 0, lnum + 1, count);
2857}
2858
2859/*
2860 * Like appended_lines(), but adjust marks first.
2861 */
2862 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002863appended_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864{
Bram Moolenaar82faa252016-06-04 20:14:07 +02002865 /* Skip mark_adjust when adding a line after the last one, there can't
2866 * be marks there. */
2867 if (lnum + count < curbuf->b_ml.ml_line_count)
2868 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 changed_lines(lnum + 1, 0, lnum + 1, count);
2870}
2871
2872/*
2873 * Deleted "count" lines at line "lnum" in the current buffer.
2874 * Must be called AFTER the change and after mark_adjust().
2875 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2876 */
2877 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002878deleted_lines(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879{
2880 changed_lines(lnum, 0, lnum + count, -count);
2881}
2882
2883/*
2884 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002885 * Make sure the cursor is on a valid line before calling, a GUI callback may
2886 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 */
2888 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002889deleted_lines_mark(linenr_T lnum, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890{
2891 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
2892 changed_lines(lnum, 0, lnum + count, -count);
2893}
2894
2895/*
2896 * Changed lines for the current buffer.
2897 * Must be called AFTER the change and after mark_adjust().
2898 * - mark the buffer changed by calling changed()
2899 * - mark the windows on this buffer to be redisplayed
2900 * - invalidate cached values
2901 * "lnum" is the first line that needs displaying, "lnume" the first line
2902 * below the changed lines (BEFORE the change).
2903 * When only inserting lines, "lnum" and "lnume" are equal.
2904 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002905 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 */
2907 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002908changed_lines(
2909 linenr_T lnum, /* first line with change */
2910 colnr_T col, /* column in first line with change */
2911 linenr_T lnume, /* line below last changed line */
2912 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002914 changed_lines_buf(curbuf, lnum, lnume, xtra);
2915
2916#ifdef FEAT_DIFF
2917 if (xtra == 0 && curwin->w_p_diff)
2918 {
2919 /* When the number of lines doesn't change then mark_adjust() isn't
2920 * called and other diff buffers still need to be marked for
2921 * displaying. */
2922 win_T *wp;
2923 linenr_T wlnum;
2924
Bram Moolenaar29323592016-07-24 22:04:11 +02002925 FOR_ALL_WINDOWS(wp)
Bram Moolenaardba8a912005-04-24 22:08:39 +00002926 if (wp->w_p_diff && wp != curwin)
2927 {
2928 redraw_win_later(wp, VALID);
2929 wlnum = diff_lnum_win(lnum, wp);
2930 if (wlnum > 0)
2931 changed_lines_buf(wp->w_buffer, wlnum,
2932 lnume - lnum + wlnum, 0L);
2933 }
2934 }
2935#endif
2936
2937 changed_common(lnum, col, lnume, xtra);
2938}
2939
2940 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002941changed_lines_buf(
2942 buf_T *buf,
2943 linenr_T lnum, /* first line with change */
2944 linenr_T lnume, /* line below last changed line */
2945 long xtra) /* number of extra lines (negative when deleting) */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002946{
2947 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 {
2949 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002950 if (lnum < buf->b_mod_top)
2951 buf->b_mod_top = lnum;
2952 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 {
2954 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002955 buf->b_mod_bot += xtra;
2956 if (buf->b_mod_bot < lnum)
2957 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00002959 if (lnume + xtra > buf->b_mod_bot)
2960 buf->b_mod_bot = lnume + xtra;
2961 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 }
2963 else
2964 {
2965 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002966 buf->b_mod_set = TRUE;
2967 buf->b_mod_top = lnum;
2968 buf->b_mod_bot = lnume + xtra;
2969 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971}
2972
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002973/*
2974 * Common code for when a change is was made.
2975 * See changed_lines() for the arguments.
2976 * Careful: may trigger autocommands that reload the buffer.
2977 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01002979changed_common(
2980 linenr_T lnum,
2981 colnr_T col,
2982 linenr_T lnume,
2983 long xtra)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984{
2985 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00002986#ifdef FEAT_WINDOWS
2987 tabpage_T *tp;
2988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 int i;
2990#ifdef FEAT_JUMPLIST
2991 int cols;
2992 pos_T *p;
2993 int add;
2994#endif
2995
2996 /* mark the buffer as modified */
2997 changed();
2998
2999 /* set the '. mark */
3000 if (!cmdmod.keepjumps)
3001 {
3002 curbuf->b_last_change.lnum = lnum;
3003 curbuf->b_last_change.col = col;
3004
3005#ifdef FEAT_JUMPLIST
3006 /* Create a new entry if a new undo-able change was started or we
3007 * don't have an entry yet. */
3008 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3009 {
3010 if (curbuf->b_changelistlen == 0)
3011 add = TRUE;
3012 else
3013 {
3014 /* Don't create a new entry when the line number is the same
3015 * as the last one and the column is not too far away. Avoids
3016 * creating many entries for typing "xxxxx". */
3017 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3018 if (p->lnum != lnum)
3019 add = TRUE;
3020 else
3021 {
3022 cols = comp_textwidth(FALSE);
3023 if (cols == 0)
3024 cols = 79;
3025 add = (p->col + cols < col || col + cols < p->col);
3026 }
3027 }
3028 if (add)
3029 {
3030 /* This is the first of a new sequence of undo-able changes
3031 * and it's at some distance of the last change. Use a new
3032 * position in the changelist. */
3033 curbuf->b_new_change = FALSE;
3034
3035 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3036 {
3037 /* changelist is full: remove oldest entry */
3038 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3039 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3040 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003041 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 {
3043 /* Correct position in changelist for other windows on
3044 * this buffer. */
3045 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3046 --wp->w_changelistidx;
3047 }
3048 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003049 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 {
3051 /* For other windows, if the position in the changelist is
3052 * at the end it stays at the end. */
3053 if (wp->w_buffer == curbuf
3054 && wp->w_changelistidx == curbuf->b_changelistlen)
3055 ++wp->w_changelistidx;
3056 }
3057 ++curbuf->b_changelistlen;
3058 }
3059 }
3060 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3061 curbuf->b_last_change;
3062 /* The current window is always after the last change, so that "g,"
3063 * takes you back to it. */
3064 curwin->w_changelistidx = curbuf->b_changelistlen;
3065#endif
3066 }
3067
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003068 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 {
3070 if (wp->w_buffer == curbuf)
3071 {
3072 /* Mark this window to be redrawn later. */
3073 if (wp->w_redr_type < VALID)
3074 wp->w_redr_type = VALID;
3075
3076 /* Check if a change in the buffer has invalidated the cached
3077 * values for the cursor. */
3078#ifdef FEAT_FOLDING
3079 /*
3080 * Update the folds for this window. Can't postpone this, because
3081 * a following operator might work on the whole fold: ">>dd".
3082 */
3083 foldUpdate(wp, lnum, lnume + xtra - 1);
3084
3085 /* The change may cause lines above or below the change to become
3086 * included in a fold. Set lnum/lnume to the first/last line that
3087 * might be displayed differently.
3088 * Set w_cline_folded here as an efficient way to update it when
3089 * inserting lines just above a closed fold. */
3090 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3091 if (wp->w_cursor.lnum == lnum)
3092 wp->w_cline_folded = i;
3093 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3094 if (wp->w_cursor.lnum == lnume)
3095 wp->w_cline_folded = i;
3096
3097 /* If the changed line is in a range of previously folded lines,
3098 * compare with the first line in that range. */
3099 if (wp->w_cursor.lnum <= lnum)
3100 {
3101 i = find_wl_entry(wp, lnum);
3102 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3103 changed_line_abv_curs_win(wp);
3104 }
3105#endif
3106
3107 if (wp->w_cursor.lnum > lnum)
3108 changed_line_abv_curs_win(wp);
3109 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3110 changed_cline_bef_curs_win(wp);
3111 if (wp->w_botline >= lnum)
3112 {
3113 /* Assume that botline doesn't change (inserted lines make
3114 * other lines scroll down below botline). */
3115 approximate_botline_win(wp);
3116 }
3117
3118 /* Check if any w_lines[] entries have become invalid.
3119 * For entries below the change: Correct the lnums for
3120 * inserted/deleted lines. Makes it possible to stop displaying
3121 * after the change. */
3122 for (i = 0; i < wp->w_lines_valid; ++i)
3123 if (wp->w_lines[i].wl_valid)
3124 {
3125 if (wp->w_lines[i].wl_lnum >= lnum)
3126 {
3127 if (wp->w_lines[i].wl_lnum < lnume)
3128 {
3129 /* line included in change */
3130 wp->w_lines[i].wl_valid = FALSE;
3131 }
3132 else if (xtra != 0)
3133 {
3134 /* line below change */
3135 wp->w_lines[i].wl_lnum += xtra;
3136#ifdef FEAT_FOLDING
3137 wp->w_lines[i].wl_lastlnum += xtra;
3138#endif
3139 }
3140 }
3141#ifdef FEAT_FOLDING
3142 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3143 {
3144 /* change somewhere inside this range of folded lines,
3145 * may need to be redrawn */
3146 wp->w_lines[i].wl_valid = FALSE;
3147 }
3148#endif
3149 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003150
3151#ifdef FEAT_FOLDING
3152 /* Take care of side effects for setting w_topline when folds have
3153 * changed. Esp. when the buffer was changed in another window. */
3154 if (hasAnyFolding(wp))
3155 set_topline(wp, wp->w_topline);
3156#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003157 /* relative numbering may require updating more */
3158 if (wp->w_p_rnu)
3159 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 }
3161 }
3162
3163 /* Call update_screen() later, which checks out what needs to be redrawn,
3164 * since it notices b_mod_set and then uses b_mod_*. */
3165 if (must_redraw < VALID)
3166 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003167
3168#ifdef FEAT_AUTOCMD
3169 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003170 if (lnum <= curwin->w_cursor.lnum
3171 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003172 last_cursormoved.lnum = 0;
3173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174}
3175
3176/*
3177 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3178 */
3179 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003180unchanged(
3181 buf_T *buf,
3182 int ff) /* also reset 'fileformat' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003184 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 {
3186 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003187 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 if (ff)
3189 save_file_ff(buf);
3190#ifdef FEAT_WINDOWS
3191 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003192 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193#endif
3194#ifdef FEAT_TITLE
3195 need_maketitle = TRUE; /* set window title later */
3196#endif
3197 }
3198 ++buf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199#ifdef FEAT_NETBEANS_INTG
3200 netbeans_unmodified(buf);
3201#endif
3202}
3203
3204#if defined(FEAT_WINDOWS) || defined(PROTO)
3205/*
3206 * check_status: called when the status bars for the buffer 'buf'
3207 * need to be updated
3208 */
3209 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003210check_status(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211{
3212 win_T *wp;
3213
Bram Moolenaar29323592016-07-24 22:04:11 +02003214 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 if (wp->w_buffer == buf && wp->w_status_height)
3216 {
3217 wp->w_redr_status = TRUE;
3218 if (must_redraw < VALID)
3219 must_redraw = VALID;
3220 }
3221}
3222#endif
3223
3224/*
3225 * If the file is readonly, give a warning message with the first change.
3226 * Don't do this for autocommands.
3227 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003228 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003230 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 */
3232 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003233change_warning(
3234 int col) /* column for message; non-zero when in insert
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 mode and 'showmode' is on */
3236{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003237 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3238
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 if (curbuf->b_did_warn == FALSE
3240 && curbufIsChanged() == 0
3241#ifdef FEAT_AUTOCMD
3242 && !autocmd_busy
3243#endif
3244 && curbuf->b_p_ro)
3245 {
3246#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003247 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003249 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 if (!curbuf->b_p_ro)
3251 return;
3252#endif
3253 /*
3254 * Do what msg() does, but with a column offset if the warning should
3255 * be after the mode message.
3256 */
3257 msg_start();
3258 if (msg_row == Rows - 1)
3259 msg_col = col;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003260 msg_source(hl_attr(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003261 MSG_PUTS_ATTR(_(w_readonly), hl_attr(HLF_W) | MSG_HIST);
3262#ifdef FEAT_EVAL
3263 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 msg_clr_eos();
3266 (void)msg_end();
3267 if (msg_silent == 0 && !silent_mode)
3268 {
3269 out_flush();
3270 ui_delay(1000L, TRUE); /* give the user time to think about it */
3271 }
3272 curbuf->b_did_warn = TRUE;
3273 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3274 if (msg_row < Rows - 1)
3275 showmode();
3276 }
3277}
3278
3279/*
3280 * Ask for a reply from the user, a 'y' or a 'n'.
3281 * No other characters are accepted, the message is repeated until a valid
3282 * reply is entered or CTRL-C is hit.
3283 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3284 * from any buffers but directly from the user.
3285 *
3286 * return the 'y' or 'n'
3287 */
3288 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003289ask_yesno(char_u *str, int direct)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290{
3291 int r = ' ';
3292 int save_State = State;
3293
3294 if (exiting) /* put terminal in raw mode for this question */
3295 settmode(TMODE_RAW);
3296 ++no_wait_return;
3297#ifdef USE_ON_FLY_SCROLL
3298 dont_scroll = TRUE; /* disallow scrolling here */
3299#endif
3300 State = CONFIRM; /* mouse behaves like with :confirm */
3301#ifdef FEAT_MOUSE
3302 setmouse(); /* disables mouse for xterm */
3303#endif
3304 ++no_mapping;
3305 ++allow_keys; /* no mapping here, but recognize keys */
3306
3307 while (r != 'y' && r != 'n')
3308 {
3309 /* same highlighting as for wait_return */
3310 smsg_attr(hl_attr(HLF_R), (char_u *)"%s (y/n)?", str);
3311 if (direct)
3312 r = get_keystroke();
3313 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003314 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 if (r == Ctrl_C || r == ESC)
3316 r = 'n';
3317 msg_putchar(r); /* show what you typed */
3318 out_flush();
3319 }
3320 --no_wait_return;
3321 State = save_State;
3322#ifdef FEAT_MOUSE
3323 setmouse();
3324#endif
3325 --no_mapping;
3326 --allow_keys;
3327
3328 return r;
3329}
3330
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003331#if defined(FEAT_MOUSE) || defined(PROTO)
3332/*
3333 * Return TRUE if "c" is a mouse key.
3334 */
3335 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003336is_mouse_key(int c)
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003337{
3338 return c == K_LEFTMOUSE
3339 || c == K_LEFTMOUSE_NM
3340 || c == K_LEFTDRAG
3341 || c == K_LEFTRELEASE
3342 || c == K_LEFTRELEASE_NM
3343 || c == K_MIDDLEMOUSE
3344 || c == K_MIDDLEDRAG
3345 || c == K_MIDDLERELEASE
3346 || c == K_RIGHTMOUSE
3347 || c == K_RIGHTDRAG
3348 || c == K_RIGHTRELEASE
3349 || c == K_MOUSEDOWN
3350 || c == K_MOUSEUP
3351 || c == K_MOUSELEFT
3352 || c == K_MOUSERIGHT
3353 || c == K_X1MOUSE
3354 || c == K_X1DRAG
3355 || c == K_X1RELEASE
3356 || c == K_X2MOUSE
3357 || c == K_X2DRAG
3358 || c == K_X2RELEASE;
3359}
3360#endif
3361
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362/*
3363 * Get a key stroke directly from the user.
3364 * Ignores mouse clicks and scrollbar events, except a click for the left
3365 * button (used at the more prompt).
3366 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3367 * Disadvantage: typeahead is ignored.
3368 * Translates the interrupt character for unix to ESC.
3369 */
3370 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003371get_keystroke(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003373 char_u *buf = NULL;
3374 int buflen = 150;
3375 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 int len = 0;
3377 int n;
3378 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003379 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380
3381 mapped_ctrl_c = FALSE; /* mappings are not used here */
3382 for (;;)
3383 {
3384 cursor_on();
3385 out_flush();
3386
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003387 /* Leave some room for check_termcode() to insert a key code into (max
3388 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3389 * bytes. */
3390 maxlen = (buflen - 6 - len) / 3;
3391 if (buf == NULL)
3392 buf = alloc(buflen);
3393 else if (maxlen < 10)
3394 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003395 char_u *t_buf = buf;
3396
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003397 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003398 * escape sequence. */
3399 buflen += 100;
3400 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003401 if (buf == NULL)
3402 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003403 maxlen = (buflen - 6 - len) / 3;
3404 }
3405 if (buf == NULL)
3406 {
3407 do_outofmem_msg((long_u)buflen);
3408 return ESC; /* panic! */
3409 }
3410
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003412 * terminal code to complete. */
3413 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 if (n > 0)
3415 {
3416 /* Replace zero and CSI by a special key code. */
Bram Moolenaar6bff02e2016-08-16 22:50:55 +02003417 n = fix_input_buffer(buf + len, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003419 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003421 else if (len > 0)
3422 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423
Bram Moolenaar4395a712006-09-05 18:57:57 +00003424 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003425 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003426 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003428
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003429 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003430 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003431 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003432 {
3433 /* Redrawing was postponed, do it now. */
3434 update_screen(0);
3435 setcursor(); /* put cursor back where it belongs */
3436 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003437 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003438 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003439 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003441 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 continue;
3443
3444 /* Handle modifier and/or special key code. */
3445 n = buf[0];
3446 if (n == K_SPECIAL)
3447 {
3448 n = TO_SPECIAL(buf[1], buf[2]);
3449 if (buf[1] == KS_MODIFIER
3450 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003451#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003452 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003453#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003454#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 || n == K_VER_SCROLLBAR
3456 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457#endif
3458 )
3459 {
3460 if (buf[1] == KS_MODIFIER)
3461 mod_mask = buf[2];
3462 len -= 3;
3463 if (len > 0)
3464 mch_memmove(buf, buf + 3, (size_t)len);
3465 continue;
3466 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003467 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 }
3469#ifdef FEAT_MBYTE
3470 if (has_mbyte)
3471 {
3472 if (MB_BYTE2LEN(n) > len)
3473 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003474 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 n = (*mb_ptr2char)(buf);
3476 }
3477#endif
3478#ifdef UNIX
3479 if (n == intr_char)
3480 n = ESC;
3481#endif
3482 break;
3483 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003484 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485
3486 mapped_ctrl_c = save_mapped_ctrl_c;
3487 return n;
3488}
3489
3490/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003491 * Get a number from the user.
3492 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 */
3494 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003495get_number(
3496 int colon, /* allow colon to abort */
3497 int *mouse_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498{
3499 int n = 0;
3500 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003501 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003503 if (mouse_used != NULL)
3504 *mouse_used = FALSE;
3505
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 /* When not printing messages, the user won't know what to type, return a
3507 * zero (as if CR was hit). */
3508 if (msg_silent != 0)
3509 return 0;
3510
3511#ifdef USE_ON_FLY_SCROLL
3512 dont_scroll = TRUE; /* disallow scrolling here */
3513#endif
3514 ++no_mapping;
3515 ++allow_keys; /* no mapping here, but recognize keys */
3516 for (;;)
3517 {
3518 windgoto(msg_row, msg_col);
3519 c = safe_vgetc();
3520 if (VIM_ISDIGIT(c))
3521 {
3522 n = n * 10 + c - '0';
3523 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003524 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 }
3526 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3527 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003528 if (typed > 0)
3529 {
3530 MSG_PUTS("\b \b");
3531 --typed;
3532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003535#ifdef FEAT_MOUSE
3536 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3537 {
3538 *mouse_used = TRUE;
3539 n = mouse_row + 1;
3540 break;
3541 }
3542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 else if (n == 0 && c == ':' && colon)
3544 {
3545 stuffcharReadbuff(':');
3546 if (!exmode_active)
3547 cmdline_row = msg_row;
3548 skip_redraw = TRUE; /* skip redraw once */
3549 do_redraw = FALSE;
3550 break;
3551 }
3552 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3553 break;
3554 }
3555 --no_mapping;
3556 --allow_keys;
3557 return n;
3558}
3559
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003560/*
3561 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003562 * When "mouse_used" is not NULL allow using the mouse and in that case return
3563 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003564 */
3565 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01003566prompt_for_number(int *mouse_used)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003567{
3568 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003569 int save_cmdline_row;
3570 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003571
3572 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003573 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003574 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003575 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003576 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003577
Bram Moolenaar203335e2006-09-03 14:35:42 +00003578 /* Set the state such that text can be selected/copied/pasted and we still
3579 * get mouse events. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003580 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003581 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003582 save_State = State;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003583 State = CMDLINE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003584
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003585 i = get_number(TRUE, mouse_used);
3586 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003587 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003588 /* don't call wait_return() now */
3589 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003590 cmdline_row = msg_row - 1;
3591 need_wait_return = FALSE;
3592 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003593 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003594 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003595 else
3596 cmdline_row = save_cmdline_row;
3597 State = save_State;
3598
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003599 return i;
3600}
3601
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003603msgmore(long n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604{
3605 long pn;
3606
3607 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3609 return;
3610
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003611 /* We don't want to overwrite another important message, but do overwrite
3612 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3613 * then "put" reports the last action. */
3614 if (keep_msg != NULL && !keep_msg_more)
3615 return;
3616
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 if (n > 0)
3618 pn = n;
3619 else
3620 pn = -n;
3621
3622 if (pn > p_report)
3623 {
3624 if (pn == 1)
3625 {
3626 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003627 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3628 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003630 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3631 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 }
3633 else
3634 {
3635 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003636 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3637 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003639 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3640 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 }
3642 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003643 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 if (msg(msg_buf))
3645 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003646 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003647 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 }
3649 }
3650}
3651
3652/*
3653 * flush map and typeahead buffers and give a warning for an error
3654 */
3655 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003656beep_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657{
3658 if (emsg_silent == 0)
3659 {
3660 flush_buffers(FALSE);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003661 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 }
3663}
3664
3665/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003666 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 */
3668 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003669vim_beep(
3670 unsigned val) /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671{
3672 if (emsg_silent == 0)
3673 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003674 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3675 {
3676 if (p_vb
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677#ifdef FEAT_GUI
Bram Moolenaar165bc692015-07-21 17:53:25 +02003678 /* While the GUI is starting up the termcap is set for the
3679 * GUI but the output still goes to a terminal. */
3680 && !(gui.in_use && gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681#endif
Bram Moolenaar165bc692015-07-21 17:53:25 +02003682 )
Bram Moolenaar165bc692015-07-21 17:53:25 +02003683 out_str(T_VB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02003685 out_char(BELL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003687
3688 /* When 'verbose' is set and we are sourcing a script or executing a
3689 * function give the user a hint where the beep comes from. */
3690 if (vim_strchr(p_debug, 'e') != NULL)
3691 {
3692 msg_source(hl_attr(HLF_W));
3693 msg_attr((char_u *)_("Beep!"), hl_attr(HLF_W));
3694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 }
3696}
3697
3698/*
3699 * To get the "real" home directory:
3700 * - get value of $HOME
3701 * For Unix:
3702 * - go to that directory
3703 * - do mch_dirname() to get the real name of that directory.
3704 * This also works with mounts and links.
3705 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3706 */
3707static char_u *homedir = NULL;
3708
3709 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003710init_homedir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711{
3712 char_u *var;
3713
Bram Moolenaar05159a02005-02-26 23:04:13 +00003714 /* In case we are called a second time (when 'encoding' changes). */
3715 vim_free(homedir);
3716 homedir = NULL;
3717
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718#ifdef VMS
3719 var = mch_getenv((char_u *)"SYS$LOGIN");
3720#else
3721 var = mch_getenv((char_u *)"HOME");
3722#endif
3723
3724 if (var != NULL && *var == NUL) /* empty is same as not set */
3725 var = NULL;
3726
3727#ifdef WIN3264
3728 /*
3729 * Weird but true: $HOME may contain an indirect reference to another
3730 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3731 * when $HOME is being set.
3732 */
3733 if (var != NULL && *var == '%')
3734 {
3735 char_u *p;
3736 char_u *exp;
3737
3738 p = vim_strchr(var + 1, '%');
3739 if (p != NULL)
3740 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003741 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 exp = mch_getenv(NameBuff);
3743 if (exp != NULL && *exp != NUL
3744 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3745 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003746 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 var = NameBuff;
3748 /* Also set $HOME, it's needed for _viminfo. */
3749 vim_setenv((char_u *)"HOME", NameBuff);
3750 }
3751 }
3752 }
3753
3754 /*
3755 * Typically, $HOME is not defined on Windows, unless the user has
3756 * specifically defined it for Vim's sake. However, on Windows NT
3757 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3758 * each user. Try constructing $HOME from these.
3759 */
3760 if (var == NULL)
3761 {
3762 char_u *homedrive, *homepath;
3763
3764 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3765 homepath = mch_getenv((char_u *)"HOMEPATH");
Bram Moolenaar6f977012010-01-06 17:53:38 +01003766 if (homepath == NULL || *homepath == NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003767 homepath = (char_u *)"\\";
Bram Moolenaar6f977012010-01-06 17:53:38 +01003768 if (homedrive != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3770 {
3771 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3772 if (NameBuff[0] != NUL)
3773 {
3774 var = NameBuff;
3775 /* Also set $HOME, it's needed for _viminfo. */
3776 vim_setenv((char_u *)"HOME", NameBuff);
3777 }
3778 }
3779 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003780
3781# if defined(FEAT_MBYTE)
3782 if (enc_utf8 && var != NULL)
3783 {
3784 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003785 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003786
3787 /* Convert from active codepage to UTF-8. Other conversions are
3788 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003789 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003790 if (pp != NULL)
3791 {
3792 homedir = pp;
3793 return;
3794 }
3795 }
3796# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797#endif
3798
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003799#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 /*
3801 * Default home dir is C:/
3802 * Best assumption we can make in such a situation.
3803 */
3804 if (var == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003805 var = (char_u *)"C:/";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806#endif
3807 if (var != NULL)
3808 {
3809#ifdef UNIX
3810 /*
3811 * Change to the directory and get the actual path. This resolves
3812 * links. Don't do it when we can't return.
3813 */
3814 if (mch_dirname(NameBuff, MAXPATHL) == OK
3815 && mch_chdir((char *)NameBuff) == 0)
3816 {
3817 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3818 var = IObuff;
3819 if (mch_chdir((char *)NameBuff) != 0)
3820 EMSG(_(e_prev_dir));
3821 }
3822#endif
3823 homedir = vim_strsave(var);
3824 }
3825}
3826
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003827#if defined(EXITFREE) || defined(PROTO)
3828 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003829free_homedir(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003830{
3831 vim_free(homedir);
3832}
Bram Moolenaar24305862012-08-15 14:05:05 +02003833
3834# ifdef FEAT_CMDL_COMPL
3835 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003836free_users(void)
Bram Moolenaar24305862012-08-15 14:05:05 +02003837{
3838 ga_clear_strings(&ga_users);
3839}
3840# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003841#endif
3842
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003844 * Call expand_env() and store the result in an allocated string.
3845 * This is not very memory efficient, this expects the result to be freed
3846 * again soon.
3847 */
3848 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003849expand_env_save(char_u *src)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003850{
3851 return expand_env_save_opt(src, FALSE);
3852}
3853
3854/*
3855 * Idem, but when "one" is TRUE handle the string as one file name, only
3856 * expand "~" at the start.
3857 */
3858 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01003859expand_env_save_opt(char_u *src, int one)
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003860{
3861 char_u *p;
3862
3863 p = alloc(MAXPATHL);
3864 if (p != NULL)
3865 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3866 return p;
3867}
3868
3869/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 * Expand environment variable with path name.
3871 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003872 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 * If anything fails no expansion is done and dst equals src.
3874 */
3875 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003876expand_env(
3877 char_u *src, /* input string e.g. "$HOME/vim.hlp" */
3878 char_u *dst, /* where to put the result */
3879 int dstlen) /* maximum length of the result */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003881 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882}
3883
3884 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01003885expand_env_esc(
3886 char_u *srcp, /* input string e.g. "$HOME/vim.hlp" */
3887 char_u *dst, /* where to put the result */
3888 int dstlen, /* maximum length of the result */
3889 int esc, /* escape spaces in expanded variables */
3890 int one, /* "srcp" is one file name */
3891 char_u *startstr) /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003893 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 char_u *tail;
3895 int c;
3896 char_u *var;
3897 int copy_char;
3898 int mustfree; /* var was allocated, need to free it later */
3899 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003900 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003902 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003903 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003904
3905 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 --dstlen; /* leave one char space for "\," */
3907 while (*src && dstlen > 0)
3908 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003909#ifdef FEAT_EVAL
3910 /* Skip over `=expr`. */
3911 if (src[0] == '`' && src[1] == '=')
3912 {
3913 size_t len;
3914
3915 var = src;
3916 src += 2;
3917 (void)skip_expr(&src);
3918 if (*src == '`')
3919 ++src;
3920 len = src - var;
3921 if (len > (size_t)dstlen)
3922 len = dstlen;
3923 vim_strncpy(dst, var, len);
3924 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02003925 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003926 continue;
3927 }
3928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003930 if ((*src == '$'
3931#ifdef VMS
3932 && at_start
3933#endif
3934 )
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003935#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 || *src == '%'
3937#endif
3938 || (*src == '~' && at_start))
3939 {
3940 mustfree = FALSE;
3941
3942 /*
3943 * The variable name is copied into dst temporarily, because it may
3944 * be a string in read-only memory and a NUL needs to be appended.
3945 */
3946 if (*src != '~') /* environment var */
3947 {
3948 tail = src + 1;
3949 var = dst;
3950 c = dstlen - 1;
3951
3952#ifdef UNIX
3953 /* Unix has ${var-name} type environment vars */
3954 if (*tail == '{' && !vim_isIDc('{'))
3955 {
3956 tail++; /* ignore '{' */
3957 while (c-- > 0 && *tail && *tail != '}')
3958 *var++ = *tail++;
3959 }
3960 else
3961#endif
3962 {
3963 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003964#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 || (*src == '%' && *tail != '%')
3966#endif
3967 ))
3968 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 }
3971 }
3972
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003973#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974# ifdef UNIX
3975 if (src[1] == '{' && *tail != '}')
3976# else
3977 if (*src == '%' && *tail != '%')
3978# endif
3979 var = NULL;
3980 else
3981 {
3982# ifdef UNIX
3983 if (src[1] == '{')
3984# else
3985 if (*src == '%')
3986#endif
3987 ++tail;
3988#endif
3989 *var = NUL;
3990 var = vim_getenv(dst, &mustfree);
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003991#if defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 }
3993#endif
3994 }
3995 /* home directory */
3996 else if ( src[1] == NUL
3997 || vim_ispathsep(src[1])
3998 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
3999 {
4000 var = homedir;
4001 tail = src + 1;
4002 }
4003 else /* user directory */
4004 {
4005#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4006 /*
4007 * Copy ~user to dst[], so we can put a NUL after it.
4008 */
4009 tail = src;
4010 var = dst;
4011 c = dstlen - 1;
4012 while ( c-- > 0
4013 && *tail
4014 && vim_isfilec(*tail)
4015 && !vim_ispathsep(*tail))
4016 *var++ = *tail++;
4017 *var = NUL;
4018# ifdef UNIX
4019 /*
4020 * If the system supports getpwnam(), use it.
4021 * Otherwise, or if getpwnam() fails, the shell is used to
4022 * expand ~user. This is slower and may fail if the shell
4023 * does not support ~user (old versions of /bin/sh).
4024 */
4025# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4026 {
4027 struct passwd *pw;
4028
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004029 /* Note: memory allocated by getpwnam() is never freed.
4030 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 pw = getpwnam((char *)dst + 1);
4032 if (pw != NULL)
4033 var = (char_u *)pw->pw_dir;
4034 else
4035 var = NULL;
4036 }
4037 if (var == NULL)
4038# endif
4039 {
4040 expand_T xpc;
4041
4042 ExpandInit(&xpc);
4043 xpc.xp_context = EXPAND_FILES;
4044 var = ExpandOne(&xpc, dst, NULL,
4045 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 mustfree = TRUE;
4047 }
4048
4049# else /* !UNIX, thus VMS */
4050 /*
4051 * USER_HOME is a comma-separated list of
4052 * directories to search for the user account in.
4053 */
4054 {
4055 char_u test[MAXPATHL], paths[MAXPATHL];
4056 char_u *path, *next_path, *ptr;
Bram Moolenaar8767f522016-07-01 17:17:39 +02004057 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058
4059 STRCPY(paths, USER_HOME);
4060 next_path = paths;
4061 while (*next_path)
4062 {
4063 for (path = next_path; *next_path && *next_path != ',';
4064 next_path++);
4065 if (*next_path)
4066 *next_path++ = NUL;
4067 STRCPY(test, path);
4068 STRCAT(test, "/");
4069 STRCAT(test, dst + 1);
4070 if (mch_stat(test, &st) == 0)
4071 {
4072 var = alloc(STRLEN(test) + 1);
4073 STRCPY(var, test);
4074 mustfree = TRUE;
4075 break;
4076 }
4077 }
4078 }
4079# endif /* UNIX */
4080#else
4081 /* cannot expand user's home directory, so don't try */
4082 var = NULL;
4083 tail = (char_u *)""; /* for gcc */
4084#endif /* UNIX || VMS */
4085 }
4086
4087#ifdef BACKSLASH_IN_FILENAME
4088 /* If 'shellslash' is set change backslashes to forward slashes.
4089 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4090 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4091 {
4092 char_u *p = vim_strsave(var);
4093
4094 if (p != NULL)
4095 {
4096 if (mustfree)
4097 vim_free(var);
4098 var = p;
4099 mustfree = TRUE;
4100 forward_slash(var);
4101 }
4102 }
4103#endif
4104
4105 /* If "var" contains white space, escape it with a backslash.
4106 * Required for ":e ~/tt" when $HOME includes a space. */
4107 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4108 {
4109 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4110
4111 if (p != NULL)
4112 {
4113 if (mustfree)
4114 vim_free(var);
4115 var = p;
4116 mustfree = TRUE;
4117 }
4118 }
4119
4120 if (var != NULL && *var != NUL
4121 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4122 {
4123 STRCPY(dst, var);
4124 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004125 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 /* if var[] ends in a path separator and tail[] starts
4127 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004128 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4130 && dst[-1] != ':'
4131#endif
4132 && vim_ispathsep(*tail))
4133 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004134 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 src = tail;
4136 copy_char = FALSE;
4137 }
4138 if (mustfree)
4139 vim_free(var);
4140 }
4141
4142 if (copy_char) /* copy at least one char */
4143 {
4144 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004145 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004146 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4147 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 */
4149 at_start = FALSE;
4150 if (src[0] == '\\' && src[1] != NUL)
4151 {
4152 *dst++ = *src++;
4153 --dstlen;
4154 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004155 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 at_start = TRUE;
4157 *dst++ = *src++;
4158 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004159
4160 if (startstr != NULL && src - startstr_len >= srcp
4161 && STRNCMP(src - startstr_len, startstr, startstr_len) == 0)
4162 at_start = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 }
4164 }
4165 *dst = NUL;
4166}
4167
4168/*
4169 * Vim's version of getenv().
4170 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004171 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004172 * "mustfree" is set to TRUE when returned is allocated, it must be
4173 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 */
4175 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004176vim_getenv(char_u *name, int *mustfree)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177{
4178 char_u *p;
4179 char_u *pend;
4180 int vimruntime;
4181
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004182#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 /* use "C:/" when $HOME is not set */
4184 if (STRCMP(name, "HOME") == 0)
4185 return homedir;
4186#endif
4187
4188 p = mch_getenv(name);
4189 if (p != NULL && *p == NUL) /* empty is the same as not set */
4190 p = NULL;
4191
4192 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004193 {
4194#if defined(FEAT_MBYTE) && defined(WIN3264)
4195 if (enc_utf8)
4196 {
4197 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004198 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004199
4200 /* Convert from active codepage to UTF-8. Other conversions are
4201 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004202 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004203 if (pp != NULL)
4204 {
4205 p = pp;
4206 *mustfree = TRUE;
4207 }
4208 }
4209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212
4213 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4214 if (!vimruntime && STRCMP(name, "VIM") != 0)
4215 return NULL;
4216
4217 /*
4218 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4219 * Don't do this when default_vimruntime_dir is non-empty.
4220 */
4221 if (vimruntime
4222#ifdef HAVE_PATHDEF
4223 && *default_vimruntime_dir == NUL
4224#endif
4225 )
4226 {
4227 p = mch_getenv((char_u *)"VIM");
4228 if (p != NULL && *p == NUL) /* empty is the same as not set */
4229 p = NULL;
4230 if (p != NULL)
4231 {
4232 p = vim_version_dir(p);
4233 if (p != NULL)
4234 *mustfree = TRUE;
4235 else
4236 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004237
4238#if defined(FEAT_MBYTE) && defined(WIN3264)
4239 if (enc_utf8)
4240 {
4241 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004242 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004243
4244 /* Convert from active codepage to UTF-8. Other conversions
4245 * are not done, because they would fail for non-ASCII
4246 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004247 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004248 if (pp != NULL)
4249 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004250 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004251 vim_free(p);
4252 p = pp;
4253 *mustfree = TRUE;
4254 }
4255 }
4256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 }
4258 }
4259
4260 /*
4261 * When expanding $VIM or $VIMRUNTIME fails, try using:
4262 * - the directory name from 'helpfile' (unless it contains '$')
4263 * - the executable name from argv[0]
4264 */
4265 if (p == NULL)
4266 {
4267 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4268 p = p_hf;
4269#ifdef USE_EXE_NAME
4270 /*
4271 * Use the name of the executable, obtained from argv[0].
4272 */
4273 else
4274 p = exe_name;
4275#endif
4276 if (p != NULL)
4277 {
4278 /* remove the file name */
4279 pend = gettail(p);
4280
4281 /* remove "doc/" from 'helpfile', if present */
4282 if (p == p_hf)
4283 pend = remove_tail(p, pend, (char_u *)"doc");
4284
4285#ifdef USE_EXE_NAME
4286# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004287 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 if (p == exe_name)
4289 {
4290 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004291 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004293 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4294 if (pend1 != pend)
4295 {
4296 pnew = alloc((unsigned)(pend1 - p) + 15);
4297 if (pnew != NULL)
4298 {
4299 STRNCPY(pnew, p, (pend1 - p));
4300 STRCPY(pnew + (pend1 - p), "Resources/vim");
4301 p = pnew;
4302 pend = p + STRLEN(p);
4303 }
4304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 }
4306# endif
4307 /* remove "src/" from exe_name, if present */
4308 if (p == exe_name)
4309 pend = remove_tail(p, pend, (char_u *)"src");
4310#endif
4311
4312 /* for $VIM, remove "runtime/" or "vim54/", if present */
4313 if (!vimruntime)
4314 {
4315 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4316 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4317 }
4318
4319 /* remove trailing path separator */
4320#ifndef MACOS_CLASSIC
4321 /* With MacOS path (with colons) the final colon is required */
Bram Moolenaare21877a2008-02-13 09:58:14 +00004322 /* to avoid confusion between absolute and relative path */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004323 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 --pend;
4325#endif
4326
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004327#ifdef MACOS_X
4328 if (p == exe_name || p == p_hf)
4329#endif
4330 /* check that the result is a directory name */
4331 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332
4333 if (p != NULL && !mch_isdir(p))
4334 {
4335 vim_free(p);
4336 p = NULL;
4337 }
4338 else
4339 {
4340#ifdef USE_EXE_NAME
4341 /* may add "/vim54" or "/runtime" if it exists */
4342 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4343 {
4344 vim_free(p);
4345 p = pend;
4346 }
4347#endif
4348 *mustfree = TRUE;
4349 }
4350 }
4351 }
4352
4353#ifdef HAVE_PATHDEF
4354 /* When there is a pathdef.c file we can use default_vim_dir and
4355 * default_vimruntime_dir */
4356 if (p == NULL)
4357 {
4358 /* Only use default_vimruntime_dir when it is not empty */
4359 if (vimruntime && *default_vimruntime_dir != NUL)
4360 {
4361 p = default_vimruntime_dir;
4362 *mustfree = FALSE;
4363 }
4364 else if (*default_vim_dir != NUL)
4365 {
4366 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4367 *mustfree = TRUE;
4368 else
4369 {
4370 p = default_vim_dir;
4371 *mustfree = FALSE;
4372 }
4373 }
4374 }
4375#endif
4376
4377 /*
4378 * Set the environment variable, so that the new value can be found fast
4379 * next time, and others can also use it (e.g. Perl).
4380 */
4381 if (p != NULL)
4382 {
4383 if (vimruntime)
4384 {
4385 vim_setenv((char_u *)"VIMRUNTIME", p);
4386 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 }
4388 else
4389 {
4390 vim_setenv((char_u *)"VIM", p);
4391 didset_vim = TRUE;
4392 }
4393 }
4394 return p;
4395}
4396
4397/*
4398 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4399 * Return NULL if not, return its name in allocated memory otherwise.
4400 */
4401 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004402vim_version_dir(char_u *vimdir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403{
4404 char_u *p;
4405
4406 if (vimdir == NULL || *vimdir == NUL)
4407 return NULL;
4408 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4409 if (p != NULL && mch_isdir(p))
4410 return p;
4411 vim_free(p);
4412 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4413 if (p != NULL && mch_isdir(p))
4414 return p;
4415 vim_free(p);
4416 return NULL;
4417}
4418
4419/*
4420 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4421 * the length of "name/". Otherwise return "pend".
4422 */
4423 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004424remove_tail(char_u *p, char_u *pend, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425{
4426 int len = (int)STRLEN(name) + 1;
4427 char_u *newend = pend - len;
4428
4429 if (newend >= p
4430 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004431 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 return newend;
4433 return pend;
4434}
4435
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 * Our portable version of setenv.
4438 */
4439 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004440vim_setenv(char_u *name, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441{
4442#ifdef HAVE_SETENV
4443 mch_setenv((char *)name, (char *)val, 1);
4444#else
4445 char_u *envbuf;
4446
4447 /*
4448 * Putenv does not copy the string, it has to remain
4449 * valid. The allocated memory will never be freed.
4450 */
4451 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4452 if (envbuf != NULL)
4453 {
4454 sprintf((char *)envbuf, "%s=%s", name, val);
4455 putenv((char *)envbuf);
4456 }
4457#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004458#ifdef FEAT_GETTEXT
4459 /*
4460 * When setting $VIMRUNTIME adjust the directory to find message
4461 * translations to $VIMRUNTIME/lang.
4462 */
4463 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4464 {
4465 char_u *buf = concat_str(val, (char_u *)"/lang");
4466
4467 if (buf != NULL)
4468 {
4469 bindtextdomain(VIMPACKAGE, (char *)buf);
4470 vim_free(buf);
4471 }
4472 }
4473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474}
4475
4476#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4477/*
4478 * Function given to ExpandGeneric() to obtain an environment variable name.
4479 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004481get_env_name(
4482 expand_T *xp UNUSED,
4483 int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484{
4485# if defined(AMIGA) || defined(__MRC__) || defined(__SC__)
4486 /*
4487 * No environ[] on the Amiga and on the Mac (using MPW).
4488 */
4489 return NULL;
4490# else
4491# ifndef __WIN32__
4492 /* Borland C++ 5.2 has this in a header file. */
4493 extern char **environ;
4494# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004495# define ENVNAMELEN 100
4496 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 char_u *str;
4498 int n;
4499
4500 str = (char_u *)environ[idx];
4501 if (str == NULL)
4502 return NULL;
4503
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004504 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 {
4506 if (str[n] == '=' || str[n] == NUL)
4507 break;
4508 name[n] = str[n];
4509 }
4510 name[n] = NUL;
4511 return name;
4512# endif
4513}
Bram Moolenaar24305862012-08-15 14:05:05 +02004514
4515/*
4516 * Find all user names for user completion.
4517 * Done only once and then cached.
4518 */
4519 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004520init_users(void)
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004521{
Bram Moolenaar24305862012-08-15 14:05:05 +02004522 static int lazy_init_done = FALSE;
4523
4524 if (lazy_init_done)
4525 return;
4526
4527 lazy_init_done = TRUE;
4528 ga_init2(&ga_users, sizeof(char_u *), 20);
4529
4530# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4531 {
4532 char_u* user;
4533 struct passwd* pw;
4534
4535 setpwent();
4536 while ((pw = getpwent()) != NULL)
4537 /* pw->pw_name shouldn't be NULL but just in case... */
4538 if (pw->pw_name != NULL)
4539 {
4540 if (ga_grow(&ga_users, 1) == FAIL)
4541 break;
4542 user = vim_strsave((char_u*)pw->pw_name);
4543 if (user == NULL)
4544 break;
4545 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4546 }
4547 endpwent();
4548 }
4549# endif
4550}
4551
4552/*
4553 * Function given to ExpandGeneric() to obtain an user names.
4554 */
4555 char_u*
Bram Moolenaar9b578142016-01-30 19:39:49 +01004556get_users(expand_T *xp UNUSED, int idx)
Bram Moolenaar24305862012-08-15 14:05:05 +02004557{
4558 init_users();
4559 if (idx < ga_users.ga_len)
4560 return ((char_u **)ga_users.ga_data)[idx];
4561 return NULL;
4562}
4563
4564/*
4565 * Check whether name matches a user name. Return:
4566 * 0 if name does not match any user name.
4567 * 1 if name partially matches the beginning of a user name.
4568 * 2 is name fully matches a user name.
4569 */
Bram Moolenaar9b578142016-01-30 19:39:49 +01004570int match_user(char_u* name)
Bram Moolenaar24305862012-08-15 14:05:05 +02004571{
4572 int i;
4573 int n = (int)STRLEN(name);
4574 int result = 0;
4575
4576 init_users();
4577 for (i = 0; i < ga_users.ga_len; i++)
4578 {
4579 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4580 return 2; /* full match */
4581 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4582 result = 1; /* partial match */
4583 }
4584 return result;
4585}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586#endif
4587
4588/*
4589 * Replace home directory by "~" in each space or comma separated file name in
4590 * 'src'.
4591 * If anything fails (except when out of space) dst equals src.
4592 */
4593 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01004594home_replace(
4595 buf_T *buf, /* when not NULL, check for help files */
4596 char_u *src, /* input file name */
4597 char_u *dst, /* where to put the result */
4598 int dstlen, /* maximum length of the result */
4599 int one) /* if TRUE, only replace one file name, include
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 spaces and commas in the file name. */
4601{
4602 size_t dirlen = 0, envlen = 0;
4603 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004604 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 char_u *p;
4606
4607 if (src == NULL)
4608 {
4609 *dst = NUL;
4610 return;
4611 }
4612
4613 /*
4614 * If the file is a help file, remove the path completely.
4615 */
4616 if (buf != NULL && buf->b_help)
4617 {
4618 STRCPY(dst, gettail(src));
4619 return;
4620 }
4621
4622 /*
4623 * We check both the value of the $HOME environment variable and the
4624 * "real" home directory.
4625 */
4626 if (homedir != NULL)
4627 dirlen = STRLEN(homedir);
4628
4629#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004630 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004632 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4633#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004634 /* Empty is the same as not set. */
4635 if (homedir_env != NULL && *homedir_env == NUL)
4636 homedir_env = NULL;
4637
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004638#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004639 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004640 {
4641 int usedlen = 0;
4642 int flen;
4643 char_u *fbuf = NULL;
4644
4645 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004646 (void)modify_fname((char_u *)":p", &usedlen,
4647 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004648 flen = (int)STRLEN(homedir_env);
4649 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4650 /* Remove the trailing / that is added to a directory. */
4651 homedir_env[flen - 1] = NUL;
4652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653#endif
4654
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 if (homedir_env != NULL)
4656 envlen = STRLEN(homedir_env);
4657
4658 if (!one)
4659 src = skipwhite(src);
4660 while (*src && dstlen > 0)
4661 {
4662 /*
4663 * Here we are at the beginning of a file name.
4664 * First, check to see if the beginning of the file name matches
4665 * $HOME or the "real" home directory. Check that there is a '/'
4666 * after the match (so that if e.g. the file is "/home/pieter/bla",
4667 * and the home directory is "/home/piet", the file does not end up
4668 * as "~er/bla" (which would seem to indicate the file "bla" in user
4669 * er's home directory)).
4670 */
4671 p = homedir;
4672 len = dirlen;
4673 for (;;)
4674 {
4675 if ( len
4676 && fnamencmp(src, p, len) == 0
4677 && (vim_ispathsep(src[len])
4678 || (!one && (src[len] == ',' || src[len] == ' '))
4679 || src[len] == NUL))
4680 {
4681 src += len;
4682 if (--dstlen > 0)
4683 *dst++ = '~';
4684
4685 /*
4686 * If it's just the home directory, add "/".
4687 */
4688 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4689 *dst++ = '/';
4690 break;
4691 }
4692 if (p == homedir_env)
4693 break;
4694 p = homedir_env;
4695 len = envlen;
4696 }
4697
4698 /* if (!one) skip to separator: space or comma */
4699 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4700 *dst++ = *src++;
4701 /* skip separator */
4702 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4703 *dst++ = *src++;
4704 }
4705 /* if (dstlen == 0) out of space, what to do??? */
4706
4707 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004708
4709 if (homedir_env != homedir_env_orig)
4710 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711}
4712
4713/*
4714 * Like home_replace, store the replaced string in allocated memory.
4715 * When something fails, NULL is returned.
4716 */
4717 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004718home_replace_save(
4719 buf_T *buf, /* when not NULL, check for help files */
4720 char_u *src) /* input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721{
4722 char_u *dst;
4723 unsigned len;
4724
4725 len = 3; /* space for "~/" and trailing NUL */
4726 if (src != NULL) /* just in case */
4727 len += (unsigned)STRLEN(src);
4728 dst = alloc(len);
4729 if (dst != NULL)
4730 home_replace(buf, src, dst, len, TRUE);
4731 return dst;
4732}
4733
4734/*
4735 * Compare two file names and return:
4736 * FPC_SAME if they both exist and are the same file.
4737 * FPC_SAMEX if they both don't exist and have the same file name.
4738 * FPC_DIFF if they both exist and are different files.
4739 * FPC_NOTX if they both don't exist.
4740 * FPC_DIFFX if one of them doesn't exist.
4741 * For the first name environment variables are expanded
4742 */
4743 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004744fullpathcmp(
4745 char_u *s1,
4746 char_u *s2,
4747 int checkname) /* when both don't exist, check file names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748{
4749#ifdef UNIX
4750 char_u exp1[MAXPATHL];
4751 char_u full1[MAXPATHL];
4752 char_u full2[MAXPATHL];
Bram Moolenaar8767f522016-07-01 17:17:39 +02004753 stat_T st1, st2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 int r1, r2;
4755
4756 expand_env(s1, exp1, MAXPATHL);
4757 r1 = mch_stat((char *)exp1, &st1);
4758 r2 = mch_stat((char *)s2, &st2);
4759 if (r1 != 0 && r2 != 0)
4760 {
4761 /* if mch_stat() doesn't work, may compare the names */
4762 if (checkname)
4763 {
4764 if (fnamecmp(exp1, s2) == 0)
4765 return FPC_SAMEX;
4766 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4767 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4768 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4769 return FPC_SAMEX;
4770 }
4771 return FPC_NOTX;
4772 }
4773 if (r1 != 0 || r2 != 0)
4774 return FPC_DIFFX;
4775 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4776 return FPC_SAME;
4777 return FPC_DIFF;
4778#else
4779 char_u *exp1; /* expanded s1 */
4780 char_u *full1; /* full path of s1 */
4781 char_u *full2; /* full path of s2 */
4782 int retval = FPC_DIFF;
4783 int r1, r2;
4784
4785 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4786 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4787 {
4788 full1 = exp1 + MAXPATHL;
4789 full2 = full1 + MAXPATHL;
4790
4791 expand_env(s1, exp1, MAXPATHL);
4792 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4793 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4794
4795 /* If vim_FullName() fails, the file probably doesn't exist. */
4796 if (r1 != OK && r2 != OK)
4797 {
4798 if (checkname && fnamecmp(exp1, s2) == 0)
4799 retval = FPC_SAMEX;
4800 else
4801 retval = FPC_NOTX;
4802 }
4803 else if (r1 != OK || r2 != OK)
4804 retval = FPC_DIFFX;
4805 else if (fnamecmp(full1, full2))
4806 retval = FPC_DIFF;
4807 else
4808 retval = FPC_SAME;
4809 vim_free(exp1);
4810 }
4811 return retval;
4812#endif
4813}
4814
4815/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004816 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004817 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004818 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 */
4820 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004821gettail(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822{
4823 char_u *p1, *p2;
4824
4825 if (fname == NULL)
4826 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01004827 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01004829 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 p1 = p2 + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004831 mb_ptr_adv(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 }
4833 return p1;
4834}
4835
Bram Moolenaar31710262010-08-13 13:36:15 +02004836#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004837static char_u *gettail_dir(char_u *fname);
Bram Moolenaar31710262010-08-13 13:36:15 +02004838
4839/*
4840 * Return the end of the directory name, on the first path
4841 * separator:
4842 * "/path/file", "/path/dir/", "/path//dir", "/file"
4843 * ^ ^ ^ ^
4844 */
4845 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004846gettail_dir(char_u *fname)
Bram Moolenaar31710262010-08-13 13:36:15 +02004847{
4848 char_u *dir_end = fname;
4849 char_u *next_dir_end = fname;
4850 int look_for_sep = TRUE;
4851 char_u *p;
4852
4853 for (p = fname; *p != NUL; )
4854 {
4855 if (vim_ispathsep(*p))
4856 {
4857 if (look_for_sep)
4858 {
4859 next_dir_end = p;
4860 look_for_sep = FALSE;
4861 }
4862 }
4863 else
4864 {
4865 if (!look_for_sep)
4866 dir_end = next_dir_end;
4867 look_for_sep = TRUE;
4868 }
4869 mb_ptr_adv(p);
4870 }
4871 return dir_end;
4872}
4873#endif
4874
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004876 * Get pointer to tail of "fname", including path separators. Putting a NUL
4877 * here leaves the directory name. Takes care of "c:/" and "//".
4878 * Always returns a valid pointer.
4879 */
4880 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004881gettail_sep(char_u *fname)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004882{
4883 char_u *p;
4884 char_u *t;
4885
4886 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4887 t = gettail(fname);
4888 while (t > p && after_pathsep(fname, t))
4889 --t;
4890#ifdef VMS
4891 /* path separator is part of the path */
4892 ++t;
4893#endif
4894 return t;
4895}
4896
4897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 * get the next path component (just after the next path separator).
4899 */
4900 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004901getnextcomp(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902{
4903 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004904 mb_ptr_adv(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 if (*fname)
4906 ++fname;
4907 return fname;
4908}
4909
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910/*
4911 * Get a pointer to one character past the head of a path name.
4912 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4913 * If there is no head, path is returned.
4914 */
4915 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01004916get_past_head(char_u *path)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917{
4918 char_u *retval;
4919
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004920#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 /* may skip "c:" */
4922 if (isalpha(path[0]) && path[1] == ':')
4923 retval = path + 2;
4924 else
4925 retval = path;
4926#else
4927# if defined(AMIGA)
4928 /* may skip "label:" */
4929 retval = vim_strchr(path, ':');
4930 if (retval == NULL)
4931 retval = path;
4932# else /* Unix */
4933 retval = path;
4934# endif
4935#endif
4936
4937 while (vim_ispathsep(*retval))
4938 ++retval;
4939
4940 return retval;
4941}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942
4943/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01004944 * Return TRUE if 'c' is a path separator.
4945 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 */
4947 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004948vim_ispathsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949{
Bram Moolenaare60acc12011-05-10 16:41:25 +02004950#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02004952#else
4953# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004955# else
4956# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
4958 return (c == ':' || c == '[' || c == ']' || c == '/'
4959 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02004960# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004962# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02004964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965}
4966
Bram Moolenaar69c35002013-11-04 02:54:12 +01004967/*
4968 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
4969 */
4970 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004971vim_ispathsep_nocolon(int c)
Bram Moolenaar69c35002013-11-04 02:54:12 +01004972{
4973 return vim_ispathsep(c)
4974#ifdef BACKSLASH_IN_FILENAME
4975 && c != ':'
4976#endif
4977 ;
4978}
4979
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980#if defined(FEAT_SEARCHPATH) || defined(PROTO)
4981/*
4982 * return TRUE if 'c' is a path list separator.
4983 */
4984 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01004985vim_ispathlistsep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986{
4987#ifdef UNIX
4988 return (c == ':');
4989#else
Bram Moolenaar25394022007-05-10 19:06:20 +00004990 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991#endif
4992}
4993#endif
4994
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004995#if defined(FEAT_GUI_TABLINE) || defined(FEAT_WINDOWS) \
4996 || defined(FEAT_EVAL) || defined(PROTO)
4997/*
4998 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
4999 * It's done in-place.
5000 */
5001 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005002shorten_dir(char_u *str)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005003{
5004 char_u *tail, *s, *d;
5005 int skip = FALSE;
5006
5007 tail = gettail(str);
5008 d = str;
5009 for (s = str; ; ++s)
5010 {
5011 if (s >= tail) /* copy the whole tail */
5012 {
5013 *d++ = *s;
5014 if (*s == NUL)
5015 break;
5016 }
5017 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5018 {
5019 *d++ = *s;
5020 skip = FALSE;
5021 }
5022 else if (!skip)
5023 {
5024 *d++ = *s; /* copy next char */
5025 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5026 skip = TRUE;
5027# ifdef FEAT_MBYTE
5028 if (has_mbyte)
5029 {
5030 int l = mb_ptr2len(s);
5031
5032 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005033 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005034 }
5035# endif
5036 }
5037 }
5038}
5039#endif
5040
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005041/*
5042 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5043 * Also returns TRUE if there is no directory name.
5044 * "fname" must be writable!.
5045 */
5046 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005047dir_of_file_exists(char_u *fname)
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005048{
5049 char_u *p;
5050 int c;
5051 int retval;
5052
5053 p = gettail_sep(fname);
5054 if (p == fname)
5055 return TRUE;
5056 c = *p;
5057 *p = NUL;
5058 retval = mch_isdir(fname);
5059 *p = c;
5060 return retval;
5061}
5062
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005064 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5065 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 */
5067 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005068vim_fnamecmp(char_u *x, char_u *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005070#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005072#else
5073 if (p_fic)
5074 return MB_STRICMP(x, y);
5075 return STRCMP(x, y);
5076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077}
5078
5079 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005080vim_fnamencmp(char_u *x, char_u *y, size_t len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005082#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005083 char_u *px = x;
5084 char_u *py = y;
5085 int cx = NUL;
5086 int cy = NUL;
5087
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005088 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005090 cx = PTR2CHAR(px);
5091 cy = PTR2CHAR(py);
5092 if (cx == NUL || cy == NUL
5093 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5094 && !(cx == '/' && cy == '\\')
5095 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005097 len -= MB_PTR2LEN(px);
5098 px += MB_PTR2LEN(px);
5099 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 }
5101 if (len == 0)
5102 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005103 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005104#else
5105 if (p_fic)
5106 return MB_STRNICMP(x, y, len);
5107 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005109}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110
5111/*
5112 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005113 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 */
5115 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005116concat_fnames(char_u *fname1, char_u *fname2, int sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117{
5118 char_u *dest;
5119
5120 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5121 if (dest != NULL)
5122 {
5123 STRCPY(dest, fname1);
5124 if (sep)
5125 add_pathsep(dest);
5126 STRCAT(dest, fname2);
5127 }
5128 return dest;
5129}
5130
Bram Moolenaard6754642005-01-17 22:18:45 +00005131/*
5132 * Concatenate two strings and return the result in allocated memory.
5133 * Returns NULL when out of memory.
5134 */
5135 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005136concat_str(char_u *str1, char_u *str2)
Bram Moolenaard6754642005-01-17 22:18:45 +00005137{
5138 char_u *dest;
5139 size_t l = STRLEN(str1);
5140
5141 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5142 if (dest != NULL)
5143 {
5144 STRCPY(dest, str1);
5145 STRCPY(dest + l, str2);
5146 }
5147 return dest;
5148}
Bram Moolenaard6754642005-01-17 22:18:45 +00005149
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150/*
5151 * Add a path separator to a file name, unless it already ends in a path
5152 * separator.
5153 */
5154 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005155add_pathsep(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005157 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 STRCAT(p, PATHSEPSTR);
5159}
5160
5161/*
5162 * FullName_save - Make an allocated copy of a full file name.
5163 * Returns NULL when out of memory.
5164 */
5165 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005166FullName_save(
5167 char_u *fname,
5168 int force) /* force expansion, even when it already looks
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005169 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170{
5171 char_u *buf;
5172 char_u *new_fname = NULL;
5173
5174 if (fname == NULL)
5175 return NULL;
5176
5177 buf = alloc((unsigned)MAXPATHL);
5178 if (buf != NULL)
5179 {
5180 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5181 new_fname = vim_strsave(buf);
5182 else
5183 new_fname = vim_strsave(fname);
5184 vim_free(buf);
5185 }
5186 return new_fname;
5187}
5188
5189#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5190
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005191static char_u *skip_string(char_u *p);
5192static pos_T *ind_find_start_comment(void);
5193static pos_T *ind_find_start_CORS(void);
5194static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195
5196/*
5197 * Find the start of a comment, not knowing if we are in a comment right now.
5198 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005199 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005201 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005202ind_find_start_comment(void) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005203{
5204 return find_start_comment(curbuf->b_ind_maxcomment);
5205}
5206
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005208find_start_comment(int ind_maxcomment) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209{
5210 pos_T *pos;
5211 char_u *line;
5212 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005213 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005215 for (;;)
5216 {
5217 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5218 if (pos == NULL)
5219 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005221 /*
5222 * Check if the comment start we found is inside a string.
5223 * If it is then restrict the search to below this line and try again.
5224 */
5225 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005226 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005227 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005228 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005229 break;
5230 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5231 if (cur_maxcomment <= 0)
5232 {
5233 pos = NULL;
5234 break;
5235 }
5236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 return pos;
5238}
5239
5240/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005241 * Find the start of a comment or raw string, not knowing if we are in a
5242 * comment or raw string right now.
5243 * Search starts at w_cursor.lnum and goes backwards.
5244 * Return NULL when not inside a comment or raw string.
5245 * "CORS" -> Comment Or Raw String
5246 */
5247 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005248ind_find_start_CORS(void) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005249{
Bram Moolenaar089af182015-10-07 11:41:49 +02005250 static pos_T comment_pos_copy;
5251 pos_T *comment_pos;
5252 pos_T *rs_pos;
5253
5254 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5255 if (comment_pos != NULL)
5256 {
5257 /* Need to make a copy of the static pos in findmatchlimit(),
5258 * calling find_start_rawstring() may change it. */
5259 comment_pos_copy = *comment_pos;
5260 comment_pos = &comment_pos_copy;
5261 }
5262 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005263
5264 /* If comment_pos is before rs_pos the raw string is inside the comment.
5265 * If rs_pos is before comment_pos the comment is inside the raw string. */
5266 if (comment_pos == NULL || (rs_pos != NULL && lt(*rs_pos, *comment_pos)))
5267 return rs_pos;
5268 return comment_pos;
5269}
5270
5271/*
5272 * Find the start of a raw string, not knowing if we are in one right now.
5273 * Search starts at w_cursor.lnum and goes backwards.
5274 * Return NULL when not inside a raw string.
5275 */
5276 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005277find_start_rawstring(int ind_maxcomment) /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005278{
5279 pos_T *pos;
5280 char_u *line;
5281 char_u *p;
5282 int cur_maxcomment = ind_maxcomment;
5283
5284 for (;;)
5285 {
5286 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5287 if (pos == NULL)
5288 break;
5289
5290 /*
5291 * Check if the raw string start we found is inside a string.
5292 * If it is then restrict the search to below this line and try again.
5293 */
5294 line = ml_get(pos->lnum);
5295 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5296 p = skip_string(p);
5297 if ((colnr_T)(p - line) <= pos->col)
5298 break;
5299 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5300 if (cur_maxcomment <= 0)
5301 {
5302 pos = NULL;
5303 break;
5304 }
5305 }
5306 return pos;
5307}
5308
5309/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 * Skip to the end of a "string" and a 'c' character.
5311 * If there is no string or character, return argument unmodified.
5312 */
5313 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005314skip_string(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315{
5316 int i;
5317
5318 /*
5319 * We loop, because strings may be concatenated: "date""time".
5320 */
5321 for ( ; ; ++p)
5322 {
5323 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5324 {
5325 if (!p[1]) /* ' at end of line */
5326 break;
5327 i = 2;
5328 if (p[1] == '\\') /* '\n' or '\000' */
5329 {
5330 ++i;
5331 while (vim_isdigit(p[i - 1])) /* '\000' */
5332 ++i;
5333 }
5334 if (p[i] == '\'') /* check for trailing ' */
5335 {
5336 p += i;
5337 continue;
5338 }
5339 }
5340 else if (p[0] == '"') /* start of string */
5341 {
5342 for (++p; p[0]; ++p)
5343 {
5344 if (p[0] == '\\' && p[1] != NUL)
5345 ++p;
5346 else if (p[0] == '"') /* end of string */
5347 break;
5348 }
5349 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005350 continue; /* continue for another string */
5351 }
5352 else if (p[0] == 'R' && p[1] == '"')
5353 {
5354 /* Raw string: R"[delim](...)[delim]" */
5355 char_u *delim = p + 2;
5356 char_u *paren = vim_strchr(delim, '(');
5357
5358 if (paren != NULL)
5359 {
5360 size_t delim_len = paren - delim;
5361
5362 for (p += 3; *p; ++p)
5363 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5364 && p[delim_len + 1] == '"')
5365 {
5366 p += delim_len + 1;
5367 break;
5368 }
5369 if (p[0] == '"')
5370 continue; /* continue for another string */
5371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 }
5373 break; /* no string found */
5374 }
5375 if (!*p)
5376 --p; /* backup from NUL */
5377 return p;
5378}
5379#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5380
5381#if defined(FEAT_CINDENT) || defined(PROTO)
5382
5383/*
5384 * Do C or expression indenting on the current line.
5385 */
5386 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01005387do_c_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388{
5389# ifdef FEAT_EVAL
5390 if (*curbuf->b_p_inde != NUL)
5391 fixthisline(get_expr_indent);
5392 else
5393# endif
5394 fixthisline(get_c_indent);
5395}
5396
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005397/* Find result cache for cpp_baseclass */
5398typedef struct {
5399 int found;
5400 lpos_T lpos;
5401} cpp_baseclass_cache_T;
5402
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403/*
5404 * Functions for C-indenting.
5405 * Most of this originally comes from Eric Fischer.
5406 */
5407/*
5408 * Below "XXX" means that this function may unlock the current line.
5409 */
5410
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005411static char_u *cin_skipcomment(char_u *);
5412static int cin_nocode(char_u *);
5413static pos_T *find_line_comment(void);
5414static int cin_has_js_key(char_u *text);
5415static int cin_islabel_skip(char_u **);
5416static int cin_isdefault(char_u *);
5417static char_u *after_label(char_u *l);
5418static int get_indent_nolabel(linenr_T lnum);
5419static int skip_label(linenr_T, char_u **pp);
5420static int cin_first_id_amount(void);
5421static int cin_get_equal_amount(linenr_T lnum);
5422static int cin_ispreproc(char_u *);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005423static int cin_iscomment(char_u *);
5424static int cin_islinecomment(char_u *);
5425static int cin_isterminated(char_u *, int, int);
5426static int cin_isinit(void);
5427static int cin_isfuncdecl(char_u **, linenr_T, linenr_T);
5428static int cin_isif(char_u *);
5429static int cin_iselse(char_u *);
5430static int cin_isdo(char_u *);
5431static int cin_iswhileofdo(char_u *, linenr_T);
5432static int cin_is_if_for_while_before_offset(char_u *line, int *poffset);
5433static int cin_iswhileofdo_end(int terminated);
5434static int cin_isbreak(char_u *);
5435static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached);
5436static int get_baseclass_amount(int col);
5437static int cin_ends_in(char_u *, char_u *, char_u *);
5438static int cin_starts_with(char_u *s, char *word);
5439static int cin_skip2pos(pos_T *trypos);
5440static pos_T *find_start_brace(void);
5441static pos_T *find_match_paren(int);
5442static pos_T *find_match_char(int c, int ind_maxparen);
5443static int corr_ind_maxparen(pos_T *startpos);
5444static int find_last_paren(char_u *l, int start, int end);
5445static int find_match(int lookfor, linenr_T ourscope);
5446static int cin_is_cpp_namespace(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447
5448/*
5449 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005450 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 */
5452 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005453cin_skipcomment(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454{
5455 while (*s)
5456 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005457 char_u *prev_s = s;
5458
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005460
5461 /* Perl/shell # comment comment continues until eol. Require a space
5462 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005463 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005464 {
5465 s += STRLEN(s);
5466 break;
5467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 if (*s != '/')
5469 break;
5470 ++s;
5471 if (*s == '/') /* slash-slash comment continues till eol */
5472 {
5473 s += STRLEN(s);
5474 break;
5475 }
5476 if (*s != '*')
5477 break;
5478 for (++s; *s; ++s) /* skip slash-star comment */
5479 if (s[0] == '*' && s[1] == '/')
5480 {
5481 s += 2;
5482 break;
5483 }
5484 }
5485 return s;
5486}
5487
5488/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005489 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 * not considered code.
5491 */
5492 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005493cin_nocode(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494{
5495 return *cin_skipcomment(s) == NUL;
5496}
5497
5498/*
5499 * Check previous lines for a "//" line comment, skipping over blank lines.
5500 */
5501 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005502find_line_comment(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503{
5504 static pos_T pos;
5505 char_u *line;
5506 char_u *p;
5507
5508 pos = curwin->w_cursor;
5509 while (--pos.lnum > 0)
5510 {
5511 line = ml_get(pos.lnum);
5512 p = skipwhite(line);
5513 if (cin_islinecomment(p))
5514 {
5515 pos.col = (int)(p - line);
5516 return &pos;
5517 }
5518 if (*p != NUL)
5519 break;
5520 }
5521 return NULL;
5522}
5523
5524/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005525 * Return TRUE if "text" starts with "key:".
5526 */
5527 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005528cin_has_js_key(char_u *text)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005529{
5530 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005531 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005532
5533 if (*s == '\'' || *s == '"')
5534 {
5535 /* can be 'key': or "key": */
5536 quote = *s;
5537 ++s;
5538 }
5539 if (!vim_isIDc(*s)) /* need at least one ID character */
5540 return FALSE;
5541
5542 while (vim_isIDc(*s))
5543 ++s;
5544 if (*s == quote)
5545 ++s;
5546
5547 s = cin_skipcomment(s);
5548
5549 /* "::" is not a label, it's C++ */
5550 return (*s == ':' && s[1] != ':');
5551}
5552
5553/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005555 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 */
5557 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005558cin_islabel_skip(char_u **s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559{
5560 if (!vim_isIDc(**s)) /* need at least one ID character */
5561 return FALSE;
5562
5563 while (vim_isIDc(**s))
5564 (*s)++;
5565
5566 *s = cin_skipcomment(*s);
5567
5568 /* "::" is not a label, it's C++ */
5569 return (**s == ':' && *++*s != ':');
5570}
5571
5572/*
5573 * Recognize a label: "label:".
5574 * Note: curwin->w_cursor must be where we are looking for the label.
5575 */
5576 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005577cin_islabel(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578{
5579 char_u *s;
5580
5581 s = cin_skipcomment(ml_get_curline());
5582
5583 /*
5584 * Exclude "default" from labels, since it should be indented
5585 * like a switch label. Same for C++ scope declarations.
5586 */
5587 if (cin_isdefault(s))
5588 return FALSE;
5589 if (cin_isscopedecl(s))
5590 return FALSE;
5591
5592 if (cin_islabel_skip(&s))
5593 {
5594 /*
5595 * Only accept a label if the previous line is terminated or is a case
5596 * label.
5597 */
5598 pos_T cursor_save;
5599 pos_T *trypos;
5600 char_u *line;
5601
5602 cursor_save = curwin->w_cursor;
5603 while (curwin->w_cursor.lnum > 1)
5604 {
5605 --curwin->w_cursor.lnum;
5606
5607 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005608 * If we're in a comment or raw string now, skip to the start of
5609 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 */
5611 curwin->w_cursor.col = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005612 if ((trypos = ind_find_start_CORS()) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 curwin->w_cursor = *trypos;
5614
5615 line = ml_get_curline();
5616 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5617 continue;
5618 if (*(line = cin_skipcomment(line)) == NUL)
5619 continue;
5620
5621 curwin->w_cursor = cursor_save;
5622 if (cin_isterminated(line, TRUE, FALSE)
5623 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005624 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 || (cin_islabel_skip(&line) && cin_nocode(line)))
5626 return TRUE;
5627 return FALSE;
5628 }
5629 curwin->w_cursor = cursor_save;
5630 return TRUE; /* label at start of file??? */
5631 }
5632 return FALSE;
5633}
5634
5635/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005636 * Recognize structure initialization and enumerations:
5637 * "[typedef] [static|public|protected|private] enum"
5638 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 */
5640 static int
5641cin_isinit(void)
5642{
5643 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005644 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645
5646 s = cin_skipcomment(ml_get_curline());
5647
Bram Moolenaar75342212013-03-07 13:13:52 +01005648 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 s = cin_skipcomment(s + 7);
5650
Bram Moolenaar75342212013-03-07 13:13:52 +01005651 for (;;)
5652 {
5653 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005654
Bram Moolenaar75342212013-03-07 13:13:52 +01005655 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5656 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005657 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005658 if (cin_starts_with(s, skip[i]))
5659 {
5660 s = cin_skipcomment(s + l);
5661 l = 0;
5662 break;
5663 }
5664 }
5665 if (l != 0)
5666 break;
5667 }
5668
5669 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 return TRUE;
5671
5672 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5673 return TRUE;
5674
5675 return FALSE;
5676}
5677
5678/*
5679 * Recognize a switch label: "case .*:" or "default:".
5680 */
5681 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005682cin_iscase(
5683 char_u *s,
5684 int strict) /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685{
5686 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005687 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 {
5689 for (s += 4; *s; ++s)
5690 {
5691 s = cin_skipcomment(s);
5692 if (*s == ':')
5693 {
5694 if (s[1] == ':') /* skip over "::" for C++ */
5695 ++s;
5696 else
5697 return TRUE;
5698 }
5699 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005700 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5702 return FALSE; /* stop at comment */
5703 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005704 {
5705 /* JS etc. */
5706 if (strict)
5707 return FALSE; /* stop at string */
5708 else
5709 return TRUE;
5710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 }
5712 return FALSE;
5713 }
5714
5715 if (cin_isdefault(s))
5716 return TRUE;
5717 return FALSE;
5718}
5719
5720/*
5721 * Recognize a "default" switch label.
5722 */
5723 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005724cin_isdefault(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725{
5726 return (STRNCMP(s, "default", 7) == 0
5727 && *(s = cin_skipcomment(s + 7)) == ':'
5728 && s[1] != ':');
5729}
5730
5731/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005732 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 */
5734 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005735cin_isscopedecl(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736{
5737 int i;
5738
5739 s = cin_skipcomment(s);
5740 if (STRNCMP(s, "public", 6) == 0)
5741 i = 6;
5742 else if (STRNCMP(s, "protected", 9) == 0)
5743 i = 9;
5744 else if (STRNCMP(s, "private", 7) == 0)
5745 i = 7;
5746 else
5747 return FALSE;
5748 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5749}
5750
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005751/* Maximum number of lines to search back for a "namespace" line. */
5752#define FIND_NAMESPACE_LIM 20
5753
5754/*
5755 * Recognize a "namespace" scope declaration.
5756 */
5757 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005758cin_is_cpp_namespace(char_u *s)
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005759{
5760 char_u *p;
5761 int has_name = FALSE;
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005762 int has_name_start = FALSE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005763
5764 s = cin_skipcomment(s);
5765 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5766 {
5767 p = cin_skipcomment(skipwhite(s + 9));
5768 while (*p != NUL)
5769 {
5770 if (vim_iswhite(*p))
5771 {
5772 has_name = TRUE; /* found end of a name */
5773 p = cin_skipcomment(skipwhite(p));
5774 }
5775 else if (*p == '{')
5776 {
5777 break;
5778 }
5779 else if (vim_iswordc(*p))
5780 {
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005781 has_name_start = TRUE;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005782 if (has_name)
5783 return FALSE; /* word character after skipping past name */
5784 ++p;
5785 }
Bram Moolenaarca8b8d62016-11-17 21:30:27 +01005786 else if (p[0] == ':' && p[1] == ':' && vim_iswordc(p[2]))
5787 {
5788 if (!has_name_start || has_name)
5789 return FALSE;
5790 /* C++ 17 nested namespace */
5791 p += 3;
5792 }
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005793 else
5794 {
5795 return FALSE;
5796 }
5797 }
5798 return TRUE;
5799 }
5800 return FALSE;
5801}
5802
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803/*
5804 * Return a pointer to the first non-empty non-comment character after a ':'.
5805 * Return NULL if not found.
5806 * case 234: a = b;
5807 * ^
5808 */
5809 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +01005810after_label(char_u *l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811{
5812 for ( ; *l; ++l)
5813 {
5814 if (*l == ':')
5815 {
5816 if (l[1] == ':') /* skip over "::" for C++ */
5817 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005818 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 break;
5820 }
5821 else if (*l == '\'' && l[1] && l[2] == '\'')
5822 l += 2; /* skip over 'x' */
5823 }
5824 if (*l == NUL)
5825 return NULL;
5826 l = cin_skipcomment(l + 1);
5827 if (*l == NUL)
5828 return NULL;
5829 return l;
5830}
5831
5832/*
5833 * Get indent of line "lnum", skipping a label.
5834 * Return 0 if there is nothing after the label.
5835 */
5836 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005837get_indent_nolabel (linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838{
5839 char_u *l;
5840 pos_T fp;
5841 colnr_T col;
5842 char_u *p;
5843
5844 l = ml_get(lnum);
5845 p = after_label(l);
5846 if (p == NULL)
5847 return 0;
5848
5849 fp.col = (colnr_T)(p - l);
5850 fp.lnum = lnum;
5851 getvcol(curwin, &fp, &col, NULL, NULL);
5852 return (int)col;
5853}
5854
5855/*
5856 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005857 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 * label: if (asdf && asdfasdf)
5859 * ^
5860 */
5861 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005862skip_label(linenr_T lnum, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863{
5864 char_u *l;
5865 int amount;
5866 pos_T cursor_save;
5867
5868 cursor_save = curwin->w_cursor;
5869 curwin->w_cursor.lnum = lnum;
5870 l = ml_get_curline();
5871 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005872 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 {
5874 amount = get_indent_nolabel(lnum);
5875 l = after_label(ml_get_curline());
5876 if (l == NULL) /* just in case */
5877 l = ml_get_curline();
5878 }
5879 else
5880 {
5881 amount = get_indent();
5882 l = ml_get_curline();
5883 }
5884 *pp = l;
5885
5886 curwin->w_cursor = cursor_save;
5887 return amount;
5888}
5889
5890/*
5891 * Return the indent of the first variable name after a type in a declaration.
5892 * int a, indent of "a"
5893 * static struct foo b, indent of "b"
5894 * enum bla c, indent of "c"
5895 * Returns zero when it doesn't look like a declaration.
5896 */
5897 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005898cin_first_id_amount(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899{
5900 char_u *line, *p, *s;
5901 int len;
5902 pos_T fp;
5903 colnr_T col;
5904
5905 line = ml_get_curline();
5906 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005907 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 if (len == 6 && STRNCMP(p, "static", 6) == 0)
5909 {
5910 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005911 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 }
5913 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
5914 p = skipwhite(p + 6);
5915 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
5916 p = skipwhite(p + 4);
5917 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
5918 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
5919 {
5920 s = skipwhite(p + len);
5921 if ((STRNCMP(s, "int", 3) == 0 && vim_iswhite(s[3]))
5922 || (STRNCMP(s, "long", 4) == 0 && vim_iswhite(s[4]))
5923 || (STRNCMP(s, "short", 5) == 0 && vim_iswhite(s[5]))
5924 || (STRNCMP(s, "char", 4) == 0 && vim_iswhite(s[4])))
5925 p = s;
5926 }
5927 for (len = 0; vim_isIDc(p[len]); ++len)
5928 ;
5929 if (len == 0 || !vim_iswhite(p[len]) || cin_nocode(p))
5930 return 0;
5931
5932 p = skipwhite(p + len);
5933 fp.lnum = curwin->w_cursor.lnum;
5934 fp.col = (colnr_T)(p - line);
5935 getvcol(curwin, &fp, &col, NULL, NULL);
5936 return (int)col;
5937}
5938
5939/*
5940 * Return the indent of the first non-blank after an equal sign.
5941 * char *foo = "here";
5942 * Return zero if no (useful) equal sign found.
5943 * Return -1 if the line above "lnum" ends in a backslash.
5944 * foo = "asdf\
5945 * asdf\
5946 * here";
5947 */
5948 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005949cin_get_equal_amount(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950{
5951 char_u *line;
5952 char_u *s;
5953 colnr_T col;
5954 pos_T fp;
5955
5956 if (lnum > 1)
5957 {
5958 line = ml_get(lnum - 1);
5959 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
5960 return -1;
5961 }
5962
5963 line = s = ml_get(lnum);
5964 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
5965 {
5966 if (cin_iscomment(s)) /* ignore comments */
5967 s = cin_skipcomment(s);
5968 else
5969 ++s;
5970 }
5971 if (*s != '=')
5972 return 0;
5973
5974 s = skipwhite(s + 1);
5975 if (cin_nocode(s))
5976 return 0;
5977
5978 if (*s == '"') /* nice alignment for continued strings */
5979 ++s;
5980
5981 fp.lnum = lnum;
5982 fp.col = (colnr_T)(s - line);
5983 getvcol(curwin, &fp, &col, NULL, NULL);
5984 return (int)col;
5985}
5986
5987/*
5988 * Recognize a preprocessor statement: Any line that starts with '#'.
5989 */
5990 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01005991cin_ispreproc(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02005993 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 return TRUE;
5995 return FALSE;
5996}
5997
5998/*
5999 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6000 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6001 * start and return the line in "*pp".
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006002 * Put the amount of indent in "*amount".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 */
6004 static int
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006005cin_ispreproc_cont(char_u **pp, linenr_T *lnump, int *amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006{
6007 char_u *line = *pp;
6008 linenr_T lnum = *lnump;
6009 int retval = FALSE;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006010 int candidate_amount = *amount;
6011
6012 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6013 candidate_amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006015 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 {
6017 if (cin_ispreproc(line))
6018 {
6019 retval = TRUE;
6020 *lnump = lnum;
6021 break;
6022 }
6023 if (lnum == 1)
6024 break;
6025 line = ml_get(--lnum);
6026 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6027 break;
6028 }
6029
6030 if (lnum != *lnump)
6031 *pp = ml_get(*lnump);
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01006032 if (retval)
6033 *amount = candidate_amount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 return retval;
6035}
6036
6037/*
6038 * Recognize the start of a C or C++ comment.
6039 */
6040 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006041cin_iscomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042{
6043 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6044}
6045
6046/*
6047 * Recognize the start of a "//" comment.
6048 */
6049 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006050cin_islinecomment(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051{
6052 return (p[0] == '/' && p[1] == '/');
6053}
6054
6055/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006056 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6057 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006059 * If a line begins with an "else", only consider it terminated if no unmatched
6060 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 * Return the character terminating the line (ending char's have precedence if
6062 * both apply in order to determine initializations).
6063 */
6064 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006065cin_isterminated(
6066 char_u *s,
6067 int incl_open, /* include '{' at the end as terminator */
6068 int incl_comma) /* recognize a trailing comma */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006070 char_u found_start = 0;
6071 unsigned n_open = 0;
6072 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073
6074 s = cin_skipcomment(s);
6075
6076 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6077 found_start = *s;
6078
Bram Moolenaar496f9512011-05-19 16:35:09 +02006079 if (!found_start)
6080 is_else = cin_iselse(s);
6081
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 while (*s)
6083 {
6084 /* skip over comments, "" strings and 'c'haracters */
6085 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006086 if (*s == '}' && n_open > 0)
6087 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006088 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006089 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 && cin_nocode(s + 1))
6091 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006092 else if (*s == '{')
6093 {
6094 if (incl_open && cin_nocode(s + 1))
6095 return *s;
6096 else
6097 ++n_open;
6098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099
6100 if (*s)
6101 s++;
6102 }
6103 return found_start;
6104}
6105
6106/*
6107 * Recognize the basic picture of a function declaration -- it needs to
6108 * have an open paren somewhere and a close paren at the end of the line and
6109 * no semicolons anywhere.
6110 * When a line ends in a comma we continue looking in the next line.
6111 * "sp" points to a string with the line. When looking at other lines it must
6112 * be restored to the line. When it's NULL fetch lines here.
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006113 * "first_lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006114 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 */
6116 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006117cin_isfuncdecl(
6118 char_u **sp,
6119 linenr_T first_lnum,
6120 linenr_T min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121{
6122 char_u *s;
6123 linenr_T lnum = first_lnum;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006124 linenr_T save_lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006126 pos_T *trypos;
6127 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128
6129 if (sp == NULL)
6130 s = ml_get(lnum);
6131 else
6132 s = *sp;
6133
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006134 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006135 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006136 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006137 {
6138 lnum = trypos->lnum;
6139 if (lnum < min_lnum)
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006140 {
6141 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006142 return FALSE;
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006143 }
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006144
6145 s = ml_get(lnum);
6146 }
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006147 curwin->w_cursor.lnum = save_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006148
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006149 /* Ignore line starting with #. */
6150 if (cin_ispreproc(s))
6151 return FALSE;
6152
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6154 {
6155 if (cin_iscomment(s)) /* ignore comments */
6156 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006157 else if (*s == ':')
6158 {
6159 if (*(s + 1) == ':')
6160 s += 2;
6161 else
6162 /* To avoid a mistake in the following situation:
6163 * A::A(int a, int b)
6164 * : a(0) // <--not a function decl
6165 * , b(0)
6166 * {...
6167 */
6168 return FALSE;
6169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 else
6171 ++s;
6172 }
6173 if (*s != '(')
6174 return FALSE; /* ';', ' or " before any () or no '(' */
6175
6176 while (*s && *s != ';' && *s != '\'' && *s != '"')
6177 {
6178 if (*s == ')' && cin_nocode(s + 1))
6179 {
6180 /* ')' at the end: may have found a match
6181 * Check for he previous line not to end in a backslash:
6182 * #if defined(x) && \
6183 * defined(y)
6184 */
6185 lnum = first_lnum - 1;
6186 s = ml_get(lnum);
6187 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6188 retval = TRUE;
6189 goto done;
6190 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006191 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006193 int comma = (*s == ',');
6194
6195 /* ',' at the end: continue looking in the next line.
6196 * At the end: check for ',' in the next line, for this style:
6197 * func(arg1
6198 * , arg2) */
6199 for (;;)
6200 {
6201 if (lnum >= curbuf->b_ml.ml_line_count)
6202 break;
6203 s = ml_get(++lnum);
6204 if (!cin_ispreproc(s))
6205 break;
6206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 if (lnum >= curbuf->b_ml.ml_line_count)
6208 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006209 /* Require a comma at end of the line or a comma or ')' at the
6210 * start of next line. */
6211 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006212 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006213 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006214 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 }
6216 else if (cin_iscomment(s)) /* ignore comments */
6217 s = cin_skipcomment(s);
6218 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006219 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006221 just_started = FALSE;
6222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 }
6224
6225done:
6226 if (lnum != first_lnum && sp != NULL)
6227 *sp = ml_get(first_lnum);
6228
6229 return retval;
6230}
6231
6232 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006233cin_isif(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234{
Bram Moolenaar9b578142016-01-30 19:39:49 +01006235 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236}
6237
6238 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006239cin_iselse(
6240 char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241{
6242 if (*p == '}') /* accept "} else" */
6243 p = cin_skipcomment(p + 1);
6244 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6245}
6246
6247 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006248cin_isdo(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249{
6250 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6251}
6252
6253/*
6254 * Check if this is a "while" that should have a matching "do".
6255 * We only accept a "while (condition) ;", with only white space between the
6256 * ')' and ';'. The condition may be spread over several lines.
6257 */
6258 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006259cin_iswhileofdo (char_u *p, linenr_T lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260{
6261 pos_T cursor_save;
6262 pos_T *trypos;
6263 int retval = FALSE;
6264
6265 p = cin_skipcomment(p);
6266 if (*p == '}') /* accept "} while (cond);" */
6267 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006268 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 {
6270 cursor_save = curwin->w_cursor;
6271 curwin->w_cursor.lnum = lnum;
6272 curwin->w_cursor.col = 0;
6273 p = ml_get_curline();
6274 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6275 {
6276 ++p;
6277 ++curwin->w_cursor.col;
6278 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006279 if ((trypos = findmatchlimit(NULL, 0, 0,
6280 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6282 retval = TRUE;
6283 curwin->w_cursor = cursor_save;
6284 }
6285 return retval;
6286}
6287
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006288/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006289 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006290 * Return 0 if there is none.
6291 * Otherwise return !0 and update "*poffset" to point to the place where the
6292 * string was found.
6293 */
6294 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006295cin_is_if_for_while_before_offset(char_u *line, int *poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006296{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006297 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006298
6299 if (offset-- < 2)
6300 return 0;
6301 while (offset > 2 && vim_iswhite(line[offset]))
6302 --offset;
6303
6304 offset -= 1;
6305 if (!STRNCMP(line + offset, "if", 2))
6306 goto probablyFound;
6307
6308 if (offset >= 1)
6309 {
6310 offset -= 1;
6311 if (!STRNCMP(line + offset, "for", 3))
6312 goto probablyFound;
6313
6314 if (offset >= 2)
6315 {
6316 offset -= 2;
6317 if (!STRNCMP(line + offset, "while", 5))
6318 goto probablyFound;
6319 }
6320 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006321 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006322
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006323probablyFound:
6324 if (!offset || !vim_isIDc(line[offset - 1]))
6325 {
6326 *poffset = offset;
6327 return 1;
6328 }
6329 return 0;
6330}
6331
6332/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006333 * Return TRUE if we are at the end of a do-while.
6334 * do
6335 * nothing;
6336 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006337 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006338 * Adjust the cursor to the line with "while".
6339 */
6340 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006341cin_iswhileofdo_end(int terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006342{
6343 char_u *line;
6344 char_u *p;
6345 char_u *s;
6346 pos_T *trypos;
6347 int i;
6348
6349 if (terminated != ';') /* there must be a ';' at the end */
6350 return FALSE;
6351
6352 p = line = ml_get_curline();
6353 while (*p != NUL)
6354 {
6355 p = cin_skipcomment(p);
6356 if (*p == ')')
6357 {
6358 s = skipwhite(p + 1);
6359 if (*s == ';' && cin_nocode(s + 1))
6360 {
6361 /* Found ");" at end of the line, now check there is "while"
6362 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006363 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006364 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006365 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006366 if (trypos != NULL)
6367 {
6368 s = cin_skipcomment(ml_get(trypos->lnum));
6369 if (*s == '}') /* accept "} while (cond);" */
6370 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006371 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006372 {
6373 curwin->w_cursor.lnum = trypos->lnum;
6374 return TRUE;
6375 }
6376 }
6377
6378 /* Searching may have made "line" invalid, get it again. */
6379 line = ml_get_curline();
6380 p = line + i;
6381 }
6382 }
6383 if (*p != NUL)
6384 ++p;
6385 }
6386 return FALSE;
6387}
6388
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006390cin_isbreak(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391{
6392 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6393}
6394
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006395/*
6396 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 * constructor-initialization. eg:
6398 *
6399 * class MyClass :
6400 * baseClass <-- here
6401 * class MyClass : public baseClass,
6402 * anotherBaseClass <-- here (should probably lineup ??)
6403 * MyClass::MyClass(...) :
6404 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006405 *
6406 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 */
6408 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006409cin_is_cpp_baseclass(
6410 cpp_baseclass_cache_T *cached) /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006412 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 char_u *s;
6414 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006415 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006416 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006418 if (pos->lnum <= lnum)
6419 return cached->found; /* Use the cached result */
6420
6421 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006423 s = skipwhite(line);
6424 if (*s == '#') /* skip #define FOO x ? (x) : x */
6425 return FALSE;
6426 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 if (*s == NUL)
6428 return FALSE;
6429
6430 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6431
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006432 /* Search for a line starting with '#', empty, ending in ';' or containing
6433 * '{' or '}' and start below it. This handles the following situations:
6434 * a = cond ?
6435 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006436 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006437 * func::foo()
6438 * : something
6439 * {}
6440 * Foo::Foo (int one, int two)
6441 * : something(4),
6442 * somethingelse(3)
6443 * {}
6444 */
6445 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006447 line = ml_get(lnum - 1);
6448 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006449 if (*s == '#' || *s == NUL)
6450 break;
6451 while (*s != NUL)
6452 {
6453 s = cin_skipcomment(s);
6454 if (*s == '{' || *s == '}'
6455 || (*s == ';' && cin_nocode(s + 1)))
6456 break;
6457 if (*s != NUL)
6458 ++s;
6459 }
6460 if (*s != NUL)
6461 break;
6462 --lnum;
6463 }
6464
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006465 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006466 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006467 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006468 for (;;)
6469 {
6470 if (*s == NUL)
6471 {
6472 if (lnum == curwin->w_cursor.lnum)
6473 break;
6474 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006475 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006476 s = line;
6477 }
6478 if (s == line)
6479 {
6480 /* don't recognize "case (foo):" as a baseclass */
6481 if (cin_iscase(s, FALSE))
6482 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006483 s = cin_skipcomment(line);
6484 if (*s == NUL)
6485 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006486 }
6487
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006488 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006489 s = skip_string(s) + 1;
6490 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 {
6492 if (s[1] == ':')
6493 {
6494 /* skip double colon. It can't be a constructor
6495 * initialization any more */
6496 lookfor_ctor_init = FALSE;
6497 s = cin_skipcomment(s + 2);
6498 }
6499 else if (lookfor_ctor_init || class_or_struct)
6500 {
6501 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006502 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 cpp_base_class = TRUE;
6504 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006505 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 s = cin_skipcomment(s + 1);
6507 }
6508 else
6509 s = cin_skipcomment(s + 1);
6510 }
6511 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6512 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6513 {
6514 class_or_struct = TRUE;
6515 lookfor_ctor_init = FALSE;
6516
6517 if (*s == 'c')
6518 s = cin_skipcomment(s + 5);
6519 else
6520 s = cin_skipcomment(s + 6);
6521 }
6522 else
6523 {
6524 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6525 {
6526 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6527 }
6528 else if (s[0] == ')')
6529 {
6530 /* Constructor-initialization is assumed if we come across
6531 * something like "):" */
6532 class_or_struct = FALSE;
6533 lookfor_ctor_init = TRUE;
6534 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006535 else if (s[0] == '?')
6536 {
6537 /* Avoid seeing '() :' after '?' as constructor init. */
6538 return FALSE;
6539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 else if (!vim_isIDc(s[0]))
6541 {
6542 /* if it is not an identifier, we are wrong */
6543 class_or_struct = FALSE;
6544 lookfor_ctor_init = FALSE;
6545 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006546 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 {
6548 /* it can't be a constructor-initialization any more */
6549 lookfor_ctor_init = FALSE;
6550
6551 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006552 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006553 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 }
6555
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006556 /* When the line ends in a comma don't align with it. */
6557 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006558 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006559
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 s = cin_skipcomment(s + 1);
6561 }
6562 }
6563
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006564 cached->found = cpp_base_class;
6565 if (cpp_base_class)
6566 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 return cpp_base_class;
6568}
6569
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006570 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006571get_baseclass_amount(int col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006572{
6573 int amount;
6574 colnr_T vcol;
6575 pos_T *trypos;
6576
6577 if (col == 0)
6578 {
6579 amount = get_indent();
6580 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006581 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006582 amount = get_indent_lnum(trypos->lnum); /* XXX */
6583 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006584 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006585 }
6586 else
6587 {
6588 curwin->w_cursor.col = col;
6589 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6590 amount = (int)vcol;
6591 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006592 if (amount < curbuf->b_ind_cpp_baseclass)
6593 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006594 return amount;
6595}
6596
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597/*
6598 * Return TRUE if string "s" ends with the string "find", possibly followed by
6599 * white space and comments. Skip strings and comments.
6600 * Ignore "ignore" after "find" if it's not NULL.
6601 */
6602 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006603cin_ends_in(char_u *s, char_u *find, char_u *ignore)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604{
6605 char_u *p = s;
6606 char_u *r;
6607 int len = (int)STRLEN(find);
6608
6609 while (*p != NUL)
6610 {
6611 p = cin_skipcomment(p);
6612 if (STRNCMP(p, find, len) == 0)
6613 {
6614 r = skipwhite(p + len);
6615 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6616 r = skipwhite(r + STRLEN(ignore));
6617 if (cin_nocode(r))
6618 return TRUE;
6619 }
6620 if (*p != NUL)
6621 ++p;
6622 }
6623 return FALSE;
6624}
6625
6626/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006627 * Return TRUE when "s" starts with "word" and then a non-ID character.
6628 */
6629 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006630cin_starts_with(char_u *s, char *word)
Bram Moolenaar75342212013-03-07 13:13:52 +01006631{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006632 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006633
6634 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6635}
6636
6637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 * Skip strings, chars and comments until at or past "trypos".
6639 * Return the column found.
6640 */
6641 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006642cin_skip2pos(pos_T *trypos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643{
6644 char_u *line;
6645 char_u *p;
6646
6647 p = line = ml_get(trypos->lnum);
6648 while (*p && (colnr_T)(p - line) < trypos->col)
6649 {
6650 if (cin_iscomment(p))
6651 p = cin_skipcomment(p);
6652 else
6653 {
6654 p = skip_string(p);
6655 ++p;
6656 }
6657 }
6658 return (int)(p - line);
6659}
6660
6661/*
6662 * Find the '{' at the start of the block we are in.
6663 * Return NULL if no match found.
6664 * Ignore a '{' that is in a comment, makes indenting the next three lines
6665 * work. */
6666/* foo() */
6667/* { */
6668/* } */
6669
6670 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006671find_start_brace(void) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672{
6673 pos_T cursor_save;
6674 pos_T *trypos;
6675 pos_T *pos;
6676 static pos_T pos_copy;
6677
6678 cursor_save = curwin->w_cursor;
6679 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6680 {
6681 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6682 trypos = &pos_copy;
6683 curwin->w_cursor = *trypos;
6684 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006685 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006687 && (pos = ind_find_start_CORS()) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 break;
6689 if (pos != NULL)
6690 curwin->w_cursor.lnum = pos->lnum;
6691 }
6692 curwin->w_cursor = cursor_save;
6693 return trypos;
6694}
6695
6696/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006697 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006698 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 */
6700 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006701find_match_paren(int ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702{
Bram Moolenaar80c3fd72016-09-10 15:52:55 +02006703 return find_match_char('(', ind_maxparen);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006704}
6705
6706 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006707find_match_char (int c, int ind_maxparen) /* XXX */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006708{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 pos_T cursor_save;
6710 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006711 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006712 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713
6714 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006715 ind_maxp_wk = ind_maxparen;
6716retry:
6717 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 {
6719 /* check if the ( is in a // comment */
6720 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01006721 {
6722 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
6723 if (ind_maxp_wk > 0)
6724 {
6725 curwin->w_cursor = *trypos;
6726 curwin->w_cursor.col = 0; /* XXX */
6727 goto retry;
6728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 else
6732 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01006733 pos_T *trypos_wk;
6734
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6736 trypos = &pos_copy;
6737 curwin->w_cursor = *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006738 if ((trypos_wk = ind_find_start_CORS()) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01006739 {
6740 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
6741 - trypos_wk->lnum);
6742 if (ind_maxp_wk > 0)
6743 {
6744 curwin->w_cursor = *trypos_wk;
6745 goto retry;
6746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 }
6750 }
6751 curwin->w_cursor = cursor_save;
6752 return trypos;
6753}
6754
6755/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006756 * Find the matching '(', ignoring it if it is in a comment or before an
6757 * unmatched {.
6758 * Return NULL if no match found.
6759 */
6760 static pos_T *
Bram Moolenaar9b578142016-01-30 19:39:49 +01006761find_match_paren_after_brace (int ind_maxparen) /* XXX */
Bram Moolenaar81439a62014-07-02 18:27:48 +02006762{
6763 pos_T *trypos = find_match_paren(ind_maxparen);
6764
6765 if (trypos != NULL)
6766 {
6767 pos_T *tryposBrace = find_start_brace();
6768
6769 /* If both an unmatched '(' and '{' is found. Ignore the '('
6770 * position if the '{' is further down. */
6771 if (tryposBrace != NULL
6772 && (trypos->lnum != tryposBrace->lnum
6773 ? trypos->lnum < tryposBrace->lnum
6774 : trypos->col < tryposBrace->col))
6775 trypos = NULL;
6776 }
6777 return trypos;
6778}
6779
6780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 * Return ind_maxparen corrected for the difference in line number between the
6782 * cursor position and "startpos". This makes sure that searching for a
6783 * matching paren above the cursor line doesn't find a match because of
6784 * looking a few lines further.
6785 */
6786 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006787corr_ind_maxparen(pos_T *startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788{
6789 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6790
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006791 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
6792 return curbuf->b_ind_maxparen - (int)n;
6793 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794}
6795
6796/*
6797 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006798 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 */
6800 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01006801find_last_paren(char_u *l, int start, int end)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802{
6803 int i;
6804 int retval = FALSE;
6805 int open_count = 0;
6806
6807 curwin->w_cursor.col = 0; /* default is start of line */
6808
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006809 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 {
6811 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6812 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6813 if (l[i] == start)
6814 ++open_count;
6815 else if (l[i] == end)
6816 {
6817 if (open_count > 0)
6818 --open_count;
6819 else
6820 {
6821 curwin->w_cursor.col = i;
6822 retval = TRUE;
6823 }
6824 }
6825 }
6826 return retval;
6827}
6828
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006829/*
6830 * Parse 'cinoptions' and set the values in "curbuf".
6831 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
6832 */
6833 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01006834parse_cino(buf_T *buf)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006835{
6836 char_u *p;
6837 char_u *l;
6838 char_u *digits;
6839 int n;
6840 int divider;
6841 int fraction = 0;
6842 int sw = (int)get_sw_value(buf);
6843
6844 /*
6845 * Set the default values.
6846 */
6847 /* Spaces from a block's opening brace the prevailing indent for that
6848 * block should be. */
6849 buf->b_ind_level = sw;
6850
6851 /* Spaces from the edge of the line an open brace that's at the end of a
6852 * line is imagined to be. */
6853 buf->b_ind_open_imag = 0;
6854
6855 /* Spaces from the prevailing indent for a line that is not preceded by
6856 * an opening brace. */
6857 buf->b_ind_no_brace = 0;
6858
6859 /* Column where the first { of a function should be located }. */
6860 buf->b_ind_first_open = 0;
6861
6862 /* Spaces from the prevailing indent a leftmost open brace should be
6863 * located. */
6864 buf->b_ind_open_extra = 0;
6865
6866 /* Spaces from the matching open brace (real location for one at the left
6867 * edge; imaginary location from one that ends a line) the matching close
6868 * brace should be located. */
6869 buf->b_ind_close_extra = 0;
6870
6871 /* Spaces from the edge of the line an open brace sitting in the leftmost
6872 * column is imagined to be. */
6873 buf->b_ind_open_left_imag = 0;
6874
6875 /* Spaces jump labels should be shifted to the left if N is non-negative,
6876 * otherwise the jump label will be put to column 1. */
6877 buf->b_ind_jump_label = -1;
6878
6879 /* Spaces from the switch() indent a "case xx" label should be located. */
6880 buf->b_ind_case = sw;
6881
6882 /* Spaces from the "case xx:" code after a switch() should be located. */
6883 buf->b_ind_case_code = sw;
6884
6885 /* Lineup break at end of case in switch() with case label. */
6886 buf->b_ind_case_break = 0;
6887
6888 /* Spaces from the class declaration indent a scope declaration label
6889 * should be located. */
6890 buf->b_ind_scopedecl = sw;
6891
6892 /* Spaces from the scope declaration label code should be located. */
6893 buf->b_ind_scopedecl_code = sw;
6894
6895 /* Amount K&R-style parameters should be indented. */
6896 buf->b_ind_param = sw;
6897
6898 /* Amount a function type spec should be indented. */
6899 buf->b_ind_func_type = sw;
6900
6901 /* Amount a cpp base class declaration or constructor initialization
6902 * should be indented. */
6903 buf->b_ind_cpp_baseclass = sw;
6904
6905 /* additional spaces beyond the prevailing indent a continuation line
6906 * should be located. */
6907 buf->b_ind_continuation = sw;
6908
6909 /* Spaces from the indent of the line with an unclosed parentheses. */
6910 buf->b_ind_unclosed = sw * 2;
6911
6912 /* Spaces from the indent of the line with an unclosed parentheses, which
6913 * itself is also unclosed. */
6914 buf->b_ind_unclosed2 = sw;
6915
6916 /* Suppress ignoring spaces from the indent of a line starting with an
6917 * unclosed parentheses. */
6918 buf->b_ind_unclosed_noignore = 0;
6919
6920 /* If the opening paren is the last nonwhite character on the line, and
6921 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
6922 * context (for very long lines). */
6923 buf->b_ind_unclosed_wrapped = 0;
6924
6925 /* Suppress ignoring white space when lining up with the character after
6926 * an unclosed parentheses. */
6927 buf->b_ind_unclosed_whiteok = 0;
6928
6929 /* Indent a closing parentheses under the line start of the matching
6930 * opening parentheses. */
6931 buf->b_ind_matching_paren = 0;
6932
6933 /* Indent a closing parentheses under the previous line. */
6934 buf->b_ind_paren_prev = 0;
6935
6936 /* Extra indent for comments. */
6937 buf->b_ind_comment = 0;
6938
6939 /* Spaces from the comment opener when there is nothing after it. */
6940 buf->b_ind_in_comment = 3;
6941
6942 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
6943 * after the comment opener. */
6944 buf->b_ind_in_comment2 = 0;
6945
6946 /* Max lines to search for an open paren. */
6947 buf->b_ind_maxparen = 20;
6948
6949 /* Max lines to search for an open comment. */
6950 buf->b_ind_maxcomment = 70;
6951
6952 /* Handle braces for java code. */
6953 buf->b_ind_java = 0;
6954
6955 /* Not to confuse JS object properties with labels. */
6956 buf->b_ind_js = 0;
6957
6958 /* Handle blocked cases correctly. */
6959 buf->b_ind_keep_case_label = 0;
6960
6961 /* Handle C++ namespace. */
6962 buf->b_ind_cpp_namespace = 0;
6963
6964 /* Handle continuation lines containing conditions of if(), for() and
6965 * while(). */
6966 buf->b_ind_if_for_while = 0;
6967
6968 for (p = buf->b_p_cino; *p; )
6969 {
6970 l = p++;
6971 if (*p == '-')
6972 ++p;
6973 digits = p; /* remember where the digits start */
6974 n = getdigits(&p);
6975 divider = 0;
6976 if (*p == '.') /* ".5s" means a fraction */
6977 {
6978 fraction = atol((char *)++p);
6979 while (VIM_ISDIGIT(*p))
6980 {
6981 ++p;
6982 if (divider)
6983 divider *= 10;
6984 else
6985 divider = 10;
6986 }
6987 }
6988 if (*p == 's') /* "2s" means two times 'shiftwidth' */
6989 {
6990 if (p == digits)
6991 n = sw; /* just "s" is one 'shiftwidth' */
6992 else
6993 {
6994 n *= sw;
6995 if (divider)
6996 n += (sw * fraction + divider / 2) / divider;
6997 }
6998 ++p;
6999 }
7000 if (l[1] == '-')
7001 n = -n;
7002
7003 /* When adding an entry here, also update the default 'cinoptions' in
7004 * doc/indent.txt, and add explanation for it! */
7005 switch (*l)
7006 {
7007 case '>': buf->b_ind_level = n; break;
7008 case 'e': buf->b_ind_open_imag = n; break;
7009 case 'n': buf->b_ind_no_brace = n; break;
7010 case 'f': buf->b_ind_first_open = n; break;
7011 case '{': buf->b_ind_open_extra = n; break;
7012 case '}': buf->b_ind_close_extra = n; break;
7013 case '^': buf->b_ind_open_left_imag = n; break;
7014 case 'L': buf->b_ind_jump_label = n; break;
7015 case ':': buf->b_ind_case = n; break;
7016 case '=': buf->b_ind_case_code = n; break;
7017 case 'b': buf->b_ind_case_break = n; break;
7018 case 'p': buf->b_ind_param = n; break;
7019 case 't': buf->b_ind_func_type = n; break;
7020 case '/': buf->b_ind_comment = n; break;
7021 case 'c': buf->b_ind_in_comment = n; break;
7022 case 'C': buf->b_ind_in_comment2 = n; break;
7023 case 'i': buf->b_ind_cpp_baseclass = n; break;
7024 case '+': buf->b_ind_continuation = n; break;
7025 case '(': buf->b_ind_unclosed = n; break;
7026 case 'u': buf->b_ind_unclosed2 = n; break;
7027 case 'U': buf->b_ind_unclosed_noignore = n; break;
7028 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7029 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7030 case 'm': buf->b_ind_matching_paren = n; break;
7031 case 'M': buf->b_ind_paren_prev = n; break;
7032 case ')': buf->b_ind_maxparen = n; break;
7033 case '*': buf->b_ind_maxcomment = n; break;
7034 case 'g': buf->b_ind_scopedecl = n; break;
7035 case 'h': buf->b_ind_scopedecl_code = n; break;
7036 case 'j': buf->b_ind_java = n; break;
7037 case 'J': buf->b_ind_js = n; break;
7038 case 'l': buf->b_ind_keep_case_label = n; break;
7039 case '#': buf->b_ind_hash_comment = n; break;
7040 case 'N': buf->b_ind_cpp_namespace = n; break;
7041 case 'k': buf->b_ind_if_for_while = n; break;
7042 }
7043 if (*p == ',')
7044 ++p;
7045 }
7046}
7047
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007048/*
7049 * Return the desired indent for C code.
7050 * Return -1 if the indent should be left alone (inside a raw string).
7051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01007053get_c_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 pos_T cur_curpos;
7056 int amount;
7057 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007058 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 colnr_T col;
7060 char_u *theline;
7061 char_u *linecopy;
7062 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007063 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007065 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 pos_T our_paren_pos;
7067 char_u *start;
7068 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007069#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070#define BRACE_AT_START 2 /* '{' is at start of line */
7071#define BRACE_AT_END 3 /* '{' is at end of line */
7072 linenr_T ourscope;
7073 char_u *l;
7074 char_u *look;
7075 char_u terminated;
7076 int lookfor;
7077#define LOOKFOR_INITIAL 0
7078#define LOOKFOR_IF 1
7079#define LOOKFOR_DO 2
7080#define LOOKFOR_CASE 3
7081#define LOOKFOR_ANY 4
7082#define LOOKFOR_TERM 5
7083#define LOOKFOR_UNTERM 6
7084#define LOOKFOR_SCOPEDECL 7
7085#define LOOKFOR_NOBREAK 8
7086#define LOOKFOR_CPP_BASECLASS 9
7087#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007088#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007089#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090
7091 int whilelevel;
7092 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 int n;
7094 int iscase;
7095 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007096 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007098 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007099 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007100 int js_cur_has_key = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007101 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007103 /* make a copy, value is changed below */
7104 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105
7106 /* remember where the cursor was when we started */
7107 cur_curpos = curwin->w_cursor;
7108
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007109 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007110 if (cur_curpos.lnum == 1)
7111 return 0;
7112
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 /* Get a copy of the current contents of the line.
7114 * This is required, because only the most recent line obtained with
7115 * ml_get is valid! */
7116 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7117 if (linecopy == NULL)
7118 return 0;
7119
7120 /*
7121 * In insert mode and the cursor is on a ')' truncate the line at the
7122 * cursor position. We don't want to line up with the matching '(' when
7123 * inserting new stuff.
7124 * For unknown reasons the cursor might be past the end of the line, thus
7125 * check for that.
7126 */
7127 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007128 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 && linecopy[curwin->w_cursor.col] == ')')
7130 linecopy[curwin->w_cursor.col] = NUL;
7131
7132 theline = skipwhite(linecopy);
7133
7134 /* move the cursor to the start of the line */
7135
7136 curwin->w_cursor.col = 0;
7137
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007138 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007139
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007141 * If we are inside a raw string don't change the indent.
7142 * Ignore a raw string inside a comment.
7143 */
7144 comment_pos = ind_find_start_comment();
7145 if (comment_pos != NULL)
7146 {
7147 /* findmatchlimit() static pos is overwritten, make a copy */
7148 tryposCopy = *comment_pos;
7149 comment_pos = &tryposCopy;
7150 }
7151 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
7152 if (trypos != NULL && (comment_pos == NULL || lt(*trypos, *comment_pos)))
7153 {
7154 amount = -1;
7155 goto laterend;
7156 }
7157
7158 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 * #defines and so on always go at the left when included in 'cinkeys'.
7160 */
7161 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007162 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007163 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007164 goto theend;
7165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166
7167 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007168 * Is it a non-case label? Then that goes at the left margin too unless:
7169 * - JS flag is set.
7170 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007172 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007173 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 {
7175 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007176 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 }
7178
7179 /*
7180 * If we're inside a "//" comment and there is a "//" comment in a
7181 * previous line, lineup with that one.
7182 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007183 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 && (trypos = find_line_comment()) != NULL) /* XXX */
7185 {
7186 /* find how indented the line beginning the comment is */
7187 getvcol(curwin, trypos, &col, NULL, NULL);
7188 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007189 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 }
7191
7192 /*
7193 * If we're inside a comment and not looking at the start of the
7194 * comment, try using the 'comments' option.
7195 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007196 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 {
7198 int lead_start_len = 2;
7199 int lead_middle_len = 1;
7200 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7201 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7202 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7203 char_u *p;
7204 int start_align = 0;
7205 int start_off = 0;
7206 int done = FALSE;
7207
7208 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007209 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007211 *lead_start = NUL;
7212 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213
7214 p = curbuf->b_p_com;
7215 while (*p != NUL)
7216 {
7217 int align = 0;
7218 int off = 0;
7219 int what = 0;
7220
7221 while (*p != NUL && *p != ':')
7222 {
7223 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7224 what = *p++;
7225 else if (*p == COM_LEFT || *p == COM_RIGHT)
7226 align = *p++;
7227 else if (VIM_ISDIGIT(*p) || *p == '-')
7228 off = getdigits(&p);
7229 else
7230 ++p;
7231 }
7232
7233 if (*p == ':')
7234 ++p;
7235 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7236 if (what == COM_START)
7237 {
7238 STRCPY(lead_start, lead_end);
7239 lead_start_len = (int)STRLEN(lead_start);
7240 start_off = off;
7241 start_align = align;
7242 }
7243 else if (what == COM_MIDDLE)
7244 {
7245 STRCPY(lead_middle, lead_end);
7246 lead_middle_len = (int)STRLEN(lead_middle);
7247 }
7248 else if (what == COM_END)
7249 {
7250 /* If our line starts with the middle comment string, line it
7251 * up with the comment opener per the 'comments' option. */
7252 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7253 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7254 {
7255 done = TRUE;
7256 if (curwin->w_cursor.lnum > 1)
7257 {
7258 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007259 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 * the middle comment string matches in the previous
7261 * line, use the indent of that line. XXX */
7262 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7263 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7264 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7265 else if (STRNCMP(look, lead_middle,
7266 lead_middle_len) == 0)
7267 {
7268 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7269 break;
7270 }
7271 /* If the start comment string doesn't match with the
7272 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007273 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 lead_start, lead_start_len) != 0)
7275 continue;
7276 }
7277 if (start_off != 0)
7278 amount += start_off;
7279 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007280 amount += vim_strsize(lead_start)
7281 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 break;
7283 }
7284
7285 /* If our line starts with the end comment string, line it up
7286 * with the middle comment */
7287 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7288 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7289 {
7290 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7291 /* XXX */
7292 if (off != 0)
7293 amount += off;
7294 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007295 amount += vim_strsize(lead_start)
7296 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 done = TRUE;
7298 break;
7299 }
7300 }
7301 }
7302
7303 /* If our line starts with an asterisk, line up with the
7304 * asterisk in the comment opener; otherwise, line up
7305 * with the first character of the comment text.
7306 */
7307 if (done)
7308 ;
7309 else if (theline[0] == '*')
7310 amount += 1;
7311 else
7312 {
7313 /*
7314 * If we are more than one line away from the comment opener, take
7315 * the indent of the previous non-empty line. If 'cino' has "CO"
7316 * and we are just below the comment opener and there are any
7317 * white characters after it line up with the text after it;
7318 * otherwise, add the amount specified by "c" in 'cino'
7319 */
7320 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007321 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 {
7323 if (linewhite(lnum)) /* skip blank lines */
7324 continue;
7325 amount = get_indent_lnum(lnum); /* XXX */
7326 break;
7327 }
7328 if (amount == -1) /* use the comment opener */
7329 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007330 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007332 start = ml_get(comment_pos->lnum);
7333 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007335 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007337 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007339 if (curbuf->b_ind_in_comment2 || *look == NUL)
7340 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 }
7342 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007343 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 }
7345
7346 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007347 * Are we looking at a ']' that has a match?
7348 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007349 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007350 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7351 {
7352 /* align with the line containing the '['. */
7353 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007354 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007355 }
7356
7357 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 * Are we inside parentheses or braces?
7359 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007360 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007361 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007362 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 || trypos != NULL)
7364 {
7365 if (trypos != NULL && tryposBrace != NULL)
7366 {
7367 /* Both an unmatched '(' and '{' is found. Use the one which is
7368 * closer to the current cursor position, set the other to NULL. */
7369 if (trypos->lnum != tryposBrace->lnum
7370 ? trypos->lnum < tryposBrace->lnum
7371 : trypos->col < tryposBrace->col)
7372 trypos = NULL;
7373 else
7374 tryposBrace = NULL;
7375 }
7376
7377 if (trypos != NULL)
7378 {
7379 /*
7380 * If the matching paren is more than one line away, use the indent of
7381 * a previous non-empty line that matches the same paren.
7382 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007383 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007385 /* Line up with the start of the matching paren line. */
7386 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7387 }
7388 else
7389 {
7390 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007391 our_paren_pos = *trypos;
7392 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007394 l = skipwhite(ml_get(lnum));
7395 if (cin_nocode(l)) /* skip comment lines */
7396 continue;
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007397 if (cin_ispreproc_cont(&l, &lnum, &amount))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007398 continue; /* ignore #define, #if, etc. */
7399 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007401 /* Skip a comment or raw string. XXX */
7402 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007403 {
7404 lnum = trypos->lnum + 1;
7405 continue;
7406 }
7407
7408 /* XXX */
7409 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007410 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007411 && trypos->lnum == our_paren_pos.lnum
7412 && trypos->col == our_paren_pos.col)
7413 {
7414 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007416 if (theline[0] == ')')
7417 {
7418 if (our_paren_pos.lnum != lnum
7419 && cur_amount > amount)
7420 cur_amount = amount;
7421 amount = -1;
7422 }
7423 break;
7424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 }
7426 }
7427
7428 /*
7429 * Line up with line where the matching paren is. XXX
7430 * If the line starts with a '(' or the indent for unclosed
7431 * parentheses is zero, line up with the unclosed parentheses.
7432 */
7433 if (amount == -1)
7434 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007435 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007436 int is_if_for_while = 0;
7437
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007438 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007439 {
7440 /* Look for the outermost opening parenthesis on this line
7441 * and check whether it belongs to an "if", "for" or "while". */
7442
7443 pos_T cursor_save = curwin->w_cursor;
7444 pos_T outermost;
7445 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007446
7447 trypos = &our_paren_pos;
7448 do {
7449 outermost = *trypos;
7450 curwin->w_cursor.lnum = outermost.lnum;
7451 curwin->w_cursor.col = outermost.col;
7452
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007453 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007454 } while (trypos && trypos->lnum == outermost.lnum);
7455
7456 curwin->w_cursor = cursor_save;
7457
7458 line = ml_get(outermost.lnum);
7459
7460 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007461 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007462 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007463
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007464 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007465 look = skipwhite(look);
7466 if (*look == '(')
7467 {
7468 linenr_T save_lnum = curwin->w_cursor.lnum;
7469 char_u *line;
7470 int look_col;
7471
7472 /* Ignore a '(' in front of the line that has a match before
7473 * our matching '('. */
7474 curwin->w_cursor.lnum = our_paren_pos.lnum;
7475 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007476 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007477 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007478 if ((trypos = findmatchlimit(NULL, ')', 0,
7479 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007480 != NULL
7481 && trypos->lnum == our_paren_pos.lnum
7482 && trypos->col < our_paren_pos.col)
7483 ignore_paren_col = trypos->col + 1;
7484
7485 curwin->w_cursor.lnum = save_lnum;
7486 look = ml_get(our_paren_pos.lnum) + look_col;
7487 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007488 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7489 && is_if_for_while == 0)
7490 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007491 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 {
7493 /*
7494 * If we're looking at a close paren, line up right there;
7495 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007496 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 * the last nonwhite character of the line, use either the
7498 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007499 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 * lines).
7501 */
7502 if (theline[0] != ')')
7503 {
7504 cur_amount = MAXCOL;
7505 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007506 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 && cin_ends_in(l, (char_u *)"(", NULL))
7508 {
7509 /* look for opening unmatched paren, indent one level
7510 * for each additional level */
7511 n = 1;
7512 for (col = 0; col < our_paren_pos.col; ++col)
7513 {
7514 switch (l[col])
7515 {
7516 case '(':
7517 case '{': ++n;
7518 break;
7519
7520 case ')':
7521 case '}': if (n > 1)
7522 --n;
7523 break;
7524 }
7525 }
7526
7527 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007528 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007530 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 our_paren_pos.col++;
7532 else
7533 {
7534 col = our_paren_pos.col + 1;
7535 while (vim_iswhite(l[col]))
7536 col++;
7537 if (l[col] != NUL) /* In case of trailing space */
7538 our_paren_pos.col = col;
7539 else
7540 our_paren_pos.col++;
7541 }
7542 }
7543
7544 /*
7545 * Find how indented the paren is, or the character after it
7546 * if we did the above "if".
7547 */
7548 if (our_paren_pos.col > 0)
7549 {
7550 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7551 if (cur_amount > (int)col)
7552 cur_amount = col;
7553 }
7554 }
7555
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007556 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 {
7558 /* Line up with the start of the matching paren line. */
7559 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007560 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7561 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007562 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 {
7564 if (cur_amount != MAXCOL)
7565 amount = cur_amount;
7566 }
7567 else
7568 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007569 /* Add b_ind_unclosed2 for each '(' before our matching one,
7570 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007572 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 {
7574 --our_paren_pos.col;
7575 switch (*ml_get_pos(&our_paren_pos))
7576 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007577 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 col = our_paren_pos.col;
7579 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007580 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 col = MAXCOL;
7582 break;
7583 }
7584 }
7585
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007586 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 * braces */
7588 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007589 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 else
7591 {
7592 curwin->w_cursor.lnum = our_paren_pos.lnum;
7593 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007594 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7595 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007596 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007598 {
7599 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007600 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007601 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007602 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 }
7605 /*
7606 * For a line starting with ')' use the minimum of the two
7607 * positions, to avoid giving it more indent than the previous
7608 * lines:
7609 * func_long_name( if (x
7610 * arg && yy
7611 * ) ^ not here ) ^ not here
7612 */
7613 if (cur_amount < amount)
7614 amount = cur_amount;
7615 }
7616 }
7617
7618 /* add extra indent for a comment */
7619 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007620 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 else
7623 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007624 /*
7625 * We are inside braces, there is a { before this line at the position
7626 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007627 * Make a copy of tryposBrace, it may point to pos_copy inside
7628 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007629 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007630 tryposCopy = *tryposBrace;
7631 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 ourscope = trypos->lnum;
7634 start = ml_get(ourscope);
7635
7636 /*
7637 * Now figure out how indented the line is in general.
7638 * If the brace was at the start of the line, we use that;
7639 * otherwise, check out the indentation of the line as
7640 * a whole and then add the "imaginary indent" to that.
7641 */
7642 look = skipwhite(start);
7643 if (*look == '{')
7644 {
7645 getvcol(curwin, trypos, &col, NULL, NULL);
7646 amount = col;
7647 if (*start == '{')
7648 start_brace = BRACE_IN_COL0;
7649 else
7650 start_brace = BRACE_AT_START;
7651 }
7652 else
7653 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007654 /* That opening brace might have been on a continuation
7655 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 curwin->w_cursor.lnum = ourscope;
7657
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007658 /* Position the cursor over the rightmost paren, so that
7659 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 lnum = ourscope;
7661 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007662 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7663 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 lnum = trypos->lnum;
7665
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007666 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 * case 1: if (asdf &&
7668 * ldfd) {
7669 * }
7670 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007671 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7672 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007674 else if (curbuf->b_ind_js)
7675 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007677 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678
7679 start_brace = BRACE_AT_END;
7680 }
7681
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007682 /* For Javascript check if the line starts with "key:". */
7683 if (curbuf->b_ind_js)
7684 js_cur_has_key = cin_has_js_key(theline);
7685
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007687 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 * we want to be. otherwise, add the amount of room
7689 * that an indent is supposed to be.
7690 */
7691 if (theline[0] == '}')
7692 {
7693 /*
7694 * they may want closing braces to line up with something
7695 * other than the open brace. indulge them, if so.
7696 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007697 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 }
7699 else
7700 {
7701 /*
7702 * If we're looking at an "else", try to find an "if"
7703 * to match it with.
7704 * If we're looking at a "while", try to find a "do"
7705 * to match it with.
7706 */
7707 lookfor = LOOKFOR_INITIAL;
7708 if (cin_iselse(theline))
7709 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007710 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 lookfor = LOOKFOR_DO;
7712 if (lookfor != LOOKFOR_INITIAL)
7713 {
7714 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007715 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 {
7717 amount = get_indent(); /* XXX */
7718 goto theend;
7719 }
7720 }
7721
7722 /*
7723 * We get here if we are not on an "while-of-do" or "else" (or
7724 * failed to find a matching "if").
7725 * Search backwards for something to line up with.
7726 * First set amount for when we don't find anything.
7727 */
7728
7729 /*
7730 * if the '{' is _really_ at the left margin, use the imaginary
7731 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007732 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 */
7734
7735 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7736 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007737 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007738 lookfor_cpp_namespace = TRUE;
7739 }
7740 else if (start_brace == BRACE_AT_START &&
7741 lookfor_cpp_namespace) /* '{' is at start */
7742 {
7743
7744 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 }
7746 else
7747 {
7748 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007749 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007750 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007751
7752 l = skipwhite(ml_get_curline());
7753 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007754 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 else
7757 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007758 /* Compensate for adding b_ind_open_extra later. */
7759 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 if (amount < 0)
7761 amount = 0;
7762 }
7763 }
7764
7765 lookfor_break = FALSE;
7766
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007767 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 {
7769 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007770 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 }
7772 else if (cin_isscopedecl(theline)) /* private:, ... */
7773 {
7774 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007775 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 }
7777 else
7778 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007779 if (curbuf->b_ind_case_break && cin_isbreak(theline))
7780 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 lookfor_break = TRUE;
7782
7783 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007784 /* b_ind_level from start of block */
7785 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 }
7787 scope_amount = amount;
7788 whilelevel = 0;
7789
7790 /*
7791 * Search backwards. If we find something we recognize, line up
7792 * with that.
7793 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007794 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 * the usual amount relative to the conditional
7796 * that opens the block.
7797 */
7798 curwin->w_cursor = cur_curpos;
7799 for (;;)
7800 {
7801 curwin->w_cursor.lnum--;
7802 curwin->w_cursor.col = 0;
7803
7804 /*
7805 * If we went all the way back to the start of our scope, line
7806 * up with it.
7807 */
7808 if (curwin->w_cursor.lnum <= ourscope)
7809 {
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007810 /* We reached end of scope:
7811 * If looking for a enum or structure initialization
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 * go further back:
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007813 * If it is an initializer (enum xxx or xxx =), then
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 * don't add ind_continuation, otherwise it is a variable
7815 * declaration:
7816 * int x,
7817 * here; <-- add ind_continuation
7818 */
7819 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7820 {
7821 if (curwin->w_cursor.lnum == 0
7822 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007823 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007825 /* nothing found (abuse curbuf->b_ind_maxparen as
7826 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 * initialization) */
7828 if (cont_amount > 0)
7829 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007830 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 amount += ind_continuation;
7832 break;
7833 }
7834
7835 l = ml_get_curline();
7836
7837 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007838 * If we're in a comment or raw string now, skip to
7839 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007841 trypos = ind_find_start_CORS();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 if (trypos != NULL)
7843 {
7844 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007845 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 continue;
7847 }
7848
7849 /*
7850 * Skip preprocessor directives and blank lines.
7851 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007852 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
7853 &amount))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 continue;
7855
7856 if (cin_nocode(l))
7857 continue;
7858
7859 terminated = cin_isterminated(l, FALSE, TRUE);
7860
7861 /*
7862 * If we are at top level and the line looks like a
7863 * function declaration, we are done
7864 * (it's a variable declaration).
7865 */
7866 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007867 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 {
7869 /* if the line is terminated with another ','
7870 * it is a continued variable initialization.
7871 * don't add extra indent.
7872 * TODO: does not work, if a function
7873 * declaration is split over multiple lines:
7874 * cin_isfuncdecl returns FALSE then.
7875 */
7876 if (terminated == ',')
7877 break;
7878
7879 /* if it es a enum declaration or an assignment,
7880 * we are done.
7881 */
7882 if (terminated != ';' && cin_isinit())
7883 break;
7884
7885 /* nothing useful found */
7886 if (terminated == 0 || terminated == '{')
7887 continue;
7888 }
7889
7890 if (terminated != ';')
7891 {
7892 /* Skip parens and braces. Position the cursor
7893 * over the rightmost paren, so that matching it
7894 * will take us back to the start of the line.
7895 */ /* XXX */
7896 trypos = NULL;
7897 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007898 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007899 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900
7901 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007902 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903
7904 if (trypos != NULL)
7905 {
7906 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007907 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 continue;
7909 }
7910 }
7911
7912 /* it's a variable declaration, add indentation
7913 * like in
7914 * int a,
7915 * b;
7916 */
7917 if (cont_amount > 0)
7918 amount = cont_amount;
7919 else
7920 amount += ind_continuation;
7921 }
7922 else if (lookfor == LOOKFOR_UNTERM)
7923 {
7924 if (cont_amount > 0)
7925 amount = cont_amount;
7926 else
7927 amount += ind_continuation;
7928 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02007929 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007930 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02007931 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01007932 && lookfor != LOOKFOR_CPP_BASECLASS
7933 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02007934 {
7935 amount = scope_amount;
7936 if (theline[0] == '{')
7937 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007938 amount += curbuf->b_ind_open_extra;
7939 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007940 }
7941 }
7942
7943 if (lookfor_cpp_namespace)
7944 {
7945 /*
7946 * Looking for C++ namespace, need to look further
7947 * back.
7948 */
7949 if (curwin->w_cursor.lnum == ourscope)
7950 continue;
7951
7952 if (curwin->w_cursor.lnum == 0
7953 || curwin->w_cursor.lnum
7954 < ourscope - FIND_NAMESPACE_LIM)
7955 break;
7956
7957 l = ml_get_curline();
7958
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007959 /* If we're in a comment or raw string now, skip
7960 * to the start of it. */
7961 trypos = ind_find_start_CORS();
Bram Moolenaare79d1532011-10-04 18:03:47 +02007962 if (trypos != NULL)
7963 {
7964 curwin->w_cursor.lnum = trypos->lnum + 1;
7965 curwin->w_cursor.col = 0;
7966 continue;
7967 }
7968
7969 /* Skip preprocessor directives and blank lines. */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01007970 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum,
7971 &amount))
Bram Moolenaare79d1532011-10-04 18:03:47 +02007972 continue;
7973
7974 /* Finally the actual check for "namespace". */
7975 if (cin_is_cpp_namespace(l))
7976 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007977 amount += curbuf->b_ind_cpp_namespace
7978 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007979 break;
7980 }
7981
7982 if (cin_nocode(l))
7983 continue;
7984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 }
7986 break;
7987 }
7988
7989 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007990 * If we're in a comment or raw string now, skip to the start
7991 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007993 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 {
7995 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007996 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 continue;
7998 }
7999
8000 l = ml_get_curline();
8001
8002 /*
8003 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008004 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008006 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 if (iscase || cin_isscopedecl(l))
8008 {
8009 /* we are only looking for cpp base class
8010 * declaration/initialization any longer */
8011 if (lookfor == LOOKFOR_CPP_BASECLASS)
8012 break;
8013
8014 /* When looking for a "do" we are not interested in
8015 * labels. */
8016 if (whilelevel > 0)
8017 continue;
8018
8019 /*
8020 * case xx:
8021 * c = 99 + <- this indent plus continuation
8022 *-> here;
8023 */
8024 if (lookfor == LOOKFOR_UNTERM
8025 || lookfor == LOOKFOR_ENUM_OR_INIT)
8026 {
8027 if (cont_amount > 0)
8028 amount = cont_amount;
8029 else
8030 amount += ind_continuation;
8031 break;
8032 }
8033
8034 /*
8035 * case xx: <- line up with this case
8036 * x = 333;
8037 * case yy:
8038 */
8039 if ( (iscase && lookfor == LOOKFOR_CASE)
8040 || (iscase && lookfor_break)
8041 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8042 {
8043 /*
8044 * Check that this case label is not for another
8045 * switch()
8046 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008047 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008048 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 {
8050 amount = get_indent(); /* XXX */
8051 break;
8052 }
8053 continue;
8054 }
8055
8056 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8057
8058 /*
8059 * case xx: if (cond) <- line up with this if
8060 * y = y + 1;
8061 * -> s = 99;
8062 *
8063 * case xx:
8064 * if (cond) <- line up with this line
8065 * y = y + 1;
8066 * -> s = 99;
8067 */
8068 if (lookfor == LOOKFOR_TERM)
8069 {
8070 if (n)
8071 amount = n;
8072
8073 if (!lookfor_break)
8074 break;
8075 }
8076
8077 /*
8078 * case xx: x = x + 1; <- line up with this x
8079 * -> y = y + 1;
8080 *
8081 * case xx: if (cond) <- line up with this if
8082 * -> y = y + 1;
8083 */
8084 if (n)
8085 {
8086 amount = n;
8087 l = after_label(ml_get_curline());
8088 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008089 {
8090 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008091 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008092 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008093 amount += curbuf->b_ind_level
8094 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 break;
8097 }
8098
8099 /*
8100 * Try to get the indent of a statement before the switch
8101 * label. If nothing is found, line up relative to the
8102 * switch label.
8103 * break; <- may line up with this line
8104 * case xx:
8105 * -> y = 1;
8106 */
8107 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008108 ? curbuf->b_ind_case_code
8109 : curbuf->b_ind_scopedecl_code);
8110 lookfor = curbuf->b_ind_case_break
8111 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 continue;
8113 }
8114
8115 /*
8116 * Looking for a switch() label or C++ scope declaration,
8117 * ignore other lines, skip {}-blocks.
8118 */
8119 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8120 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008121 if (find_last_paren(l, '{', '}')
8122 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008123 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008125 curwin->w_cursor.col = 0;
8126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 continue;
8128 }
8129
8130 /*
8131 * Ignore jump labels with nothing after them.
8132 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008133 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 {
8135 l = after_label(ml_get_curline());
8136 if (l == NULL || cin_nocode(l))
8137 continue;
8138 }
8139
8140 /*
8141 * Ignore #defines, #if, etc.
8142 * Ignore comment and empty lines.
8143 * (need to get the line again, cin_islabel() may have
8144 * unlocked it)
8145 */
8146 l = ml_get_curline();
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008147 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 || cin_nocode(l))
8149 continue;
8150
8151 /*
8152 * Are we at the start of a cpp base class declaration or
8153 * constructor initialization?
8154 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008155 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008156 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008157 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008158 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008159 l = ml_get_curline();
8160 }
8161 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {
8163 if (lookfor == LOOKFOR_UNTERM)
8164 {
8165 if (cont_amount > 0)
8166 amount = cont_amount;
8167 else
8168 amount += ind_continuation;
8169 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008170 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008172 /* Need to find start of the declaration. */
8173 lookfor = LOOKFOR_UNTERM;
8174 ind_continuation = 0;
8175 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 }
8177 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008178 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008179 amount = get_baseclass_amount(
8180 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 break;
8182 }
8183 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8184 {
8185 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008186 * declaration or initialization before the opening brace.
8187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 if (cin_isterminated(l, TRUE, FALSE))
8189 break;
8190 else
8191 continue;
8192 }
8193
8194 /*
8195 * What happens next depends on the line being terminated.
8196 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008197 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 * 123,
8199 * sizeof
8200 * here
8201 * Otherwise check whether it is a enumeration or structure
8202 * initialisation (not indented) or a variable declaration
8203 * (indented).
8204 */
8205 terminated = cin_isterminated(l, FALSE, TRUE);
8206
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008207 if (js_cur_has_key)
8208 {
8209 js_cur_has_key = 0; /* only check the first line */
8210 if (curbuf->b_ind_js && terminated == ',')
8211 {
8212 /* For Javascript we might be inside an object:
8213 * key: something, <- align with this
8214 * key: something
8215 * or:
8216 * key: something + <- align with this
8217 * something,
8218 * key: something
8219 */
8220 lookfor = LOOKFOR_JS_KEY;
8221 }
8222 }
8223 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8224 {
8225 amount = get_indent();
8226 break;
8227 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008228 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008229 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008230 if (tryposBrace != NULL && tryposBrace->lnum
8231 >= curwin->w_cursor.lnum)
8232 break;
8233 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008234 /* line below current line is the one that starts a
8235 * (possibly broken) line ending in a comma. */
8236 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008237 else
8238 {
8239 amount = get_indent();
8240 if (curwin->w_cursor.lnum - 1 == ourscope)
8241 /* line above is start of the scope, thus current
8242 * line is the one that stars a (possibly broken)
8243 * line ending in a comma. */
8244 break;
8245 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008246 }
8247
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8249 && terminated == ','))
8250 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008251 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8252 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008253 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 /*
8255 * if we're in the middle of a paren thing,
8256 * go back to the line that starts it so
8257 * we can get the right prevailing indent
8258 * if ( foo &&
8259 * bar )
8260 */
8261 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008262 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008264 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 */
8266 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008267 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008268 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8269 || (trypos->lnum == tryposBrace->lnum
8270 && trypos->col < tryposBrace->col)))
8271 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272
8273 /*
8274 * If we are looking for ',', we also look for matching
8275 * braces.
8276 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008277 if (trypos == NULL && terminated == ','
8278 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008279 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280
8281 if (trypos != NULL)
8282 {
8283 /*
8284 * Check if we are on a case label now. This is
8285 * handled above.
8286 * case xx: if ( asdf &&
8287 * asdf)
8288 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008289 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008291 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 {
8293 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008294 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 continue;
8296 }
8297 }
8298
8299 /*
8300 * Skip over continuation lines to find the one to get the
8301 * indent from
8302 * char *usethis = "bla\
8303 * bla",
8304 * here;
8305 */
8306 if (terminated == ',')
8307 {
8308 while (curwin->w_cursor.lnum > 1)
8309 {
8310 l = ml_get(curwin->w_cursor.lnum - 1);
8311 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8312 break;
8313 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008314 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 }
8316 }
8317
8318 /*
8319 * Get indent and pointer to text for current line,
8320 * ignoring any jump label. XXX
8321 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008322 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008323 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008324 else
8325 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 /*
8327 * If this is just above the line we are indenting, and it
8328 * starts with a '{', line it up with this line.
8329 * while (not)
8330 * -> {
8331 * }
8332 */
8333 if (terminated != ',' && lookfor != LOOKFOR_TERM
8334 && theline[0] == '{')
8335 {
8336 amount = cur_amount;
8337 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008338 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 * doesn't start with a '{', which must have a match
8340 * in the same line (scope is the same). Probably:
8341 * { 1, 2 },
8342 * -> { 3, 4 }
8343 */
8344 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008345 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008347 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 {
8349 /* have to look back, whether it is a cpp base
8350 * class declaration or initialization */
8351 lookfor = LOOKFOR_CPP_BASECLASS;
8352 continue;
8353 }
8354 break;
8355 }
8356
8357 /*
8358 * Check if we are after an "if", "while", etc.
8359 * Also allow " } else".
8360 */
8361 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8362 {
8363 /*
8364 * Found an unterminated line after an if (), line up
8365 * with the last one.
8366 * if (cond)
8367 * 100 +
8368 * -> here;
8369 */
8370 if (lookfor == LOOKFOR_UNTERM
8371 || lookfor == LOOKFOR_ENUM_OR_INIT)
8372 {
8373 if (cont_amount > 0)
8374 amount = cont_amount;
8375 else
8376 amount += ind_continuation;
8377 break;
8378 }
8379
8380 /*
8381 * If this is just above the line we are indenting, we
8382 * are finished.
8383 * while (not)
8384 * -> here;
8385 * Otherwise this indent can be used when the line
8386 * before this is terminated.
8387 * yyy;
8388 * if (stat)
8389 * while (not)
8390 * xxx;
8391 * -> here;
8392 */
8393 amount = cur_amount;
8394 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008395 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 if (lookfor != LOOKFOR_TERM)
8397 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008398 amount += curbuf->b_ind_level
8399 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 break;
8401 }
8402
8403 /*
8404 * Special trick: when expecting the while () after a
8405 * do, line up with the while()
8406 * do
8407 * x = 1;
8408 * -> here
8409 */
8410 l = skipwhite(ml_get_curline());
8411 if (cin_isdo(l))
8412 {
8413 if (whilelevel == 0)
8414 break;
8415 --whilelevel;
8416 }
8417
8418 /*
8419 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008420 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 * Need to use the scope of this "else". XXX
8422 * If whilelevel != 0 continue looking for a "do {".
8423 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008424 if (cin_iselse(l) && whilelevel == 0)
8425 {
8426 /* If we're looking at "} else", let's make sure we
8427 * find the opening brace of the enclosing scope,
8428 * not the one from "if () {". */
8429 if (*l == '}')
8430 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008431 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008432
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008433 if ((trypos = find_start_brace()) == NULL
8434 || find_match(LOOKFOR_IF, trypos->lnum)
8435 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008436 break;
8437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 }
8439
8440 /*
8441 * If we're below an unterminated line that is not an
8442 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008443 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 * the line before this one.
8445 */
8446 else
8447 {
8448 /*
8449 * Found two unterminated lines on a row, line up with
8450 * the last one.
8451 * c = 99 +
8452 * 100 +
8453 * -> here;
8454 */
8455 if (lookfor == LOOKFOR_UNTERM)
8456 {
8457 /* When line ends in a comma add extra indent */
8458 if (terminated == ',')
8459 amount += ind_continuation;
8460 break;
8461 }
8462
8463 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8464 {
8465 /* Found two lines ending in ',', lineup with the
8466 * lowest one, but check for cpp base class
8467 * declaration/initialization, if it is an
8468 * opening brace or we are looking just for
8469 * enumerations/initializations. */
8470 if (terminated == ',')
8471 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008472 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 break;
8474
8475 lookfor = LOOKFOR_CPP_BASECLASS;
8476 continue;
8477 }
8478
8479 /* Ignore unterminated lines in between, but
8480 * reduce indent. */
8481 if (amount > cur_amount)
8482 amount = cur_amount;
8483 }
8484 else
8485 {
8486 /*
8487 * Found first unterminated line on a row, may
8488 * line up with this line, remember its indent
8489 * 100 +
8490 * -> here;
8491 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008492 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008494
8495 n = (int)STRLEN(l);
8496 if (terminated == ',' && (*skipwhite(l) == ']'
8497 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008498 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499
8500 /*
8501 * If previous line ends in ',', check whether we
8502 * are in an initialization or enum
8503 * struct xxx =
8504 * {
8505 * sizeof a,
8506 * 124 };
8507 * or a normal possible continuation line.
8508 * but only, of no other statement has been found
8509 * yet.
8510 */
8511 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8512 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008513 if (curbuf->b_ind_js)
8514 {
8515 /* Search for a line ending in a comma
8516 * and line up with the line below it
8517 * (could be the current line).
8518 * some = [
8519 * 1, <- line up here
8520 * 2,
8521 * some = [
8522 * 3 + <- line up here
8523 * 4 *
8524 * 5,
8525 * 6,
8526 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008527 if (cin_iscomment(skipwhite(l)))
8528 break;
8529 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008530 trypos = find_match_char('[',
8531 curbuf->b_ind_maxparen);
8532 if (trypos != NULL)
8533 {
8534 if (trypos->lnum
8535 == curwin->w_cursor.lnum - 1)
8536 {
8537 /* Current line is first inside
8538 * [], line up with it. */
8539 break;
8540 }
8541 ourscope = trypos->lnum;
8542 }
8543 }
8544 else
8545 {
8546 lookfor = LOOKFOR_ENUM_OR_INIT;
8547 cont_amount = cin_first_id_amount();
8548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 }
8550 else
8551 {
8552 if (lookfor == LOOKFOR_INITIAL
8553 && *l != NUL
8554 && l[STRLEN(l) - 1] == '\\')
8555 /* XXX */
8556 cont_amount = cin_get_equal_amount(
8557 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008558 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008559 && lookfor != LOOKFOR_JS_KEY
8560 && lookfor != LOOKFOR_COMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 lookfor = LOOKFOR_UNTERM;
8562 }
8563 }
8564 }
8565 }
8566
8567 /*
8568 * Check if we are after a while (cond);
8569 * If so: Ignore until the matching "do".
8570 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008571 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 {
8573 /*
8574 * Found an unterminated line after a while ();, line up
8575 * with the last one.
8576 * while (cond);
8577 * 100 + <- line up with this one
8578 * -> here;
8579 */
8580 if (lookfor == LOOKFOR_UNTERM
8581 || lookfor == LOOKFOR_ENUM_OR_INIT)
8582 {
8583 if (cont_amount > 0)
8584 amount = cont_amount;
8585 else
8586 amount += ind_continuation;
8587 break;
8588 }
8589
8590 if (whilelevel == 0)
8591 {
8592 lookfor = LOOKFOR_TERM;
8593 amount = get_indent(); /* XXX */
8594 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008595 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 }
8597 ++whilelevel;
8598 }
8599
8600 /*
8601 * We are after a "normal" statement.
8602 * If we had another statement we can stop now and use the
8603 * indent of that other statement.
8604 * Otherwise the indent of the current statement may be used,
8605 * search backwards for the next "normal" statement.
8606 */
8607 else
8608 {
8609 /*
8610 * Skip single break line, if before a switch label. It
8611 * may be lined up with the case label.
8612 */
8613 if (lookfor == LOOKFOR_NOBREAK
8614 && cin_isbreak(skipwhite(ml_get_curline())))
8615 {
8616 lookfor = LOOKFOR_ANY;
8617 continue;
8618 }
8619
8620 /*
8621 * Handle "do {" line.
8622 */
8623 if (whilelevel > 0)
8624 {
8625 l = cin_skipcomment(ml_get_curline());
8626 if (cin_isdo(l))
8627 {
8628 amount = get_indent(); /* XXX */
8629 --whilelevel;
8630 continue;
8631 }
8632 }
8633
8634 /*
8635 * Found a terminated line above an unterminated line. Add
8636 * the amount for a continuation line.
8637 * x = 1;
8638 * y = foo +
8639 * -> here;
8640 * or
8641 * int x = 1;
8642 * int foo,
8643 * -> here;
8644 */
8645 if (lookfor == LOOKFOR_UNTERM
8646 || lookfor == LOOKFOR_ENUM_OR_INIT)
8647 {
8648 if (cont_amount > 0)
8649 amount = cont_amount;
8650 else
8651 amount += ind_continuation;
8652 break;
8653 }
8654
8655 /*
8656 * Found a terminated line above a terminated line or "if"
8657 * etc. line. Use the amount of the line below us.
8658 * x = 1; x = 1;
8659 * if (asdf) y = 2;
8660 * while (asdf) ->here;
8661 * here;
8662 * ->foo;
8663 */
8664 if (lookfor == LOOKFOR_TERM)
8665 {
8666 if (!lookfor_break && whilelevel == 0)
8667 break;
8668 }
8669
8670 /*
8671 * First line above the one we're indenting is terminated.
8672 * To know what needs to be done look further backward for
8673 * a terminated line.
8674 */
8675 else
8676 {
8677 /*
8678 * position the cursor over the rightmost paren, so
8679 * that matching it will take us back to the start of
8680 * the line. Helps for:
8681 * func(asdr,
8682 * asdfasdf);
8683 * here;
8684 */
8685term_again:
8686 l = ml_get_curline();
8687 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008688 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008689 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 {
8691 /*
8692 * Check if we are on a case label now. This is
8693 * handled above.
8694 * case xx: if ( asdf &&
8695 * asdf)
8696 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008697 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008699 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 {
8701 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008702 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 continue;
8704 }
8705 }
8706
8707 /* When aligning with the case statement, don't align
8708 * with a statement after it.
8709 * case 1: { <-- don't use this { position
8710 * stat;
8711 * }
8712 * case 2:
8713 * stat;
8714 * }
8715 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008716 iscase = (curbuf->b_ind_keep_case_label
8717 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718
8719 /*
8720 * Get indent and pointer to text for current line,
8721 * ignoring any jump label.
8722 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008723 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724
8725 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008726 amount += curbuf->b_ind_open_extra;
8727 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008728 l = skipwhite(l);
8729 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008730 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8732
8733 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008734 * When a terminated line starts with "else" skip to
8735 * the matching "if":
8736 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008737 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008738 * Need to use the scope of this "else". XXX
8739 * If whilelevel != 0 continue looking for a "do {".
8740 */
8741 if (lookfor == LOOKFOR_TERM
8742 && *l != '}'
8743 && cin_iselse(l)
8744 && whilelevel == 0)
8745 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008746 if ((trypos = find_start_brace()) == NULL
8747 || find_match(LOOKFOR_IF, trypos->lnum)
8748 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008749 break;
8750 continue;
8751 }
8752
8753 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 * If we're at the end of a block, skip to the start of
8755 * that block.
8756 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008757 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008758 if (find_last_paren(l, '{', '}') /* XXX */
8759 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008761 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 /* if not "else {" check for terminated again */
8763 /* but skip block for "} else {" */
8764 l = cin_skipcomment(ml_get_curline());
8765 if (*l == '}' || !cin_iselse(l))
8766 goto term_again;
8767 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008768 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 }
8770 }
8771 }
8772 }
8773 }
8774 }
8775
8776 /* add extra indent for a comment */
8777 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008778 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008779
8780 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008781 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
8782 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008783
8784 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008786
8787 /*
8788 * ok -- we're not inside any sort of structure at all!
8789 *
8790 * This means we're at the top level, and everything should
8791 * basically just match where the previous line is, except
8792 * for the lines immediately following a function declaration,
8793 * which are K&R-style parameters and need to be indented.
8794 *
8795 * if our line starts with an open brace, forget about any
8796 * prevailing indent and make sure it looks like the start
8797 * of a function
8798 */
8799
8800 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008802 amount = curbuf->b_ind_first_open;
8803 goto theend;
8804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008806 /*
8807 * If the NEXT line is a function declaration, the current
8808 * line needs to be indented as a function type spec.
8809 * Don't do this if the current line looks like a comment or if the
8810 * current line is terminated, ie. ends in ';', or if the current line
8811 * contains { or }: "void f() {\n if (1)"
8812 */
8813 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8814 && !cin_nocode(theline)
8815 && vim_strchr(theline, '{') == NULL
8816 && vim_strchr(theline, '}') == NULL
8817 && !cin_ends_in(theline, (char_u *)":", NULL)
8818 && !cin_ends_in(theline, (char_u *)",", NULL)
8819 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
8820 cur_curpos.lnum + 1)
8821 && !cin_isterminated(theline, FALSE, TRUE))
8822 {
8823 amount = curbuf->b_ind_func_type;
8824 goto theend;
8825 }
8826
8827 /* search backwards until we find something we recognize */
8828 amount = 0;
8829 curwin->w_cursor = cur_curpos;
8830 while (curwin->w_cursor.lnum > 1)
8831 {
8832 curwin->w_cursor.lnum--;
8833 curwin->w_cursor.col = 0;
8834
8835 l = ml_get_curline();
8836
8837 /*
8838 * If we're in a comment or raw string now, skip to the start
8839 * of it.
8840 */ /* XXX */
8841 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008843 curwin->w_cursor.lnum = trypos->lnum + 1;
8844 curwin->w_cursor.col = 0;
8845 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 }
8847
8848 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008849 * Are we at the start of a cpp base class declaration or
8850 * constructor initialization?
8851 */ /* XXX */
8852 n = FALSE;
8853 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008855 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
8856 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008858 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008860 /* XXX */
8861 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
8862 break;
8863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008865 /*
8866 * Skip preprocessor directives and blank lines.
8867 */
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008868 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum, &amount))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008869 continue;
8870
8871 if (cin_nocode(l))
8872 continue;
8873
8874 /*
8875 * If the previous line ends in ',', use one level of
8876 * indentation:
8877 * int foo,
8878 * bar;
8879 * do this before checking for '}' in case of eg.
8880 * enum foobar
8881 * {
8882 * ...
8883 * } foo,
8884 * bar;
8885 */
8886 n = 0;
8887 if (cin_ends_in(l, (char_u *)",", NULL)
8888 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
8889 {
8890 /* take us back to opening paren */
8891 if (find_last_paren(l, '(', ')')
8892 && (trypos = find_match_paren(
8893 curbuf->b_ind_maxparen)) != NULL)
8894 curwin->w_cursor = *trypos;
8895
8896 /* For a line ending in ',' that is a continuation line go
8897 * back to the first line with a backslash:
8898 * char *foo = "bla\
8899 * bla",
8900 * here;
8901 */
8902 while (n == 0 && curwin->w_cursor.lnum > 1)
8903 {
8904 l = ml_get(curwin->w_cursor.lnum - 1);
8905 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8906 break;
8907 --curwin->w_cursor.lnum;
8908 curwin->w_cursor.col = 0;
8909 }
8910
8911 amount = get_indent(); /* XXX */
8912
8913 if (amount == 0)
8914 amount = cin_first_id_amount();
8915 if (amount == 0)
8916 amount = ind_continuation;
8917 break;
8918 }
8919
8920 /*
8921 * If the line looks like a function declaration, and we're
8922 * not in a comment, put it the left margin.
8923 */
8924 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
8925 break;
8926 l = ml_get_curline();
8927
8928 /*
8929 * Finding the closing '}' of a previous function. Put
8930 * current line at the left margin. For when 'cino' has "fs".
8931 */
8932 if (*skipwhite(l) == '}')
8933 break;
8934
8935 /* (matching {)
8936 * If the previous line ends on '};' (maybe followed by
8937 * comments) align at column 0. For example:
8938 * char *string_array[] = { "foo",
8939 * / * x * / "b};ar" }; / * foobar * /
8940 */
8941 if (cin_ends_in(l, (char_u *)"};", NULL))
8942 break;
8943
8944 /*
8945 * If the previous line ends on '[' we are probably in an
8946 * array constant:
8947 * something = [
8948 * 234, <- extra indent
8949 */
8950 if (cin_ends_in(l, (char_u *)"[", NULL))
8951 {
8952 amount = get_indent() + ind_continuation;
8953 break;
8954 }
8955
8956 /*
8957 * Find a line only has a semicolon that belongs to a previous
8958 * line ending in '}', e.g. before an #endif. Don't increase
8959 * indent then.
8960 */
8961 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
8962 {
8963 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964
8965 while (curwin->w_cursor.lnum > 1)
8966 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008967 look = ml_get(--curwin->w_cursor.lnum);
8968 if (!(cin_nocode(look) || cin_ispreproc_cont(
Bram Moolenaarc6aa4752017-01-07 15:39:43 +01008969 &look, &curwin->w_cursor.lnum, &amount)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008971 }
8972 if (curwin->w_cursor.lnum > 0
8973 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008976 curwin->w_cursor = curpos_save;
8977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008979 /*
8980 * If the PREVIOUS line is a function declaration, the current
8981 * line (and the ones that follow) needs to be indented as
8982 * parameters.
8983 */
8984 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
8985 {
8986 amount = curbuf->b_ind_param;
8987 break;
8988 }
8989
8990 /*
8991 * If the previous line ends in ';' and the line before the
8992 * previous line ends in ',' or '\', ident to column zero:
8993 * int foo,
8994 * bar;
8995 * indent_to_0 here;
8996 */
8997 if (cin_ends_in(l, (char_u *)";", NULL))
8998 {
8999 l = ml_get(curwin->w_cursor.lnum - 1);
9000 if (cin_ends_in(l, (char_u *)",", NULL)
9001 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9002 break;
9003 l = ml_get_curline();
9004 }
9005
9006 /*
9007 * Doesn't look like anything interesting -- so just
9008 * use the indent of this line.
9009 *
9010 * Position the cursor over the rightmost paren, so that
9011 * matching it will take us back to the start of the line.
9012 */
9013 find_last_paren(l, '(', ')');
9014
9015 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9016 curwin->w_cursor = *trypos;
9017 amount = get_indent(); /* XXX */
9018 break;
9019 }
9020
9021 /* add extra indent for a comment */
9022 if (cin_iscomment(theline))
9023 amount += curbuf->b_ind_comment;
9024
9025 /* add extra indent if the previous line ended in a backslash:
9026 * "asdfasdf\
9027 * here";
9028 * char *foo = "asdf\
9029 * here";
9030 */
9031 if (cur_curpos.lnum > 1)
9032 {
9033 l = ml_get(cur_curpos.lnum - 1);
9034 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9035 {
9036 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9037 if (cur_amount > 0)
9038 amount = cur_amount;
9039 else if (cur_amount == 0)
9040 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 }
9042 }
9043
9044theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009045 if (amount < 0)
9046 amount = 0;
9047
9048laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 /* put the cursor back where it belongs */
9050 curwin->w_cursor = cur_curpos;
9051
9052 vim_free(linecopy);
9053
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 return amount;
9055}
9056
9057 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009058find_match(int lookfor, linenr_T ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059{
9060 char_u *look;
9061 pos_T *theirscope;
9062 char_u *mightbeif;
9063 int elselevel;
9064 int whilelevel;
9065
9066 if (lookfor == LOOKFOR_IF)
9067 {
9068 elselevel = 1;
9069 whilelevel = 0;
9070 }
9071 else
9072 {
9073 elselevel = 0;
9074 whilelevel = 1;
9075 }
9076
9077 curwin->w_cursor.col = 0;
9078
9079 while (curwin->w_cursor.lnum > ourscope + 1)
9080 {
9081 curwin->w_cursor.lnum--;
9082 curwin->w_cursor.col = 0;
9083
9084 look = cin_skipcomment(ml_get_curline());
9085 if (cin_iselse(look)
9086 || cin_isif(look)
9087 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009088 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 {
9090 /*
9091 * if we've gone outside the braces entirely,
9092 * we must be out of scope...
9093 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009094 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 if (theirscope == NULL)
9096 break;
9097
9098 /*
9099 * and if the brace enclosing this is further
9100 * back than the one enclosing the else, we're
9101 * out of luck too.
9102 */
9103 if (theirscope->lnum < ourscope)
9104 break;
9105
9106 /*
9107 * and if they're enclosed in a *deeper* brace,
9108 * then we can ignore it because it's in a
9109 * different scope...
9110 */
9111 if (theirscope->lnum > ourscope)
9112 continue;
9113
9114 /*
9115 * if it was an "else" (that's not an "else if")
9116 * then we need to go back to another if, so
9117 * increment elselevel
9118 */
9119 look = cin_skipcomment(ml_get_curline());
9120 if (cin_iselse(look))
9121 {
9122 mightbeif = cin_skipcomment(look + 4);
9123 if (!cin_isif(mightbeif))
9124 ++elselevel;
9125 continue;
9126 }
9127
9128 /*
9129 * if it was a "while" then we need to go back to
9130 * another "do", so increment whilelevel. XXX
9131 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009132 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 {
9134 ++whilelevel;
9135 continue;
9136 }
9137
9138 /* If it's an "if" decrement elselevel */
9139 look = cin_skipcomment(ml_get_curline());
9140 if (cin_isif(look))
9141 {
9142 elselevel--;
9143 /*
9144 * When looking for an "if" ignore "while"s that
9145 * get in the way.
9146 */
9147 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9148 whilelevel = 0;
9149 }
9150
9151 /* If it's a "do" decrement whilelevel */
9152 if (cin_isdo(look))
9153 whilelevel--;
9154
9155 /*
9156 * if we've used up all the elses, then
9157 * this must be the if that we want!
9158 * match the indent level of that if.
9159 */
9160 if (elselevel <= 0 && whilelevel <= 0)
9161 {
9162 return OK;
9163 }
9164 }
9165 }
9166 return FAIL;
9167}
9168
9169# if defined(FEAT_EVAL) || defined(PROTO)
9170/*
9171 * Get indent level from 'indentexpr'.
9172 */
9173 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009174get_expr_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175{
9176 int indent;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009177 pos_T save_pos;
9178 colnr_T save_curswant;
9179 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009181 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9182 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009184 /* Save and restore cursor position and curswant, in case it was changed
9185 * via :normal commands */
9186 save_pos = curwin->w_cursor;
9187 save_curswant = curwin->w_curswant;
9188 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009190 if (use_sandbox)
9191 ++sandbox;
9192 ++textlock;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02009193 indent = (int)eval_to_number(curbuf->b_p_inde);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009194 if (use_sandbox)
9195 --sandbox;
9196 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197
9198 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9199 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9200 * command. */
9201 save_State = State;
9202 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009203 curwin->w_cursor = save_pos;
9204 curwin->w_curswant = save_curswant;
9205 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 check_cursor();
9207 State = save_State;
9208
9209 /* If there is an error, just keep the current indent. */
9210 if (indent < 0)
9211 indent = get_indent();
9212
9213 return indent;
9214}
9215# endif
9216
9217#endif /* FEAT_CINDENT */
9218
9219#if defined(FEAT_LISP) || defined(PROTO)
9220
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009221static int lisp_match(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222
9223 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009224lisp_match(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225{
9226 char_u buf[LSIZE];
9227 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009228 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229
9230 while (*word != NUL)
9231 {
9232 (void)copy_option_part(&word, buf, LSIZE, ",");
9233 len = (int)STRLEN(buf);
9234 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9235 return TRUE;
9236 }
9237 return FALSE;
9238}
9239
9240/*
9241 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9242 * The incompatible newer method is quite a bit better at indenting
9243 * code in lisp-like languages than the traditional one; it's still
9244 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9245 *
9246 * TODO:
9247 * Findmatch() should be adapted for lisp, also to make showmatch
9248 * work correctly: now (v5.3) it seems all C/C++ oriented:
9249 * - it does not recognize the #\( and #\) notations as character literals
9250 * - it doesn't know about comments starting with a semicolon
9251 * - it incorrectly interprets '(' as a character literal
9252 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009253 * Update from Sergey Khorev:
9254 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 */
9256 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009257get_lisp_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009259 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260 int amount;
9261 char_u *that;
9262 colnr_T col;
9263 colnr_T firsttry;
9264 int parencount, quotecount;
9265 int vi_lisp;
9266
9267 /* Set vi_lisp to use the vi-compatible method */
9268 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9269
9270 realpos = curwin->w_cursor;
9271 curwin->w_cursor.col = 0;
9272
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009273 if ((pos = findmatch(NULL, '(')) == NULL)
9274 pos = findmatch(NULL, '[');
9275 else
9276 {
9277 paren = *pos;
9278 pos = findmatch(NULL, '[');
9279 if (pos == NULL || ltp(pos, &paren))
9280 pos = &paren;
9281 }
9282 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 {
9284 /* Extra trick: Take the indent of the first previous non-white
9285 * line that is at the same () level. */
9286 amount = -1;
9287 parencount = 0;
9288
9289 while (--curwin->w_cursor.lnum >= pos->lnum)
9290 {
9291 if (linewhite(curwin->w_cursor.lnum))
9292 continue;
9293 for (that = ml_get_curline(); *that != NUL; ++that)
9294 {
9295 if (*that == ';')
9296 {
9297 while (*(that + 1) != NUL)
9298 ++that;
9299 continue;
9300 }
9301 if (*that == '\\')
9302 {
9303 if (*(that + 1) != NUL)
9304 ++that;
9305 continue;
9306 }
9307 if (*that == '"' && *(that + 1) != NUL)
9308 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009309 while (*++that && *that != '"')
9310 {
9311 /* skipping escaped characters in the string */
9312 if (*that == '\\')
9313 {
9314 if (*++that == NUL)
9315 break;
9316 if (that[1] == NUL)
9317 {
9318 ++that;
9319 break;
9320 }
9321 }
9322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009324 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009326 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 --parencount;
9328 }
9329 if (parencount == 0)
9330 {
9331 amount = get_indent();
9332 break;
9333 }
9334 }
9335
9336 if (amount == -1)
9337 {
9338 curwin->w_cursor.lnum = pos->lnum;
9339 curwin->w_cursor.col = pos->col;
9340 col = pos->col;
9341
9342 that = ml_get_curline();
9343
9344 if (vi_lisp && get_indent() == 0)
9345 amount = 2;
9346 else
9347 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009348 char_u *line = that;
9349
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 amount = 0;
9351 while (*that && col)
9352 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009353 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 col--;
9355 }
9356
9357 /*
9358 * Some keywords require "body" indenting rules (the
9359 * non-standard-lisp ones are Scheme special forms):
9360 *
9361 * (let ((a 1)) instead (let ((a 1))
9362 * (...)) of (...))
9363 */
9364
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009365 if (!vi_lisp && (*that == '(' || *that == '[')
9366 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 amount += 2;
9368 else
9369 {
9370 that++;
9371 amount++;
9372 firsttry = amount;
9373
9374 while (vim_iswhite(*that))
9375 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009376 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 ++that;
9378 }
9379
9380 if (*that && *that != ';') /* not a comment line */
9381 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009382 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009384 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 firsttry++;
9386
9387 parencount = 0;
9388 quotecount = 0;
9389
9390 if (vi_lisp
9391 || (*that != '"'
9392 && *that != '\''
9393 && *that != '#'
9394 && (*that < '0' || *that > '9')))
9395 {
9396 while (*that
9397 && (!vim_iswhite(*that)
9398 || quotecount
9399 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009400 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 && !quotecount
9402 && !parencount
9403 && vi_lisp)))
9404 {
9405 if (*that == '"')
9406 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009407 if ((*that == '(' || *that == '[')
9408 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009410 if ((*that == ')' || *that == ']')
9411 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 --parencount;
9413 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009414 amount += lbr_chartabsize_adv(
9415 line, &that, (colnr_T)amount);
9416 amount += lbr_chartabsize_adv(
9417 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 }
9419 }
9420 while (vim_iswhite(*that))
9421 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009422 amount += lbr_chartabsize(
9423 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 that++;
9425 }
9426 if (!*that || *that == ';')
9427 amount = firsttry;
9428 }
9429 }
9430 }
9431 }
9432 }
9433 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009434 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435
9436 curwin->w_cursor = realpos;
9437
9438 return amount;
9439}
9440#endif /* FEAT_LISP */
9441
9442 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009443prepare_to_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009445#if defined(SIGHUP) && defined(SIG_IGN)
9446 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9447 * makes Vim exit and then handling SIGHUP causes various reentrance
9448 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009449 signal(SIGHUP, SIG_IGN);
9450#endif
9451
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452#ifdef FEAT_GUI
9453 if (gui.in_use)
9454 {
9455 gui.dying = TRUE;
9456 out_trash(); /* trash any pending output */
9457 }
9458 else
9459#endif
9460 {
9461 windgoto((int)Rows - 1, 0);
9462
9463 /*
9464 * Switch terminal mode back now, so messages end up on the "normal"
9465 * screen (if there are two screens).
9466 */
9467 settmode(TMODE_COOK);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02009468 stoptermcap();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469 out_flush();
9470 }
9471}
9472
9473/*
9474 * Preserve files and exit.
9475 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009476 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9477 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 */
9479 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009480preserve_exit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481{
9482 buf_T *buf;
9483
9484 prepare_to_exit();
9485
Bram Moolenaar4770d092006-01-12 23:22:24 +00009486 /* Setting this will prevent free() calls. That avoids calling free()
9487 * recursively when free() was invoked with a bad pointer. */
9488 really_exiting = TRUE;
9489
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 out_str(IObuff);
9491 screen_start(); /* don't know where cursor is now */
9492 out_flush();
9493
9494 ml_close_notmod(); /* close all not-modified buffers */
9495
Bram Moolenaar29323592016-07-24 22:04:11 +02009496 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 {
9498 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9499 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009500 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 screen_start(); /* don't know where cursor is now */
9502 out_flush();
9503 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9504 break;
9505 }
9506 }
9507
9508 ml_close_all(FALSE); /* close all memfiles, without deleting */
9509
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009510 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511
9512 getout(1);
9513}
9514
9515/*
9516 * return TRUE if "fname" exists.
9517 */
9518 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009519vim_fexists(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520{
Bram Moolenaar8767f522016-07-01 17:17:39 +02009521 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522
9523 if (mch_stat((char *)fname, &st))
9524 return FALSE;
9525 return TRUE;
9526}
9527
9528/*
9529 * Check for CTRL-C pressed, but only once in a while.
9530 * Should be used instead of ui_breakcheck() for functions that check for
9531 * each line in the file. Calling ui_breakcheck() each time takes too much
9532 * time, because it can be a system call.
9533 */
9534
9535#ifndef BREAKCHECK_SKIP
9536# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9537# define BREAKCHECK_SKIP 200
9538# else
9539# define BREAKCHECK_SKIP 32
9540# endif
9541#endif
9542
9543static int breakcheck_count = 0;
9544
9545 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009546line_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547{
9548 if (++breakcheck_count >= BREAKCHECK_SKIP)
9549 {
9550 breakcheck_count = 0;
9551 ui_breakcheck();
9552 }
9553}
9554
9555/*
9556 * Like line_breakcheck() but check 10 times less often.
9557 */
9558 void
Bram Moolenaar9b578142016-01-30 19:39:49 +01009559fast_breakcheck(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560{
9561 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9562 {
9563 breakcheck_count = 0;
9564 ui_breakcheck();
9565 }
9566}
9567
9568/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009569 * Invoke expand_wildcards() for one pattern.
9570 * Expand items like "%:h" before the expansion.
9571 * Returns OK or FAIL.
9572 */
9573 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009574expand_wildcards_eval(
9575 char_u **pat, /* pointer to input pattern */
9576 int *num_file, /* resulting number of files */
9577 char_u ***file, /* array of resulting files */
9578 int flags) /* EW_DIR, etc. */
Bram Moolenaard7834d32009-12-02 16:14:36 +00009579{
9580 int ret = FAIL;
9581 char_u *eval_pat = NULL;
9582 char_u *exp_pat = *pat;
9583 char_u *ignored_msg;
9584 int usedlen;
9585
9586 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9587 {
9588 ++emsg_off;
9589 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9590 NULL, &ignored_msg, NULL);
9591 --emsg_off;
9592 if (eval_pat != NULL)
9593 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9594 }
9595
9596 if (exp_pat != NULL)
9597 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9598
9599 if (eval_pat != NULL)
9600 {
9601 vim_free(exp_pat);
9602 vim_free(eval_pat);
9603 }
9604
9605 return ret;
9606}
9607
9608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9610 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009611 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 */
9613 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009614expand_wildcards(
9615 int num_pat, /* number of input patterns */
9616 char_u **pat, /* array of input patterns */
9617 int *num_files, /* resulting number of files */
9618 char_u ***files, /* array of resulting files */
9619 int flags) /* EW_DIR, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620{
9621 int retval;
9622 int i, j;
9623 char_u *p;
9624 int non_suf_match; /* number without matching suffix */
9625
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009626 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627
9628 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009629 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 return retval;
9631
9632#ifdef FEAT_WILDIGN
9633 /*
9634 * Remove names that match 'wildignore'.
9635 */
9636 if (*p_wig)
9637 {
9638 char_u *ffname;
9639
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009640 /* check all files in (*files)[] */
9641 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009643 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644 if (ffname == NULL) /* out of memory */
9645 break;
9646# ifdef VMS
9647 vms_remove_version(ffname);
9648# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009649 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009651 /* remove this matching files from the list */
9652 vim_free((*files)[i]);
9653 for (j = i; j + 1 < *num_files; ++j)
9654 (*files)[j] = (*files)[j + 1];
9655 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 --i;
9657 }
9658 vim_free(ffname);
9659 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009660
9661 /* If the number of matches is now zero, we fail. */
9662 if (*num_files == 0)
9663 {
9664 vim_free(*files);
9665 *files = NULL;
9666 return FAIL;
9667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 }
9669#endif
9670
9671 /*
9672 * Move the names where 'suffixes' match to the end.
9673 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009674 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 {
9676 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009677 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009679 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680 {
9681 /*
9682 * Move the name without matching suffix to the front
9683 * of the list.
9684 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009685 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009687 (*files)[j] = (*files)[j - 1];
9688 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 }
9690 }
9691 }
9692
9693 return retval;
9694}
9695
9696/*
9697 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9698 */
9699 int
Bram Moolenaar9b578142016-01-30 19:39:49 +01009700match_suffix(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701{
9702 int fnamelen, setsuflen;
9703 char_u *setsuf;
9704#define MAXSUFLEN 30 /* maximum length of a file suffix */
9705 char_u suf_buf[MAXSUFLEN];
9706
9707 fnamelen = (int)STRLEN(fname);
9708 setsuflen = 0;
9709 for (setsuf = p_su; *setsuf; )
9710 {
9711 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009712 if (setsuflen == 0)
9713 {
9714 char_u *tail = gettail(fname);
9715
9716 /* empty entry: match name without a '.' */
9717 if (vim_strchr(tail, '.') == NULL)
9718 {
9719 setsuflen = 1;
9720 break;
9721 }
9722 }
9723 else
9724 {
9725 if (fnamelen >= setsuflen
9726 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9727 (size_t)setsuflen) == 0)
9728 break;
9729 setsuflen = 0;
9730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 }
9732 return (setsuflen != 0);
9733}
9734
9735#if !defined(NO_EXPANDPATH) || defined(PROTO)
9736
9737# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009738static int vim_backtick(char_u *p);
9739static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740# endif
9741
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009742# if defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743/*
9744 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9745 * it's shared between these systems.
9746 */
Bram Moolenaar48e330a2016-02-23 14:53:34 +01009747# if defined(PROTO)
9748# define _cdecl
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749# else
9750# ifdef __BORLANDC__
9751# define _cdecl _RTLENTRYF
9752# endif
9753# endif
9754
9755/*
9756 * comparison function for qsort in dos_expandpath()
9757 */
9758 static int _cdecl
9759pstrcmp(const void *a, const void *b)
9760{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009761 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762}
9763
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009765 * Recursively expand one path component into all matching files and/or
9766 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 * Return the number of matches found.
9768 * "path" has backslashes before chars that are not to be expanded, starting
9769 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009770 * Return the number of matches found.
9771 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 */
9773 static int
9774dos_expandpath(
9775 garray_T *gap,
9776 char_u *path,
9777 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009778 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009779 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009781 char_u *buf;
9782 char_u *path_end;
9783 char_u *p, *s, *e;
9784 int start_len = gap->ga_len;
9785 char_u *pat;
9786 regmatch_T regmatch;
9787 int starts_with_dot;
9788 int matches;
9789 int len;
9790 int starstar = FALSE;
9791 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 WIN32_FIND_DATA fb;
9793 HANDLE hFind = (HANDLE)0;
9794# ifdef FEAT_MBYTE
9795 WIN32_FIND_DATAW wfb;
9796 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9797# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009799 int ok;
9800
9801 /* Expanding "**" may take a long time, check for CTRL-C. */
9802 if (stardepth > 0)
9803 {
9804 ui_breakcheck();
9805 if (got_int)
9806 return 0;
9807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808
Bram Moolenaar7314efd2015-10-31 15:32:52 +01009809 /* Make room for file name. When doing encoding conversion the actual
9810 * length may be quite a bit longer, thus use the maximum possible length. */
9811 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 if (buf == NULL)
9813 return 0;
9814
9815 /*
9816 * Find the first part in the path name that contains a wildcard or a ~1.
9817 * Copy it into buf, including the preceding characters.
9818 */
9819 p = buf;
9820 s = buf;
9821 e = NULL;
9822 path_end = path;
9823 while (*path_end != NUL)
9824 {
9825 /* May ignore a wildcard that has a backslash before it; it will
9826 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9827 if (path_end >= path + wildoff && rem_backslash(path_end))
9828 *p++ = *path_end++;
9829 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9830 {
9831 if (e != NULL)
9832 break;
9833 s = p + 1;
9834 }
9835 else if (path_end >= path + wildoff
9836 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9837 e = p;
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009838# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839 if (has_mbyte)
9840 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009841 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 STRNCPY(p, path_end, len);
9843 p += len;
9844 path_end += len;
9845 }
9846 else
Bram Moolenaar780d4c32016-03-25 17:21:19 +01009847# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 *p++ = *path_end++;
9849 }
9850 e = p;
9851 *e = NUL;
9852
9853 /* now we have one wildcard component between s and e */
9854 /* Remove backslashes between "wildoff" and the start of the wildcard
9855 * component. */
9856 for (p = buf + wildoff; p < s; ++p)
9857 if (rem_backslash(p))
9858 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009859 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860 --e;
9861 --s;
9862 }
9863
Bram Moolenaar231334e2005-07-25 20:46:57 +00009864 /* Check for "**" between "s" and "e". */
9865 for (p = s; p < e; ++p)
9866 if (p[0] == '*' && p[1] == '*')
9867 starstar = TRUE;
9868
Bram Moolenaard82103e2016-01-17 17:04:05 +01009869 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9871 if (pat == NULL)
9872 {
9873 vim_free(buf);
9874 return 0;
9875 }
9876
9877 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009878 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009879 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 regmatch.rm_ic = TRUE; /* Always ignore case */
9881 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009882 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009883 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 vim_free(pat);
9885
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009886 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 {
9888 vim_free(buf);
9889 return 0;
9890 }
9891
9892 /* remember the pattern or file name being looked for */
9893 matchname = vim_strsave(s);
9894
Bram Moolenaar231334e2005-07-25 20:46:57 +00009895 /* If "**" is by itself, this is the first time we encounter it and more
9896 * is following then find matches without any directory. */
9897 if (!didstar && stardepth < 100 && starstar && e - s == 2
9898 && *path_end == '/')
9899 {
9900 STRCPY(s, path_end + 1);
9901 ++stardepth;
9902 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9903 --stardepth;
9904 }
9905
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906 /* Scan all files in the directory with "dir/ *.*" */
9907 STRCPY(s, "*.*");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908# ifdef FEAT_MBYTE
9909 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
9910 {
9911 /* The active codepage differs from 'encoding'. Attempt using the
9912 * wide function. If it fails because it is not implemented fall back
9913 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009914 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915 if (wn != NULL)
9916 {
9917 hFind = FindFirstFileW(wn, &wfb);
9918 if (hFind == INVALID_HANDLE_VALUE
9919 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
9920 {
9921 vim_free(wn);
9922 wn = NULL;
9923 }
9924 }
9925 }
9926
9927 if (wn == NULL)
9928# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01009929 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931
9932 while (ok)
9933 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934# ifdef FEAT_MBYTE
9935 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009936 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 else
9938# endif
9939 p = (char_u *)fb.cFileName;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940 /* Ignore entries starting with a dot, unless when asked for. Accept
9941 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +01009942 if ((p[0] != '.' || starts_with_dot
9943 || ((flags & EW_DODOT)
9944 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009946 || (regmatch.regprog != NULL
9947 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009948 || ((flags & EW_NOTWILD)
9949 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 STRCPY(s, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009952 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009953
9954 if (starstar && stardepth < 100)
9955 {
9956 /* For "**" in the pattern first go deeper in the tree to
9957 * find matches. */
9958 STRCPY(buf + len, "/**");
9959 STRCPY(buf + len + 3, path_end);
9960 ++stardepth;
9961 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
9962 --stardepth;
9963 }
9964
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965 STRCPY(buf + len, path_end);
9966 if (mch_has_exp_wildcard(path_end))
9967 {
9968 /* need to expand another component of the path */
9969 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009970 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 }
9972 else
9973 {
9974 /* no more wildcards, check if there is a match */
9975 /* remove backslashes for the remaining components only */
9976 if (*path_end != 0)
9977 backslash_halve(buf + len + 1);
9978 if (mch_getperm(buf) >= 0) /* add existing file */
9979 addfile(gap, buf, flags);
9980 }
9981 }
9982
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983# ifdef FEAT_MBYTE
9984 if (wn != NULL)
9985 {
9986 vim_free(p);
9987 ok = FindNextFileW(hFind, &wfb);
9988 }
9989 else
9990# endif
9991 ok = FindNextFile(hFind, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992
9993 /* If no more matches and no match was used, try expanding the name
9994 * itself. Finds the long name of a short filename. */
9995 if (!ok && matchname != NULL && gap->ga_len == start_len)
9996 {
9997 STRCPY(s, matchname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 FindClose(hFind);
9999# ifdef FEAT_MBYTE
10000 if (wn != NULL)
10001 {
10002 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010003 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 if (wn != NULL)
10005 hFind = FindFirstFileW(wn, &wfb);
10006 }
10007 if (wn == NULL)
10008# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010009 hFind = FindFirstFile((LPCSTR)buf, &fb);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 ok = (hFind != INVALID_HANDLE_VALUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011 vim_free(matchname);
10012 matchname = NULL;
10013 }
10014 }
10015
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 FindClose(hFind);
10017# ifdef FEAT_MBYTE
10018 vim_free(wn);
10019# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010021 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010022 vim_free(matchname);
10023
10024 matches = gap->ga_len - start_len;
10025 if (matches > 0)
10026 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10027 sizeof(char_u *), pstrcmp);
10028 return matches;
10029}
10030
10031 int
10032mch_expandpath(
10033 garray_T *gap,
10034 char_u *path,
10035 int flags) /* EW_* flags */
10036{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010037 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038}
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010039# endif /* WIN3264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040
Bram Moolenaar231334e2005-07-25 20:46:57 +000010041#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10042 || defined(PROTO)
10043/*
10044 * Unix style wildcard expansion code.
10045 * It's here because it's used both for Unix and Mac.
10046 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010047static int pstrcmp(const void *, const void *);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010048
10049 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010050pstrcmp(const void *a, const void *b)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010051{
10052 return (pathcmp(*(char **)a, *(char **)b, -1));
10053}
10054
10055/*
10056 * Recursively expand one path component into all matching files and/or
10057 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10058 * "path" has backslashes before chars that are not to be expanded, starting
10059 * at "path + wildoff".
10060 * Return the number of matches found.
10061 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10062 */
10063 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010064unix_expandpath(
10065 garray_T *gap,
10066 char_u *path,
10067 int wildoff,
10068 int flags, /* EW_* flags */
10069 int didstar) /* expanded "**" once already */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010070{
10071 char_u *buf;
10072 char_u *path_end;
10073 char_u *p, *s, *e;
10074 int start_len = gap->ga_len;
10075 char_u *pat;
10076 regmatch_T regmatch;
10077 int starts_with_dot;
10078 int matches;
10079 int len;
10080 int starstar = FALSE;
10081 static int stardepth = 0; /* depth for "**" expansion */
10082
10083 DIR *dirp;
10084 struct dirent *dp;
10085
10086 /* Expanding "**" may take a long time, check for CTRL-C. */
10087 if (stardepth > 0)
10088 {
10089 ui_breakcheck();
10090 if (got_int)
10091 return 0;
10092 }
10093
10094 /* make room for file name */
10095 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10096 if (buf == NULL)
10097 return 0;
10098
10099 /*
10100 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010101 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010102 * Copy it into "buf", including the preceding characters.
10103 */
10104 p = buf;
10105 s = buf;
10106 e = NULL;
10107 path_end = path;
10108 while (*path_end != NUL)
10109 {
10110 /* May ignore a wildcard that has a backslash before it; it will
10111 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10112 if (path_end >= path + wildoff && rem_backslash(path_end))
10113 *p++ = *path_end++;
10114 else if (*path_end == '/')
10115 {
10116 if (e != NULL)
10117 break;
10118 s = p + 1;
10119 }
10120 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010121 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010122 || (!p_fic && (flags & EW_ICASE)
10123 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010124 e = p;
10125#ifdef FEAT_MBYTE
10126 if (has_mbyte)
10127 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010128 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010129 STRNCPY(p, path_end, len);
10130 p += len;
10131 path_end += len;
10132 }
10133 else
10134#endif
10135 *p++ = *path_end++;
10136 }
10137 e = p;
10138 *e = NUL;
10139
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010140 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010141 /* Remove backslashes between "wildoff" and the start of the wildcard
10142 * component. */
10143 for (p = buf + wildoff; p < s; ++p)
10144 if (rem_backslash(p))
10145 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010146 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010147 --e;
10148 --s;
10149 }
10150
10151 /* Check for "**" between "s" and "e". */
10152 for (p = s; p < e; ++p)
10153 if (p[0] == '*' && p[1] == '*')
10154 starstar = TRUE;
10155
10156 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010157 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010158 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10159 if (pat == NULL)
10160 {
10161 vim_free(buf);
10162 return 0;
10163 }
10164
10165 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010166 if (flags & EW_ICASE)
10167 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10168 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010169 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010170 if (flags & (EW_NOERROR | EW_NOTWILD))
10171 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010172 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010173 if (flags & (EW_NOERROR | EW_NOTWILD))
10174 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010175 vim_free(pat);
10176
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010177 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010178 {
10179 vim_free(buf);
10180 return 0;
10181 }
10182
10183 /* If "**" is by itself, this is the first time we encounter it and more
10184 * is following then find matches without any directory. */
10185 if (!didstar && stardepth < 100 && starstar && e - s == 2
10186 && *path_end == '/')
10187 {
10188 STRCPY(s, path_end + 1);
10189 ++stardepth;
10190 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10191 --stardepth;
10192 }
10193
10194 /* open the directory for scanning */
10195 *s = NUL;
10196 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10197
10198 /* Find all matching entries */
10199 if (dirp != NULL)
10200 {
10201 for (;;)
10202 {
10203 dp = readdir(dirp);
10204 if (dp == NULL)
10205 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010206 if ((dp->d_name[0] != '.' || starts_with_dot
10207 || ((flags & EW_DODOT)
10208 && dp->d_name[1] != NUL
10209 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010210 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10211 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010212 || ((flags & EW_NOTWILD)
10213 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010214 {
10215 STRCPY(s, dp->d_name);
10216 len = STRLEN(buf);
10217
10218 if (starstar && stardepth < 100)
10219 {
10220 /* For "**" in the pattern first go deeper in the tree to
10221 * find matches. */
10222 STRCPY(buf + len, "/**");
10223 STRCPY(buf + len + 3, path_end);
10224 ++stardepth;
10225 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10226 --stardepth;
10227 }
10228
10229 STRCPY(buf + len, path_end);
10230 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10231 {
10232 /* need to expand another component of the path */
10233 /* remove backslashes for the remaining components only */
10234 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10235 }
10236 else
10237 {
Bram Moolenaar8767f522016-07-01 17:17:39 +020010238 stat_T sb;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010239
Bram Moolenaar231334e2005-07-25 20:46:57 +000010240 /* no more wildcards, check if there is a match */
10241 /* remove backslashes for the remaining components only */
10242 if (*path_end != NUL)
10243 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010244 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010245 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010246 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010247 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010248#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010249 size_t precomp_len = STRLEN(buf)+1;
10250 char_u *precomp_buf =
10251 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010252
Bram Moolenaar231334e2005-07-25 20:46:57 +000010253 if (precomp_buf)
10254 {
10255 mch_memmove(buf, precomp_buf, precomp_len);
10256 vim_free(precomp_buf);
10257 }
10258#endif
10259 addfile(gap, buf, flags);
10260 }
10261 }
10262 }
10263 }
10264
10265 closedir(dirp);
10266 }
10267
10268 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010269 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010270
10271 matches = gap->ga_len - start_len;
10272 if (matches > 0)
10273 qsort(((char_u **)gap->ga_data) + start_len, matches,
10274 sizeof(char_u *), pstrcmp);
10275 return matches;
10276}
10277#endif
10278
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010279#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010280static int find_previous_pathsep(char_u *path, char_u **psep);
10281static int is_unique(char_u *maybe_unique, garray_T *gap, int i);
10282static void expand_path_option(char_u *curdir, garray_T *gap);
10283static char_u *get_path_cutoff(char_u *fname, garray_T *gap);
10284static void uniquefy_paths(garray_T *gap, char_u *pattern);
10285static int expand_in_path(garray_T *gap, char_u *pattern, int flags);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010286
10287/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010288 * Moves "*psep" back to the previous path separator in "path".
10289 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010290 */
10291 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010292find_previous_pathsep(char_u *path, char_u **psep)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010293{
10294 /* skip the current separator */
10295 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010296 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010297
10298 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010299 while (*psep > path)
10300 {
10301 if (vim_ispathsep(**psep))
10302 return OK;
10303 mb_ptr_back(path, *psep);
10304 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010305
10306 return FAIL;
10307}
10308
10309/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010310 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10311 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010312 */
10313 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010314is_unique(char_u *maybe_unique, garray_T *gap, int i)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010315{
10316 int j;
10317 int candidate_len;
10318 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010319 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010320 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010321
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010322 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010323 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010324 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010325 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010326
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010327 candidate_len = (int)STRLEN(maybe_unique);
10328 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010329 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010330 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010331
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010332 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010333 if (fnamecmp(maybe_unique, rival) == 0
10334 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010335 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010336 }
10337
Bram Moolenaar162bd912010-07-28 22:29:10 +020010338 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010339}
10340
10341/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010342 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010343 * paths are expanded to their equivalent fullpath. This includes the "."
10344 * (relative to current buffer directory) and empty path (relative to current
10345 * directory) notations.
10346 *
10347 * TODO: handle upward search (;) and path limiter (**N) notations by
10348 * expanding each into their equivalent path(s).
10349 */
10350 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010351expand_path_option(char_u *curdir, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010352{
10353 char_u *path_option = *curbuf->b_p_path == NUL
10354 ? p_path : curbuf->b_p_path;
10355 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010356 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010357 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010358
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010359 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010360 return;
10361
10362 while (*path_option != NUL)
10363 {
10364 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10365
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010366 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010367 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010368 /* Relative to current buffer:
10369 * "/path/file" + "." -> "/path/"
10370 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010371 if (curbuf->b_ffname == NULL)
10372 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010373 p = gettail(curbuf->b_ffname);
10374 len = (int)(p - curbuf->b_ffname);
10375 if (len + (int)STRLEN(buf) >= MAXPATHL)
10376 continue;
10377 if (buf[1] == NUL)
10378 buf[len] = NUL;
10379 else
10380 STRMOVE(buf + len, buf + 2);
10381 mch_memmove(buf, curbuf->b_ffname, len);
10382 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010383 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010384 else if (buf[0] == NUL)
10385 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010386 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010387 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010388 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010389 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010390 else if (!mch_isFullName(buf))
10391 {
10392 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010393 len = (int)STRLEN(curdir);
10394 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010395 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010396 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010397 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010398 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010399 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010400 }
10401
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010402 if (ga_grow(gap, 1) == FAIL)
10403 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010404
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010405# if defined(MSWIN)
Bram Moolenaar811fe632013-04-24 17:34:20 +020010406 /* Avoid the path ending in a backslash, it fails when a comma is
10407 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010408 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010409 if (buf[len - 1] == '\\')
10410 buf[len - 1] = '/';
10411# endif
10412
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010413 p = vim_strsave(buf);
10414 if (p == NULL)
10415 break;
10416 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010417 }
10418
10419 vim_free(buf);
10420}
10421
10422/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010423 * Returns a pointer to the file or directory name in "fname" that matches the
10424 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010425 *
10426 * path: /foo/bar/baz
10427 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010428 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010429 */
10430 static char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010010431get_path_cutoff(char_u *fname, garray_T *gap)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010432{
10433 int i;
10434 int maxlen = 0;
10435 char_u **path_part = (char_u **)gap->ga_data;
10436 char_u *cutoff = NULL;
10437
10438 for (i = 0; i < gap->ga_len; i++)
10439 {
10440 int j = 0;
10441
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010442 while ((fname[j] == path_part[i][j]
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010443# if defined(MSWIN)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010444 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10445#endif
10446 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010447 j++;
10448 if (j > maxlen)
10449 {
10450 maxlen = j;
10451 cutoff = &fname[j];
10452 }
10453 }
10454
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010455 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010456 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010457 while (vim_ispathsep(*cutoff))
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010458 mb_ptr_adv(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010459
10460 return cutoff;
10461}
10462
10463/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010464 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10465 * that they are unique with respect to each other while conserving the part
10466 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010467 */
10468 static void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010469uniquefy_paths(garray_T *gap, char_u *pattern)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010470{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010471 int i;
10472 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010473 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010474 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010475 char_u *pat;
10476 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010477 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010478 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010479 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010480 char_u **in_curdir = NULL;
10481 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010482
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010483 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010484 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010485
10486 /*
10487 * We need to prepend a '*' at the beginning of file_pattern so that the
10488 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010489 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010490 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010491 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010492 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010493 if (file_pattern == NULL)
10494 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010495 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010496 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010497 STRCAT(file_pattern, pattern);
10498 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10499 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010500 if (pat == NULL)
10501 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010502
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010503 regmatch.rm_ic = TRUE; /* always ignore case */
10504 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10505 vim_free(pat);
10506 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010507 return;
10508
Bram Moolenaar162bd912010-07-28 22:29:10 +020010509 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010510 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010511 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010512 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010513
10514 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010515 if (in_curdir == NULL)
10516 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010517
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010518 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010519 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010520 char_u *path = fnames[i];
10521 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010522 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010523 char_u *pathsep_p;
10524 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010525
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010526 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010527 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010528 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010529 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010530 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010531
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010532 /* Shorten the filename while maintaining its uniqueness */
10533 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010534
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010535 /* Don't assume all files can be reached without path when search
10536 * pattern starts with star star slash, so only remove path_cutoff
10537 * when possible. */
10538 if (pattern[0] == '*' && pattern[1] == '*'
10539 && vim_ispathsep_nocolon(pattern[2])
10540 && path_cutoff != NULL
10541 && vim_regexec(&regmatch, path_cutoff, (colnr_T)0)
10542 && is_unique(path_cutoff, gap, i))
10543 {
10544 sort_again = TRUE;
10545 mch_memmove(path, path_cutoff, STRLEN(path_cutoff) + 1);
10546 }
10547 else
10548 {
10549 /* Here all files can be reached without path, so get shortest
10550 * unique path. We start at the end of the path. */
10551 pathsep_p = path + len - 1;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010552
Bram Moolenaar73d4e4c2016-08-27 21:55:13 +020010553 while (find_previous_pathsep(path, &pathsep_p))
10554 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10555 && is_unique(pathsep_p + 1, gap, i)
10556 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10557 {
10558 sort_again = TRUE;
10559 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10560 break;
10561 }
10562 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010563
10564 if (mch_isFullName(path))
10565 {
10566 /*
10567 * Last resort: shorten relative to curdir if possible.
10568 * 'possible' means:
10569 * 1. It is under the current directory.
10570 * 2. The result is actually shorter than the original.
10571 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010572 * Before curdir After
10573 * /foo/bar/file.txt /foo/bar ./file.txt
10574 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10575 * /file.txt / /file.txt
10576 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010577 */
10578 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010579 if (short_name != NULL && short_name > path + 1
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010580#if defined(MSWIN)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010581 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010582 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010583 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010584 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010585 && !vim_ispathsep(*short_name)
10586#endif
10587 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010588 {
10589 STRCPY(path, ".");
10590 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010591 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010592 }
10593 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010594 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010595 }
10596
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010597 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010598 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010599 {
10600 char_u *rel_path;
10601 char_u *path = in_curdir[i];
10602
10603 if (path == NULL)
10604 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010605
10606 /* If the {filename} is not unique, change it to ./{filename}.
10607 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010608 short_name = shorten_fname(path, curdir);
10609 if (short_name == NULL)
10610 short_name = path;
10611 if (is_unique(short_name, gap, i))
10612 {
10613 STRCPY(fnames[i], short_name);
10614 continue;
10615 }
10616
10617 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10618 if (rel_path == NULL)
10619 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010620 STRCPY(rel_path, ".");
10621 add_pathsep(rel_path);
10622 STRCAT(rel_path, short_name);
10623
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010624 vim_free(fnames[i]);
10625 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010626 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010627 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010628 }
10629
Bram Moolenaar162bd912010-07-28 22:29:10 +020010630theend:
10631 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010632 if (in_curdir != NULL)
10633 {
10634 for (i = 0; i < gap->ga_len; i++)
10635 vim_free(in_curdir[i]);
10636 vim_free(in_curdir);
10637 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010638 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010639 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010640
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010641 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010642 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010643}
10644
10645/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010646 * Calls globpath() with 'path' values for the given pattern and stores the
10647 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010648 * Returns the total number of matches.
10649 */
10650 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010651expand_in_path(
10652 garray_T *gap,
10653 char_u *pattern,
10654 int flags) /* EW_* flags */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010655{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010656 char_u *curdir;
10657 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010658 char_u *paths = NULL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010659
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010660 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010661 return 0;
10662 mch_dirname(curdir, MAXPATHL);
10663
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010664 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010665 expand_path_option(curdir, &path_ga);
10666 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010667 if (path_ga.ga_len == 0)
10668 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010669
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010670 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010671 ga_clear_strings(&path_ga);
10672 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010673 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010674
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010675 globpath(paths, pattern, gap, (flags & EW_ICASE) ? WILD_ICASE : 0);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010676 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010677
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010678 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010679}
10680#endif
10681
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010682#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10683/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010684 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10685 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010686 */
10687 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010010688remove_duplicates(garray_T *gap)
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010689{
10690 int i;
10691 int j;
10692 char_u **fnames = (char_u **)gap->ga_data;
10693
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010694 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010695 for (i = gap->ga_len - 1; i > 0; --i)
10696 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10697 {
10698 vim_free(fnames[i]);
10699 for (j = i + 1; j < gap->ga_len; ++j)
10700 fnames[j - 1] = fnames[j];
10701 --gap->ga_len;
10702 }
10703}
10704#endif
10705
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010706static int has_env_var(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010707
10708/*
10709 * Return TRUE if "p" contains what looks like an environment variable.
10710 * Allowing for escaping.
10711 */
10712 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010713has_env_var(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010714{
10715 for ( ; *p; mb_ptr_adv(p))
10716 {
10717 if (*p == '\\' && p[1] != NUL)
10718 ++p;
10719 else if (vim_strchr((char_u *)
Bram Moolenaar48e330a2016-02-23 14:53:34 +010010720#if defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010721 "$%"
10722#else
10723 "$"
10724#endif
10725 , *p) != NULL)
10726 return TRUE;
10727 }
10728 return FALSE;
10729}
10730
10731#ifdef SPECIAL_WILDCHAR
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010732static int has_special_wildchar(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010733
10734/*
10735 * Return TRUE if "p" contains a special wildcard character.
10736 * Allowing for escaping.
10737 */
10738 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010739has_special_wildchar(char_u *p)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010740{
10741 for ( ; *p; mb_ptr_adv(p))
10742 {
10743 if (*p == '\\' && p[1] != NUL)
10744 ++p;
10745 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10746 return TRUE;
10747 }
10748 return FALSE;
10749}
10750#endif
10751
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752/*
10753 * Generic wildcard expansion code.
10754 *
10755 * Characters in "pat" that should not be expanded must be preceded with a
10756 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10757 *
10758 * Return FAIL when no single file was found. In this case "num_file" is not
10759 * set, and "file" may contain an error message.
10760 * Return OK when some files found. "num_file" is set to the number of
10761 * matches, "file" to the array of matches. Call FreeWild() later.
10762 */
10763 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010764gen_expand_wildcards(
10765 int num_pat, /* number of input patterns */
10766 char_u **pat, /* array of input patterns */
10767 int *num_file, /* resulting number of files */
10768 char_u ***file, /* array of resulting files */
10769 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770{
10771 int i;
10772 garray_T ga;
10773 char_u *p;
10774 static int recursive = FALSE;
10775 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020010776 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010777#if defined(FEAT_SEARCHPATH)
10778 int did_expand_in_path = FALSE;
10779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780
10781 /*
10782 * expand_env() is called to expand things like "~user". If this fails,
10783 * it calls ExpandOne(), which brings us back here. In this case, always
10784 * call the machine specific expansion function, if possible. Otherwise,
10785 * return FAIL.
10786 */
10787 if (recursive)
10788#ifdef SPECIAL_WILDCHAR
10789 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10790#else
10791 return FAIL;
10792#endif
10793
10794#ifdef SPECIAL_WILDCHAR
10795 /*
10796 * If there are any special wildcard characters which we cannot handle
10797 * here, call machine specific function for all the expansion. This
10798 * avoids starting the shell for each argument separately.
10799 * For `=expr` do use the internal function.
10800 */
10801 for (i = 0; i < num_pat; i++)
10802 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010803 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804# ifdef VIM_BACKTICK
10805 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10806# endif
10807 )
10808 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10809 }
10810#endif
10811
10812 recursive = TRUE;
10813
10814 /*
10815 * The matching file names are stored in a growarray. Init it empty.
10816 */
10817 ga_init2(&ga, (int)sizeof(char_u *), 30);
10818
10819 for (i = 0; i < num_pat; ++i)
10820 {
10821 add_pat = -1;
10822 p = pat[i];
10823
10824#ifdef VIM_BACKTICK
10825 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020010826 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020010828 if (add_pat == -1)
10829 retval = FAIL;
10830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831 else
10832#endif
10833 {
10834 /*
10835 * First expand environment variables, "~/" and "~user/".
10836 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010837 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010839 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840 if (p == NULL)
10841 p = pat[i];
10842#ifdef UNIX
10843 /*
10844 * On Unix, if expand_env() can't expand an environment
10845 * variable, use the shell to do that. Discard previously
10846 * found file names and start all over again.
10847 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010848 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849 {
10850 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010851 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020010853 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 recursive = FALSE;
10855 return i;
10856 }
10857#endif
10858 }
10859
10860 /*
10861 * If there are wildcards: Expand file names and add each match to
10862 * the list. If there is no match, and EW_NOTFOUND is given, add
10863 * the pattern.
10864 * If there are no wildcards: Add the file name if it exists or
10865 * when EW_NOTFOUND is given.
10866 */
10867 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010868 {
10869#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010870 if ((flags & EW_PATH)
10871 && !mch_isFullName(p)
10872 && !(p[0] == '.'
10873 && (vim_ispathsep(p[1])
10874 || (p[1] == '.' && vim_ispathsep(p[2]))))
10875 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010876 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010877 /* :find completion where 'path' is used.
10878 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010879 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010880 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010881 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010882 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010883 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010884 else
10885#endif
10886 add_pat = mch_expandpath(&ga, p, flags);
10887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 }
10889
10890 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
10891 {
10892 char_u *t = backslash_halve_save(p);
10893
10894#if defined(MACOS_CLASSIC)
10895 slash_to_colon(t);
10896#endif
10897 /* When EW_NOTFOUND is used, always add files and dirs. Makes
10898 * "vim c:/" work. */
10899 if (flags & EW_NOTFOUND)
10900 addfile(&ga, t, flags | EW_DIR | EW_FILE);
Bram Moolenaar00efded2016-07-07 14:29:10 +020010901 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 addfile(&ga, t, flags);
10903 vim_free(t);
10904 }
10905
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010906#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010907 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010908 uniquefy_paths(&ga, p);
10909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910 if (p != pat[i])
10911 vim_free(p);
10912 }
10913
10914 *num_file = ga.ga_len;
10915 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
10916
10917 recursive = FALSE;
10918
Bram Moolenaar336bd622016-01-17 18:23:58 +010010919 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920}
10921
10922# ifdef VIM_BACKTICK
10923
10924/*
10925 * Return TRUE if we can expand this backtick thing here.
10926 */
10927 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010928vim_backtick(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929{
10930 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
10931}
10932
10933/*
10934 * Expand an item in `backticks` by executing it as a command.
10935 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020010936 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937 */
10938 static int
Bram Moolenaar9b578142016-01-30 19:39:49 +010010939expand_backtick(
10940 garray_T *gap,
10941 char_u *pat,
10942 int flags) /* EW_* flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943{
10944 char_u *p;
10945 char_u *cmd;
10946 char_u *buffer;
10947 int cnt = 0;
10948 int i;
10949
10950 /* Create the command: lop off the backticks. */
10951 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
10952 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020010953 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954
10955#ifdef FEAT_EVAL
10956 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000010957 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958 else
10959#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010960 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010961 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 vim_free(cmd);
10963 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020010964 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965
10966 cmd = buffer;
10967 while (*cmd != NUL)
10968 {
10969 cmd = skipwhite(cmd); /* skip over white space */
10970 p = cmd;
10971 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
10972 ++p;
10973 /* add an entry if it is not empty */
10974 if (p > cmd)
10975 {
10976 i = *p;
10977 *p = NUL;
10978 addfile(gap, cmd, flags);
10979 *p = i;
10980 ++cnt;
10981 }
10982 cmd = p;
10983 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
10984 ++cmd;
10985 }
10986
10987 vim_free(buffer);
10988 return cnt;
10989}
10990# endif /* VIM_BACKTICK */
10991
10992/*
10993 * Add a file to a file list. Accepted flags:
10994 * EW_DIR add directories
10995 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010996 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997 * EW_NOTFOUND add even when it doesn't exist
10998 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010999 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000 */
11001 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011002addfile(
11003 garray_T *gap,
11004 char_u *f, /* filename */
11005 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006{
11007 char_u *p;
11008 int isdir;
Bram Moolenaar8767f522016-07-01 17:17:39 +020011009 stat_T sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011011 /* if the file/dir/link doesn't exist, may not add it */
11012 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011013 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014 return;
11015
11016#ifdef FNAME_ILLEGAL
11017 /* if the file/dir contains illegal characters, don't add it */
11018 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11019 return;
11020#endif
11021
11022 isdir = mch_isdir(f);
11023 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11024 return;
11025
Bram Moolenaarb5971142015-03-21 17:32:19 +010011026 /* If the file isn't executable, may not add it. Do accept directories.
11027 * When invoked from expand_shellcmd() do not use $PATH. */
11028 if (!isdir && (flags & EW_EXEC)
11029 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011030 return;
11031
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 /* Make room for another item in the file list. */
11033 if (ga_grow(gap, 1) == FAIL)
11034 return;
11035
11036 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11037 if (p == NULL)
11038 return;
11039
11040 STRCPY(p, f);
11041#ifdef BACKSLASH_IN_FILENAME
11042 slash_adjust(p);
11043#endif
11044 /*
11045 * Append a slash or backslash after directory names if none is present.
11046 */
11047#ifndef DONT_ADD_PATHSEP_TO_DIR
11048 if (isdir && (flags & EW_ADDSLASH))
11049 add_pathsep(p);
11050#endif
11051 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052}
11053#endif /* !NO_EXPANDPATH */
11054
11055#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11056
11057#ifndef SEEK_SET
11058# define SEEK_SET 0
11059#endif
11060#ifndef SEEK_END
11061# define SEEK_END 2
11062#endif
11063
11064/*
11065 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011066 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11067 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068 * Returns an allocated string, or NULL for error.
11069 */
11070 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011071get_cmd_output(
11072 char_u *cmd,
11073 char_u *infile, /* optional input file name */
11074 int flags, /* can be SHELL_SILENT */
11075 int *ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076{
11077 char_u *tempname;
11078 char_u *command;
11079 char_u *buffer = NULL;
11080 int len;
11081 int i = 0;
11082 FILE *fd;
11083
11084 if (check_restricted() || check_secure())
11085 return NULL;
11086
11087 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011088 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089 {
11090 EMSG(_(e_notmp));
11091 return NULL;
11092 }
11093
11094 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011095 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096 if (command == NULL)
11097 goto done;
11098
11099 /*
11100 * Call the shell to execute the command (errors are ignored).
11101 * Don't check timestamps here.
11102 */
11103 ++no_check_timestamps;
11104 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11105 --no_check_timestamps;
11106
11107 vim_free(command);
11108
11109 /*
11110 * read the names from the file into memory
11111 */
11112# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011113 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114 fd = mch_fopen((char *)tempname, "r");
11115# else
11116 fd = mch_fopen((char *)tempname, READBIN);
11117# endif
11118
11119 if (fd == NULL)
11120 {
11121 EMSG2(_(e_notopen), tempname);
11122 goto done;
11123 }
11124
11125 fseek(fd, 0L, SEEK_END);
11126 len = ftell(fd); /* get size of temp file */
11127 fseek(fd, 0L, SEEK_SET);
11128
11129 buffer = alloc(len + 1);
11130 if (buffer != NULL)
11131 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11132 fclose(fd);
11133 mch_remove(tempname);
11134 if (buffer == NULL)
11135 goto done;
11136#ifdef VMS
11137 len = i; /* VMS doesn't give us what we asked for... */
11138#endif
11139 if (i != len)
11140 {
11141 EMSG2(_(e_notread), tempname);
11142 vim_free(buffer);
11143 buffer = NULL;
11144 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011145 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011146 {
11147 /* Change NUL into SOH, otherwise the string is truncated. */
11148 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011149 if (buffer[i] == NUL)
11150 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011151
Bram Moolenaar162bd912010-07-28 22:29:10 +020011152 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011153 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011154 else
11155 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156
11157done:
11158 vim_free(tempname);
11159 return buffer;
11160}
11161#endif
11162
11163/*
11164 * Free the list of files returned by expand_wildcards() or other expansion
11165 * functions.
11166 */
11167 void
Bram Moolenaar9b578142016-01-30 19:39:49 +010011168FreeWild(int count, char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011170 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011171 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172 while (count--)
11173 vim_free(files[count]);
11174 vim_free(files);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175}
11176
11177/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011178 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179 * Don't do this when still processing a command or a mapping.
11180 * Don't do this when inside a ":normal" command.
11181 */
11182 int
Bram Moolenaar9b578142016-01-30 19:39:49 +010011183goto_im(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184{
11185 return (p_im && stuff_empty() && typebuf_typed());
11186}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011187
11188/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011189 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011190 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11191 * - Remove any argument. E.g., "csh -f" -> "csh".
11192 * But don't allow a space in the path, so that this works:
11193 * "/usr/bin/csh --rcfile ~/.cshrc"
11194 * But don't do that for Windows, it's common to have a space in the path.
11195 */
11196 char_u *
Bram Moolenaar9b578142016-01-30 19:39:49 +010011197get_isolated_shell_name(void)
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011198{
11199 char_u *p;
11200
11201#ifdef WIN3264
11202 p = gettail(p_sh);
11203 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11204#else
11205 p = skiptowhite(p_sh);
11206 if (*p == NUL)
11207 {
11208 /* No white space, use the tail. */
11209 p = vim_strsave(gettail(p_sh));
11210 }
11211 else
11212 {
11213 char_u *p1, *p2;
11214
11215 /* Find the last path separator before the space. */
11216 p1 = p_sh;
11217 for (p2 = p_sh; p2 < p; mb_ptr_adv(p2))
11218 if (vim_ispathsep(*p2))
11219 p1 = p2 + 1;
11220 p = vim_strnsave(p1, (int)(p - p1));
11221 }
11222#endif
11223 return p;
11224}