blob: ec7792e7381af36c76c664516018ebb154b0e456 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * misc1.c: functions that didn't seem to fit elsewhere
12 */
13
14#include "vim.h"
15#include "version.h"
16
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010017static char_u *vim_version_dir(char_u *vimdir);
18static char_u *remove_tail(char_u *p, char_u *pend, char_u *name);
Bram Moolenaar06ae70d2013-06-17 19:26:36 +020019#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010020static void init_users(void);
Bram Moolenaar06ae70d2013-06-17 19:26:36 +020021#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010022static int copy_indent(int size, char_u *src);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023
Bram Moolenaar24305862012-08-15 14:05:05 +020024/* All user names (for ~user completion as done by shell). */
25#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
26static garray_T ga_users;
27#endif
28
Bram Moolenaar071d4272004-06-13 20:20:40 +000029/*
30 * Count the size (in window cells) of the indent in the current line.
31 */
32 int
33get_indent()
34{
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
42get_indent_lnum(lnum)
43 linenr_T lnum;
44{
Bram Moolenaar597a4222014-06-25 14:39:50 +020045 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046}
47
48#if defined(FEAT_FOLDING) || defined(PROTO)
49/*
50 * Count the size (in window cells) of the indent in line "lnum" of buffer
51 * "buf".
52 */
53 int
54get_indent_buf(buf, lnum)
55 buf_T *buf;
56 linenr_T lnum;
57{
Bram Moolenaar597a4222014-06-25 14:39:50 +020058 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000059}
60#endif
61
62/*
63 * count the size (in window cells) of the indent in line "ptr", with
64 * 'tabstop' at "ts"
65 */
Bram Moolenaar4399ef42005-02-12 14:29:27 +000066 int
Bram Moolenaar597a4222014-06-25 14:39:50 +020067get_indent_str(ptr, ts, list)
Bram Moolenaar071d4272004-06-13 20:20:40 +000068 char_u *ptr;
69 int ts;
Bram Moolenaar597a4222014-06-25 14:39:50 +020070 int list; /* if TRUE, count only screen size for tabs */
Bram Moolenaar071d4272004-06-13 20:20:40 +000071{
72 int count = 0;
73
74 for ( ; *ptr; ++ptr)
75 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020076 if (*ptr == TAB)
77 {
78 if (!list || lcs_tab1) /* count a tab for what it is worth */
79 count += ts - (count % ts);
80 else
Bram Moolenaare4df1642014-08-29 12:58:44 +020081 /* In list mode, when tab is not set, count screen char width
82 * for Tab, displays: ^I */
Bram Moolenaar597a4222014-06-25 14:39:50 +020083 count += ptr2cells(ptr);
84 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 else if (*ptr == ' ')
86 ++count; /* count a space for one */
87 else
88 break;
89 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +000090 return count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000091}
92
93/*
94 * Set the indent of the current line.
95 * Leaves the cursor on the first non-blank in the line.
96 * Caller must take care of undo.
97 * "flags":
98 * SIN_CHANGED: call changed_bytes() if the line was changed.
99 * SIN_INSERT: insert the indent in front of the line.
100 * SIN_UNDO: save line for undo before changing it.
101 * Returns TRUE if the line was changed.
102 */
103 int
104set_indent(size, flags)
Bram Moolenaar5002c292007-07-24 13:26:15 +0000105 int size; /* measured in spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106 int flags;
107{
108 char_u *p;
109 char_u *newline;
110 char_u *oldline;
111 char_u *s;
112 int todo;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000113 int ind_len; /* measured in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 int line_len;
115 int doit = FALSE;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000116 int ind_done = 0; /* measured in spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117 int tab_pad;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000118 int retval = FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000119 int orig_char_len = -1; /* number of initial whitespace chars when
Bram Moolenaar5002c292007-07-24 13:26:15 +0000120 'et' and 'pi' are both set */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122 /*
123 * First check if there is anything to do and compute the number of
124 * characters needed for the indent.
125 */
126 todo = size;
127 ind_len = 0;
128 p = oldline = ml_get_curline();
129
130 /* Calculate the buffer size for the new indent, and check to see if it
131 * isn't already set */
132
Bram Moolenaar5002c292007-07-24 13:26:15 +0000133 /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and
134 * 'preserveindent' are set count the number of characters at the
135 * beginning of the line to be copied */
136 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137 {
138 /* If 'preserveindent' is set then reuse as much as possible of
139 * the existing indent structure for the new indent */
140 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
141 {
142 ind_done = 0;
143
144 /* count as many characters as we can use */
145 while (todo > 0 && vim_iswhite(*p))
146 {
147 if (*p == TAB)
148 {
149 tab_pad = (int)curbuf->b_p_ts
150 - (ind_done % (int)curbuf->b_p_ts);
151 /* stop if this tab will overshoot the target */
152 if (todo < tab_pad)
153 break;
154 todo -= tab_pad;
155 ++ind_len;
156 ind_done += tab_pad;
157 }
158 else
159 {
160 --todo;
161 ++ind_len;
162 ++ind_done;
163 }
164 ++p;
165 }
166
Bram Moolenaar5002c292007-07-24 13:26:15 +0000167 /* Set initial number of whitespace chars to copy if we are
168 * preserving indent but expandtab is set */
169 if (curbuf->b_p_et)
170 orig_char_len = ind_len;
171
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 /* Fill to next tabstop with a tab, if possible */
173 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000174 if (todo >= tab_pad && orig_char_len == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 {
176 doit = TRUE;
177 todo -= tab_pad;
178 ++ind_len;
179 /* ind_done += tab_pad; */
180 }
181 }
182
183 /* count tabs required for indent */
184 while (todo >= (int)curbuf->b_p_ts)
185 {
186 if (*p != TAB)
187 doit = TRUE;
188 else
189 ++p;
190 todo -= (int)curbuf->b_p_ts;
191 ++ind_len;
192 /* ind_done += (int)curbuf->b_p_ts; */
193 }
194 }
195 /* count spaces required for indent */
196 while (todo > 0)
197 {
198 if (*p != ' ')
199 doit = TRUE;
200 else
201 ++p;
202 --todo;
203 ++ind_len;
204 /* ++ind_done; */
205 }
206
207 /* Return if the indent is OK already. */
208 if (!doit && !vim_iswhite(*p) && !(flags & SIN_INSERT))
209 return FALSE;
210
211 /* Allocate memory for the new line. */
212 if (flags & SIN_INSERT)
213 p = oldline;
214 else
215 p = skipwhite(p);
216 line_len = (int)STRLEN(p) + 1;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000217
218 /* If 'preserveindent' and 'expandtab' are both set keep the original
219 * characters and allocate accordingly. We will fill the rest with spaces
220 * after the if (!curbuf->b_p_et) below. */
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000221 if (orig_char_len != -1)
Bram Moolenaar5002c292007-07-24 13:26:15 +0000222 {
223 newline = alloc(orig_char_len + size - ind_done + line_len);
224 if (newline == NULL)
225 return FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000226 todo = size - ind_done;
227 ind_len = orig_char_len + todo; /* Set total length of indent in
228 * characters, which may have been
229 * undercounted until now */
Bram Moolenaar5002c292007-07-24 13:26:15 +0000230 p = oldline;
231 s = newline;
232 while (orig_char_len > 0)
233 {
234 *s++ = *p++;
235 orig_char_len--;
236 }
Bram Moolenaar913626c2008-01-03 11:43:42 +0000237
Bram Moolenaar5002c292007-07-24 13:26:15 +0000238 /* Skip over any additional white space (useful when newindent is less
239 * than old) */
240 while (vim_iswhite(*p))
Bram Moolenaar913626c2008-01-03 11:43:42 +0000241 ++p;
Bram Moolenaarcc00b952007-08-11 12:32:57 +0000242
Bram Moolenaar5002c292007-07-24 13:26:15 +0000243 }
244 else
245 {
246 todo = size;
247 newline = alloc(ind_len + line_len);
248 if (newline == NULL)
249 return FALSE;
250 s = newline;
251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252
253 /* Put the characters in the new line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 /* if 'expandtab' isn't set: use TABs */
255 if (!curbuf->b_p_et)
256 {
257 /* If 'preserveindent' is set then reuse as much as possible of
258 * the existing indent structure for the new indent */
259 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
260 {
261 p = oldline;
262 ind_done = 0;
263
264 while (todo > 0 && vim_iswhite(*p))
265 {
266 if (*p == TAB)
267 {
268 tab_pad = (int)curbuf->b_p_ts
269 - (ind_done % (int)curbuf->b_p_ts);
270 /* stop if this tab will overshoot the target */
271 if (todo < tab_pad)
272 break;
273 todo -= tab_pad;
274 ind_done += tab_pad;
275 }
276 else
277 {
278 --todo;
279 ++ind_done;
280 }
281 *s++ = *p++;
282 }
283
284 /* Fill to next tabstop with a tab, if possible */
285 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
286 if (todo >= tab_pad)
287 {
288 *s++ = TAB;
289 todo -= tab_pad;
290 }
291
292 p = skipwhite(p);
293 }
294
295 while (todo >= (int)curbuf->b_p_ts)
296 {
297 *s++ = TAB;
298 todo -= (int)curbuf->b_p_ts;
299 }
300 }
301 while (todo > 0)
302 {
303 *s++ = ' ';
304 --todo;
305 }
306 mch_memmove(s, p, (size_t)line_len);
307
308 /* Replace the line (unless undo fails). */
309 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
310 {
311 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
312 if (flags & SIN_CHANGED)
313 changed_bytes(curwin->w_cursor.lnum, 0);
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200314 /* Correct saved cursor position if it is in this line. */
315 if (saved_cursor.lnum == curwin->w_cursor.lnum)
316 {
317 if (saved_cursor.col >= (colnr_T)(p - oldline))
318 /* cursor was after the indent, adjust for the number of
319 * bytes added/removed */
320 saved_cursor.col += ind_len - (colnr_T)(p - oldline);
321 else if (saved_cursor.col >= (colnr_T)(s - newline))
322 /* cursor was in the indent, and is now after it, put it back
323 * at the start of the indent (replacing spaces with TAB) */
324 saved_cursor.col = (colnr_T)(s - newline);
325 }
Bram Moolenaar5409c052005-03-18 20:27:04 +0000326 retval = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 }
328 else
329 vim_free(newline);
330
331 curwin->w_cursor.col = ind_len;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000332 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333}
334
335/*
336 * Copy the indent from ptr to the current line (and fill to size)
337 * Leaves the cursor on the first non-blank in the line.
338 * Returns TRUE if the line was changed.
339 */
340 static int
341copy_indent(size, src)
342 int size;
343 char_u *src;
344{
345 char_u *p = NULL;
346 char_u *line = NULL;
347 char_u *s;
348 int todo;
349 int ind_len;
350 int line_len = 0;
351 int tab_pad;
352 int ind_done;
353 int round;
354
355 /* Round 1: compute the number of characters needed for the indent
356 * Round 2: copy the characters. */
357 for (round = 1; round <= 2; ++round)
358 {
359 todo = size;
360 ind_len = 0;
361 ind_done = 0;
362 s = src;
363
364 /* Count/copy the usable portion of the source line */
365 while (todo > 0 && vim_iswhite(*s))
366 {
367 if (*s == TAB)
368 {
369 tab_pad = (int)curbuf->b_p_ts
370 - (ind_done % (int)curbuf->b_p_ts);
371 /* Stop if this tab will overshoot the target */
372 if (todo < tab_pad)
373 break;
374 todo -= tab_pad;
375 ind_done += tab_pad;
376 }
377 else
378 {
379 --todo;
380 ++ind_done;
381 }
382 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000383 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 *p++ = *s;
385 ++s;
386 }
387
388 /* Fill to next tabstop with a tab, if possible */
389 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200390 if (todo >= tab_pad && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 {
392 todo -= tab_pad;
393 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000394 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 *p++ = TAB;
396 }
397
398 /* Add tabs required for indent */
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200399 while (todo >= (int)curbuf->b_p_ts && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 {
401 todo -= (int)curbuf->b_p_ts;
402 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000403 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 *p++ = TAB;
405 }
406
407 /* Count/add spaces required for indent */
408 while (todo > 0)
409 {
410 --todo;
411 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000412 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 *p++ = ' ';
414 }
415
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000416 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 {
418 /* Allocate memory for the result: the copied indent, new indent
419 * and the rest of the line. */
420 line_len = (int)STRLEN(ml_get_curline()) + 1;
421 line = alloc(ind_len + line_len);
422 if (line == NULL)
423 return FALSE;
424 p = line;
425 }
426 }
427
428 /* Append the original line */
429 mch_memmove(p, ml_get_curline(), (size_t)line_len);
430
431 /* Replace the line */
432 ml_replace(curwin->w_cursor.lnum, line, FALSE);
433
434 /* Put the cursor after the indent. */
435 curwin->w_cursor.col = ind_len;
436 return TRUE;
437}
438
439/*
440 * Return the indent of the current line after a number. Return -1 if no
441 * number was found. Used for 'n' in 'formatoptions': numbered list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000442 * Since a pattern is used it can actually handle more than numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 */
444 int
445get_number_indent(lnum)
446 linenr_T lnum;
447{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 colnr_T col;
449 pos_T pos;
450
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200451 regmatch_T regmatch;
452 int lead_len = 0; /* length of comment leader */
453
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 if (lnum > curbuf->b_ml.ml_line_count)
455 return -1;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000456 pos.lnum = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200457
458#ifdef FEAT_COMMENTS
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200459 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
460 if ((State & INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200461 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000462#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200463 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
464 if (regmatch.regprog != NULL)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200465 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200466 regmatch.rm_ic = FALSE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200467
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200468 /* vim_regexec() expects a pointer to a line. This lets us
469 * start matching for the flp beyond any comment leader... */
470 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200471 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200472 pos.lnum = lnum;
473 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200474#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200475 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200476#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200477 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200478 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200479 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000480
481 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 getvcol(curwin, &pos, &col, NULL, NULL);
484 return (int)col;
485}
486
Bram Moolenaar597a4222014-06-25 14:39:50 +0200487#if defined(FEAT_LINEBREAK) || defined(PROTO)
488/*
489 * Return appropriate space number for breakindent, taking influencing
490 * parameters into account. Window must be specified, since it is not
491 * necessarily always the current one.
492 */
493 int
494get_breakindent_win(wp, line)
495 win_T *wp;
496 char_u *line; /* start of the line */
497{
498 static int prev_indent = 0; /* cached indent value */
499 static long prev_ts = 0L; /* cached tabstop value */
500 static char_u *prev_line = NULL; /* cached pointer to line */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200501 static int prev_tick = 0; /* changedtick of cached value */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200502 int bri = 0;
503 /* window width minus window margin space, i.e. what rests for text */
504 const int eff_wwidth = W_WIDTH(wp)
505 - ((wp->w_p_nu || wp->w_p_rnu)
506 && (vim_strchr(p_cpo, CPO_NUMCOL) == NULL)
507 ? number_width(wp) + 1 : 0);
508
509 /* used cached indent, unless pointer or 'tabstop' changed */
Bram Moolenaara40aa762014-06-25 22:55:38 +0200510 if (prev_line != line || prev_ts != wp->w_buffer->b_p_ts
511 || prev_tick != wp->w_buffer->b_changedtick)
Bram Moolenaar597a4222014-06-25 14:39:50 +0200512 {
513 prev_line = line;
514 prev_ts = wp->w_buffer->b_p_ts;
Bram Moolenaara40aa762014-06-25 22:55:38 +0200515 prev_tick = wp->w_buffer->b_changedtick;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200516 prev_indent = get_indent_str(line,
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200517 (int)wp->w_buffer->b_p_ts, wp->w_p_list);
Bram Moolenaar597a4222014-06-25 14:39:50 +0200518 }
Bram Moolenaar9d7a5922014-06-26 21:24:56 +0200519 bri = prev_indent + wp->w_p_brishift;
Bram Moolenaar597a4222014-06-25 14:39:50 +0200520
521 /* indent minus the length of the showbreak string */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200522 if (wp->w_p_brisbr)
523 bri -= vim_strsize(p_sbr);
524
525 /* Add offset for number column, if 'n' is in 'cpoptions' */
526 bri += win_col_off2(wp);
527
528 /* never indent past left window margin */
529 if (bri < 0)
530 bri = 0;
531 /* always leave at least bri_min characters on the left,
532 * if text width is sufficient */
533 else if (bri > eff_wwidth - wp->w_p_brimin)
534 bri = (eff_wwidth - wp->w_p_brimin < 0)
535 ? 0 : eff_wwidth - wp->w_p_brimin;
536
537 return bri;
538}
539#endif
540
541
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
543
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100544static int cin_is_cinword(char_u *line);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545
546/*
547 * Return TRUE if the string "line" starts with a word from 'cinwords'.
548 */
549 static int
550cin_is_cinword(line)
551 char_u *line;
552{
553 char_u *cinw;
554 char_u *cinw_buf;
555 int cinw_len;
556 int retval = FALSE;
557 int len;
558
559 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1;
560 cinw_buf = alloc((unsigned)cinw_len);
561 if (cinw_buf != NULL)
562 {
563 line = skipwhite(line);
564 for (cinw = curbuf->b_p_cinw; *cinw; )
565 {
566 len = copy_option_part(&cinw, cinw_buf, cinw_len, ",");
567 if (STRNCMP(line, cinw_buf, len) == 0
568 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1])))
569 {
570 retval = TRUE;
571 break;
572 }
573 }
574 vim_free(cinw_buf);
575 }
576 return retval;
577}
578#endif
579
580/*
581 * open_line: Add a new line below or above the current line.
582 *
583 * For VREPLACE mode, we only add a new line when we get to the end of the
584 * file, otherwise we just start replacing the next line.
585 *
586 * Caller must take care of undo. Since VREPLACE may affect any number of
587 * lines however, it may call u_save_cursor() again when starting to change a
588 * new line.
589 * "flags": OPENLINE_DELSPACES delete spaces after cursor
590 * OPENLINE_DO_COM format comments
591 * OPENLINE_KEEPTRAIL keep trailing spaces
592 * OPENLINE_MARKFIX adjust mark positions after the line break
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200593 * OPENLINE_COM_LIST format comments with list or 2nd line indent
594 *
595 * "second_line_indent": indent for after ^^D in Insert mode or if flag
596 * OPENLINE_COM_LIST
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 *
598 * Return TRUE for success, FALSE for failure
599 */
600 int
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200601open_line(dir, flags, second_line_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 int dir; /* FORWARD or BACKWARD */
603 int flags;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200604 int second_line_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605{
606 char_u *saved_line; /* copy of the original line */
607 char_u *next_line = NULL; /* copy of the next line */
608 char_u *p_extra = NULL; /* what goes to next line */
609 int less_cols = 0; /* less columns for mark in new line */
610 int less_cols_off = 0; /* columns to skip for mark adjust */
611 pos_T old_cursor; /* old cursor position */
612 int newcol = 0; /* new cursor column */
613 int newindent = 0; /* auto-indent of the new line */
614 int n;
615 int trunc_line = FALSE; /* truncate current line afterwards */
616 int retval = FALSE; /* return value, default is FAIL */
617#ifdef FEAT_COMMENTS
618 int extra_len = 0; /* length of p_extra string */
619 int lead_len; /* length of comment leader */
620 char_u *lead_flags; /* position in 'comments' for comment leader */
621 char_u *leader = NULL; /* copy of comment leader */
622#endif
623 char_u *allocated = NULL; /* allocated memory */
624#if defined(FEAT_SMARTINDENT) || defined(FEAT_VREPLACE) || defined(FEAT_LISP) \
625 || defined(FEAT_CINDENT) || defined(FEAT_COMMENTS)
626 char_u *p;
627#endif
628 int saved_char = NUL; /* init for GCC */
629#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
630 pos_T *pos;
631#endif
632#ifdef FEAT_SMARTINDENT
633 int do_si = (!p_paste && curbuf->b_p_si
634# ifdef FEAT_CINDENT
635 && !curbuf->b_p_cin
636# endif
637 );
638 int no_si = FALSE; /* reset did_si afterwards */
639 int first_char = NUL; /* init for GCC */
640#endif
641#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
642 int vreplace_mode;
643#endif
644 int did_append; /* appended a new line */
645 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */
646
647 /*
648 * make a copy of the current line so we can mess with it
649 */
650 saved_line = vim_strsave(ml_get_curline());
651 if (saved_line == NULL) /* out of memory! */
652 return FALSE;
653
654#ifdef FEAT_VREPLACE
655 if (State & VREPLACE_FLAG)
656 {
657 /*
658 * With VREPLACE we make a copy of the next line, which we will be
659 * starting to replace. First make the new line empty and let vim play
660 * with the indenting and comment leader to its heart's content. Then
661 * we grab what it ended up putting on the new line, put back the
662 * original line, and call ins_char() to put each new character onto
663 * the line, replacing what was there before and pushing the right
664 * stuff onto the replace stack. -- webb.
665 */
666 if (curwin->w_cursor.lnum < orig_line_count)
667 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
668 else
669 next_line = vim_strsave((char_u *)"");
670 if (next_line == NULL) /* out of memory! */
671 goto theend;
672
673 /*
674 * In VREPLACE mode, a NL replaces the rest of the line, and starts
675 * replacing the next line, so push all of the characters left on the
676 * line onto the replace stack. We'll push any other characters that
677 * might be replaced at the start of the next line (due to autoindent
678 * etc) a bit later.
679 */
680 replace_push(NUL); /* Call twice because BS over NL expects it */
681 replace_push(NUL);
682 p = saved_line + curwin->w_cursor.col;
683 while (*p != NUL)
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000684 {
685#ifdef FEAT_MBYTE
686 if (has_mbyte)
687 p += replace_push_mb(p);
688 else
689#endif
690 replace_push(*p++);
691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 saved_line[curwin->w_cursor.col] = NUL;
693 }
694#endif
695
696 if ((State & INSERT)
697#ifdef FEAT_VREPLACE
698 && !(State & VREPLACE_FLAG)
699#endif
700 )
701 {
702 p_extra = saved_line + curwin->w_cursor.col;
703#ifdef FEAT_SMARTINDENT
704 if (do_si) /* need first char after new line break */
705 {
706 p = skipwhite(p_extra);
707 first_char = *p;
708 }
709#endif
710#ifdef FEAT_COMMENTS
711 extra_len = (int)STRLEN(p_extra);
712#endif
713 saved_char = *p_extra;
714 *p_extra = NUL;
715 }
716
717 u_clearline(); /* cannot do "U" command when adding lines */
718#ifdef FEAT_SMARTINDENT
719 did_si = FALSE;
720#endif
721 ai_col = 0;
722
723 /*
724 * If we just did an auto-indent, then we didn't type anything on
725 * the prior line, and it should be truncated. Do this even if 'ai' is not
726 * set because automatically inserting a comment leader also sets did_ai.
727 */
728 if (dir == FORWARD && did_ai)
729 trunc_line = TRUE;
730
731 /*
732 * If 'autoindent' and/or 'smartindent' is set, try to figure out what
733 * indent to use for the new line.
734 */
735 if (curbuf->b_p_ai
736#ifdef FEAT_SMARTINDENT
737 || do_si
738#endif
739 )
740 {
741 /*
742 * count white space on current line
743 */
Bram Moolenaar597a4222014-06-25 14:39:50 +0200744 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200745 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
746 newindent = second_line_indent; /* for ^^D command in insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747
748#ifdef FEAT_SMARTINDENT
749 /*
750 * Do smart indenting.
751 * In insert/replace mode (only when dir == FORWARD)
752 * we may move some text to the next line. If it starts with '{'
753 * don't add an indent. Fixes inserting a NL before '{' in line
754 * "if (condition) {"
755 */
756 if (!trunc_line && do_si && *saved_line != NUL
757 && (p_extra == NULL || first_char != '{'))
758 {
759 char_u *ptr;
760 char_u last_char;
761
762 old_cursor = curwin->w_cursor;
763 ptr = saved_line;
764# ifdef FEAT_COMMENTS
765 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200766 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 else
768 lead_len = 0;
769# endif
770 if (dir == FORWARD)
771 {
772 /*
773 * Skip preprocessor directives, unless they are
774 * recognised as comments.
775 */
776 if (
777# ifdef FEAT_COMMENTS
778 lead_len == 0 &&
779# endif
780 ptr[0] == '#')
781 {
782 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
783 ptr = ml_get(--curwin->w_cursor.lnum);
784 newindent = get_indent();
785 }
786# ifdef FEAT_COMMENTS
787 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200788 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 else
790 lead_len = 0;
791 if (lead_len > 0)
792 {
793 /*
794 * This case gets the following right:
795 * \*
796 * * A comment (read '\' as '/').
797 * *\
798 * #define IN_THE_WAY
799 * This should line up here;
800 */
801 p = skipwhite(ptr);
802 if (p[0] == '/' && p[1] == '*')
803 p++;
804 if (p[0] == '*')
805 {
806 for (p++; *p; p++)
807 {
808 if (p[0] == '/' && p[-1] == '*')
809 {
810 /*
811 * End of C comment, indent should line up
812 * with the line containing the start of
813 * the comment
814 */
815 curwin->w_cursor.col = (colnr_T)(p - ptr);
816 if ((pos = findmatch(NULL, NUL)) != NULL)
817 {
818 curwin->w_cursor.lnum = pos->lnum;
819 newindent = get_indent();
820 }
821 }
822 }
823 }
824 }
825 else /* Not a comment line */
826# endif
827 {
828 /* Find last non-blank in line */
829 p = ptr + STRLEN(ptr) - 1;
830 while (p > ptr && vim_iswhite(*p))
831 --p;
832 last_char = *p;
833
834 /*
835 * find the character just before the '{' or ';'
836 */
837 if (last_char == '{' || last_char == ';')
838 {
839 if (p > ptr)
840 --p;
841 while (p > ptr && vim_iswhite(*p))
842 --p;
843 }
844 /*
845 * Try to catch lines that are split over multiple
846 * lines. eg:
847 * if (condition &&
848 * condition) {
849 * Should line up here!
850 * }
851 */
852 if (*p == ')')
853 {
854 curwin->w_cursor.col = (colnr_T)(p - ptr);
855 if ((pos = findmatch(NULL, '(')) != NULL)
856 {
857 curwin->w_cursor.lnum = pos->lnum;
858 newindent = get_indent();
859 ptr = ml_get_curline();
860 }
861 }
862 /*
863 * If last character is '{' do indent, without
864 * checking for "if" and the like.
865 */
866 if (last_char == '{')
867 {
868 did_si = TRUE; /* do indent */
869 no_si = TRUE; /* don't delete it when '{' typed */
870 }
871 /*
872 * Look for "if" and the like, use 'cinwords'.
873 * Don't do this if the previous line ended in ';' or
874 * '}'.
875 */
876 else if (last_char != ';' && last_char != '}'
877 && cin_is_cinword(ptr))
878 did_si = TRUE;
879 }
880 }
881 else /* dir == BACKWARD */
882 {
883 /*
884 * Skip preprocessor directives, unless they are
885 * recognised as comments.
886 */
887 if (
888# ifdef FEAT_COMMENTS
889 lead_len == 0 &&
890# endif
891 ptr[0] == '#')
892 {
893 int was_backslashed = FALSE;
894
895 while ((ptr[0] == '#' || was_backslashed) &&
896 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
897 {
898 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
899 was_backslashed = TRUE;
900 else
901 was_backslashed = FALSE;
902 ptr = ml_get(++curwin->w_cursor.lnum);
903 }
904 if (was_backslashed)
905 newindent = 0; /* Got to end of file */
906 else
907 newindent = get_indent();
908 }
909 p = skipwhite(ptr);
910 if (*p == '}') /* if line starts with '}': do indent */
911 did_si = TRUE;
912 else /* can delete indent when '{' typed */
913 can_si_back = TRUE;
914 }
915 curwin->w_cursor = old_cursor;
916 }
917 if (do_si)
918 can_si = TRUE;
919#endif /* FEAT_SMARTINDENT */
920
921 did_ai = TRUE;
922 }
923
924#ifdef FEAT_COMMENTS
925 /*
926 * Find out if the current line starts with a comment leader.
927 * This may then be inserted in front of the new line.
928 */
929 end_comment_pending = NUL;
930 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200931 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 else
933 lead_len = 0;
934 if (lead_len > 0)
935 {
936 char_u *lead_repl = NULL; /* replaces comment leader */
937 int lead_repl_len = 0; /* length of *lead_repl */
938 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
939 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
940 char_u *comment_end = NULL; /* where lead_end has been found */
941 int extra_space = FALSE; /* append extra space */
942 int current_flag;
943 int require_blank = FALSE; /* requires blank after middle */
944 char_u *p2;
945
946 /*
947 * If the comment leader has the start, middle or end flag, it may not
948 * be used or may be replaced with the middle leader.
949 */
950 for (p = lead_flags; *p && *p != ':'; ++p)
951 {
952 if (*p == COM_BLANK)
953 {
954 require_blank = TRUE;
955 continue;
956 }
957 if (*p == COM_START || *p == COM_MIDDLE)
958 {
959 current_flag = *p;
960 if (*p == COM_START)
961 {
962 /*
963 * Doing "O" on a start of comment does not insert leader.
964 */
965 if (dir == BACKWARD)
966 {
967 lead_len = 0;
968 break;
969 }
970
971 /* find start of middle part */
972 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
973 require_blank = FALSE;
974 }
975
976 /*
977 * Isolate the strings of the middle and end leader.
978 */
979 while (*p && p[-1] != ':') /* find end of middle flags */
980 {
981 if (*p == COM_BLANK)
982 require_blank = TRUE;
983 ++p;
984 }
985 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
986
987 while (*p && p[-1] != ':') /* find end of end flags */
988 {
989 /* Check whether we allow automatic ending of comments */
990 if (*p == COM_AUTO_END)
991 end_comment_pending = -1; /* means we want to set it */
992 ++p;
993 }
994 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
995
996 if (end_comment_pending == -1) /* we can set it now */
997 end_comment_pending = lead_end[n - 1];
998
999 /*
1000 * If the end of the comment is in the same line, don't use
1001 * the comment leader.
1002 */
1003 if (dir == FORWARD)
1004 {
1005 for (p = saved_line + lead_len; *p; ++p)
1006 if (STRNCMP(p, lead_end, n) == 0)
1007 {
1008 comment_end = p;
1009 lead_len = 0;
1010 break;
1011 }
1012 }
1013
1014 /*
1015 * Doing "o" on a start of comment inserts the middle leader.
1016 */
1017 if (lead_len > 0)
1018 {
1019 if (current_flag == COM_START)
1020 {
1021 lead_repl = lead_middle;
1022 lead_repl_len = (int)STRLEN(lead_middle);
1023 }
1024
1025 /*
1026 * If we have hit RETURN immediately after the start
1027 * comment leader, then put a space after the middle
1028 * comment leader on the next line.
1029 */
1030 if (!vim_iswhite(saved_line[lead_len - 1])
1031 && ((p_extra != NULL
1032 && (int)curwin->w_cursor.col == lead_len)
1033 || (p_extra == NULL
1034 && saved_line[lead_len] == NUL)
1035 || require_blank))
1036 extra_space = TRUE;
1037 }
1038 break;
1039 }
1040 if (*p == COM_END)
1041 {
1042 /*
1043 * Doing "o" on the end of a comment does not insert leader.
1044 * Remember where the end is, might want to use it to find the
1045 * start (for C-comments).
1046 */
1047 if (dir == FORWARD)
1048 {
1049 comment_end = skipwhite(saved_line);
1050 lead_len = 0;
1051 break;
1052 }
1053
1054 /*
1055 * Doing "O" on the end of a comment inserts the middle leader.
1056 * Find the string for the middle leader, searching backwards.
1057 */
1058 while (p > curbuf->b_p_com && *p != ',')
1059 --p;
1060 for (lead_repl = p; lead_repl > curbuf->b_p_com
1061 && lead_repl[-1] != ':'; --lead_repl)
1062 ;
1063 lead_repl_len = (int)(p - lead_repl);
1064
1065 /* We can probably always add an extra space when doing "O" on
1066 * the comment-end */
1067 extra_space = TRUE;
1068
1069 /* Check whether we allow automatic ending of comments */
1070 for (p2 = p; *p2 && *p2 != ':'; p2++)
1071 {
1072 if (*p2 == COM_AUTO_END)
1073 end_comment_pending = -1; /* means we want to set it */
1074 }
1075 if (end_comment_pending == -1)
1076 {
1077 /* Find last character in end-comment string */
1078 while (*p2 && *p2 != ',')
1079 p2++;
1080 end_comment_pending = p2[-1];
1081 }
1082 break;
1083 }
1084 if (*p == COM_FIRST)
1085 {
1086 /*
1087 * Comment leader for first line only: Don't repeat leader
1088 * when using "O", blank out leader when using "o".
1089 */
1090 if (dir == BACKWARD)
1091 lead_len = 0;
1092 else
1093 {
1094 lead_repl = (char_u *)"";
1095 lead_repl_len = 0;
1096 }
1097 break;
1098 }
1099 }
1100 if (lead_len)
1101 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001102 /* allocate buffer (may concatenate p_extra later) */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001103 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001104 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 allocated = leader; /* remember to free it later */
1106
1107 if (leader == NULL)
1108 lead_len = 0;
1109 else
1110 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001111 vim_strncpy(leader, saved_line, lead_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112
1113 /*
1114 * Replace leader with lead_repl, right or left adjusted
1115 */
1116 if (lead_repl != NULL)
1117 {
1118 int c = 0;
1119 int off = 0;
1120
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001121 for (p = lead_flags; *p != NUL && *p != ':'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 {
1123 if (*p == COM_RIGHT || *p == COM_LEFT)
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001124 c = *p++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 else if (VIM_ISDIGIT(*p) || *p == '-')
1126 off = getdigits(&p);
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001127 else
1128 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 }
1130 if (c == COM_RIGHT) /* right adjusted leader */
1131 {
1132 /* find last non-white in the leader to line up with */
1133 for (p = leader + lead_len - 1; p > leader
1134 && vim_iswhite(*p); --p)
1135 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 ++p;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001137
1138#ifdef FEAT_MBYTE
1139 /* Compute the length of the replaced characters in
1140 * screen characters, not bytes. */
1141 {
1142 int repl_size = vim_strnsize(lead_repl,
1143 lead_repl_len);
1144 int old_size = 0;
1145 char_u *endp = p;
1146 int l;
1147
1148 while (old_size < repl_size && p > leader)
1149 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001150 mb_ptr_back(leader, p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001151 old_size += ptr2cells(p);
1152 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001153 l = lead_repl_len - (int)(endp - p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001154 if (l != 0)
1155 mch_memmove(endp + l, endp,
1156 (size_t)((leader + lead_len) - endp));
1157 lead_len += l;
1158 }
1159#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 if (p < leader + lead_repl_len)
1161 p = leader;
1162 else
1163 p -= lead_repl_len;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1166 if (p + lead_repl_len > leader + lead_len)
1167 p[lead_repl_len] = NUL;
1168
1169 /* blank-out any other chars from the old leader. */
1170 while (--p >= leader)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001171 {
1172#ifdef FEAT_MBYTE
1173 int l = mb_head_off(leader, p);
1174
1175 if (l > 1)
1176 {
1177 p -= l;
1178 if (ptr2cells(p) > 1)
1179 {
1180 p[1] = ' ';
1181 --l;
1182 }
1183 mch_memmove(p + 1, p + l + 1,
1184 (size_t)((leader + lead_len) - (p + l + 1)));
1185 lead_len -= l;
1186 *p = ' ';
1187 }
1188 else
1189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 if (!vim_iswhite(*p))
1191 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 }
1194 else /* left adjusted leader */
1195 {
1196 p = skipwhite(leader);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001197#ifdef FEAT_MBYTE
1198 /* Compute the length of the replaced characters in
1199 * screen characters, not bytes. Move the part that is
1200 * not to be overwritten. */
1201 {
1202 int repl_size = vim_strnsize(lead_repl,
1203 lead_repl_len);
1204 int i;
1205 int l;
1206
1207 for (i = 0; p[i] != NUL && i < lead_len; i += l)
1208 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001209 l = (*mb_ptr2len)(p + i);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001210 if (vim_strnsize(p, i + l) > repl_size)
1211 break;
1212 }
1213 if (i != lead_repl_len)
1214 {
1215 mch_memmove(p + lead_repl_len, p + i,
Bram Moolenaar2d7ff052009-11-17 15:08:26 +00001216 (size_t)(lead_len - i - (p - leader)));
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001217 lead_len += lead_repl_len - i;
1218 }
1219 }
1220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1222
1223 /* Replace any remaining non-white chars in the old
1224 * leader by spaces. Keep Tabs, the indent must
1225 * remain the same. */
1226 for (p += lead_repl_len; p < leader + lead_len; ++p)
1227 if (!vim_iswhite(*p))
1228 {
1229 /* Don't put a space before a TAB. */
1230 if (p + 1 < leader + lead_len && p[1] == TAB)
1231 {
1232 --lead_len;
1233 mch_memmove(p, p + 1,
1234 (leader + lead_len) - p);
1235 }
1236 else
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001237 {
1238#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001239 int l = (*mb_ptr2len)(p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001240
1241 if (l > 1)
1242 {
1243 if (ptr2cells(p) > 1)
1244 {
1245 /* Replace a double-wide char with
1246 * two spaces */
1247 --l;
1248 *p++ = ' ';
1249 }
1250 mch_memmove(p + 1, p + l,
1251 (leader + lead_len) - p);
1252 lead_len -= l - 1;
1253 }
1254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 }
1258 *p = NUL;
1259 }
1260
1261 /* Recompute the indent, it may have changed. */
1262 if (curbuf->b_p_ai
1263#ifdef FEAT_SMARTINDENT
1264 || do_si
1265#endif
1266 )
Bram Moolenaar597a4222014-06-25 14:39:50 +02001267 newindent = get_indent_str(leader, (int)curbuf->b_p_ts, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268
1269 /* Add the indent offset */
1270 if (newindent + off < 0)
1271 {
1272 off = -newindent;
1273 newindent = 0;
1274 }
1275 else
1276 newindent += off;
1277
1278 /* Correct trailing spaces for the shift, so that
1279 * alignment remains equal. */
1280 while (off > 0 && lead_len > 0
1281 && leader[lead_len - 1] == ' ')
1282 {
1283 /* Don't do it when there is a tab before the space */
1284 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1285 break;
1286 --lead_len;
1287 --off;
1288 }
1289
1290 /* If the leader ends in white space, don't add an
1291 * extra space */
1292 if (lead_len > 0 && vim_iswhite(leader[lead_len - 1]))
1293 extra_space = FALSE;
1294 leader[lead_len] = NUL;
1295 }
1296
1297 if (extra_space)
1298 {
1299 leader[lead_len++] = ' ';
1300 leader[lead_len] = NUL;
1301 }
1302
1303 newcol = lead_len;
1304
1305 /*
1306 * if a new indent will be set below, remove the indent that
1307 * is in the comment leader
1308 */
1309 if (newindent
1310#ifdef FEAT_SMARTINDENT
1311 || did_si
1312#endif
1313 )
1314 {
1315 while (lead_len && vim_iswhite(*leader))
1316 {
1317 --lead_len;
1318 --newcol;
1319 ++leader;
1320 }
1321 }
1322
1323 }
1324#ifdef FEAT_SMARTINDENT
1325 did_si = can_si = FALSE;
1326#endif
1327 }
1328 else if (comment_end != NULL)
1329 {
1330 /*
1331 * We have finished a comment, so we don't use the leader.
1332 * If this was a C-comment and 'ai' or 'si' is set do a normal
1333 * indent to align with the line containing the start of the
1334 * comment.
1335 */
1336 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1337 (curbuf->b_p_ai
1338#ifdef FEAT_SMARTINDENT
1339 || do_si
1340#endif
1341 ))
1342 {
1343 old_cursor = curwin->w_cursor;
1344 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1345 if ((pos = findmatch(NULL, NUL)) != NULL)
1346 {
1347 curwin->w_cursor.lnum = pos->lnum;
1348 newindent = get_indent();
1349 }
1350 curwin->w_cursor = old_cursor;
1351 }
1352 }
1353 }
1354#endif
1355
1356 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */
1357 if (p_extra != NULL)
1358 {
1359 *p_extra = saved_char; /* restore char that NUL replaced */
1360
1361 /*
1362 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
1363 * non-blank.
1364 *
1365 * When in REPLACE mode, put the deleted blanks on the replace stack,
1366 * preceded by a NUL, so they can be put back when a BS is entered.
1367 */
1368 if (REPLACE_NORMAL(State))
1369 replace_push(NUL); /* end of extra blanks */
1370 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
1371 {
1372 while ((*p_extra == ' ' || *p_extra == '\t')
1373#ifdef FEAT_MBYTE
1374 && (!enc_utf8
1375 || !utf_iscomposing(utf_ptr2char(p_extra + 1)))
1376#endif
1377 )
1378 {
1379 if (REPLACE_NORMAL(State))
1380 replace_push(*p_extra);
1381 ++p_extra;
1382 ++less_cols_off;
1383 }
1384 }
1385 if (*p_extra != NUL)
1386 did_ai = FALSE; /* append some text, don't truncate now */
1387
1388 /* columns for marks adjusted for removed columns */
1389 less_cols = (int)(p_extra - saved_line);
1390 }
1391
1392 if (p_extra == NULL)
1393 p_extra = (char_u *)""; /* append empty line */
1394
1395#ifdef FEAT_COMMENTS
1396 /* concatenate leader and p_extra, if there is a leader */
1397 if (lead_len)
1398 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001399 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
1400 {
1401 int i;
Bram Moolenaar36105782012-06-14 20:59:25 +02001402 int padding = second_line_indent
1403 - (newindent + (int)STRLEN(leader));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001404
1405 /* Here whitespace is inserted after the comment char.
1406 * Below, set_indent(newindent, SIN_INSERT) will insert the
1407 * whitespace needed before the comment char. */
1408 for (i = 0; i < padding; i++)
1409 {
1410 STRCAT(leader, " ");
Bram Moolenaar5fb9ec52012-07-25 16:10:03 +02001411 less_cols--;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001412 newcol++;
1413 }
1414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 STRCAT(leader, p_extra);
1416 p_extra = leader;
1417 did_ai = TRUE; /* So truncating blanks works with comments */
1418 less_cols -= lead_len;
1419 }
1420 else
1421 end_comment_pending = NUL; /* turns out there was no leader */
1422#endif
1423
1424 old_cursor = curwin->w_cursor;
1425 if (dir == BACKWARD)
1426 --curwin->w_cursor.lnum;
1427#ifdef FEAT_VREPLACE
1428 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
1429#endif
1430 {
1431 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
1432 == FAIL)
1433 goto theend;
1434 /* Postpone calling changed_lines(), because it would mess up folding
1435 * with markers. */
1436 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
1437 did_append = TRUE;
1438 }
1439#ifdef FEAT_VREPLACE
1440 else
1441 {
1442 /*
1443 * In VREPLACE mode we are starting to replace the next line.
1444 */
1445 curwin->w_cursor.lnum++;
1446 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1447 {
1448 /* In case we NL to a new line, BS to the previous one, and NL
1449 * again, we don't want to save the new line for undo twice.
1450 */
1451 (void)u_save_cursor(); /* errors are ignored! */
1452 vr_lines_changed++;
1453 }
1454 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1455 changed_bytes(curwin->w_cursor.lnum, 0);
1456 curwin->w_cursor.lnum--;
1457 did_append = FALSE;
1458 }
1459#endif
1460
1461 if (newindent
1462#ifdef FEAT_SMARTINDENT
1463 || did_si
1464#endif
1465 )
1466 {
1467 ++curwin->w_cursor.lnum;
1468#ifdef FEAT_SMARTINDENT
1469 if (did_si)
1470 {
Bram Moolenaar75a8d742014-05-07 15:10:21 +02001471 int sw = (int)get_sw_value(curbuf);
Bram Moolenaar14f24742012-08-08 18:01:05 +02001472
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001474 newindent -= newindent % sw;
1475 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 }
1477#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001478 /* Copy the indent */
1479 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 {
1481 (void)copy_indent(newindent, saved_line);
1482
1483 /*
1484 * Set the 'preserveindent' option so that any further screwing
1485 * with the line doesn't entirely destroy our efforts to preserve
1486 * it. It gets restored at the function end.
1487 */
1488 curbuf->b_p_pi = TRUE;
1489 }
1490 else
1491 (void)set_indent(newindent, SIN_INSERT);
1492 less_cols -= curwin->w_cursor.col;
1493
1494 ai_col = curwin->w_cursor.col;
1495
1496 /*
1497 * In REPLACE mode, for each character in the new indent, there must
1498 * be a NUL on the replace stack, for when it is deleted with BS
1499 */
1500 if (REPLACE_NORMAL(State))
1501 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1502 replace_push(NUL);
1503 newcol += curwin->w_cursor.col;
1504#ifdef FEAT_SMARTINDENT
1505 if (no_si)
1506 did_si = FALSE;
1507#endif
1508 }
1509
1510#ifdef FEAT_COMMENTS
1511 /*
1512 * In REPLACE mode, for each character in the extra leader, there must be
1513 * a NUL on the replace stack, for when it is deleted with BS.
1514 */
1515 if (REPLACE_NORMAL(State))
1516 while (lead_len-- > 0)
1517 replace_push(NUL);
1518#endif
1519
1520 curwin->w_cursor = old_cursor;
1521
1522 if (dir == FORWARD)
1523 {
1524 if (trunc_line || (State & INSERT))
1525 {
1526 /* truncate current line at cursor */
1527 saved_line[curwin->w_cursor.col] = NUL;
1528 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1529 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1530 truncate_spaces(saved_line);
1531 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1532 saved_line = NULL;
1533 if (did_append)
1534 {
1535 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1536 curwin->w_cursor.lnum + 1, 1L);
1537 did_append = FALSE;
1538
1539 /* Move marks after the line break to the new line. */
1540 if (flags & OPENLINE_MARKFIX)
1541 mark_col_adjust(curwin->w_cursor.lnum,
1542 curwin->w_cursor.col + less_cols_off,
1543 1L, (long)-less_cols);
1544 }
1545 else
1546 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1547 }
1548
1549 /*
1550 * Put the cursor on the new line. Careful: the scrollup() above may
1551 * have moved w_cursor, we must use old_cursor.
1552 */
1553 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1554 }
1555 if (did_append)
1556 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1557
1558 curwin->w_cursor.col = newcol;
1559#ifdef FEAT_VIRTUALEDIT
1560 curwin->w_cursor.coladd = 0;
1561#endif
1562
1563#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1564 /*
1565 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1566 * fixthisline() from doing it (via change_indent()) by telling it we're in
1567 * normal INSERT mode.
1568 */
1569 if (State & VREPLACE_FLAG)
1570 {
1571 vreplace_mode = State; /* So we know to put things right later */
1572 State = INSERT;
1573 }
1574 else
1575 vreplace_mode = 0;
1576#endif
1577#ifdef FEAT_LISP
1578 /*
1579 * May do lisp indenting.
1580 */
1581 if (!p_paste
1582# ifdef FEAT_COMMENTS
1583 && leader == NULL
1584# endif
1585 && curbuf->b_p_lisp
1586 && curbuf->b_p_ai)
1587 {
1588 fixthisline(get_lisp_indent);
1589 p = ml_get_curline();
1590 ai_col = (colnr_T)(skipwhite(p) - p);
1591 }
1592#endif
1593#ifdef FEAT_CINDENT
1594 /*
1595 * May do indenting after opening a new line.
1596 */
1597 if (!p_paste
1598 && (curbuf->b_p_cin
1599# ifdef FEAT_EVAL
1600 || *curbuf->b_p_inde != NUL
1601# endif
1602 )
1603 && in_cinkeys(dir == FORWARD
1604 ? KEY_OPEN_FORW
1605 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1606 {
1607 do_c_expr_indent();
1608 p = ml_get_curline();
1609 ai_col = (colnr_T)(skipwhite(p) - p);
1610 }
1611#endif
1612#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1613 if (vreplace_mode != 0)
1614 State = vreplace_mode;
1615#endif
1616
1617#ifdef FEAT_VREPLACE
1618 /*
1619 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1620 * original line, and inserts the new stuff char by char, pushing old stuff
1621 * onto the replace stack (via ins_char()).
1622 */
1623 if (State & VREPLACE_FLAG)
1624 {
1625 /* Put new line in p_extra */
1626 p_extra = vim_strsave(ml_get_curline());
1627 if (p_extra == NULL)
1628 goto theend;
1629
1630 /* Put back original line */
1631 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1632
1633 /* Insert new stuff into line again */
1634 curwin->w_cursor.col = 0;
1635#ifdef FEAT_VIRTUALEDIT
1636 curwin->w_cursor.coladd = 0;
1637#endif
1638 ins_bytes(p_extra); /* will call changed_bytes() */
1639 vim_free(p_extra);
1640 next_line = NULL;
1641 }
1642#endif
1643
1644 retval = TRUE; /* success! */
1645theend:
1646 curbuf->b_p_pi = saved_pi;
1647 vim_free(saved_line);
1648 vim_free(next_line);
1649 vim_free(allocated);
1650 return retval;
1651}
1652
1653#if defined(FEAT_COMMENTS) || defined(PROTO)
1654/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +02001655 * get_leader_len() returns the length in bytes of the prefix of the given
1656 * string which introduces a comment. If this string is not a comment then
1657 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 * When "flags" is not NULL, it is set to point to the flags of the recognized
1659 * comment leader.
1660 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001661 * If "include_space" is set, include trailing whitespace while calculating the
1662 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 */
1664 int
Bram Moolenaar81340392012-06-06 16:12:59 +02001665get_leader_len(line, flags, backward, include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 char_u *line;
1667 char_u **flags;
1668 int backward;
Bram Moolenaar81340392012-06-06 16:12:59 +02001669 int include_space;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670{
1671 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001672 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 int got_com = FALSE;
1674 int found_one;
1675 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1676 char_u *string; /* pointer to comment string */
1677 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001678 int middle_match_len = 0;
1679 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001680 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681
Bram Moolenaar81340392012-06-06 16:12:59 +02001682 result = i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 while (vim_iswhite(line[i])) /* leading white space is ignored */
1684 ++i;
1685
1686 /*
1687 * Repeat to match several nested comment strings.
1688 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001689 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 {
1691 /*
1692 * scan through the 'comments' option for a match
1693 */
1694 found_one = FALSE;
1695 for (list = curbuf->b_p_com; *list; )
1696 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001697 /* Get one option part into part_buf[]. Advance "list" to next
1698 * one. Put "string" at start of string. */
1699 if (!got_com && flags != NULL)
1700 *flags = list; /* remember where flags started */
1701 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1703 string = vim_strchr(part_buf, ':');
1704 if (string == NULL) /* missing ':', ignore this part */
1705 continue;
1706 *string++ = NUL; /* isolate flags from string */
1707
Bram Moolenaara4271d52011-05-10 13:38:27 +02001708 /* If we found a middle match previously, use that match when this
1709 * is not a middle or end. */
1710 if (middle_match_len != 0
1711 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1712 && vim_strchr(part_buf, COM_END) == NULL)
1713 break;
1714
1715 /* When we already found a nested comment, only accept further
1716 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1718 continue;
1719
Bram Moolenaara4271d52011-05-10 13:38:27 +02001720 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1722 continue;
1723
Bram Moolenaara4271d52011-05-10 13:38:27 +02001724 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 * When string starts with white space, must have some white space
1726 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001727 * TABs and spaces). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 if (vim_iswhite(string[0]))
1729 {
1730 if (i == 0 || !vim_iswhite(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001731 continue; /* missing white space */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 while (vim_iswhite(string[0]))
1733 ++string;
1734 }
1735 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1736 ;
1737 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001738 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
Bram Moolenaara4271d52011-05-10 13:38:27 +02001740 /* When 'b' flag used, there must be white space or an
1741 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 if (vim_strchr(part_buf, COM_BLANK) != NULL
1743 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1744 continue;
1745
Bram Moolenaara4271d52011-05-10 13:38:27 +02001746 /* We have found a match, stop searching unless this is a middle
1747 * comment. The middle comment can be a substring of the end
1748 * comment in which case it's better to return the length of the
1749 * end comment and its flags. Thus we keep searching with middle
1750 * and end matches and use an end match if it matches better. */
1751 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1752 {
1753 if (middle_match_len == 0)
1754 {
1755 middle_match_len = j;
1756 saved_flags = prev_list;
1757 }
1758 continue;
1759 }
1760 if (middle_match_len != 0 && j > middle_match_len)
1761 /* Use this match instead of the middle match, since it's a
1762 * longer thus better match. */
1763 middle_match_len = 0;
1764
1765 if (middle_match_len == 0)
1766 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 found_one = TRUE;
1768 break;
1769 }
1770
Bram Moolenaara4271d52011-05-10 13:38:27 +02001771 if (middle_match_len != 0)
1772 {
1773 /* Use the previously found middle match after failing to find a
1774 * match with an end. */
1775 if (!got_com && flags != NULL)
1776 *flags = saved_flags;
1777 i += middle_match_len;
1778 found_one = TRUE;
1779 }
1780
1781 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 if (!found_one)
1783 break;
1784
Bram Moolenaar81340392012-06-06 16:12:59 +02001785 result = i;
1786
Bram Moolenaara4271d52011-05-10 13:38:27 +02001787 /* Include any trailing white space. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 while (vim_iswhite(line[i]))
1789 ++i;
1790
Bram Moolenaar81340392012-06-06 16:12:59 +02001791 if (include_space)
1792 result = i;
1793
Bram Moolenaara4271d52011-05-10 13:38:27 +02001794 /* If this comment doesn't nest, stop here. */
1795 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 if (vim_strchr(part_buf, COM_NEST) == NULL)
1797 break;
1798 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001799 return result;
1800}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001801
Bram Moolenaar81340392012-06-06 16:12:59 +02001802/*
1803 * Return the offset at which the last comment in line starts. If there is no
1804 * comment in the whole line, -1 is returned.
1805 *
1806 * When "flags" is not null, it is set to point to the flags describing the
1807 * recognized comment leader.
1808 */
1809 int
1810get_last_leader_offset(line, flags)
1811 char_u *line;
1812 char_u **flags;
1813{
1814 int result = -1;
1815 int i, j;
1816 int lower_check_bound = 0;
1817 char_u *string;
1818 char_u *com_leader;
1819 char_u *com_flags;
1820 char_u *list;
1821 int found_one;
1822 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1823
1824 /*
1825 * Repeat to match several nested comment strings.
1826 */
1827 i = (int)STRLEN(line);
1828 while (--i >= lower_check_bound)
1829 {
1830 /*
1831 * scan through the 'comments' option for a match
1832 */
1833 found_one = FALSE;
1834 for (list = curbuf->b_p_com; *list; )
1835 {
1836 char_u *flags_save = list;
1837
1838 /*
1839 * Get one option part into part_buf[]. Advance list to next one.
1840 * put string at start of string.
1841 */
1842 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1843 string = vim_strchr(part_buf, ':');
1844 if (string == NULL) /* If everything is fine, this cannot actually
1845 * happen. */
1846 {
1847 continue;
1848 }
1849 *string++ = NUL; /* Isolate flags from string. */
1850 com_leader = string;
1851
1852 /*
1853 * Line contents and string must match.
1854 * When string starts with white space, must have some white space
1855 * (but the amount does not need to match, there might be a mix of
1856 * TABs and spaces).
1857 */
1858 if (vim_iswhite(string[0]))
1859 {
1860 if (i == 0 || !vim_iswhite(line[i - 1]))
1861 continue;
1862 while (vim_iswhite(string[0]))
1863 ++string;
1864 }
1865 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1866 /* do nothing */;
1867 if (string[j] != NUL)
1868 continue;
1869
1870 /*
1871 * When 'b' flag used, there must be white space or an
1872 * end-of-line after the string in the line.
1873 */
1874 if (vim_strchr(part_buf, COM_BLANK) != NULL
1875 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1876 {
1877 continue;
1878 }
1879
1880 /*
1881 * We have found a match, stop searching.
1882 */
1883 found_one = TRUE;
1884
1885 if (flags)
1886 *flags = flags_save;
1887 com_flags = flags_save;
1888
1889 break;
1890 }
1891
1892 if (found_one)
1893 {
1894 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
1895 int len1, len2, off;
1896
1897 result = i;
1898 /*
1899 * If this comment nests, continue searching.
1900 */
1901 if (vim_strchr(part_buf, COM_NEST) != NULL)
1902 continue;
1903
1904 lower_check_bound = i;
1905
1906 /* Let's verify whether the comment leader found is a substring
1907 * of other comment leaders. If it is, let's adjust the
1908 * lower_check_bound so that we make sure that we have determined
1909 * the comment leader correctly.
1910 */
1911
1912 while (vim_iswhite(*com_leader))
1913 ++com_leader;
1914 len1 = (int)STRLEN(com_leader);
1915
1916 for (list = curbuf->b_p_com; *list; )
1917 {
1918 char_u *flags_save = list;
1919
1920 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
1921 if (flags_save == com_flags)
1922 continue;
1923 string = vim_strchr(part_buf2, ':');
1924 ++string;
1925 while (vim_iswhite(*string))
1926 ++string;
1927 len2 = (int)STRLEN(string);
1928 if (len2 == 0)
1929 continue;
1930
1931 /* Now we have to verify whether string ends with a substring
1932 * beginning the com_leader. */
1933 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
1934 {
1935 --off;
1936 if (!STRNCMP(string + off, com_leader, len2 - off))
1937 {
1938 if (i - off < lower_check_bound)
1939 lower_check_bound = i - off;
1940 }
1941 }
1942 }
1943 }
1944 }
1945 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946}
1947#endif
1948
1949/*
1950 * Return the number of window lines occupied by buffer line "lnum".
1951 */
1952 int
1953plines(lnum)
1954 linenr_T lnum;
1955{
1956 return plines_win(curwin, lnum, TRUE);
1957}
1958
1959 int
1960plines_win(wp, lnum, winheight)
1961 win_T *wp;
1962 linenr_T lnum;
1963 int winheight; /* when TRUE limit to window height */
1964{
1965#if defined(FEAT_DIFF) || defined(PROTO)
1966 /* Check for filler lines above this buffer line. When folded the result
1967 * is one line anyway. */
1968 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
1969}
1970
1971 int
1972plines_nofill(lnum)
1973 linenr_T lnum;
1974{
1975 return plines_win_nofill(curwin, lnum, TRUE);
1976}
1977
1978 int
1979plines_win_nofill(wp, lnum, winheight)
1980 win_T *wp;
1981 linenr_T lnum;
1982 int winheight; /* when TRUE limit to window height */
1983{
1984#endif
1985 int lines;
1986
1987 if (!wp->w_p_wrap)
1988 return 1;
1989
1990#ifdef FEAT_VERTSPLIT
1991 if (wp->w_width == 0)
1992 return 1;
1993#endif
1994
1995#ifdef FEAT_FOLDING
1996 /* A folded lines is handled just like an empty line. */
1997 /* NOTE: Caller must handle lines that are MAYBE folded. */
1998 if (lineFolded(wp, lnum) == TRUE)
1999 return 1;
2000#endif
2001
2002 lines = plines_win_nofold(wp, lnum);
2003 if (winheight > 0 && lines > wp->w_height)
2004 return (int)wp->w_height;
2005 return lines;
2006}
2007
2008/*
2009 * Return number of window lines physical line "lnum" will occupy in window
2010 * "wp". Does not care about folding, 'wrap' or 'diff'.
2011 */
2012 int
2013plines_win_nofold(wp, lnum)
2014 win_T *wp;
2015 linenr_T lnum;
2016{
2017 char_u *s;
2018 long col;
2019 int width;
2020
2021 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2022 if (*s == NUL) /* empty line */
2023 return 1;
2024 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
2025
2026 /*
2027 * If list mode is on, then the '$' at the end of the line may take up one
2028 * extra column.
2029 */
2030 if (wp->w_p_list && lcs_eol != NUL)
2031 col += 1;
2032
2033 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002034 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 */
2036 width = W_WIDTH(wp) - win_col_off(wp);
2037 if (width <= 0)
2038 return 32000;
2039 if (col <= width)
2040 return 1;
2041 col -= width;
2042 width += win_col_off2(wp);
2043 return (col + (width - 1)) / width + 1;
2044}
2045
2046/*
2047 * Like plines_win(), but only reports the number of physical screen lines
2048 * used from the start of the line to the given column number.
2049 */
2050 int
2051plines_win_col(wp, lnum, column)
2052 win_T *wp;
2053 linenr_T lnum;
2054 long column;
2055{
2056 long col;
2057 char_u *s;
2058 int lines = 0;
2059 int width;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002060 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061
2062#ifdef FEAT_DIFF
2063 /* Check for filler lines above this buffer line. When folded the result
2064 * is one line anyway. */
2065 lines = diff_check_fill(wp, lnum);
2066#endif
2067
2068 if (!wp->w_p_wrap)
2069 return lines + 1;
2070
2071#ifdef FEAT_VERTSPLIT
2072 if (wp->w_width == 0)
2073 return lines + 1;
2074#endif
2075
Bram Moolenaar597a4222014-06-25 14:39:50 +02002076 line = s = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077
2078 col = 0;
2079 while (*s != NUL && --column >= 0)
2080 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02002081 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002082 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 }
2084
2085 /*
2086 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2087 * INSERT mode, then col must be adjusted so that it represents the last
2088 * screen position of the TAB. This only fixes an error when the TAB wraps
2089 * from one screen line to the next (when 'columns' is not a multiple of
2090 * 'ts') -- webb.
2091 */
2092 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
Bram Moolenaar597a4222014-06-25 14:39:50 +02002093 col += win_lbr_chartabsize(wp, line, s, (colnr_T)col, NULL) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094
2095 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002096 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 */
2098 width = W_WIDTH(wp) - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002099 if (width <= 0)
2100 return 9999;
2101
2102 lines += 1;
2103 if (col > width)
2104 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2105 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106}
2107
2108 int
2109plines_m_win(wp, first, last)
2110 win_T *wp;
2111 linenr_T first, last;
2112{
2113 int count = 0;
2114
2115 while (first <= last)
2116 {
2117#ifdef FEAT_FOLDING
2118 int x;
2119
2120 /* Check if there are any really folded lines, but also included lines
2121 * that are maybe folded. */
2122 x = foldedCount(wp, first, NULL);
2123 if (x > 0)
2124 {
2125 ++count; /* count 1 for "+-- folded" line */
2126 first += x;
2127 }
2128 else
2129#endif
2130 {
2131#ifdef FEAT_DIFF
2132 if (first == wp->w_topline)
2133 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2134 else
2135#endif
2136 count += plines_win(wp, first, TRUE);
2137 ++first;
2138 }
2139 }
2140 return (count);
2141}
2142
2143#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO)
2144/*
2145 * Insert string "p" at the cursor position. Stops at a NUL byte.
2146 * Handles Replace mode and multi-byte characters.
2147 */
2148 void
2149ins_bytes(p)
2150 char_u *p;
2151{
2152 ins_bytes_len(p, (int)STRLEN(p));
2153}
2154#endif
2155
2156#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2157 || defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO)
2158/*
2159 * Insert string "p" with length "len" at the cursor position.
2160 * Handles Replace mode and multi-byte characters.
2161 */
2162 void
2163ins_bytes_len(p, len)
2164 char_u *p;
2165 int len;
2166{
2167 int i;
2168# ifdef FEAT_MBYTE
2169 int n;
2170
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002171 if (has_mbyte)
2172 for (i = 0; i < len; i += n)
2173 {
2174 if (enc_utf8)
2175 /* avoid reading past p[len] */
2176 n = utfc_ptr2len_len(p + i, len - i);
2177 else
2178 n = (*mb_ptr2len)(p + i);
2179 ins_char_bytes(p + i, n);
2180 }
2181 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182# endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002183 for (i = 0; i < len; ++i)
2184 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185}
2186#endif
2187
2188/*
2189 * Insert or replace a single character at the cursor position.
2190 * When in REPLACE or VREPLACE mode, replace any existing character.
2191 * Caller must have prepared for undo.
2192 * For multi-byte characters we get the whole character, the caller must
2193 * convert bytes to a character.
2194 */
2195 void
2196ins_char(c)
2197 int c;
2198{
2199#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002200 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 int n;
2202
2203 n = (*mb_char2bytes)(c, buf);
2204
2205 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2206 * Happens for CTRL-Vu9900. */
2207 if (buf[0] == 0)
2208 buf[0] = '\n';
2209
2210 ins_char_bytes(buf, n);
2211}
2212
2213 void
2214ins_char_bytes(buf, charlen)
2215 char_u *buf;
2216 int charlen;
2217{
2218 int c = buf[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219#endif
2220 int newlen; /* nr of bytes inserted */
2221 int oldlen; /* nr of bytes deleted (0 when not replacing) */
2222 char_u *p;
2223 char_u *newp;
2224 char_u *oldp;
2225 int linelen; /* length of old line including NUL */
2226 colnr_T col;
2227 linenr_T lnum = curwin->w_cursor.lnum;
2228 int i;
2229
2230#ifdef FEAT_VIRTUALEDIT
2231 /* Break tabs if needed. */
2232 if (virtual_active() && curwin->w_cursor.coladd > 0)
2233 coladvance_force(getviscol());
2234#endif
2235
2236 col = curwin->w_cursor.col;
2237 oldp = ml_get(lnum);
2238 linelen = (int)STRLEN(oldp) + 1;
2239
2240 /* The lengths default to the values for when not replacing. */
2241 oldlen = 0;
2242#ifdef FEAT_MBYTE
2243 newlen = charlen;
2244#else
2245 newlen = 1;
2246#endif
2247
2248 if (State & REPLACE_FLAG)
2249 {
2250#ifdef FEAT_VREPLACE
2251 if (State & VREPLACE_FLAG)
2252 {
2253 colnr_T new_vcol = 0; /* init for GCC */
2254 colnr_T vcol;
2255 int old_list;
2256#ifndef FEAT_MBYTE
2257 char_u buf[2];
2258#endif
2259
2260 /*
2261 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2262 * Returns the old value of list, so when finished,
2263 * curwin->w_p_list should be set back to this.
2264 */
2265 old_list = curwin->w_p_list;
2266 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2267 curwin->w_p_list = FALSE;
2268
2269 /*
2270 * In virtual replace mode each character may replace one or more
2271 * characters (zero if it's a TAB). Count the number of bytes to
2272 * be deleted to make room for the new character, counting screen
2273 * cells. May result in adding spaces to fill a gap.
2274 */
2275 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2276#ifndef FEAT_MBYTE
2277 buf[0] = c;
2278 buf[1] = NUL;
2279#endif
2280 new_vcol = vcol + chartabsize(buf, vcol);
2281 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2282 {
2283 vcol += chartabsize(oldp + col + oldlen, vcol);
2284 /* Don't need to remove a TAB that takes us to the right
2285 * position. */
2286 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2287 break;
2288#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002289 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290#else
2291 ++oldlen;
2292#endif
2293 /* Deleted a bit too much, insert spaces. */
2294 if (vcol > new_vcol)
2295 newlen += vcol - new_vcol;
2296 }
2297 curwin->w_p_list = old_list;
2298 }
2299 else
2300#endif
2301 if (oldp[col] != NUL)
2302 {
2303 /* normal replace */
2304#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002305 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306#else
2307 oldlen = 1;
2308#endif
2309 }
2310
2311
2312 /* Push the replaced bytes onto the replace stack, so that they can be
2313 * put back when BS is used. The bytes of a multi-byte character are
2314 * done the other way around, so that the first byte is popped off
2315 * first (it tells the byte length of the character). */
2316 replace_push(NUL);
2317 for (i = 0; i < oldlen; ++i)
2318 {
2319#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002320 if (has_mbyte)
2321 i += replace_push_mb(oldp + col + i) - 1;
2322 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002324 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 }
2326 }
2327
2328 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2329 if (newp == NULL)
2330 return;
2331
2332 /* Copy bytes before the cursor. */
2333 if (col > 0)
2334 mch_memmove(newp, oldp, (size_t)col);
2335
2336 /* Copy bytes after the changed character(s). */
2337 p = newp + col;
2338 mch_memmove(p + newlen, oldp + col + oldlen,
2339 (size_t)(linelen - col - oldlen));
2340
2341 /* Insert or overwrite the new character. */
2342#ifdef FEAT_MBYTE
2343 mch_memmove(p, buf, charlen);
2344 i = charlen;
2345#else
2346 *p = c;
2347 i = 1;
2348#endif
2349
2350 /* Fill with spaces when necessary. */
2351 while (i < newlen)
2352 p[i++] = ' ';
2353
2354 /* Replace the line in the buffer. */
2355 ml_replace(lnum, newp, FALSE);
2356
2357 /* mark the buffer as changed and prepare for displaying */
2358 changed_bytes(lnum, col);
2359
2360 /*
2361 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2362 * show the match for right parens and braces.
2363 */
2364 if (p_sm && (State & INSERT)
2365 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002366#ifdef FEAT_INS_EXPAND
2367 && !ins_compl_active()
2368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002370 {
2371#ifdef FEAT_MBYTE
2372 if (has_mbyte)
2373 showmatch(mb_ptr2char(buf));
2374 else
2375#endif
2376 showmatch(c);
2377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378
2379#ifdef FEAT_RIGHTLEFT
2380 if (!p_ri || (State & REPLACE_FLAG))
2381#endif
2382 {
2383 /* Normal insert: move cursor right */
2384#ifdef FEAT_MBYTE
2385 curwin->w_cursor.col += charlen;
2386#else
2387 ++curwin->w_cursor.col;
2388#endif
2389 }
2390 /*
2391 * TODO: should try to update w_row here, to avoid recomputing it later.
2392 */
2393}
2394
2395/*
2396 * Insert a string at the cursor position.
2397 * Note: Does NOT handle Replace mode.
2398 * Caller must have prepared for undo.
2399 */
2400 void
2401ins_str(s)
2402 char_u *s;
2403{
2404 char_u *oldp, *newp;
2405 int newlen = (int)STRLEN(s);
2406 int oldlen;
2407 colnr_T col;
2408 linenr_T lnum = curwin->w_cursor.lnum;
2409
2410#ifdef FEAT_VIRTUALEDIT
2411 if (virtual_active() && curwin->w_cursor.coladd > 0)
2412 coladvance_force(getviscol());
2413#endif
2414
2415 col = curwin->w_cursor.col;
2416 oldp = ml_get(lnum);
2417 oldlen = (int)STRLEN(oldp);
2418
2419 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2420 if (newp == NULL)
2421 return;
2422 if (col > 0)
2423 mch_memmove(newp, oldp, (size_t)col);
2424 mch_memmove(newp + col, s, (size_t)newlen);
2425 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2426 ml_replace(lnum, newp, FALSE);
2427 changed_bytes(lnum, col);
2428 curwin->w_cursor.col += newlen;
2429}
2430
2431/*
2432 * Delete one character under the cursor.
2433 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2434 * Caller must have prepared for undo.
2435 *
2436 * return FAIL for failure, OK otherwise
2437 */
2438 int
2439del_char(fixpos)
2440 int fixpos;
2441{
2442#ifdef FEAT_MBYTE
2443 if (has_mbyte)
2444 {
2445 /* Make sure the cursor is at the start of a character. */
2446 mb_adjust_cursor();
2447 if (*ml_get_cursor() == NUL)
2448 return FAIL;
2449 return del_chars(1L, fixpos);
2450 }
2451#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002452 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453}
2454
2455#if defined(FEAT_MBYTE) || defined(PROTO)
2456/*
2457 * Like del_bytes(), but delete characters instead of bytes.
2458 */
2459 int
2460del_chars(count, fixpos)
2461 long count;
2462 int fixpos;
2463{
2464 long bytes = 0;
2465 long i;
2466 char_u *p;
2467 int l;
2468
2469 p = ml_get_cursor();
2470 for (i = 0; i < count && *p != NUL; ++i)
2471 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002472 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 bytes += l;
2474 p += l;
2475 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002476 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477}
2478#endif
2479
2480/*
2481 * Delete "count" bytes under the cursor.
2482 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2483 * Caller must have prepared for undo.
2484 *
2485 * return FAIL for failure, OK otherwise
2486 */
2487 int
Bram Moolenaarca003e12006-03-17 23:19:38 +00002488del_bytes(count, fixpos_arg, use_delcombine)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 long count;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002490 int fixpos_arg;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002491 int use_delcombine UNUSED; /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492{
2493 char_u *oldp, *newp;
2494 colnr_T oldlen;
2495 linenr_T lnum = curwin->w_cursor.lnum;
2496 colnr_T col = curwin->w_cursor.col;
2497 int was_alloced;
2498 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002499 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500
2501 oldp = ml_get(lnum);
2502 oldlen = (int)STRLEN(oldp);
2503
2504 /*
2505 * Can't do anything when the cursor is on the NUL after the line.
2506 */
2507 if (col >= oldlen)
2508 return FAIL;
2509
2510#ifdef FEAT_MBYTE
2511 /* If 'delcombine' is set and deleting (less than) one character, only
2512 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002513 if (p_deco && use_delcombine && enc_utf8
2514 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002516 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 int n;
2518
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002519 (void)utfc_ptr2char(oldp + col, cc);
2520 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 {
2522 /* Find the last composing char, there can be several. */
2523 n = col;
2524 do
2525 {
2526 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002527 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 n += count;
2529 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2530 fixpos = 0;
2531 }
2532 }
2533#endif
2534
2535 /*
2536 * When count is too big, reduce it.
2537 */
2538 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2539 if (movelen <= 1)
2540 {
2541 /*
2542 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002543 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2544 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002546 if (col > 0 && fixpos && restart_edit == 0
2547#ifdef FEAT_VIRTUALEDIT
2548 && (ve_flags & VE_ONEMORE) == 0
2549#endif
2550 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 {
2552 --curwin->w_cursor.col;
2553#ifdef FEAT_VIRTUALEDIT
2554 curwin->w_cursor.coladd = 0;
2555#endif
2556#ifdef FEAT_MBYTE
2557 if (has_mbyte)
2558 curwin->w_cursor.col -=
2559 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2560#endif
2561 }
2562 count = oldlen - col;
2563 movelen = 1;
2564 }
2565
2566 /*
2567 * If the old line has been allocated the deletion can be done in the
2568 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002569 * Can't do this when using Netbeans, because we would need to invoke
2570 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002571 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002574 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002575 was_alloced = FALSE;
2576 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002578 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 if (was_alloced)
2580 newp = oldp; /* use same allocated memory */
2581 else
2582 { /* need to allocate a new line */
2583 newp = alloc((unsigned)(oldlen + 1 - count));
2584 if (newp == NULL)
2585 return FAIL;
2586 mch_memmove(newp, oldp, (size_t)col);
2587 }
2588 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2589 if (!was_alloced)
2590 ml_replace(lnum, newp, FALSE);
2591
2592 /* mark the buffer as changed and prepare for displaying */
2593 changed_bytes(lnum, curwin->w_cursor.col);
2594
2595 return OK;
2596}
2597
2598/*
2599 * Delete from cursor to end of line.
2600 * Caller must have prepared for undo.
2601 *
2602 * return FAIL for failure, OK otherwise
2603 */
2604 int
2605truncate_line(fixpos)
2606 int fixpos; /* if TRUE fix the cursor position when done */
2607{
2608 char_u *newp;
2609 linenr_T lnum = curwin->w_cursor.lnum;
2610 colnr_T col = curwin->w_cursor.col;
2611
2612 if (col == 0)
2613 newp = vim_strsave((char_u *)"");
2614 else
2615 newp = vim_strnsave(ml_get(lnum), col);
2616
2617 if (newp == NULL)
2618 return FAIL;
2619
2620 ml_replace(lnum, newp, FALSE);
2621
2622 /* mark the buffer as changed and prepare for displaying */
2623 changed_bytes(lnum, curwin->w_cursor.col);
2624
2625 /*
2626 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2627 */
2628 if (fixpos && curwin->w_cursor.col > 0)
2629 --curwin->w_cursor.col;
2630
2631 return OK;
2632}
2633
2634/*
2635 * Delete "nlines" lines at the cursor.
2636 * Saves the lines for undo first if "undo" is TRUE.
2637 */
2638 void
2639del_lines(nlines, undo)
2640 long nlines; /* number of lines to delete */
2641 int undo; /* if TRUE, prepare for undo */
2642{
2643 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002644 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645
2646 if (nlines <= 0)
2647 return;
2648
2649 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002650 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 return;
2652
2653 for (n = 0; n < nlines; )
2654 {
2655 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2656 break;
2657
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002658 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 ++n;
2660
2661 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002662 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 break;
2664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002666 /* Correct the cursor position before calling deleted_lines_mark(), it may
2667 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 curwin->w_cursor.col = 0;
2669 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002670
2671 /* adjust marks, mark the buffer as changed and prepare for displaying */
2672 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673}
2674
2675 int
2676gchar_pos(pos)
2677 pos_T *pos;
2678{
2679 char_u *ptr = ml_get_pos(pos);
2680
2681#ifdef FEAT_MBYTE
2682 if (has_mbyte)
2683 return (*mb_ptr2char)(ptr);
2684#endif
2685 return (int)*ptr;
2686}
2687
2688 int
2689gchar_cursor()
2690{
2691#ifdef FEAT_MBYTE
2692 if (has_mbyte)
2693 return (*mb_ptr2char)(ml_get_cursor());
2694#endif
2695 return (int)*ml_get_cursor();
2696}
2697
2698/*
2699 * Write a character at the current cursor position.
2700 * It is directly written into the block.
2701 */
2702 void
2703pchar_cursor(c)
2704 int c;
2705{
2706 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2707 + curwin->w_cursor.col) = c;
2708}
2709
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710/*
2711 * When extra == 0: Return TRUE if the cursor is before or on the first
2712 * non-blank in the line.
2713 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2714 * the line.
2715 */
2716 int
2717inindent(extra)
2718 int extra;
2719{
2720 char_u *ptr;
2721 colnr_T col;
2722
2723 for (col = 0, ptr = ml_get_curline(); vim_iswhite(*ptr); ++col)
2724 ++ptr;
2725 if (col >= curwin->w_cursor.col + extra)
2726 return TRUE;
2727 else
2728 return FALSE;
2729}
2730
2731/*
2732 * Skip to next part of an option argument: Skip space and comma.
2733 */
2734 char_u *
2735skip_to_option_part(p)
2736 char_u *p;
2737{
2738 if (*p == ',')
2739 ++p;
2740 while (*p == ' ')
2741 ++p;
2742 return p;
2743}
2744
2745/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002746 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 *
2748 * Most often called through changed_bytes() and changed_lines(), which also
2749 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002750 *
2751 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 */
2753 void
2754changed()
2755{
2756#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2757 /* The text of the preediting area is inserted, but this doesn't
2758 * mean a change of the buffer yet. That is delayed until the
2759 * text is committed. (this means preedit becomes empty) */
2760 if (im_is_preediting() && !xim_changed_while_preediting)
2761 return;
2762 xim_changed_while_preediting = FALSE;
2763#endif
2764
2765 if (!curbuf->b_changed)
2766 {
2767 int save_msg_scroll = msg_scroll;
2768
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002769 /* Give a warning about changing a read-only file. This may also
2770 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002772
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 /* Create a swap file if that is wanted.
2774 * Don't do this for "nofile" and "nowrite" buffer types. */
2775 if (curbuf->b_may_swap
2776#ifdef FEAT_QUICKFIX
2777 && !bt_dontwrite(curbuf)
2778#endif
2779 )
2780 {
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002781 int save_need_wait_return = need_wait_return;
2782
2783 need_wait_return = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 ml_open_file(curbuf);
2785
2786 /* The ml_open_file() can cause an ATTENTION message.
2787 * Wait two seconds, to make sure the user reads this unexpected
2788 * message. Since we could be anywhere, call wait_return() now,
2789 * and don't let the emsg() set msg_scroll. */
2790 if (need_wait_return && emsg_silent == 0)
2791 {
2792 out_flush();
2793 ui_delay(2000L, TRUE);
2794 wait_return(TRUE);
2795 msg_scroll = save_msg_scroll;
2796 }
Bram Moolenaarb01f3572016-01-15 15:17:04 +01002797 else
2798 need_wait_return = save_need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002800 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 }
2802 ++curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803}
2804
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002805/*
2806 * Internal part of changed(), no user interaction.
2807 */
2808 void
2809changed_int()
2810{
2811 curbuf->b_changed = TRUE;
2812 ml_setflags(curbuf);
2813#ifdef FEAT_WINDOWS
2814 check_status(curbuf);
2815 redraw_tabline = TRUE;
2816#endif
2817#ifdef FEAT_TITLE
2818 need_maketitle = TRUE; /* set window title later */
2819#endif
2820}
2821
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01002822static void changedOneline(buf_T *buf, linenr_T lnum);
2823static void changed_lines_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra);
2824static void changed_common(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825
2826/*
2827 * Changed bytes within a single line for the current buffer.
2828 * - marks the windows on this buffer to be redisplayed
2829 * - marks the buffer changed by calling changed()
2830 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002831 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 */
2833 void
2834changed_bytes(lnum, col)
2835 linenr_T lnum;
2836 colnr_T col;
2837{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002838 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002840
2841#ifdef FEAT_DIFF
2842 /* Diff highlighting in other diff windows may need to be updated too. */
2843 if (curwin->w_p_diff)
2844 {
2845 win_T *wp;
2846 linenr_T wlnum;
2847
2848 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2849 if (wp->w_p_diff && wp != curwin)
2850 {
2851 redraw_win_later(wp, VALID);
2852 wlnum = diff_lnum_win(lnum, wp);
2853 if (wlnum > 0)
2854 changedOneline(wp->w_buffer, wlnum);
2855 }
2856 }
2857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858}
2859
2860 static void
Bram Moolenaardba8a912005-04-24 22:08:39 +00002861changedOneline(buf, lnum)
2862 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 linenr_T lnum;
2864{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002865 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 {
2867 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002868 if (lnum < buf->b_mod_top)
2869 buf->b_mod_top = lnum;
2870 else if (lnum >= buf->b_mod_bot)
2871 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 }
2873 else
2874 {
2875 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002876 buf->b_mod_set = TRUE;
2877 buf->b_mod_top = lnum;
2878 buf->b_mod_bot = lnum + 1;
2879 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 }
2881}
2882
2883/*
2884 * Appended "count" lines below line "lnum" in the current buffer.
2885 * Must be called AFTER the change and after mark_adjust().
2886 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2887 */
2888 void
2889appended_lines(lnum, count)
2890 linenr_T lnum;
2891 long count;
2892{
2893 changed_lines(lnum + 1, 0, lnum + 1, count);
2894}
2895
2896/*
2897 * Like appended_lines(), but adjust marks first.
2898 */
2899 void
2900appended_lines_mark(lnum, count)
2901 linenr_T lnum;
2902 long count;
2903{
2904 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
2905 changed_lines(lnum + 1, 0, lnum + 1, count);
2906}
2907
2908/*
2909 * Deleted "count" lines at line "lnum" in the current buffer.
2910 * Must be called AFTER the change and after mark_adjust().
2911 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2912 */
2913 void
2914deleted_lines(lnum, count)
2915 linenr_T lnum;
2916 long count;
2917{
2918 changed_lines(lnum, 0, lnum + count, -count);
2919}
2920
2921/*
2922 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002923 * Make sure the cursor is on a valid line before calling, a GUI callback may
2924 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 */
2926 void
2927deleted_lines_mark(lnum, count)
2928 linenr_T lnum;
2929 long count;
2930{
2931 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
2932 changed_lines(lnum, 0, lnum + count, -count);
2933}
2934
2935/*
2936 * Changed lines for the current buffer.
2937 * Must be called AFTER the change and after mark_adjust().
2938 * - mark the buffer changed by calling changed()
2939 * - mark the windows on this buffer to be redisplayed
2940 * - invalidate cached values
2941 * "lnum" is the first line that needs displaying, "lnume" the first line
2942 * below the changed lines (BEFORE the change).
2943 * When only inserting lines, "lnum" and "lnume" are equal.
2944 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002945 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 */
2947 void
2948changed_lines(lnum, col, lnume, xtra)
2949 linenr_T lnum; /* first line with change */
2950 colnr_T col; /* column in first line with change */
2951 linenr_T lnume; /* line below last changed line */
2952 long xtra; /* number of extra lines (negative when deleting) */
2953{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002954 changed_lines_buf(curbuf, lnum, lnume, xtra);
2955
2956#ifdef FEAT_DIFF
2957 if (xtra == 0 && curwin->w_p_diff)
2958 {
2959 /* When the number of lines doesn't change then mark_adjust() isn't
2960 * called and other diff buffers still need to be marked for
2961 * displaying. */
2962 win_T *wp;
2963 linenr_T wlnum;
2964
2965 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2966 if (wp->w_p_diff && wp != curwin)
2967 {
2968 redraw_win_later(wp, VALID);
2969 wlnum = diff_lnum_win(lnum, wp);
2970 if (wlnum > 0)
2971 changed_lines_buf(wp->w_buffer, wlnum,
2972 lnume - lnum + wlnum, 0L);
2973 }
2974 }
2975#endif
2976
2977 changed_common(lnum, col, lnume, xtra);
2978}
2979
2980 static void
2981changed_lines_buf(buf, lnum, lnume, xtra)
2982 buf_T *buf;
2983 linenr_T lnum; /* first line with change */
2984 linenr_T lnume; /* line below last changed line */
2985 long xtra; /* number of extra lines (negative when deleting) */
2986{
2987 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 {
2989 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002990 if (lnum < buf->b_mod_top)
2991 buf->b_mod_top = lnum;
2992 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 {
2994 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002995 buf->b_mod_bot += xtra;
2996 if (buf->b_mod_bot < lnum)
2997 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00002999 if (lnume + xtra > buf->b_mod_bot)
3000 buf->b_mod_bot = lnume + xtra;
3001 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 }
3003 else
3004 {
3005 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00003006 buf->b_mod_set = TRUE;
3007 buf->b_mod_top = lnum;
3008 buf->b_mod_bot = lnume + xtra;
3009 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011}
3012
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003013/*
3014 * Common code for when a change is was made.
3015 * See changed_lines() for the arguments.
3016 * Careful: may trigger autocommands that reload the buffer.
3017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 static void
3019changed_common(lnum, col, lnume, xtra)
3020 linenr_T lnum;
3021 colnr_T col;
3022 linenr_T lnume;
3023 long xtra;
3024{
3025 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003026#ifdef FEAT_WINDOWS
3027 tabpage_T *tp;
3028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 int i;
3030#ifdef FEAT_JUMPLIST
3031 int cols;
3032 pos_T *p;
3033 int add;
3034#endif
3035
3036 /* mark the buffer as modified */
3037 changed();
3038
3039 /* set the '. mark */
3040 if (!cmdmod.keepjumps)
3041 {
3042 curbuf->b_last_change.lnum = lnum;
3043 curbuf->b_last_change.col = col;
3044
3045#ifdef FEAT_JUMPLIST
3046 /* Create a new entry if a new undo-able change was started or we
3047 * don't have an entry yet. */
3048 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
3049 {
3050 if (curbuf->b_changelistlen == 0)
3051 add = TRUE;
3052 else
3053 {
3054 /* Don't create a new entry when the line number is the same
3055 * as the last one and the column is not too far away. Avoids
3056 * creating many entries for typing "xxxxx". */
3057 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
3058 if (p->lnum != lnum)
3059 add = TRUE;
3060 else
3061 {
3062 cols = comp_textwidth(FALSE);
3063 if (cols == 0)
3064 cols = 79;
3065 add = (p->col + cols < col || col + cols < p->col);
3066 }
3067 }
3068 if (add)
3069 {
3070 /* This is the first of a new sequence of undo-able changes
3071 * and it's at some distance of the last change. Use a new
3072 * position in the changelist. */
3073 curbuf->b_new_change = FALSE;
3074
3075 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3076 {
3077 /* changelist is full: remove oldest entry */
3078 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3079 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3080 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003081 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 {
3083 /* Correct position in changelist for other windows on
3084 * this buffer. */
3085 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3086 --wp->w_changelistidx;
3087 }
3088 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003089 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 {
3091 /* For other windows, if the position in the changelist is
3092 * at the end it stays at the end. */
3093 if (wp->w_buffer == curbuf
3094 && wp->w_changelistidx == curbuf->b_changelistlen)
3095 ++wp->w_changelistidx;
3096 }
3097 ++curbuf->b_changelistlen;
3098 }
3099 }
3100 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3101 curbuf->b_last_change;
3102 /* The current window is always after the last change, so that "g,"
3103 * takes you back to it. */
3104 curwin->w_changelistidx = curbuf->b_changelistlen;
3105#endif
3106 }
3107
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003108 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 {
3110 if (wp->w_buffer == curbuf)
3111 {
3112 /* Mark this window to be redrawn later. */
3113 if (wp->w_redr_type < VALID)
3114 wp->w_redr_type = VALID;
3115
3116 /* Check if a change in the buffer has invalidated the cached
3117 * values for the cursor. */
3118#ifdef FEAT_FOLDING
3119 /*
3120 * Update the folds for this window. Can't postpone this, because
3121 * a following operator might work on the whole fold: ">>dd".
3122 */
3123 foldUpdate(wp, lnum, lnume + xtra - 1);
3124
3125 /* The change may cause lines above or below the change to become
3126 * included in a fold. Set lnum/lnume to the first/last line that
3127 * might be displayed differently.
3128 * Set w_cline_folded here as an efficient way to update it when
3129 * inserting lines just above a closed fold. */
3130 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3131 if (wp->w_cursor.lnum == lnum)
3132 wp->w_cline_folded = i;
3133 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3134 if (wp->w_cursor.lnum == lnume)
3135 wp->w_cline_folded = i;
3136
3137 /* If the changed line is in a range of previously folded lines,
3138 * compare with the first line in that range. */
3139 if (wp->w_cursor.lnum <= lnum)
3140 {
3141 i = find_wl_entry(wp, lnum);
3142 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3143 changed_line_abv_curs_win(wp);
3144 }
3145#endif
3146
3147 if (wp->w_cursor.lnum > lnum)
3148 changed_line_abv_curs_win(wp);
3149 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3150 changed_cline_bef_curs_win(wp);
3151 if (wp->w_botline >= lnum)
3152 {
3153 /* Assume that botline doesn't change (inserted lines make
3154 * other lines scroll down below botline). */
3155 approximate_botline_win(wp);
3156 }
3157
3158 /* Check if any w_lines[] entries have become invalid.
3159 * For entries below the change: Correct the lnums for
3160 * inserted/deleted lines. Makes it possible to stop displaying
3161 * after the change. */
3162 for (i = 0; i < wp->w_lines_valid; ++i)
3163 if (wp->w_lines[i].wl_valid)
3164 {
3165 if (wp->w_lines[i].wl_lnum >= lnum)
3166 {
3167 if (wp->w_lines[i].wl_lnum < lnume)
3168 {
3169 /* line included in change */
3170 wp->w_lines[i].wl_valid = FALSE;
3171 }
3172 else if (xtra != 0)
3173 {
3174 /* line below change */
3175 wp->w_lines[i].wl_lnum += xtra;
3176#ifdef FEAT_FOLDING
3177 wp->w_lines[i].wl_lastlnum += xtra;
3178#endif
3179 }
3180 }
3181#ifdef FEAT_FOLDING
3182 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3183 {
3184 /* change somewhere inside this range of folded lines,
3185 * may need to be redrawn */
3186 wp->w_lines[i].wl_valid = FALSE;
3187 }
3188#endif
3189 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003190
3191#ifdef FEAT_FOLDING
3192 /* Take care of side effects for setting w_topline when folds have
3193 * changed. Esp. when the buffer was changed in another window. */
3194 if (hasAnyFolding(wp))
3195 set_topline(wp, wp->w_topline);
3196#endif
Bram Moolenaarfd859c92014-05-13 12:44:24 +02003197 /* relative numbering may require updating more */
3198 if (wp->w_p_rnu)
3199 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 }
3201 }
3202
3203 /* Call update_screen() later, which checks out what needs to be redrawn,
3204 * since it notices b_mod_set and then uses b_mod_*. */
3205 if (must_redraw < VALID)
3206 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003207
3208#ifdef FEAT_AUTOCMD
3209 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003210 if (lnum <= curwin->w_cursor.lnum
3211 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003212 last_cursormoved.lnum = 0;
3213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214}
3215
3216/*
3217 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3218 */
3219 void
3220unchanged(buf, ff)
3221 buf_T *buf;
3222 int ff; /* also reset 'fileformat' */
3223{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003224 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 {
3226 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003227 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 if (ff)
3229 save_file_ff(buf);
3230#ifdef FEAT_WINDOWS
3231 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003232 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233#endif
3234#ifdef FEAT_TITLE
3235 need_maketitle = TRUE; /* set window title later */
3236#endif
3237 }
3238 ++buf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239#ifdef FEAT_NETBEANS_INTG
3240 netbeans_unmodified(buf);
3241#endif
3242}
3243
3244#if defined(FEAT_WINDOWS) || defined(PROTO)
3245/*
3246 * check_status: called when the status bars for the buffer 'buf'
3247 * need to be updated
3248 */
3249 void
3250check_status(buf)
3251 buf_T *buf;
3252{
3253 win_T *wp;
3254
3255 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3256 if (wp->w_buffer == buf && wp->w_status_height)
3257 {
3258 wp->w_redr_status = TRUE;
3259 if (must_redraw < VALID)
3260 must_redraw = VALID;
3261 }
3262}
3263#endif
3264
3265/*
3266 * If the file is readonly, give a warning message with the first change.
3267 * Don't do this for autocommands.
3268 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003269 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003271 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 */
3273 void
3274change_warning(col)
3275 int col; /* column for message; non-zero when in insert
3276 mode and 'showmode' is on */
3277{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003278 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3279
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 if (curbuf->b_did_warn == FALSE
3281 && curbufIsChanged() == 0
3282#ifdef FEAT_AUTOCMD
3283 && !autocmd_busy
3284#endif
3285 && curbuf->b_p_ro)
3286 {
3287#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003288 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003290 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 if (!curbuf->b_p_ro)
3292 return;
3293#endif
3294 /*
3295 * Do what msg() does, but with a column offset if the warning should
3296 * be after the mode message.
3297 */
3298 msg_start();
3299 if (msg_row == Rows - 1)
3300 msg_col = col;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003301 msg_source(hl_attr(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003302 MSG_PUTS_ATTR(_(w_readonly), hl_attr(HLF_W) | MSG_HIST);
3303#ifdef FEAT_EVAL
3304 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 msg_clr_eos();
3307 (void)msg_end();
3308 if (msg_silent == 0 && !silent_mode)
3309 {
3310 out_flush();
3311 ui_delay(1000L, TRUE); /* give the user time to think about it */
3312 }
3313 curbuf->b_did_warn = TRUE;
3314 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3315 if (msg_row < Rows - 1)
3316 showmode();
3317 }
3318}
3319
3320/*
3321 * Ask for a reply from the user, a 'y' or a 'n'.
3322 * No other characters are accepted, the message is repeated until a valid
3323 * reply is entered or CTRL-C is hit.
3324 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3325 * from any buffers but directly from the user.
3326 *
3327 * return the 'y' or 'n'
3328 */
3329 int
3330ask_yesno(str, direct)
3331 char_u *str;
3332 int direct;
3333{
3334 int r = ' ';
3335 int save_State = State;
3336
3337 if (exiting) /* put terminal in raw mode for this question */
3338 settmode(TMODE_RAW);
3339 ++no_wait_return;
3340#ifdef USE_ON_FLY_SCROLL
3341 dont_scroll = TRUE; /* disallow scrolling here */
3342#endif
3343 State = CONFIRM; /* mouse behaves like with :confirm */
3344#ifdef FEAT_MOUSE
3345 setmouse(); /* disables mouse for xterm */
3346#endif
3347 ++no_mapping;
3348 ++allow_keys; /* no mapping here, but recognize keys */
3349
3350 while (r != 'y' && r != 'n')
3351 {
3352 /* same highlighting as for wait_return */
3353 smsg_attr(hl_attr(HLF_R), (char_u *)"%s (y/n)?", str);
3354 if (direct)
3355 r = get_keystroke();
3356 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003357 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 if (r == Ctrl_C || r == ESC)
3359 r = 'n';
3360 msg_putchar(r); /* show what you typed */
3361 out_flush();
3362 }
3363 --no_wait_return;
3364 State = save_State;
3365#ifdef FEAT_MOUSE
3366 setmouse();
3367#endif
3368 --no_mapping;
3369 --allow_keys;
3370
3371 return r;
3372}
3373
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003374#if defined(FEAT_MOUSE) || defined(PROTO)
3375/*
3376 * Return TRUE if "c" is a mouse key.
3377 */
3378 int
3379is_mouse_key(c)
3380 int c;
3381{
3382 return c == K_LEFTMOUSE
3383 || c == K_LEFTMOUSE_NM
3384 || c == K_LEFTDRAG
3385 || c == K_LEFTRELEASE
3386 || c == K_LEFTRELEASE_NM
3387 || c == K_MIDDLEMOUSE
3388 || c == K_MIDDLEDRAG
3389 || c == K_MIDDLERELEASE
3390 || c == K_RIGHTMOUSE
3391 || c == K_RIGHTDRAG
3392 || c == K_RIGHTRELEASE
3393 || c == K_MOUSEDOWN
3394 || c == K_MOUSEUP
3395 || c == K_MOUSELEFT
3396 || c == K_MOUSERIGHT
3397 || c == K_X1MOUSE
3398 || c == K_X1DRAG
3399 || c == K_X1RELEASE
3400 || c == K_X2MOUSE
3401 || c == K_X2DRAG
3402 || c == K_X2RELEASE;
3403}
3404#endif
3405
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406/*
3407 * Get a key stroke directly from the user.
3408 * Ignores mouse clicks and scrollbar events, except a click for the left
3409 * button (used at the more prompt).
3410 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3411 * Disadvantage: typeahead is ignored.
3412 * Translates the interrupt character for unix to ESC.
3413 */
3414 int
3415get_keystroke()
3416{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003417 char_u *buf = NULL;
3418 int buflen = 150;
3419 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 int len = 0;
3421 int n;
3422 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003423 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424
3425 mapped_ctrl_c = FALSE; /* mappings are not used here */
3426 for (;;)
3427 {
3428 cursor_on();
3429 out_flush();
3430
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003431 /* Leave some room for check_termcode() to insert a key code into (max
3432 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3433 * bytes. */
3434 maxlen = (buflen - 6 - len) / 3;
3435 if (buf == NULL)
3436 buf = alloc(buflen);
3437 else if (maxlen < 10)
3438 {
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003439 char_u *t_buf = buf;
3440
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003441 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003442 * escape sequence. */
3443 buflen += 100;
3444 buf = vim_realloc(buf, buflen);
Bram Moolenaar9abd5c62015-02-10 18:34:01 +01003445 if (buf == NULL)
3446 vim_free(t_buf);
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003447 maxlen = (buflen - 6 - len) / 3;
3448 }
3449 if (buf == NULL)
3450 {
3451 do_outofmem_msg((long_u)buflen);
3452 return ESC; /* panic! */
3453 }
3454
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003456 * terminal code to complete. */
3457 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 if (n > 0)
3459 {
3460 /* Replace zero and CSI by a special key code. */
3461 n = fix_input_buffer(buf + len, n, FALSE);
3462 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003463 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003465 else if (len > 0)
3466 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467
Bram Moolenaar4395a712006-09-05 18:57:57 +00003468 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003469 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003470 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003472
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003473 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003474 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003475 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003476 {
3477 /* Redrawing was postponed, do it now. */
3478 update_screen(0);
3479 setcursor(); /* put cursor back where it belongs */
3480 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003481 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003482 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003483 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003485 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 continue;
3487
3488 /* Handle modifier and/or special key code. */
3489 n = buf[0];
3490 if (n == K_SPECIAL)
3491 {
3492 n = TO_SPECIAL(buf[1], buf[2]);
3493 if (buf[1] == KS_MODIFIER
3494 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003495#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003496 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003497#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003498#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 || n == K_VER_SCROLLBAR
3500 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501#endif
3502 )
3503 {
3504 if (buf[1] == KS_MODIFIER)
3505 mod_mask = buf[2];
3506 len -= 3;
3507 if (len > 0)
3508 mch_memmove(buf, buf + 3, (size_t)len);
3509 continue;
3510 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003511 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 }
3513#ifdef FEAT_MBYTE
3514 if (has_mbyte)
3515 {
3516 if (MB_BYTE2LEN(n) > len)
3517 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003518 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 n = (*mb_ptr2char)(buf);
3520 }
3521#endif
3522#ifdef UNIX
3523 if (n == intr_char)
3524 n = ESC;
3525#endif
3526 break;
3527 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003528 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529
3530 mapped_ctrl_c = save_mapped_ctrl_c;
3531 return n;
3532}
3533
3534/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003535 * Get a number from the user.
3536 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 */
3538 int
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003539get_number(colon, mouse_used)
3540 int colon; /* allow colon to abort */
3541 int *mouse_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542{
3543 int n = 0;
3544 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003545 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003547 if (mouse_used != NULL)
3548 *mouse_used = FALSE;
3549
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 /* When not printing messages, the user won't know what to type, return a
3551 * zero (as if CR was hit). */
3552 if (msg_silent != 0)
3553 return 0;
3554
3555#ifdef USE_ON_FLY_SCROLL
3556 dont_scroll = TRUE; /* disallow scrolling here */
3557#endif
3558 ++no_mapping;
3559 ++allow_keys; /* no mapping here, but recognize keys */
3560 for (;;)
3561 {
3562 windgoto(msg_row, msg_col);
3563 c = safe_vgetc();
3564 if (VIM_ISDIGIT(c))
3565 {
3566 n = n * 10 + c - '0';
3567 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003568 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 }
3570 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3571 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003572 if (typed > 0)
3573 {
3574 MSG_PUTS("\b \b");
3575 --typed;
3576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003579#ifdef FEAT_MOUSE
3580 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3581 {
3582 *mouse_used = TRUE;
3583 n = mouse_row + 1;
3584 break;
3585 }
3586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 else if (n == 0 && c == ':' && colon)
3588 {
3589 stuffcharReadbuff(':');
3590 if (!exmode_active)
3591 cmdline_row = msg_row;
3592 skip_redraw = TRUE; /* skip redraw once */
3593 do_redraw = FALSE;
3594 break;
3595 }
3596 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3597 break;
3598 }
3599 --no_mapping;
3600 --allow_keys;
3601 return n;
3602}
3603
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003604/*
3605 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003606 * When "mouse_used" is not NULL allow using the mouse and in that case return
3607 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003608 */
3609 int
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003610prompt_for_number(mouse_used)
3611 int *mouse_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003612{
3613 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003614 int save_cmdline_row;
3615 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003616
3617 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003618 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003619 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003620 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003621 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003622
Bram Moolenaar203335e2006-09-03 14:35:42 +00003623 /* Set the state such that text can be selected/copied/pasted and we still
3624 * get mouse events. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003625 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003626 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003627 save_State = State;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003628 State = CMDLINE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003629
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003630 i = get_number(TRUE, mouse_used);
3631 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003632 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003633 /* don't call wait_return() now */
3634 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003635 cmdline_row = msg_row - 1;
3636 need_wait_return = FALSE;
3637 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003638 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003639 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003640 else
3641 cmdline_row = save_cmdline_row;
3642 State = save_State;
3643
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003644 return i;
3645}
3646
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 void
3648msgmore(n)
3649 long n;
3650{
3651 long pn;
3652
3653 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3655 return;
3656
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003657 /* We don't want to overwrite another important message, but do overwrite
3658 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3659 * then "put" reports the last action. */
3660 if (keep_msg != NULL && !keep_msg_more)
3661 return;
3662
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 if (n > 0)
3664 pn = n;
3665 else
3666 pn = -n;
3667
3668 if (pn > p_report)
3669 {
3670 if (pn == 1)
3671 {
3672 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003673 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3674 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003676 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3677 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 }
3679 else
3680 {
3681 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003682 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3683 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003685 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3686 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 }
3688 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003689 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 if (msg(msg_buf))
3691 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003692 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003693 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 }
3695 }
3696}
3697
3698/*
3699 * flush map and typeahead buffers and give a warning for an error
3700 */
3701 void
3702beep_flush()
3703{
3704 if (emsg_silent == 0)
3705 {
3706 flush_buffers(FALSE);
Bram Moolenaar165bc692015-07-21 17:53:25 +02003707 vim_beep(BO_ERROR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 }
3709}
3710
3711/*
Bram Moolenaar165bc692015-07-21 17:53:25 +02003712 * Give a warning for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 */
3714 void
Bram Moolenaar165bc692015-07-21 17:53:25 +02003715vim_beep(val)
3716 unsigned val; /* one of the BO_ values, e.g., BO_OPER */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717{
3718 if (emsg_silent == 0)
3719 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003720 if (!((bo_flags & val) || (bo_flags & BO_ALL)))
3721 {
3722 if (p_vb
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723#ifdef FEAT_GUI
Bram Moolenaar165bc692015-07-21 17:53:25 +02003724 /* While the GUI is starting up the termcap is set for the
3725 * GUI but the output still goes to a terminal. */
3726 && !(gui.in_use && gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727#endif
Bram Moolenaar165bc692015-07-21 17:53:25 +02003728 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02003730 out_str(T_VB);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 }
3732 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02003733 {
3734#ifdef MSDOS
3735 /*
3736 * The number of beeps outputted is reduced to avoid having to
3737 * wait for all the beeps to finish. This is only a problem on
3738 * systems where the beeps don't overlap.
3739 */
3740 if (beep_count == 0 || beep_count == 10)
3741 {
3742 out_char(BELL);
3743 beep_count = 1;
3744 }
3745 else
3746 ++beep_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747#else
Bram Moolenaar165bc692015-07-21 17:53:25 +02003748 out_char(BELL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749#endif
Bram Moolenaar165bc692015-07-21 17:53:25 +02003750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003752
3753 /* When 'verbose' is set and we are sourcing a script or executing a
3754 * function give the user a hint where the beep comes from. */
3755 if (vim_strchr(p_debug, 'e') != NULL)
3756 {
3757 msg_source(hl_attr(HLF_W));
3758 msg_attr((char_u *)_("Beep!"), hl_attr(HLF_W));
3759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 }
3761}
3762
3763/*
3764 * To get the "real" home directory:
3765 * - get value of $HOME
3766 * For Unix:
3767 * - go to that directory
3768 * - do mch_dirname() to get the real name of that directory.
3769 * This also works with mounts and links.
3770 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3771 */
3772static char_u *homedir = NULL;
3773
3774 void
3775init_homedir()
3776{
3777 char_u *var;
3778
Bram Moolenaar05159a02005-02-26 23:04:13 +00003779 /* In case we are called a second time (when 'encoding' changes). */
3780 vim_free(homedir);
3781 homedir = NULL;
3782
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783#ifdef VMS
3784 var = mch_getenv((char_u *)"SYS$LOGIN");
3785#else
3786 var = mch_getenv((char_u *)"HOME");
3787#endif
3788
3789 if (var != NULL && *var == NUL) /* empty is same as not set */
3790 var = NULL;
3791
3792#ifdef WIN3264
3793 /*
3794 * Weird but true: $HOME may contain an indirect reference to another
3795 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3796 * when $HOME is being set.
3797 */
3798 if (var != NULL && *var == '%')
3799 {
3800 char_u *p;
3801 char_u *exp;
3802
3803 p = vim_strchr(var + 1, '%');
3804 if (p != NULL)
3805 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003806 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 exp = mch_getenv(NameBuff);
3808 if (exp != NULL && *exp != NUL
3809 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3810 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003811 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 var = NameBuff;
3813 /* Also set $HOME, it's needed for _viminfo. */
3814 vim_setenv((char_u *)"HOME", NameBuff);
3815 }
3816 }
3817 }
3818
3819 /*
3820 * Typically, $HOME is not defined on Windows, unless the user has
3821 * specifically defined it for Vim's sake. However, on Windows NT
3822 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3823 * each user. Try constructing $HOME from these.
3824 */
3825 if (var == NULL)
3826 {
3827 char_u *homedrive, *homepath;
3828
3829 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3830 homepath = mch_getenv((char_u *)"HOMEPATH");
Bram Moolenaar6f977012010-01-06 17:53:38 +01003831 if (homepath == NULL || *homepath == NUL)
3832 homepath = "\\";
3833 if (homedrive != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3835 {
3836 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3837 if (NameBuff[0] != NUL)
3838 {
3839 var = NameBuff;
3840 /* Also set $HOME, it's needed for _viminfo. */
3841 vim_setenv((char_u *)"HOME", NameBuff);
3842 }
3843 }
3844 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003845
3846# if defined(FEAT_MBYTE)
3847 if (enc_utf8 && var != NULL)
3848 {
3849 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003850 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003851
3852 /* Convert from active codepage to UTF-8. Other conversions are
3853 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003854 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003855 if (pp != NULL)
3856 {
3857 homedir = pp;
3858 return;
3859 }
3860 }
3861# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862#endif
3863
Bram Moolenaare7fedb62015-12-31 19:07:19 +01003864#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 /*
3866 * Default home dir is C:/
3867 * Best assumption we can make in such a situation.
3868 */
3869 if (var == NULL)
3870 var = "C:/";
3871#endif
3872 if (var != NULL)
3873 {
3874#ifdef UNIX
3875 /*
3876 * Change to the directory and get the actual path. This resolves
3877 * links. Don't do it when we can't return.
3878 */
3879 if (mch_dirname(NameBuff, MAXPATHL) == OK
3880 && mch_chdir((char *)NameBuff) == 0)
3881 {
3882 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3883 var = IObuff;
3884 if (mch_chdir((char *)NameBuff) != 0)
3885 EMSG(_(e_prev_dir));
3886 }
3887#endif
3888 homedir = vim_strsave(var);
3889 }
3890}
3891
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003892#if defined(EXITFREE) || defined(PROTO)
3893 void
3894free_homedir()
3895{
3896 vim_free(homedir);
3897}
Bram Moolenaar24305862012-08-15 14:05:05 +02003898
3899# ifdef FEAT_CMDL_COMPL
3900 void
3901free_users()
3902{
3903 ga_clear_strings(&ga_users);
3904}
3905# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003906#endif
3907
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003909 * Call expand_env() and store the result in an allocated string.
3910 * This is not very memory efficient, this expects the result to be freed
3911 * again soon.
3912 */
3913 char_u *
3914expand_env_save(src)
3915 char_u *src;
3916{
3917 return expand_env_save_opt(src, FALSE);
3918}
3919
3920/*
3921 * Idem, but when "one" is TRUE handle the string as one file name, only
3922 * expand "~" at the start.
3923 */
3924 char_u *
3925expand_env_save_opt(src, one)
3926 char_u *src;
3927 int one;
3928{
3929 char_u *p;
3930
3931 p = alloc(MAXPATHL);
3932 if (p != NULL)
3933 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3934 return p;
3935}
3936
3937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 * Expand environment variable with path name.
3939 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003940 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 * If anything fails no expansion is done and dst equals src.
3942 */
3943 void
3944expand_env(src, dst, dstlen)
3945 char_u *src; /* input string e.g. "$HOME/vim.hlp" */
3946 char_u *dst; /* where to put the result */
3947 int dstlen; /* maximum length of the result */
3948{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003949 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950}
3951
3952 void
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003953expand_env_esc(srcp, dst, dstlen, esc, one, startstr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003954 char_u *srcp; /* input string e.g. "$HOME/vim.hlp" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 char_u *dst; /* where to put the result */
3956 int dstlen; /* maximum length of the result */
3957 int esc; /* escape spaces in expanded variables */
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003958 int one; /* "srcp" is one file name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003959 char_u *startstr; /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003961 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 char_u *tail;
3963 int c;
3964 char_u *var;
3965 int copy_char;
3966 int mustfree; /* var was allocated, need to free it later */
3967 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003968 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003970 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003971 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003972
3973 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 --dstlen; /* leave one char space for "\," */
3975 while (*src && dstlen > 0)
3976 {
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003977#ifdef FEAT_EVAL
3978 /* Skip over `=expr`. */
3979 if (src[0] == '`' && src[1] == '=')
3980 {
3981 size_t len;
3982
3983 var = src;
3984 src += 2;
3985 (void)skip_expr(&src);
3986 if (*src == '`')
3987 ++src;
3988 len = src - var;
3989 if (len > (size_t)dstlen)
3990 len = dstlen;
3991 vim_strncpy(dst, var, len);
3992 dst += len;
Bram Moolenaar5df1ed22015-09-01 16:25:34 +02003993 dstlen -= (int)len;
Bram Moolenaarbe83b732015-08-25 14:21:19 +02003994 continue;
3995 }
3996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003998 if ((*src == '$'
3999#ifdef VMS
4000 && at_start
4001#endif
4002 )
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004003#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 || *src == '%'
4005#endif
4006 || (*src == '~' && at_start))
4007 {
4008 mustfree = FALSE;
4009
4010 /*
4011 * The variable name is copied into dst temporarily, because it may
4012 * be a string in read-only memory and a NUL needs to be appended.
4013 */
4014 if (*src != '~') /* environment var */
4015 {
4016 tail = src + 1;
4017 var = dst;
4018 c = dstlen - 1;
4019
4020#ifdef UNIX
4021 /* Unix has ${var-name} type environment vars */
4022 if (*tail == '{' && !vim_isIDc('{'))
4023 {
4024 tail++; /* ignore '{' */
4025 while (c-- > 0 && *tail && *tail != '}')
4026 *var++ = *tail++;
4027 }
4028 else
4029#endif
4030 {
4031 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004032#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 || (*src == '%' && *tail != '%')
4034#endif
4035 ))
4036 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 *var++ = *tail++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 }
4039 }
4040
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004041#if defined(MSDOS) || defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042# ifdef UNIX
4043 if (src[1] == '{' && *tail != '}')
4044# else
4045 if (*src == '%' && *tail != '%')
4046# endif
4047 var = NULL;
4048 else
4049 {
4050# ifdef UNIX
4051 if (src[1] == '{')
4052# else
4053 if (*src == '%')
4054#endif
4055 ++tail;
4056#endif
4057 *var = NUL;
4058 var = vim_getenv(dst, &mustfree);
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004059#if defined(MSDOS) || defined(MSWIN) || defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 }
4061#endif
4062 }
4063 /* home directory */
4064 else if ( src[1] == NUL
4065 || vim_ispathsep(src[1])
4066 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
4067 {
4068 var = homedir;
4069 tail = src + 1;
4070 }
4071 else /* user directory */
4072 {
4073#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
4074 /*
4075 * Copy ~user to dst[], so we can put a NUL after it.
4076 */
4077 tail = src;
4078 var = dst;
4079 c = dstlen - 1;
4080 while ( c-- > 0
4081 && *tail
4082 && vim_isfilec(*tail)
4083 && !vim_ispathsep(*tail))
4084 *var++ = *tail++;
4085 *var = NUL;
4086# ifdef UNIX
4087 /*
4088 * If the system supports getpwnam(), use it.
4089 * Otherwise, or if getpwnam() fails, the shell is used to
4090 * expand ~user. This is slower and may fail if the shell
4091 * does not support ~user (old versions of /bin/sh).
4092 */
4093# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4094 {
4095 struct passwd *pw;
4096
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004097 /* Note: memory allocated by getpwnam() is never freed.
4098 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 pw = getpwnam((char *)dst + 1);
4100 if (pw != NULL)
4101 var = (char_u *)pw->pw_dir;
4102 else
4103 var = NULL;
4104 }
4105 if (var == NULL)
4106# endif
4107 {
4108 expand_T xpc;
4109
4110 ExpandInit(&xpc);
4111 xpc.xp_context = EXPAND_FILES;
4112 var = ExpandOne(&xpc, dst, NULL,
4113 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 mustfree = TRUE;
4115 }
4116
4117# else /* !UNIX, thus VMS */
4118 /*
4119 * USER_HOME is a comma-separated list of
4120 * directories to search for the user account in.
4121 */
4122 {
4123 char_u test[MAXPATHL], paths[MAXPATHL];
4124 char_u *path, *next_path, *ptr;
4125 struct stat st;
4126
4127 STRCPY(paths, USER_HOME);
4128 next_path = paths;
4129 while (*next_path)
4130 {
4131 for (path = next_path; *next_path && *next_path != ',';
4132 next_path++);
4133 if (*next_path)
4134 *next_path++ = NUL;
4135 STRCPY(test, path);
4136 STRCAT(test, "/");
4137 STRCAT(test, dst + 1);
4138 if (mch_stat(test, &st) == 0)
4139 {
4140 var = alloc(STRLEN(test) + 1);
4141 STRCPY(var, test);
4142 mustfree = TRUE;
4143 break;
4144 }
4145 }
4146 }
4147# endif /* UNIX */
4148#else
4149 /* cannot expand user's home directory, so don't try */
4150 var = NULL;
4151 tail = (char_u *)""; /* for gcc */
4152#endif /* UNIX || VMS */
4153 }
4154
4155#ifdef BACKSLASH_IN_FILENAME
4156 /* If 'shellslash' is set change backslashes to forward slashes.
4157 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4158 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4159 {
4160 char_u *p = vim_strsave(var);
4161
4162 if (p != NULL)
4163 {
4164 if (mustfree)
4165 vim_free(var);
4166 var = p;
4167 mustfree = TRUE;
4168 forward_slash(var);
4169 }
4170 }
4171#endif
4172
4173 /* If "var" contains white space, escape it with a backslash.
4174 * Required for ":e ~/tt" when $HOME includes a space. */
4175 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4176 {
4177 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4178
4179 if (p != NULL)
4180 {
4181 if (mustfree)
4182 vim_free(var);
4183 var = p;
4184 mustfree = TRUE;
4185 }
4186 }
4187
4188 if (var != NULL && *var != NUL
4189 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4190 {
4191 STRCPY(dst, var);
4192 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004193 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 /* if var[] ends in a path separator and tail[] starts
4195 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004196 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4198 && dst[-1] != ':'
4199#endif
4200 && vim_ispathsep(*tail))
4201 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004202 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 src = tail;
4204 copy_char = FALSE;
4205 }
4206 if (mustfree)
4207 vim_free(var);
4208 }
4209
4210 if (copy_char) /* copy at least one char */
4211 {
4212 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004213 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004214 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4215 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 */
4217 at_start = FALSE;
4218 if (src[0] == '\\' && src[1] != NUL)
4219 {
4220 *dst++ = *src++;
4221 --dstlen;
4222 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004223 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 at_start = TRUE;
4225 *dst++ = *src++;
4226 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004227
4228 if (startstr != NULL && src - startstr_len >= srcp
4229 && STRNCMP(src - startstr_len, startstr, startstr_len) == 0)
4230 at_start = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 }
4232 }
4233 *dst = NUL;
4234}
4235
4236/*
4237 * Vim's version of getenv().
4238 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004239 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004240 * "mustfree" is set to TRUE when returned is allocated, it must be
4241 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 */
4243 char_u *
4244vim_getenv(name, mustfree)
4245 char_u *name;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004246 int *mustfree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247{
4248 char_u *p;
4249 char_u *pend;
4250 int vimruntime;
4251
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004252#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 /* use "C:/" when $HOME is not set */
4254 if (STRCMP(name, "HOME") == 0)
4255 return homedir;
4256#endif
4257
4258 p = mch_getenv(name);
4259 if (p != NULL && *p == NUL) /* empty is the same as not set */
4260 p = NULL;
4261
4262 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004263 {
4264#if defined(FEAT_MBYTE) && defined(WIN3264)
4265 if (enc_utf8)
4266 {
4267 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004268 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004269
4270 /* Convert from active codepage to UTF-8. Other conversions are
4271 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004272 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004273 if (pp != NULL)
4274 {
4275 p = pp;
4276 *mustfree = TRUE;
4277 }
4278 }
4279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282
4283 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4284 if (!vimruntime && STRCMP(name, "VIM") != 0)
4285 return NULL;
4286
4287 /*
4288 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4289 * Don't do this when default_vimruntime_dir is non-empty.
4290 */
4291 if (vimruntime
4292#ifdef HAVE_PATHDEF
4293 && *default_vimruntime_dir == NUL
4294#endif
4295 )
4296 {
4297 p = mch_getenv((char_u *)"VIM");
4298 if (p != NULL && *p == NUL) /* empty is the same as not set */
4299 p = NULL;
4300 if (p != NULL)
4301 {
4302 p = vim_version_dir(p);
4303 if (p != NULL)
4304 *mustfree = TRUE;
4305 else
4306 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004307
4308#if defined(FEAT_MBYTE) && defined(WIN3264)
4309 if (enc_utf8)
4310 {
4311 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004312 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004313
4314 /* Convert from active codepage to UTF-8. Other conversions
4315 * are not done, because they would fail for non-ASCII
4316 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004317 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004318 if (pp != NULL)
4319 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004320 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004321 vim_free(p);
4322 p = pp;
4323 *mustfree = TRUE;
4324 }
4325 }
4326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 }
4328 }
4329
4330 /*
4331 * When expanding $VIM or $VIMRUNTIME fails, try using:
4332 * - the directory name from 'helpfile' (unless it contains '$')
4333 * - the executable name from argv[0]
4334 */
4335 if (p == NULL)
4336 {
4337 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4338 p = p_hf;
4339#ifdef USE_EXE_NAME
4340 /*
4341 * Use the name of the executable, obtained from argv[0].
4342 */
4343 else
4344 p = exe_name;
4345#endif
4346 if (p != NULL)
4347 {
4348 /* remove the file name */
4349 pend = gettail(p);
4350
4351 /* remove "doc/" from 'helpfile', if present */
4352 if (p == p_hf)
4353 pend = remove_tail(p, pend, (char_u *)"doc");
4354
4355#ifdef USE_EXE_NAME
4356# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004357 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 if (p == exe_name)
4359 {
4360 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004361 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004363 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4364 if (pend1 != pend)
4365 {
4366 pnew = alloc((unsigned)(pend1 - p) + 15);
4367 if (pnew != NULL)
4368 {
4369 STRNCPY(pnew, p, (pend1 - p));
4370 STRCPY(pnew + (pend1 - p), "Resources/vim");
4371 p = pnew;
4372 pend = p + STRLEN(p);
4373 }
4374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 }
4376# endif
4377 /* remove "src/" from exe_name, if present */
4378 if (p == exe_name)
4379 pend = remove_tail(p, pend, (char_u *)"src");
4380#endif
4381
4382 /* for $VIM, remove "runtime/" or "vim54/", if present */
4383 if (!vimruntime)
4384 {
4385 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4386 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4387 }
4388
4389 /* remove trailing path separator */
4390#ifndef MACOS_CLASSIC
4391 /* With MacOS path (with colons) the final colon is required */
Bram Moolenaare21877a2008-02-13 09:58:14 +00004392 /* to avoid confusion between absolute and relative path */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004393 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 --pend;
4395#endif
4396
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004397#ifdef MACOS_X
4398 if (p == exe_name || p == p_hf)
4399#endif
4400 /* check that the result is a directory name */
4401 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402
4403 if (p != NULL && !mch_isdir(p))
4404 {
4405 vim_free(p);
4406 p = NULL;
4407 }
4408 else
4409 {
4410#ifdef USE_EXE_NAME
4411 /* may add "/vim54" or "/runtime" if it exists */
4412 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4413 {
4414 vim_free(p);
4415 p = pend;
4416 }
4417#endif
4418 *mustfree = TRUE;
4419 }
4420 }
4421 }
4422
4423#ifdef HAVE_PATHDEF
4424 /* When there is a pathdef.c file we can use default_vim_dir and
4425 * default_vimruntime_dir */
4426 if (p == NULL)
4427 {
4428 /* Only use default_vimruntime_dir when it is not empty */
4429 if (vimruntime && *default_vimruntime_dir != NUL)
4430 {
4431 p = default_vimruntime_dir;
4432 *mustfree = FALSE;
4433 }
4434 else if (*default_vim_dir != NUL)
4435 {
4436 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4437 *mustfree = TRUE;
4438 else
4439 {
4440 p = default_vim_dir;
4441 *mustfree = FALSE;
4442 }
4443 }
4444 }
4445#endif
4446
4447 /*
4448 * Set the environment variable, so that the new value can be found fast
4449 * next time, and others can also use it (e.g. Perl).
4450 */
4451 if (p != NULL)
4452 {
4453 if (vimruntime)
4454 {
4455 vim_setenv((char_u *)"VIMRUNTIME", p);
4456 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 }
4458 else
4459 {
4460 vim_setenv((char_u *)"VIM", p);
4461 didset_vim = TRUE;
4462 }
4463 }
4464 return p;
4465}
4466
4467/*
4468 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4469 * Return NULL if not, return its name in allocated memory otherwise.
4470 */
4471 static char_u *
4472vim_version_dir(vimdir)
4473 char_u *vimdir;
4474{
4475 char_u *p;
4476
4477 if (vimdir == NULL || *vimdir == NUL)
4478 return NULL;
4479 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4480 if (p != NULL && mch_isdir(p))
4481 return p;
4482 vim_free(p);
4483 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4484 if (p != NULL && mch_isdir(p))
4485 return p;
4486 vim_free(p);
4487 return NULL;
4488}
4489
4490/*
4491 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4492 * the length of "name/". Otherwise return "pend".
4493 */
4494 static char_u *
4495remove_tail(p, pend, name)
4496 char_u *p;
4497 char_u *pend;
4498 char_u *name;
4499{
4500 int len = (int)STRLEN(name) + 1;
4501 char_u *newend = pend - len;
4502
4503 if (newend >= p
4504 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004505 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 return newend;
4507 return pend;
4508}
4509
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 * Our portable version of setenv.
4512 */
4513 void
4514vim_setenv(name, val)
4515 char_u *name;
4516 char_u *val;
4517{
4518#ifdef HAVE_SETENV
4519 mch_setenv((char *)name, (char *)val, 1);
4520#else
4521 char_u *envbuf;
4522
4523 /*
4524 * Putenv does not copy the string, it has to remain
4525 * valid. The allocated memory will never be freed.
4526 */
4527 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4528 if (envbuf != NULL)
4529 {
4530 sprintf((char *)envbuf, "%s=%s", name, val);
4531 putenv((char *)envbuf);
4532 }
4533#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004534#ifdef FEAT_GETTEXT
4535 /*
4536 * When setting $VIMRUNTIME adjust the directory to find message
4537 * translations to $VIMRUNTIME/lang.
4538 */
4539 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4540 {
4541 char_u *buf = concat_str(val, (char_u *)"/lang");
4542
4543 if (buf != NULL)
4544 {
4545 bindtextdomain(VIMPACKAGE, (char *)buf);
4546 vim_free(buf);
4547 }
4548 }
4549#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550}
4551
4552#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4553/*
4554 * Function given to ExpandGeneric() to obtain an environment variable name.
4555 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 char_u *
4557get_env_name(xp, idx)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004558 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 int idx;
4560{
4561# if defined(AMIGA) || defined(__MRC__) || defined(__SC__)
4562 /*
4563 * No environ[] on the Amiga and on the Mac (using MPW).
4564 */
4565 return NULL;
4566# else
4567# ifndef __WIN32__
4568 /* Borland C++ 5.2 has this in a header file. */
4569 extern char **environ;
4570# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004571# define ENVNAMELEN 100
4572 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 char_u *str;
4574 int n;
4575
4576 str = (char_u *)environ[idx];
4577 if (str == NULL)
4578 return NULL;
4579
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004580 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 {
4582 if (str[n] == '=' || str[n] == NUL)
4583 break;
4584 name[n] = str[n];
4585 }
4586 name[n] = NUL;
4587 return name;
4588# endif
4589}
Bram Moolenaar24305862012-08-15 14:05:05 +02004590
4591/*
4592 * Find all user names for user completion.
4593 * Done only once and then cached.
4594 */
4595 static void
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004596init_users()
4597{
Bram Moolenaar24305862012-08-15 14:05:05 +02004598 static int lazy_init_done = FALSE;
4599
4600 if (lazy_init_done)
4601 return;
4602
4603 lazy_init_done = TRUE;
4604 ga_init2(&ga_users, sizeof(char_u *), 20);
4605
4606# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4607 {
4608 char_u* user;
4609 struct passwd* pw;
4610
4611 setpwent();
4612 while ((pw = getpwent()) != NULL)
4613 /* pw->pw_name shouldn't be NULL but just in case... */
4614 if (pw->pw_name != NULL)
4615 {
4616 if (ga_grow(&ga_users, 1) == FAIL)
4617 break;
4618 user = vim_strsave((char_u*)pw->pw_name);
4619 if (user == NULL)
4620 break;
4621 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4622 }
4623 endpwent();
4624 }
4625# endif
4626}
4627
4628/*
4629 * Function given to ExpandGeneric() to obtain an user names.
4630 */
4631 char_u*
4632get_users(xp, idx)
4633 expand_T *xp UNUSED;
4634 int idx;
4635{
4636 init_users();
4637 if (idx < ga_users.ga_len)
4638 return ((char_u **)ga_users.ga_data)[idx];
4639 return NULL;
4640}
4641
4642/*
4643 * Check whether name matches a user name. Return:
4644 * 0 if name does not match any user name.
4645 * 1 if name partially matches the beginning of a user name.
4646 * 2 is name fully matches a user name.
4647 */
4648int match_user(name)
4649 char_u* name;
4650{
4651 int i;
4652 int n = (int)STRLEN(name);
4653 int result = 0;
4654
4655 init_users();
4656 for (i = 0; i < ga_users.ga_len; i++)
4657 {
4658 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4659 return 2; /* full match */
4660 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4661 result = 1; /* partial match */
4662 }
4663 return result;
4664}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665#endif
4666
4667/*
4668 * Replace home directory by "~" in each space or comma separated file name in
4669 * 'src'.
4670 * If anything fails (except when out of space) dst equals src.
4671 */
4672 void
4673home_replace(buf, src, dst, dstlen, one)
4674 buf_T *buf; /* when not NULL, check for help files */
4675 char_u *src; /* input file name */
4676 char_u *dst; /* where to put the result */
4677 int dstlen; /* maximum length of the result */
4678 int one; /* if TRUE, only replace one file name, include
4679 spaces and commas in the file name. */
4680{
4681 size_t dirlen = 0, envlen = 0;
4682 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004683 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 char_u *p;
4685
4686 if (src == NULL)
4687 {
4688 *dst = NUL;
4689 return;
4690 }
4691
4692 /*
4693 * If the file is a help file, remove the path completely.
4694 */
4695 if (buf != NULL && buf->b_help)
4696 {
4697 STRCPY(dst, gettail(src));
4698 return;
4699 }
4700
4701 /*
4702 * We check both the value of the $HOME environment variable and the
4703 * "real" home directory.
4704 */
4705 if (homedir != NULL)
4706 dirlen = STRLEN(homedir);
4707
4708#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004709 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004711 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4712#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004713 /* Empty is the same as not set. */
4714 if (homedir_env != NULL && *homedir_env == NUL)
4715 homedir_env = NULL;
4716
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004717#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004718 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004719 {
4720 int usedlen = 0;
4721 int flen;
4722 char_u *fbuf = NULL;
4723
4724 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004725 (void)modify_fname((char_u *)":p", &usedlen,
4726 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004727 flen = (int)STRLEN(homedir_env);
4728 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4729 /* Remove the trailing / that is added to a directory. */
4730 homedir_env[flen - 1] = NUL;
4731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732#endif
4733
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 if (homedir_env != NULL)
4735 envlen = STRLEN(homedir_env);
4736
4737 if (!one)
4738 src = skipwhite(src);
4739 while (*src && dstlen > 0)
4740 {
4741 /*
4742 * Here we are at the beginning of a file name.
4743 * First, check to see if the beginning of the file name matches
4744 * $HOME or the "real" home directory. Check that there is a '/'
4745 * after the match (so that if e.g. the file is "/home/pieter/bla",
4746 * and the home directory is "/home/piet", the file does not end up
4747 * as "~er/bla" (which would seem to indicate the file "bla" in user
4748 * er's home directory)).
4749 */
4750 p = homedir;
4751 len = dirlen;
4752 for (;;)
4753 {
4754 if ( len
4755 && fnamencmp(src, p, len) == 0
4756 && (vim_ispathsep(src[len])
4757 || (!one && (src[len] == ',' || src[len] == ' '))
4758 || src[len] == NUL))
4759 {
4760 src += len;
4761 if (--dstlen > 0)
4762 *dst++ = '~';
4763
4764 /*
4765 * If it's just the home directory, add "/".
4766 */
4767 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4768 *dst++ = '/';
4769 break;
4770 }
4771 if (p == homedir_env)
4772 break;
4773 p = homedir_env;
4774 len = envlen;
4775 }
4776
4777 /* if (!one) skip to separator: space or comma */
4778 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4779 *dst++ = *src++;
4780 /* skip separator */
4781 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4782 *dst++ = *src++;
4783 }
4784 /* if (dstlen == 0) out of space, what to do??? */
4785
4786 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004787
4788 if (homedir_env != homedir_env_orig)
4789 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790}
4791
4792/*
4793 * Like home_replace, store the replaced string in allocated memory.
4794 * When something fails, NULL is returned.
4795 */
4796 char_u *
4797home_replace_save(buf, src)
4798 buf_T *buf; /* when not NULL, check for help files */
4799 char_u *src; /* input file name */
4800{
4801 char_u *dst;
4802 unsigned len;
4803
4804 len = 3; /* space for "~/" and trailing NUL */
4805 if (src != NULL) /* just in case */
4806 len += (unsigned)STRLEN(src);
4807 dst = alloc(len);
4808 if (dst != NULL)
4809 home_replace(buf, src, dst, len, TRUE);
4810 return dst;
4811}
4812
4813/*
4814 * Compare two file names and return:
4815 * FPC_SAME if they both exist and are the same file.
4816 * FPC_SAMEX if they both don't exist and have the same file name.
4817 * FPC_DIFF if they both exist and are different files.
4818 * FPC_NOTX if they both don't exist.
4819 * FPC_DIFFX if one of them doesn't exist.
4820 * For the first name environment variables are expanded
4821 */
4822 int
4823fullpathcmp(s1, s2, checkname)
4824 char_u *s1, *s2;
4825 int checkname; /* when both don't exist, check file names */
4826{
4827#ifdef UNIX
4828 char_u exp1[MAXPATHL];
4829 char_u full1[MAXPATHL];
4830 char_u full2[MAXPATHL];
4831 struct stat st1, st2;
4832 int r1, r2;
4833
4834 expand_env(s1, exp1, MAXPATHL);
4835 r1 = mch_stat((char *)exp1, &st1);
4836 r2 = mch_stat((char *)s2, &st2);
4837 if (r1 != 0 && r2 != 0)
4838 {
4839 /* if mch_stat() doesn't work, may compare the names */
4840 if (checkname)
4841 {
4842 if (fnamecmp(exp1, s2) == 0)
4843 return FPC_SAMEX;
4844 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4845 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4846 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4847 return FPC_SAMEX;
4848 }
4849 return FPC_NOTX;
4850 }
4851 if (r1 != 0 || r2 != 0)
4852 return FPC_DIFFX;
4853 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4854 return FPC_SAME;
4855 return FPC_DIFF;
4856#else
4857 char_u *exp1; /* expanded s1 */
4858 char_u *full1; /* full path of s1 */
4859 char_u *full2; /* full path of s2 */
4860 int retval = FPC_DIFF;
4861 int r1, r2;
4862
4863 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4864 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4865 {
4866 full1 = exp1 + MAXPATHL;
4867 full2 = full1 + MAXPATHL;
4868
4869 expand_env(s1, exp1, MAXPATHL);
4870 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4871 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4872
4873 /* If vim_FullName() fails, the file probably doesn't exist. */
4874 if (r1 != OK && r2 != OK)
4875 {
4876 if (checkname && fnamecmp(exp1, s2) == 0)
4877 retval = FPC_SAMEX;
4878 else
4879 retval = FPC_NOTX;
4880 }
4881 else if (r1 != OK || r2 != OK)
4882 retval = FPC_DIFFX;
4883 else if (fnamecmp(full1, full2))
4884 retval = FPC_DIFF;
4885 else
4886 retval = FPC_SAME;
4887 vim_free(exp1);
4888 }
4889 return retval;
4890#endif
4891}
4892
4893/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004894 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004895 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004896 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 */
4898 char_u *
4899gettail(fname)
4900 char_u *fname;
4901{
4902 char_u *p1, *p2;
4903
4904 if (fname == NULL)
4905 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01004906 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01004908 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 p1 = p2 + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004910 mb_ptr_adv(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 }
4912 return p1;
4913}
4914
Bram Moolenaar31710262010-08-13 13:36:15 +02004915#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01004916static char_u *gettail_dir(char_u *fname);
Bram Moolenaar31710262010-08-13 13:36:15 +02004917
4918/*
4919 * Return the end of the directory name, on the first path
4920 * separator:
4921 * "/path/file", "/path/dir/", "/path//dir", "/file"
4922 * ^ ^ ^ ^
4923 */
4924 static char_u *
4925gettail_dir(fname)
4926 char_u *fname;
4927{
4928 char_u *dir_end = fname;
4929 char_u *next_dir_end = fname;
4930 int look_for_sep = TRUE;
4931 char_u *p;
4932
4933 for (p = fname; *p != NUL; )
4934 {
4935 if (vim_ispathsep(*p))
4936 {
4937 if (look_for_sep)
4938 {
4939 next_dir_end = p;
4940 look_for_sep = FALSE;
4941 }
4942 }
4943 else
4944 {
4945 if (!look_for_sep)
4946 dir_end = next_dir_end;
4947 look_for_sep = TRUE;
4948 }
4949 mb_ptr_adv(p);
4950 }
4951 return dir_end;
4952}
4953#endif
4954
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004956 * Get pointer to tail of "fname", including path separators. Putting a NUL
4957 * here leaves the directory name. Takes care of "c:/" and "//".
4958 * Always returns a valid pointer.
4959 */
4960 char_u *
4961gettail_sep(fname)
4962 char_u *fname;
4963{
4964 char_u *p;
4965 char_u *t;
4966
4967 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4968 t = gettail(fname);
4969 while (t > p && after_pathsep(fname, t))
4970 --t;
4971#ifdef VMS
4972 /* path separator is part of the path */
4973 ++t;
4974#endif
4975 return t;
4976}
4977
4978/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 * get the next path component (just after the next path separator).
4980 */
4981 char_u *
4982getnextcomp(fname)
4983 char_u *fname;
4984{
4985 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004986 mb_ptr_adv(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 if (*fname)
4988 ++fname;
4989 return fname;
4990}
4991
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992/*
4993 * Get a pointer to one character past the head of a path name.
4994 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4995 * If there is no head, path is returned.
4996 */
4997 char_u *
4998get_past_head(path)
4999 char_u *path;
5000{
5001 char_u *retval;
5002
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005003#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 /* may skip "c:" */
5005 if (isalpha(path[0]) && path[1] == ':')
5006 retval = path + 2;
5007 else
5008 retval = path;
5009#else
5010# if defined(AMIGA)
5011 /* may skip "label:" */
5012 retval = vim_strchr(path, ':');
5013 if (retval == NULL)
5014 retval = path;
5015# else /* Unix */
5016 retval = path;
5017# endif
5018#endif
5019
5020 while (vim_ispathsep(*retval))
5021 ++retval;
5022
5023 return retval;
5024}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025
5026/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01005027 * Return TRUE if 'c' is a path separator.
5028 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 */
5030 int
5031vim_ispathsep(c)
5032 int c;
5033{
Bram Moolenaare60acc12011-05-10 16:41:25 +02005034#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02005036#else
5037# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005039# else
5040# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
5042 return (c == ':' || c == '[' || c == ']' || c == '/'
5043 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02005044# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02005046# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02005048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049}
5050
Bram Moolenaar69c35002013-11-04 02:54:12 +01005051/*
5052 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
5053 */
5054 int
5055vim_ispathsep_nocolon(c)
5056 int c;
5057{
5058 return vim_ispathsep(c)
5059#ifdef BACKSLASH_IN_FILENAME
5060 && c != ':'
5061#endif
5062 ;
5063}
5064
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5066/*
5067 * return TRUE if 'c' is a path list separator.
5068 */
5069 int
5070vim_ispathlistsep(c)
5071 int c;
5072{
5073#ifdef UNIX
5074 return (c == ':');
5075#else
Bram Moolenaar25394022007-05-10 19:06:20 +00005076 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077#endif
5078}
5079#endif
5080
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005081#if defined(FEAT_GUI_TABLINE) || defined(FEAT_WINDOWS) \
5082 || defined(FEAT_EVAL) || defined(PROTO)
5083/*
5084 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
5085 * It's done in-place.
5086 */
5087 void
5088shorten_dir(str)
5089 char_u *str;
5090{
5091 char_u *tail, *s, *d;
5092 int skip = FALSE;
5093
5094 tail = gettail(str);
5095 d = str;
5096 for (s = str; ; ++s)
5097 {
5098 if (s >= tail) /* copy the whole tail */
5099 {
5100 *d++ = *s;
5101 if (*s == NUL)
5102 break;
5103 }
5104 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5105 {
5106 *d++ = *s;
5107 skip = FALSE;
5108 }
5109 else if (!skip)
5110 {
5111 *d++ = *s; /* copy next char */
5112 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5113 skip = TRUE;
5114# ifdef FEAT_MBYTE
5115 if (has_mbyte)
5116 {
5117 int l = mb_ptr2len(s);
5118
5119 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005120 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005121 }
5122# endif
5123 }
5124 }
5125}
5126#endif
5127
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005128/*
5129 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5130 * Also returns TRUE if there is no directory name.
5131 * "fname" must be writable!.
5132 */
5133 int
5134dir_of_file_exists(fname)
5135 char_u *fname;
5136{
5137 char_u *p;
5138 int c;
5139 int retval;
5140
5141 p = gettail_sep(fname);
5142 if (p == fname)
5143 return TRUE;
5144 c = *p;
5145 *p = NUL;
5146 retval = mch_isdir(fname);
5147 *p = c;
5148 return retval;
5149}
5150
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005152 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5153 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 */
5155 int
5156vim_fnamecmp(x, y)
5157 char_u *x, *y;
5158{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005159#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005161#else
5162 if (p_fic)
5163 return MB_STRICMP(x, y);
5164 return STRCMP(x, y);
5165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166}
5167
5168 int
5169vim_fnamencmp(x, y, len)
5170 char_u *x, *y;
5171 size_t len;
5172{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005173#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005174 char_u *px = x;
5175 char_u *py = y;
5176 int cx = NUL;
5177 int cy = NUL;
5178
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005179 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005181 cx = PTR2CHAR(px);
5182 cy = PTR2CHAR(py);
5183 if (cx == NUL || cy == NUL
5184 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5185 && !(cx == '/' && cy == '\\')
5186 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005188 len -= MB_PTR2LEN(px);
5189 px += MB_PTR2LEN(px);
5190 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 }
5192 if (len == 0)
5193 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005194 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005195#else
5196 if (p_fic)
5197 return MB_STRNICMP(x, y, len);
5198 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005200}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201
5202/*
5203 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005204 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 */
5206 char_u *
5207concat_fnames(fname1, fname2, sep)
5208 char_u *fname1;
5209 char_u *fname2;
5210 int sep;
5211{
5212 char_u *dest;
5213
5214 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5215 if (dest != NULL)
5216 {
5217 STRCPY(dest, fname1);
5218 if (sep)
5219 add_pathsep(dest);
5220 STRCAT(dest, fname2);
5221 }
5222 return dest;
5223}
5224
Bram Moolenaard6754642005-01-17 22:18:45 +00005225/*
5226 * Concatenate two strings and return the result in allocated memory.
5227 * Returns NULL when out of memory.
5228 */
5229 char_u *
5230concat_str(str1, str2)
5231 char_u *str1;
5232 char_u *str2;
5233{
5234 char_u *dest;
5235 size_t l = STRLEN(str1);
5236
5237 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5238 if (dest != NULL)
5239 {
5240 STRCPY(dest, str1);
5241 STRCPY(dest + l, str2);
5242 }
5243 return dest;
5244}
Bram Moolenaard6754642005-01-17 22:18:45 +00005245
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246/*
5247 * Add a path separator to a file name, unless it already ends in a path
5248 * separator.
5249 */
5250 void
5251add_pathsep(p)
5252 char_u *p;
5253{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005254 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 STRCAT(p, PATHSEPSTR);
5256}
5257
5258/*
5259 * FullName_save - Make an allocated copy of a full file name.
5260 * Returns NULL when out of memory.
5261 */
5262 char_u *
5263FullName_save(fname, force)
5264 char_u *fname;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005265 int force; /* force expansion, even when it already looks
5266 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267{
5268 char_u *buf;
5269 char_u *new_fname = NULL;
5270
5271 if (fname == NULL)
5272 return NULL;
5273
5274 buf = alloc((unsigned)MAXPATHL);
5275 if (buf != NULL)
5276 {
5277 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5278 new_fname = vim_strsave(buf);
5279 else
5280 new_fname = vim_strsave(fname);
5281 vim_free(buf);
5282 }
5283 return new_fname;
5284}
5285
5286#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5287
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005288static char_u *skip_string(char_u *p);
5289static pos_T *ind_find_start_comment(void);
5290static pos_T *ind_find_start_CORS(void);
5291static pos_T *find_start_rawstring(int ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292
5293/*
5294 * Find the start of a comment, not knowing if we are in a comment right now.
5295 * Search starts at w_cursor.lnum and goes backwards.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005296 * Return NULL when not inside a comment.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005298 static pos_T *
5299ind_find_start_comment() /* XXX */
5300{
5301 return find_start_comment(curbuf->b_ind_maxcomment);
5302}
5303
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 pos_T *
5305find_start_comment(ind_maxcomment) /* XXX */
5306 int ind_maxcomment;
5307{
5308 pos_T *pos;
5309 char_u *line;
5310 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005311 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005313 for (;;)
5314 {
5315 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5316 if (pos == NULL)
5317 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005319 /*
5320 * Check if the comment start we found is inside a string.
5321 * If it is then restrict the search to below this line and try again.
5322 */
5323 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005324 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005325 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005326 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005327 break;
5328 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5329 if (cur_maxcomment <= 0)
5330 {
5331 pos = NULL;
5332 break;
5333 }
5334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 return pos;
5336}
5337
5338/*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005339 * Find the start of a comment or raw string, not knowing if we are in a
5340 * comment or raw string right now.
5341 * Search starts at w_cursor.lnum and goes backwards.
5342 * Return NULL when not inside a comment or raw string.
5343 * "CORS" -> Comment Or Raw String
5344 */
5345 static pos_T *
5346ind_find_start_CORS() /* XXX */
5347{
Bram Moolenaar089af182015-10-07 11:41:49 +02005348 static pos_T comment_pos_copy;
5349 pos_T *comment_pos;
5350 pos_T *rs_pos;
5351
5352 comment_pos = find_start_comment(curbuf->b_ind_maxcomment);
5353 if (comment_pos != NULL)
5354 {
5355 /* Need to make a copy of the static pos in findmatchlimit(),
5356 * calling find_start_rawstring() may change it. */
5357 comment_pos_copy = *comment_pos;
5358 comment_pos = &comment_pos_copy;
5359 }
5360 rs_pos = find_start_rawstring(curbuf->b_ind_maxcomment);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005361
5362 /* If comment_pos is before rs_pos the raw string is inside the comment.
5363 * If rs_pos is before comment_pos the comment is inside the raw string. */
5364 if (comment_pos == NULL || (rs_pos != NULL && lt(*rs_pos, *comment_pos)))
5365 return rs_pos;
5366 return comment_pos;
5367}
5368
5369/*
5370 * Find the start of a raw string, not knowing if we are in one right now.
5371 * Search starts at w_cursor.lnum and goes backwards.
5372 * Return NULL when not inside a raw string.
5373 */
5374 static pos_T *
5375find_start_rawstring(ind_maxcomment) /* XXX */
5376 int ind_maxcomment;
5377{
5378 pos_T *pos;
5379 char_u *line;
5380 char_u *p;
5381 int cur_maxcomment = ind_maxcomment;
5382
5383 for (;;)
5384 {
5385 pos = findmatchlimit(NULL, 'R', FM_BACKWARD, cur_maxcomment);
5386 if (pos == NULL)
5387 break;
5388
5389 /*
5390 * Check if the raw string start we found is inside a string.
5391 * If it is then restrict the search to below this line and try again.
5392 */
5393 line = ml_get(pos->lnum);
5394 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
5395 p = skip_string(p);
5396 if ((colnr_T)(p - line) <= pos->col)
5397 break;
5398 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5399 if (cur_maxcomment <= 0)
5400 {
5401 pos = NULL;
5402 break;
5403 }
5404 }
5405 return pos;
5406}
5407
5408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 * Skip to the end of a "string" and a 'c' character.
5410 * If there is no string or character, return argument unmodified.
5411 */
5412 static char_u *
5413skip_string(p)
5414 char_u *p;
5415{
5416 int i;
5417
5418 /*
5419 * We loop, because strings may be concatenated: "date""time".
5420 */
5421 for ( ; ; ++p)
5422 {
5423 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5424 {
5425 if (!p[1]) /* ' at end of line */
5426 break;
5427 i = 2;
5428 if (p[1] == '\\') /* '\n' or '\000' */
5429 {
5430 ++i;
5431 while (vim_isdigit(p[i - 1])) /* '\000' */
5432 ++i;
5433 }
5434 if (p[i] == '\'') /* check for trailing ' */
5435 {
5436 p += i;
5437 continue;
5438 }
5439 }
5440 else if (p[0] == '"') /* start of string */
5441 {
5442 for (++p; p[0]; ++p)
5443 {
5444 if (p[0] == '\\' && p[1] != NUL)
5445 ++p;
5446 else if (p[0] == '"') /* end of string */
5447 break;
5448 }
5449 if (p[0] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005450 continue; /* continue for another string */
5451 }
5452 else if (p[0] == 'R' && p[1] == '"')
5453 {
5454 /* Raw string: R"[delim](...)[delim]" */
5455 char_u *delim = p + 2;
5456 char_u *paren = vim_strchr(delim, '(');
5457
5458 if (paren != NULL)
5459 {
5460 size_t delim_len = paren - delim;
5461
5462 for (p += 3; *p; ++p)
5463 if (p[0] == ')' && STRNCMP(p + 1, delim, delim_len) == 0
5464 && p[delim_len + 1] == '"')
5465 {
5466 p += delim_len + 1;
5467 break;
5468 }
5469 if (p[0] == '"')
5470 continue; /* continue for another string */
5471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 }
5473 break; /* no string found */
5474 }
5475 if (!*p)
5476 --p; /* backup from NUL */
5477 return p;
5478}
5479#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5480
5481#if defined(FEAT_CINDENT) || defined(PROTO)
5482
5483/*
5484 * Do C or expression indenting on the current line.
5485 */
5486 void
5487do_c_expr_indent()
5488{
5489# ifdef FEAT_EVAL
5490 if (*curbuf->b_p_inde != NUL)
5491 fixthisline(get_expr_indent);
5492 else
5493# endif
5494 fixthisline(get_c_indent);
5495}
5496
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02005497/* Find result cache for cpp_baseclass */
5498typedef struct {
5499 int found;
5500 lpos_T lpos;
5501} cpp_baseclass_cache_T;
5502
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503/*
5504 * Functions for C-indenting.
5505 * Most of this originally comes from Eric Fischer.
5506 */
5507/*
5508 * Below "XXX" means that this function may unlock the current line.
5509 */
5510
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01005511static char_u *cin_skipcomment(char_u *);
5512static int cin_nocode(char_u *);
5513static pos_T *find_line_comment(void);
5514static int cin_has_js_key(char_u *text);
5515static int cin_islabel_skip(char_u **);
5516static int cin_isdefault(char_u *);
5517static char_u *after_label(char_u *l);
5518static int get_indent_nolabel(linenr_T lnum);
5519static int skip_label(linenr_T, char_u **pp);
5520static int cin_first_id_amount(void);
5521static int cin_get_equal_amount(linenr_T lnum);
5522static int cin_ispreproc(char_u *);
5523static int cin_ispreproc_cont(char_u **pp, linenr_T *lnump);
5524static int cin_iscomment(char_u *);
5525static int cin_islinecomment(char_u *);
5526static int cin_isterminated(char_u *, int, int);
5527static int cin_isinit(void);
5528static int cin_isfuncdecl(char_u **, linenr_T, linenr_T);
5529static int cin_isif(char_u *);
5530static int cin_iselse(char_u *);
5531static int cin_isdo(char_u *);
5532static int cin_iswhileofdo(char_u *, linenr_T);
5533static int cin_is_if_for_while_before_offset(char_u *line, int *poffset);
5534static int cin_iswhileofdo_end(int terminated);
5535static int cin_isbreak(char_u *);
5536static int cin_is_cpp_baseclass(cpp_baseclass_cache_T *cached);
5537static int get_baseclass_amount(int col);
5538static int cin_ends_in(char_u *, char_u *, char_u *);
5539static int cin_starts_with(char_u *s, char *word);
5540static int cin_skip2pos(pos_T *trypos);
5541static pos_T *find_start_brace(void);
5542static pos_T *find_match_paren(int);
5543static pos_T *find_match_char(int c, int ind_maxparen);
5544static int corr_ind_maxparen(pos_T *startpos);
5545static int find_last_paren(char_u *l, int start, int end);
5546static int find_match(int lookfor, linenr_T ourscope);
5547static int cin_is_cpp_namespace(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548
5549/*
5550 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005551 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 */
5553 static char_u *
5554cin_skipcomment(s)
5555 char_u *s;
5556{
5557 while (*s)
5558 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005559 char_u *prev_s = s;
5560
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005562
5563 /* Perl/shell # comment comment continues until eol. Require a space
5564 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005565 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005566 {
5567 s += STRLEN(s);
5568 break;
5569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 if (*s != '/')
5571 break;
5572 ++s;
5573 if (*s == '/') /* slash-slash comment continues till eol */
5574 {
5575 s += STRLEN(s);
5576 break;
5577 }
5578 if (*s != '*')
5579 break;
5580 for (++s; *s; ++s) /* skip slash-star comment */
5581 if (s[0] == '*' && s[1] == '/')
5582 {
5583 s += 2;
5584 break;
5585 }
5586 }
5587 return s;
5588}
5589
5590/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005591 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 * not considered code.
5593 */
5594 static int
5595cin_nocode(s)
5596 char_u *s;
5597{
5598 return *cin_skipcomment(s) == NUL;
5599}
5600
5601/*
5602 * Check previous lines for a "//" line comment, skipping over blank lines.
5603 */
5604 static pos_T *
5605find_line_comment() /* XXX */
5606{
5607 static pos_T pos;
5608 char_u *line;
5609 char_u *p;
5610
5611 pos = curwin->w_cursor;
5612 while (--pos.lnum > 0)
5613 {
5614 line = ml_get(pos.lnum);
5615 p = skipwhite(line);
5616 if (cin_islinecomment(p))
5617 {
5618 pos.col = (int)(p - line);
5619 return &pos;
5620 }
5621 if (*p != NUL)
5622 break;
5623 }
5624 return NULL;
5625}
5626
5627/*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005628 * Return TRUE if "text" starts with "key:".
5629 */
5630 static int
5631cin_has_js_key(text)
5632 char_u *text;
5633{
5634 char_u *s = skipwhite(text);
Bram Moolenaarece29e82014-08-06 12:49:18 +02005635 int quote = -1;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005636
5637 if (*s == '\'' || *s == '"')
5638 {
5639 /* can be 'key': or "key": */
5640 quote = *s;
5641 ++s;
5642 }
5643 if (!vim_isIDc(*s)) /* need at least one ID character */
5644 return FALSE;
5645
5646 while (vim_isIDc(*s))
5647 ++s;
5648 if (*s == quote)
5649 ++s;
5650
5651 s = cin_skipcomment(s);
5652
5653 /* "::" is not a label, it's C++ */
5654 return (*s == ':' && s[1] != ':');
5655}
5656
5657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 * Check if string matches "label:"; move to character after ':' if true.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02005659 * "*s" must point to the start of the label, if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 */
5661 static int
5662cin_islabel_skip(s)
5663 char_u **s;
5664{
5665 if (!vim_isIDc(**s)) /* need at least one ID character */
5666 return FALSE;
5667
5668 while (vim_isIDc(**s))
5669 (*s)++;
5670
5671 *s = cin_skipcomment(*s);
5672
5673 /* "::" is not a label, it's C++ */
5674 return (**s == ':' && *++*s != ':');
5675}
5676
5677/*
5678 * Recognize a label: "label:".
5679 * Note: curwin->w_cursor must be where we are looking for the label.
5680 */
5681 int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005682cin_islabel() /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683{
5684 char_u *s;
5685
5686 s = cin_skipcomment(ml_get_curline());
5687
5688 /*
5689 * Exclude "default" from labels, since it should be indented
5690 * like a switch label. Same for C++ scope declarations.
5691 */
5692 if (cin_isdefault(s))
5693 return FALSE;
5694 if (cin_isscopedecl(s))
5695 return FALSE;
5696
5697 if (cin_islabel_skip(&s))
5698 {
5699 /*
5700 * Only accept a label if the previous line is terminated or is a case
5701 * label.
5702 */
5703 pos_T cursor_save;
5704 pos_T *trypos;
5705 char_u *line;
5706
5707 cursor_save = curwin->w_cursor;
5708 while (curwin->w_cursor.lnum > 1)
5709 {
5710 --curwin->w_cursor.lnum;
5711
5712 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005713 * If we're in a comment or raw string now, skip to the start of
5714 * it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 */
5716 curwin->w_cursor.col = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02005717 if ((trypos = ind_find_start_CORS()) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 curwin->w_cursor = *trypos;
5719
5720 line = ml_get_curline();
5721 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5722 continue;
5723 if (*(line = cin_skipcomment(line)) == NUL)
5724 continue;
5725
5726 curwin->w_cursor = cursor_save;
5727 if (cin_isterminated(line, TRUE, FALSE)
5728 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005729 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 || (cin_islabel_skip(&line) && cin_nocode(line)))
5731 return TRUE;
5732 return FALSE;
5733 }
5734 curwin->w_cursor = cursor_save;
5735 return TRUE; /* label at start of file??? */
5736 }
5737 return FALSE;
5738}
5739
5740/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005741 * Recognize structure initialization and enumerations:
5742 * "[typedef] [static|public|protected|private] enum"
5743 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 */
5745 static int
5746cin_isinit(void)
5747{
5748 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005749 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750
5751 s = cin_skipcomment(ml_get_curline());
5752
Bram Moolenaar75342212013-03-07 13:13:52 +01005753 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 s = cin_skipcomment(s + 7);
5755
Bram Moolenaar75342212013-03-07 13:13:52 +01005756 for (;;)
5757 {
5758 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005759
Bram Moolenaar75342212013-03-07 13:13:52 +01005760 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5761 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005762 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005763 if (cin_starts_with(s, skip[i]))
5764 {
5765 s = cin_skipcomment(s + l);
5766 l = 0;
5767 break;
5768 }
5769 }
5770 if (l != 0)
5771 break;
5772 }
5773
5774 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 return TRUE;
5776
5777 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5778 return TRUE;
5779
5780 return FALSE;
5781}
5782
5783/*
5784 * Recognize a switch label: "case .*:" or "default:".
5785 */
5786 int
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005787cin_iscase(s, strict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 char_u *s;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005789 int strict; /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790{
5791 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005792 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 {
5794 for (s += 4; *s; ++s)
5795 {
5796 s = cin_skipcomment(s);
5797 if (*s == ':')
5798 {
5799 if (s[1] == ':') /* skip over "::" for C++ */
5800 ++s;
5801 else
5802 return TRUE;
5803 }
5804 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005805 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5807 return FALSE; /* stop at comment */
5808 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005809 {
5810 /* JS etc. */
5811 if (strict)
5812 return FALSE; /* stop at string */
5813 else
5814 return TRUE;
5815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 }
5817 return FALSE;
5818 }
5819
5820 if (cin_isdefault(s))
5821 return TRUE;
5822 return FALSE;
5823}
5824
5825/*
5826 * Recognize a "default" switch label.
5827 */
5828 static int
5829cin_isdefault(s)
5830 char_u *s;
5831{
5832 return (STRNCMP(s, "default", 7) == 0
5833 && *(s = cin_skipcomment(s + 7)) == ':'
5834 && s[1] != ':');
5835}
5836
5837/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005838 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 */
5840 int
5841cin_isscopedecl(s)
5842 char_u *s;
5843{
5844 int i;
5845
5846 s = cin_skipcomment(s);
5847 if (STRNCMP(s, "public", 6) == 0)
5848 i = 6;
5849 else if (STRNCMP(s, "protected", 9) == 0)
5850 i = 9;
5851 else if (STRNCMP(s, "private", 7) == 0)
5852 i = 7;
5853 else
5854 return FALSE;
5855 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5856}
5857
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005858/* Maximum number of lines to search back for a "namespace" line. */
5859#define FIND_NAMESPACE_LIM 20
5860
5861/*
5862 * Recognize a "namespace" scope declaration.
5863 */
5864 static int
5865cin_is_cpp_namespace(s)
5866 char_u *s;
5867{
5868 char_u *p;
5869 int has_name = FALSE;
5870
5871 s = cin_skipcomment(s);
5872 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5873 {
5874 p = cin_skipcomment(skipwhite(s + 9));
5875 while (*p != NUL)
5876 {
5877 if (vim_iswhite(*p))
5878 {
5879 has_name = TRUE; /* found end of a name */
5880 p = cin_skipcomment(skipwhite(p));
5881 }
5882 else if (*p == '{')
5883 {
5884 break;
5885 }
5886 else if (vim_iswordc(*p))
5887 {
5888 if (has_name)
5889 return FALSE; /* word character after skipping past name */
5890 ++p;
5891 }
5892 else
5893 {
5894 return FALSE;
5895 }
5896 }
5897 return TRUE;
5898 }
5899 return FALSE;
5900}
5901
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902/*
5903 * Return a pointer to the first non-empty non-comment character after a ':'.
5904 * Return NULL if not found.
5905 * case 234: a = b;
5906 * ^
5907 */
5908 static char_u *
5909after_label(l)
5910 char_u *l;
5911{
5912 for ( ; *l; ++l)
5913 {
5914 if (*l == ':')
5915 {
5916 if (l[1] == ':') /* skip over "::" for C++ */
5917 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005918 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 break;
5920 }
5921 else if (*l == '\'' && l[1] && l[2] == '\'')
5922 l += 2; /* skip over 'x' */
5923 }
5924 if (*l == NUL)
5925 return NULL;
5926 l = cin_skipcomment(l + 1);
5927 if (*l == NUL)
5928 return NULL;
5929 return l;
5930}
5931
5932/*
5933 * Get indent of line "lnum", skipping a label.
5934 * Return 0 if there is nothing after the label.
5935 */
5936 static int
5937get_indent_nolabel(lnum) /* XXX */
5938 linenr_T lnum;
5939{
5940 char_u *l;
5941 pos_T fp;
5942 colnr_T col;
5943 char_u *p;
5944
5945 l = ml_get(lnum);
5946 p = after_label(l);
5947 if (p == NULL)
5948 return 0;
5949
5950 fp.col = (colnr_T)(p - l);
5951 fp.lnum = lnum;
5952 getvcol(curwin, &fp, &col, NULL, NULL);
5953 return (int)col;
5954}
5955
5956/*
5957 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005958 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 * label: if (asdf && asdfasdf)
5960 * ^
5961 */
5962 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005963skip_label(lnum, pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 linenr_T lnum;
5965 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966{
5967 char_u *l;
5968 int amount;
5969 pos_T cursor_save;
5970
5971 cursor_save = curwin->w_cursor;
5972 curwin->w_cursor.lnum = lnum;
5973 l = ml_get_curline();
5974 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005975 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 {
5977 amount = get_indent_nolabel(lnum);
5978 l = after_label(ml_get_curline());
5979 if (l == NULL) /* just in case */
5980 l = ml_get_curline();
5981 }
5982 else
5983 {
5984 amount = get_indent();
5985 l = ml_get_curline();
5986 }
5987 *pp = l;
5988
5989 curwin->w_cursor = cursor_save;
5990 return amount;
5991}
5992
5993/*
5994 * Return the indent of the first variable name after a type in a declaration.
5995 * int a, indent of "a"
5996 * static struct foo b, indent of "b"
5997 * enum bla c, indent of "c"
5998 * Returns zero when it doesn't look like a declaration.
5999 */
6000 static int
6001cin_first_id_amount()
6002{
6003 char_u *line, *p, *s;
6004 int len;
6005 pos_T fp;
6006 colnr_T col;
6007
6008 line = ml_get_curline();
6009 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006010 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 if (len == 6 && STRNCMP(p, "static", 6) == 0)
6012 {
6013 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006014 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 }
6016 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
6017 p = skipwhite(p + 6);
6018 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
6019 p = skipwhite(p + 4);
6020 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
6021 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
6022 {
6023 s = skipwhite(p + len);
6024 if ((STRNCMP(s, "int", 3) == 0 && vim_iswhite(s[3]))
6025 || (STRNCMP(s, "long", 4) == 0 && vim_iswhite(s[4]))
6026 || (STRNCMP(s, "short", 5) == 0 && vim_iswhite(s[5]))
6027 || (STRNCMP(s, "char", 4) == 0 && vim_iswhite(s[4])))
6028 p = s;
6029 }
6030 for (len = 0; vim_isIDc(p[len]); ++len)
6031 ;
6032 if (len == 0 || !vim_iswhite(p[len]) || cin_nocode(p))
6033 return 0;
6034
6035 p = skipwhite(p + len);
6036 fp.lnum = curwin->w_cursor.lnum;
6037 fp.col = (colnr_T)(p - line);
6038 getvcol(curwin, &fp, &col, NULL, NULL);
6039 return (int)col;
6040}
6041
6042/*
6043 * Return the indent of the first non-blank after an equal sign.
6044 * char *foo = "here";
6045 * Return zero if no (useful) equal sign found.
6046 * Return -1 if the line above "lnum" ends in a backslash.
6047 * foo = "asdf\
6048 * asdf\
6049 * here";
6050 */
6051 static int
6052cin_get_equal_amount(lnum)
6053 linenr_T lnum;
6054{
6055 char_u *line;
6056 char_u *s;
6057 colnr_T col;
6058 pos_T fp;
6059
6060 if (lnum > 1)
6061 {
6062 line = ml_get(lnum - 1);
6063 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
6064 return -1;
6065 }
6066
6067 line = s = ml_get(lnum);
6068 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
6069 {
6070 if (cin_iscomment(s)) /* ignore comments */
6071 s = cin_skipcomment(s);
6072 else
6073 ++s;
6074 }
6075 if (*s != '=')
6076 return 0;
6077
6078 s = skipwhite(s + 1);
6079 if (cin_nocode(s))
6080 return 0;
6081
6082 if (*s == '"') /* nice alignment for continued strings */
6083 ++s;
6084
6085 fp.lnum = lnum;
6086 fp.col = (colnr_T)(s - line);
6087 getvcol(curwin, &fp, &col, NULL, NULL);
6088 return (int)col;
6089}
6090
6091/*
6092 * Recognize a preprocessor statement: Any line that starts with '#'.
6093 */
6094 static int
6095cin_ispreproc(s)
6096 char_u *s;
6097{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006098 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 return TRUE;
6100 return FALSE;
6101}
6102
6103/*
6104 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
6105 * continuation line of a preprocessor statement. Decrease "*lnump" to the
6106 * start and return the line in "*pp".
6107 */
6108 static int
6109cin_ispreproc_cont(pp, lnump)
6110 char_u **pp;
6111 linenr_T *lnump;
6112{
6113 char_u *line = *pp;
6114 linenr_T lnum = *lnump;
6115 int retval = FALSE;
6116
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00006117 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 {
6119 if (cin_ispreproc(line))
6120 {
6121 retval = TRUE;
6122 *lnump = lnum;
6123 break;
6124 }
6125 if (lnum == 1)
6126 break;
6127 line = ml_get(--lnum);
6128 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
6129 break;
6130 }
6131
6132 if (lnum != *lnump)
6133 *pp = ml_get(*lnump);
6134 return retval;
6135}
6136
6137/*
6138 * Recognize the start of a C or C++ comment.
6139 */
6140 static int
6141cin_iscomment(p)
6142 char_u *p;
6143{
6144 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
6145}
6146
6147/*
6148 * Recognize the start of a "//" comment.
6149 */
6150 static int
6151cin_islinecomment(p)
6152 char_u *p;
6153{
6154 return (p[0] == '/' && p[1] == '/');
6155}
6156
6157/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006158 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
6159 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02006161 * If a line begins with an "else", only consider it terminated if no unmatched
6162 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 * Return the character terminating the line (ending char's have precedence if
6164 * both apply in order to determine initializations).
6165 */
6166 static int
6167cin_isterminated(s, incl_open, incl_comma)
6168 char_u *s;
6169 int incl_open; /* include '{' at the end as terminator */
6170 int incl_comma; /* recognize a trailing comma */
6171{
Bram Moolenaar496f9512011-05-19 16:35:09 +02006172 char_u found_start = 0;
6173 unsigned n_open = 0;
6174 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175
6176 s = cin_skipcomment(s);
6177
6178 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
6179 found_start = *s;
6180
Bram Moolenaar496f9512011-05-19 16:35:09 +02006181 if (!found_start)
6182 is_else = cin_iselse(s);
6183
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 while (*s)
6185 {
6186 /* skip over comments, "" strings and 'c'haracters */
6187 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006188 if (*s == '}' && n_open > 0)
6189 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02006190 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006191 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 && cin_nocode(s + 1))
6193 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02006194 else if (*s == '{')
6195 {
6196 if (incl_open && cin_nocode(s + 1))
6197 return *s;
6198 else
6199 ++n_open;
6200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201
6202 if (*s)
6203 s++;
6204 }
6205 return found_start;
6206}
6207
6208/*
6209 * Recognize the basic picture of a function declaration -- it needs to
6210 * have an open paren somewhere and a close paren at the end of the line and
6211 * no semicolons anywhere.
6212 * When a line ends in a comma we continue looking in the next line.
6213 * "sp" points to a string with the line. When looking at other lines it must
6214 * be restored to the line. When it's NULL fetch lines here.
6215 * "lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006216 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 */
6218 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006219cin_isfuncdecl(sp, first_lnum, min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 char_u **sp;
6221 linenr_T first_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006222 linenr_T min_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223{
6224 char_u *s;
6225 linenr_T lnum = first_lnum;
6226 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006227 pos_T *trypos;
6228 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229
6230 if (sp == NULL)
6231 s = ml_get(lnum);
6232 else
6233 s = *sp;
6234
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006235 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006236 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006237 {
6238 lnum = trypos->lnum;
6239 if (lnum < min_lnum)
6240 return FALSE;
6241
6242 s = ml_get(lnum);
6243 }
6244
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006245 /* Ignore line starting with #. */
6246 if (cin_ispreproc(s))
6247 return FALSE;
6248
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6250 {
6251 if (cin_iscomment(s)) /* ignore comments */
6252 s = cin_skipcomment(s);
Bram Moolenaare01f4f82015-11-10 14:06:53 +01006253 else if (*s == ':')
6254 {
6255 if (*(s + 1) == ':')
6256 s += 2;
6257 else
6258 /* To avoid a mistake in the following situation:
6259 * A::A(int a, int b)
6260 * : a(0) // <--not a function decl
6261 * , b(0)
6262 * {...
6263 */
6264 return FALSE;
6265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 else
6267 ++s;
6268 }
6269 if (*s != '(')
6270 return FALSE; /* ';', ' or " before any () or no '(' */
6271
6272 while (*s && *s != ';' && *s != '\'' && *s != '"')
6273 {
6274 if (*s == ')' && cin_nocode(s + 1))
6275 {
6276 /* ')' at the end: may have found a match
6277 * Check for he previous line not to end in a backslash:
6278 * #if defined(x) && \
6279 * defined(y)
6280 */
6281 lnum = first_lnum - 1;
6282 s = ml_get(lnum);
6283 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6284 retval = TRUE;
6285 goto done;
6286 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006287 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006289 int comma = (*s == ',');
6290
6291 /* ',' at the end: continue looking in the next line.
6292 * At the end: check for ',' in the next line, for this style:
6293 * func(arg1
6294 * , arg2) */
6295 for (;;)
6296 {
6297 if (lnum >= curbuf->b_ml.ml_line_count)
6298 break;
6299 s = ml_get(++lnum);
6300 if (!cin_ispreproc(s))
6301 break;
6302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 if (lnum >= curbuf->b_ml.ml_line_count)
6304 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006305 /* Require a comma at end of the line or a comma or ')' at the
6306 * start of next line. */
6307 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006308 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006309 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006310 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 }
6312 else if (cin_iscomment(s)) /* ignore comments */
6313 s = cin_skipcomment(s);
6314 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006315 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006317 just_started = FALSE;
6318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 }
6320
6321done:
6322 if (lnum != first_lnum && sp != NULL)
6323 *sp = ml_get(first_lnum);
6324
6325 return retval;
6326}
6327
6328 static int
6329cin_isif(p)
6330 char_u *p;
6331{
6332 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
6333}
6334
6335 static int
6336cin_iselse(p)
6337 char_u *p;
6338{
6339 if (*p == '}') /* accept "} else" */
6340 p = cin_skipcomment(p + 1);
6341 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6342}
6343
6344 static int
6345cin_isdo(p)
6346 char_u *p;
6347{
6348 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6349}
6350
6351/*
6352 * Check if this is a "while" that should have a matching "do".
6353 * We only accept a "while (condition) ;", with only white space between the
6354 * ')' and ';'. The condition may be spread over several lines.
6355 */
6356 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006357cin_iswhileofdo(p, lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 char_u *p;
6359 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360{
6361 pos_T cursor_save;
6362 pos_T *trypos;
6363 int retval = FALSE;
6364
6365 p = cin_skipcomment(p);
6366 if (*p == '}') /* accept "} while (cond);" */
6367 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006368 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 {
6370 cursor_save = curwin->w_cursor;
6371 curwin->w_cursor.lnum = lnum;
6372 curwin->w_cursor.col = 0;
6373 p = ml_get_curline();
6374 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6375 {
6376 ++p;
6377 ++curwin->w_cursor.col;
6378 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006379 if ((trypos = findmatchlimit(NULL, 0, 0,
6380 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6382 retval = TRUE;
6383 curwin->w_cursor = cursor_save;
6384 }
6385 return retval;
6386}
6387
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006388/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006389 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006390 * Return 0 if there is none.
6391 * Otherwise return !0 and update "*poffset" to point to the place where the
6392 * string was found.
6393 */
6394 static int
Bram Moolenaarb345d492012-04-09 20:42:26 +02006395cin_is_if_for_while_before_offset(line, poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006396 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006397 int *poffset;
6398{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006399 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006400
6401 if (offset-- < 2)
6402 return 0;
6403 while (offset > 2 && vim_iswhite(line[offset]))
6404 --offset;
6405
6406 offset -= 1;
6407 if (!STRNCMP(line + offset, "if", 2))
6408 goto probablyFound;
6409
6410 if (offset >= 1)
6411 {
6412 offset -= 1;
6413 if (!STRNCMP(line + offset, "for", 3))
6414 goto probablyFound;
6415
6416 if (offset >= 2)
6417 {
6418 offset -= 2;
6419 if (!STRNCMP(line + offset, "while", 5))
6420 goto probablyFound;
6421 }
6422 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006423 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006424
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006425probablyFound:
6426 if (!offset || !vim_isIDc(line[offset - 1]))
6427 {
6428 *poffset = offset;
6429 return 1;
6430 }
6431 return 0;
6432}
6433
6434/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006435 * Return TRUE if we are at the end of a do-while.
6436 * do
6437 * nothing;
6438 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006439 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006440 * Adjust the cursor to the line with "while".
6441 */
6442 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006443cin_iswhileofdo_end(terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006444 int terminated;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006445{
6446 char_u *line;
6447 char_u *p;
6448 char_u *s;
6449 pos_T *trypos;
6450 int i;
6451
6452 if (terminated != ';') /* there must be a ';' at the end */
6453 return FALSE;
6454
6455 p = line = ml_get_curline();
6456 while (*p != NUL)
6457 {
6458 p = cin_skipcomment(p);
6459 if (*p == ')')
6460 {
6461 s = skipwhite(p + 1);
6462 if (*s == ';' && cin_nocode(s + 1))
6463 {
6464 /* Found ");" at end of the line, now check there is "while"
6465 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006466 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006467 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006468 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006469 if (trypos != NULL)
6470 {
6471 s = cin_skipcomment(ml_get(trypos->lnum));
6472 if (*s == '}') /* accept "} while (cond);" */
6473 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006474 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006475 {
6476 curwin->w_cursor.lnum = trypos->lnum;
6477 return TRUE;
6478 }
6479 }
6480
6481 /* Searching may have made "line" invalid, get it again. */
6482 line = ml_get_curline();
6483 p = line + i;
6484 }
6485 }
6486 if (*p != NUL)
6487 ++p;
6488 }
6489 return FALSE;
6490}
6491
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 static int
6493cin_isbreak(p)
6494 char_u *p;
6495{
6496 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6497}
6498
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006499/*
6500 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 * constructor-initialization. eg:
6502 *
6503 * class MyClass :
6504 * baseClass <-- here
6505 * class MyClass : public baseClass,
6506 * anotherBaseClass <-- here (should probably lineup ??)
6507 * MyClass::MyClass(...) :
6508 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006509 *
6510 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 */
6512 static int
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006513cin_is_cpp_baseclass(cached)
6514 cpp_baseclass_cache_T *cached; /* input and output */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515{
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006516 lpos_T *pos = &cached->lpos; /* find position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 char_u *s;
6518 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006519 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006520 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006522 if (pos->lnum <= lnum)
6523 return cached->found; /* Use the cached result */
6524
6525 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006527 s = skipwhite(line);
6528 if (*s == '#') /* skip #define FOO x ? (x) : x */
6529 return FALSE;
6530 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 if (*s == NUL)
6532 return FALSE;
6533
6534 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6535
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006536 /* Search for a line starting with '#', empty, ending in ';' or containing
6537 * '{' or '}' and start below it. This handles the following situations:
6538 * a = cond ?
6539 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006540 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006541 * func::foo()
6542 * : something
6543 * {}
6544 * Foo::Foo (int one, int two)
6545 * : something(4),
6546 * somethingelse(3)
6547 * {}
6548 */
6549 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006551 line = ml_get(lnum - 1);
6552 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006553 if (*s == '#' || *s == NUL)
6554 break;
6555 while (*s != NUL)
6556 {
6557 s = cin_skipcomment(s);
6558 if (*s == '{' || *s == '}'
6559 || (*s == ';' && cin_nocode(s + 1)))
6560 break;
6561 if (*s != NUL)
6562 ++s;
6563 }
6564 if (*s != NUL)
6565 break;
6566 --lnum;
6567 }
6568
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006569 pos->lnum = lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006570 line = ml_get(lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006571 s = line;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006572 for (;;)
6573 {
6574 if (*s == NUL)
6575 {
6576 if (lnum == curwin->w_cursor.lnum)
6577 break;
6578 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006579 line = ml_get(++lnum);
Bram Moolenaard1b15de2015-10-13 16:13:39 +02006580 s = line;
6581 }
6582 if (s == line)
6583 {
6584 /* don't recognize "case (foo):" as a baseclass */
6585 if (cin_iscase(s, FALSE))
6586 break;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006587 s = cin_skipcomment(line);
6588 if (*s == NUL)
6589 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006590 }
6591
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006592 if (s[0] == '"' || (s[0] == 'R' && s[1] == '"'))
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006593 s = skip_string(s) + 1;
6594 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 {
6596 if (s[1] == ':')
6597 {
6598 /* skip double colon. It can't be a constructor
6599 * initialization any more */
6600 lookfor_ctor_init = FALSE;
6601 s = cin_skipcomment(s + 2);
6602 }
6603 else if (lookfor_ctor_init || class_or_struct)
6604 {
6605 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006606 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 cpp_base_class = TRUE;
6608 lookfor_ctor_init = class_or_struct = FALSE;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006609 pos->col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 s = cin_skipcomment(s + 1);
6611 }
6612 else
6613 s = cin_skipcomment(s + 1);
6614 }
6615 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6616 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6617 {
6618 class_or_struct = TRUE;
6619 lookfor_ctor_init = FALSE;
6620
6621 if (*s == 'c')
6622 s = cin_skipcomment(s + 5);
6623 else
6624 s = cin_skipcomment(s + 6);
6625 }
6626 else
6627 {
6628 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6629 {
6630 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6631 }
6632 else if (s[0] == ')')
6633 {
6634 /* Constructor-initialization is assumed if we come across
6635 * something like "):" */
6636 class_or_struct = FALSE;
6637 lookfor_ctor_init = TRUE;
6638 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006639 else if (s[0] == '?')
6640 {
6641 /* Avoid seeing '() :' after '?' as constructor init. */
6642 return FALSE;
6643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 else if (!vim_isIDc(s[0]))
6645 {
6646 /* if it is not an identifier, we are wrong */
6647 class_or_struct = FALSE;
6648 lookfor_ctor_init = FALSE;
6649 }
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006650 else if (pos->col == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 {
6652 /* it can't be a constructor-initialization any more */
6653 lookfor_ctor_init = FALSE;
6654
6655 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006656 if (cpp_base_class)
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006657 pos->col = (colnr_T)(s - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 }
6659
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006660 /* When the line ends in a comma don't align with it. */
6661 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006662 pos->col = 0;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006663
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 s = cin_skipcomment(s + 1);
6665 }
6666 }
6667
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02006668 cached->found = cpp_base_class;
6669 if (cpp_base_class)
6670 pos->lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 return cpp_base_class;
6672}
6673
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006674 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006675get_baseclass_amount(col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006676 int col;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006677{
6678 int amount;
6679 colnr_T vcol;
6680 pos_T *trypos;
6681
6682 if (col == 0)
6683 {
6684 amount = get_indent();
6685 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006686 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006687 amount = get_indent_lnum(trypos->lnum); /* XXX */
6688 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006689 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006690 }
6691 else
6692 {
6693 curwin->w_cursor.col = col;
6694 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6695 amount = (int)vcol;
6696 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006697 if (amount < curbuf->b_ind_cpp_baseclass)
6698 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006699 return amount;
6700}
6701
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702/*
6703 * Return TRUE if string "s" ends with the string "find", possibly followed by
6704 * white space and comments. Skip strings and comments.
6705 * Ignore "ignore" after "find" if it's not NULL.
6706 */
6707 static int
6708cin_ends_in(s, find, ignore)
6709 char_u *s;
6710 char_u *find;
6711 char_u *ignore;
6712{
6713 char_u *p = s;
6714 char_u *r;
6715 int len = (int)STRLEN(find);
6716
6717 while (*p != NUL)
6718 {
6719 p = cin_skipcomment(p);
6720 if (STRNCMP(p, find, len) == 0)
6721 {
6722 r = skipwhite(p + len);
6723 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6724 r = skipwhite(r + STRLEN(ignore));
6725 if (cin_nocode(r))
6726 return TRUE;
6727 }
6728 if (*p != NUL)
6729 ++p;
6730 }
6731 return FALSE;
6732}
6733
6734/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006735 * Return TRUE when "s" starts with "word" and then a non-ID character.
6736 */
6737 static int
6738cin_starts_with(s, word)
6739 char_u *s;
6740 char *word;
6741{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006742 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006743
6744 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6745}
6746
6747/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 * Skip strings, chars and comments until at or past "trypos".
6749 * Return the column found.
6750 */
6751 static int
6752cin_skip2pos(trypos)
6753 pos_T *trypos;
6754{
6755 char_u *line;
6756 char_u *p;
6757
6758 p = line = ml_get(trypos->lnum);
6759 while (*p && (colnr_T)(p - line) < trypos->col)
6760 {
6761 if (cin_iscomment(p))
6762 p = cin_skipcomment(p);
6763 else
6764 {
6765 p = skip_string(p);
6766 ++p;
6767 }
6768 }
6769 return (int)(p - line);
6770}
6771
6772/*
6773 * Find the '{' at the start of the block we are in.
6774 * Return NULL if no match found.
6775 * Ignore a '{' that is in a comment, makes indenting the next three lines
6776 * work. */
6777/* foo() */
6778/* { */
6779/* } */
6780
6781 static pos_T *
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006782find_start_brace() /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783{
6784 pos_T cursor_save;
6785 pos_T *trypos;
6786 pos_T *pos;
6787 static pos_T pos_copy;
6788
6789 cursor_save = curwin->w_cursor;
6790 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6791 {
6792 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6793 trypos = &pos_copy;
6794 curwin->w_cursor = *trypos;
6795 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006796 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006798 && (pos = ind_find_start_CORS()) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 break;
6800 if (pos != NULL)
6801 curwin->w_cursor.lnum = pos->lnum;
6802 }
6803 curwin->w_cursor = cursor_save;
6804 return trypos;
6805}
6806
6807/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006808 * Find the matching '(', ignoring it if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006809 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 */
6811 static pos_T *
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006812find_match_paren(ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 int ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814{
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02006815 return find_match_char('(', ind_maxparen);
6816}
6817
6818 static pos_T *
6819find_match_char(c, ind_maxparen) /* XXX */
6820 int c;
6821 int ind_maxparen;
6822{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 pos_T cursor_save;
6824 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006825 static pos_T pos_copy;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006826 int ind_maxp_wk;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827
6828 cursor_save = curwin->w_cursor;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006829 ind_maxp_wk = ind_maxparen;
6830retry:
6831 if ((trypos = findmatchlimit(NULL, c, 0, ind_maxp_wk)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 {
6833 /* check if the ( is in a // comment */
6834 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
Bram Moolenaardcefba92015-03-20 19:06:06 +01006835 {
6836 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum - trypos->lnum);
6837 if (ind_maxp_wk > 0)
6838 {
6839 curwin->w_cursor = *trypos;
6840 curwin->w_cursor.col = 0; /* XXX */
6841 goto retry;
6842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 else
6846 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01006847 pos_T *trypos_wk;
6848
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6850 trypos = &pos_copy;
6851 curwin->w_cursor = *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02006852 if ((trypos_wk = ind_find_start_CORS()) != NULL) /* XXX */
Bram Moolenaardcefba92015-03-20 19:06:06 +01006853 {
6854 ind_maxp_wk = ind_maxparen - (int)(cursor_save.lnum
6855 - trypos_wk->lnum);
6856 if (ind_maxp_wk > 0)
6857 {
6858 curwin->w_cursor = *trypos_wk;
6859 goto retry;
6860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 trypos = NULL;
Bram Moolenaardcefba92015-03-20 19:06:06 +01006862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 }
6864 }
6865 curwin->w_cursor = cursor_save;
6866 return trypos;
6867}
6868
6869/*
Bram Moolenaar81439a62014-07-02 18:27:48 +02006870 * Find the matching '(', ignoring it if it is in a comment or before an
6871 * unmatched {.
6872 * Return NULL if no match found.
6873 */
6874 static pos_T *
6875find_match_paren_after_brace(ind_maxparen) /* XXX */
6876 int ind_maxparen;
6877{
6878 pos_T *trypos = find_match_paren(ind_maxparen);
6879
6880 if (trypos != NULL)
6881 {
6882 pos_T *tryposBrace = find_start_brace();
6883
6884 /* If both an unmatched '(' and '{' is found. Ignore the '('
6885 * position if the '{' is further down. */
6886 if (tryposBrace != NULL
6887 && (trypos->lnum != tryposBrace->lnum
6888 ? trypos->lnum < tryposBrace->lnum
6889 : trypos->col < tryposBrace->col))
6890 trypos = NULL;
6891 }
6892 return trypos;
6893}
6894
6895/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 * Return ind_maxparen corrected for the difference in line number between the
6897 * cursor position and "startpos". This makes sure that searching for a
6898 * matching paren above the cursor line doesn't find a match because of
6899 * looking a few lines further.
6900 */
6901 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006902corr_ind_maxparen(startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 pos_T *startpos;
6904{
6905 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6906
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006907 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
6908 return curbuf->b_ind_maxparen - (int)n;
6909 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910}
6911
6912/*
6913 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006914 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 */
6916 static int
6917find_last_paren(l, start, end)
6918 char_u *l;
6919 int start, end;
6920{
6921 int i;
6922 int retval = FALSE;
6923 int open_count = 0;
6924
6925 curwin->w_cursor.col = 0; /* default is start of line */
6926
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006927 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 {
6929 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6930 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6931 if (l[i] == start)
6932 ++open_count;
6933 else if (l[i] == end)
6934 {
6935 if (open_count > 0)
6936 --open_count;
6937 else
6938 {
6939 curwin->w_cursor.col = i;
6940 retval = TRUE;
6941 }
6942 }
6943 }
6944 return retval;
6945}
6946
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006947/*
6948 * Parse 'cinoptions' and set the values in "curbuf".
6949 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
6950 */
6951 void
6952parse_cino(buf)
6953 buf_T *buf;
6954{
6955 char_u *p;
6956 char_u *l;
6957 char_u *digits;
6958 int n;
6959 int divider;
6960 int fraction = 0;
6961 int sw = (int)get_sw_value(buf);
6962
6963 /*
6964 * Set the default values.
6965 */
6966 /* Spaces from a block's opening brace the prevailing indent for that
6967 * block should be. */
6968 buf->b_ind_level = sw;
6969
6970 /* Spaces from the edge of the line an open brace that's at the end of a
6971 * line is imagined to be. */
6972 buf->b_ind_open_imag = 0;
6973
6974 /* Spaces from the prevailing indent for a line that is not preceded by
6975 * an opening brace. */
6976 buf->b_ind_no_brace = 0;
6977
6978 /* Column where the first { of a function should be located }. */
6979 buf->b_ind_first_open = 0;
6980
6981 /* Spaces from the prevailing indent a leftmost open brace should be
6982 * located. */
6983 buf->b_ind_open_extra = 0;
6984
6985 /* Spaces from the matching open brace (real location for one at the left
6986 * edge; imaginary location from one that ends a line) the matching close
6987 * brace should be located. */
6988 buf->b_ind_close_extra = 0;
6989
6990 /* Spaces from the edge of the line an open brace sitting in the leftmost
6991 * column is imagined to be. */
6992 buf->b_ind_open_left_imag = 0;
6993
6994 /* Spaces jump labels should be shifted to the left if N is non-negative,
6995 * otherwise the jump label will be put to column 1. */
6996 buf->b_ind_jump_label = -1;
6997
6998 /* Spaces from the switch() indent a "case xx" label should be located. */
6999 buf->b_ind_case = sw;
7000
7001 /* Spaces from the "case xx:" code after a switch() should be located. */
7002 buf->b_ind_case_code = sw;
7003
7004 /* Lineup break at end of case in switch() with case label. */
7005 buf->b_ind_case_break = 0;
7006
7007 /* Spaces from the class declaration indent a scope declaration label
7008 * should be located. */
7009 buf->b_ind_scopedecl = sw;
7010
7011 /* Spaces from the scope declaration label code should be located. */
7012 buf->b_ind_scopedecl_code = sw;
7013
7014 /* Amount K&R-style parameters should be indented. */
7015 buf->b_ind_param = sw;
7016
7017 /* Amount a function type spec should be indented. */
7018 buf->b_ind_func_type = sw;
7019
7020 /* Amount a cpp base class declaration or constructor initialization
7021 * should be indented. */
7022 buf->b_ind_cpp_baseclass = sw;
7023
7024 /* additional spaces beyond the prevailing indent a continuation line
7025 * should be located. */
7026 buf->b_ind_continuation = sw;
7027
7028 /* Spaces from the indent of the line with an unclosed parentheses. */
7029 buf->b_ind_unclosed = sw * 2;
7030
7031 /* Spaces from the indent of the line with an unclosed parentheses, which
7032 * itself is also unclosed. */
7033 buf->b_ind_unclosed2 = sw;
7034
7035 /* Suppress ignoring spaces from the indent of a line starting with an
7036 * unclosed parentheses. */
7037 buf->b_ind_unclosed_noignore = 0;
7038
7039 /* If the opening paren is the last nonwhite character on the line, and
7040 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
7041 * context (for very long lines). */
7042 buf->b_ind_unclosed_wrapped = 0;
7043
7044 /* Suppress ignoring white space when lining up with the character after
7045 * an unclosed parentheses. */
7046 buf->b_ind_unclosed_whiteok = 0;
7047
7048 /* Indent a closing parentheses under the line start of the matching
7049 * opening parentheses. */
7050 buf->b_ind_matching_paren = 0;
7051
7052 /* Indent a closing parentheses under the previous line. */
7053 buf->b_ind_paren_prev = 0;
7054
7055 /* Extra indent for comments. */
7056 buf->b_ind_comment = 0;
7057
7058 /* Spaces from the comment opener when there is nothing after it. */
7059 buf->b_ind_in_comment = 3;
7060
7061 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
7062 * after the comment opener. */
7063 buf->b_ind_in_comment2 = 0;
7064
7065 /* Max lines to search for an open paren. */
7066 buf->b_ind_maxparen = 20;
7067
7068 /* Max lines to search for an open comment. */
7069 buf->b_ind_maxcomment = 70;
7070
7071 /* Handle braces for java code. */
7072 buf->b_ind_java = 0;
7073
7074 /* Not to confuse JS object properties with labels. */
7075 buf->b_ind_js = 0;
7076
7077 /* Handle blocked cases correctly. */
7078 buf->b_ind_keep_case_label = 0;
7079
7080 /* Handle C++ namespace. */
7081 buf->b_ind_cpp_namespace = 0;
7082
7083 /* Handle continuation lines containing conditions of if(), for() and
7084 * while(). */
7085 buf->b_ind_if_for_while = 0;
7086
7087 for (p = buf->b_p_cino; *p; )
7088 {
7089 l = p++;
7090 if (*p == '-')
7091 ++p;
7092 digits = p; /* remember where the digits start */
7093 n = getdigits(&p);
7094 divider = 0;
7095 if (*p == '.') /* ".5s" means a fraction */
7096 {
7097 fraction = atol((char *)++p);
7098 while (VIM_ISDIGIT(*p))
7099 {
7100 ++p;
7101 if (divider)
7102 divider *= 10;
7103 else
7104 divider = 10;
7105 }
7106 }
7107 if (*p == 's') /* "2s" means two times 'shiftwidth' */
7108 {
7109 if (p == digits)
7110 n = sw; /* just "s" is one 'shiftwidth' */
7111 else
7112 {
7113 n *= sw;
7114 if (divider)
7115 n += (sw * fraction + divider / 2) / divider;
7116 }
7117 ++p;
7118 }
7119 if (l[1] == '-')
7120 n = -n;
7121
7122 /* When adding an entry here, also update the default 'cinoptions' in
7123 * doc/indent.txt, and add explanation for it! */
7124 switch (*l)
7125 {
7126 case '>': buf->b_ind_level = n; break;
7127 case 'e': buf->b_ind_open_imag = n; break;
7128 case 'n': buf->b_ind_no_brace = n; break;
7129 case 'f': buf->b_ind_first_open = n; break;
7130 case '{': buf->b_ind_open_extra = n; break;
7131 case '}': buf->b_ind_close_extra = n; break;
7132 case '^': buf->b_ind_open_left_imag = n; break;
7133 case 'L': buf->b_ind_jump_label = n; break;
7134 case ':': buf->b_ind_case = n; break;
7135 case '=': buf->b_ind_case_code = n; break;
7136 case 'b': buf->b_ind_case_break = n; break;
7137 case 'p': buf->b_ind_param = n; break;
7138 case 't': buf->b_ind_func_type = n; break;
7139 case '/': buf->b_ind_comment = n; break;
7140 case 'c': buf->b_ind_in_comment = n; break;
7141 case 'C': buf->b_ind_in_comment2 = n; break;
7142 case 'i': buf->b_ind_cpp_baseclass = n; break;
7143 case '+': buf->b_ind_continuation = n; break;
7144 case '(': buf->b_ind_unclosed = n; break;
7145 case 'u': buf->b_ind_unclosed2 = n; break;
7146 case 'U': buf->b_ind_unclosed_noignore = n; break;
7147 case 'W': buf->b_ind_unclosed_wrapped = n; break;
7148 case 'w': buf->b_ind_unclosed_whiteok = n; break;
7149 case 'm': buf->b_ind_matching_paren = n; break;
7150 case 'M': buf->b_ind_paren_prev = n; break;
7151 case ')': buf->b_ind_maxparen = n; break;
7152 case '*': buf->b_ind_maxcomment = n; break;
7153 case 'g': buf->b_ind_scopedecl = n; break;
7154 case 'h': buf->b_ind_scopedecl_code = n; break;
7155 case 'j': buf->b_ind_java = n; break;
7156 case 'J': buf->b_ind_js = n; break;
7157 case 'l': buf->b_ind_keep_case_label = n; break;
7158 case '#': buf->b_ind_hash_comment = n; break;
7159 case 'N': buf->b_ind_cpp_namespace = n; break;
7160 case 'k': buf->b_ind_if_for_while = n; break;
7161 }
7162 if (*p == ',')
7163 ++p;
7164 }
7165}
7166
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007167/*
7168 * Return the desired indent for C code.
7169 * Return -1 if the indent should be left alone (inside a raw string).
7170 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 int
7172get_c_indent()
7173{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 pos_T cur_curpos;
7175 int amount;
7176 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007177 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 colnr_T col;
7179 char_u *theline;
7180 char_u *linecopy;
7181 pos_T *trypos;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007182 pos_T *comment_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 pos_T *tryposBrace = NULL;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007184 pos_T tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 pos_T our_paren_pos;
7186 char_u *start;
7187 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00007188#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189#define BRACE_AT_START 2 /* '{' is at start of line */
7190#define BRACE_AT_END 3 /* '{' is at end of line */
7191 linenr_T ourscope;
7192 char_u *l;
7193 char_u *look;
7194 char_u terminated;
7195 int lookfor;
7196#define LOOKFOR_INITIAL 0
7197#define LOOKFOR_IF 1
7198#define LOOKFOR_DO 2
7199#define LOOKFOR_CASE 3
7200#define LOOKFOR_ANY 4
7201#define LOOKFOR_TERM 5
7202#define LOOKFOR_UNTERM 6
7203#define LOOKFOR_SCOPEDECL 7
7204#define LOOKFOR_NOBREAK 8
7205#define LOOKFOR_CPP_BASECLASS 9
7206#define LOOKFOR_ENUM_OR_INIT 10
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007207#define LOOKFOR_JS_KEY 11
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007208#define LOOKFOR_COMMA 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209
7210 int whilelevel;
7211 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 int n;
7213 int iscase;
7214 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007215 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007217 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007218 int added_to_amount = 0;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007219 int js_cur_has_key = 0;
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02007220 cpp_baseclass_cache_T cache_cpp_baseclass = { FALSE, { MAXLNUM, 0 } };
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007222 /* make a copy, value is changed below */
7223 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224
7225 /* remember where the cursor was when we started */
7226 cur_curpos = curwin->w_cursor;
7227
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007228 /* if we are at line 1 zero indent is fine, right? */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007229 if (cur_curpos.lnum == 1)
7230 return 0;
7231
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 /* Get a copy of the current contents of the line.
7233 * This is required, because only the most recent line obtained with
7234 * ml_get is valid! */
7235 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
7236 if (linecopy == NULL)
7237 return 0;
7238
7239 /*
7240 * In insert mode and the cursor is on a ')' truncate the line at the
7241 * cursor position. We don't want to line up with the matching '(' when
7242 * inserting new stuff.
7243 * For unknown reasons the cursor might be past the end of the line, thus
7244 * check for that.
7245 */
7246 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00007247 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 && linecopy[curwin->w_cursor.col] == ')')
7249 linecopy[curwin->w_cursor.col] = NUL;
7250
7251 theline = skipwhite(linecopy);
7252
7253 /* move the cursor to the start of the line */
7254
7255 curwin->w_cursor.col = 0;
7256
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007257 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007258
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007260 * If we are inside a raw string don't change the indent.
7261 * Ignore a raw string inside a comment.
7262 */
7263 comment_pos = ind_find_start_comment();
7264 if (comment_pos != NULL)
7265 {
7266 /* findmatchlimit() static pos is overwritten, make a copy */
7267 tryposCopy = *comment_pos;
7268 comment_pos = &tryposCopy;
7269 }
7270 trypos = find_start_rawstring(curbuf->b_ind_maxcomment);
7271 if (trypos != NULL && (comment_pos == NULL || lt(*trypos, *comment_pos)))
7272 {
7273 amount = -1;
7274 goto laterend;
7275 }
7276
7277 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 * #defines and so on always go at the left when included in 'cinkeys'.
7279 */
7280 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007281 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007282 amount = curbuf->b_ind_hash_comment;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007283 goto theend;
7284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285
7286 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02007287 * Is it a non-case label? Then that goes at the left margin too unless:
7288 * - JS flag is set.
7289 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007291 if (original_line_islabel && !curbuf->b_ind_js
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007292 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 {
7294 amount = 0;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007295 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 }
7297
7298 /*
7299 * If we're inside a "//" comment and there is a "//" comment in a
7300 * previous line, lineup with that one.
7301 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007302 if (cin_islinecomment(theline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 && (trypos = find_line_comment()) != NULL) /* XXX */
7304 {
7305 /* find how indented the line beginning the comment is */
7306 getvcol(curwin, trypos, &col, NULL, NULL);
7307 amount = col;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007308 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 }
7310
7311 /*
7312 * If we're inside a comment and not looking at the start of the
7313 * comment, try using the 'comments' option.
7314 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007315 if (!cin_iscomment(theline) && comment_pos != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 {
7317 int lead_start_len = 2;
7318 int lead_middle_len = 1;
7319 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
7320 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
7321 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
7322 char_u *p;
7323 int start_align = 0;
7324 int start_off = 0;
7325 int done = FALSE;
7326
7327 /* find how indented the line beginning the comment is */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007328 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02007330 *lead_start = NUL;
7331 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332
7333 p = curbuf->b_p_com;
7334 while (*p != NUL)
7335 {
7336 int align = 0;
7337 int off = 0;
7338 int what = 0;
7339
7340 while (*p != NUL && *p != ':')
7341 {
7342 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7343 what = *p++;
7344 else if (*p == COM_LEFT || *p == COM_RIGHT)
7345 align = *p++;
7346 else if (VIM_ISDIGIT(*p) || *p == '-')
7347 off = getdigits(&p);
7348 else
7349 ++p;
7350 }
7351
7352 if (*p == ':')
7353 ++p;
7354 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7355 if (what == COM_START)
7356 {
7357 STRCPY(lead_start, lead_end);
7358 lead_start_len = (int)STRLEN(lead_start);
7359 start_off = off;
7360 start_align = align;
7361 }
7362 else if (what == COM_MIDDLE)
7363 {
7364 STRCPY(lead_middle, lead_end);
7365 lead_middle_len = (int)STRLEN(lead_middle);
7366 }
7367 else if (what == COM_END)
7368 {
7369 /* If our line starts with the middle comment string, line it
7370 * up with the comment opener per the 'comments' option. */
7371 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7372 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7373 {
7374 done = TRUE;
7375 if (curwin->w_cursor.lnum > 1)
7376 {
7377 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007378 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 * the middle comment string matches in the previous
7380 * line, use the indent of that line. XXX */
7381 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7382 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7383 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7384 else if (STRNCMP(look, lead_middle,
7385 lead_middle_len) == 0)
7386 {
7387 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7388 break;
7389 }
7390 /* If the start comment string doesn't match with the
7391 * start of the comment, skip this entry. XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007392 else if (STRNCMP(ml_get(comment_pos->lnum) + comment_pos->col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 lead_start, lead_start_len) != 0)
7394 continue;
7395 }
7396 if (start_off != 0)
7397 amount += start_off;
7398 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007399 amount += vim_strsize(lead_start)
7400 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 break;
7402 }
7403
7404 /* If our line starts with the end comment string, line it up
7405 * with the middle comment */
7406 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7407 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7408 {
7409 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7410 /* XXX */
7411 if (off != 0)
7412 amount += off;
7413 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007414 amount += vim_strsize(lead_start)
7415 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 done = TRUE;
7417 break;
7418 }
7419 }
7420 }
7421
7422 /* If our line starts with an asterisk, line up with the
7423 * asterisk in the comment opener; otherwise, line up
7424 * with the first character of the comment text.
7425 */
7426 if (done)
7427 ;
7428 else if (theline[0] == '*')
7429 amount += 1;
7430 else
7431 {
7432 /*
7433 * If we are more than one line away from the comment opener, take
7434 * the indent of the previous non-empty line. If 'cino' has "CO"
7435 * and we are just below the comment opener and there are any
7436 * white characters after it line up with the text after it;
7437 * otherwise, add the amount specified by "c" in 'cino'
7438 */
7439 amount = -1;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007440 for (lnum = cur_curpos.lnum - 1; lnum > comment_pos->lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 {
7442 if (linewhite(lnum)) /* skip blank lines */
7443 continue;
7444 amount = get_indent_lnum(lnum); /* XXX */
7445 break;
7446 }
7447 if (amount == -1) /* use the comment opener */
7448 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007449 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007451 start = ml_get(comment_pos->lnum);
7452 look = start + comment_pos->col + 2; /* skip / and * */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 if (*look != NUL) /* if something after it */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007454 comment_pos->col = (colnr_T)(skipwhite(look) - start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007456 getvcol(curwin, comment_pos, &col, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007458 if (curbuf->b_ind_in_comment2 || *look == NUL)
7459 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 }
7461 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007462 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 }
7464
7465 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007466 * Are we looking at a ']' that has a match?
7467 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007468 if (*skipwhite(theline) == ']'
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007469 && (trypos = find_match_char('[', curbuf->b_ind_maxparen)) != NULL)
7470 {
7471 /* align with the line containing the '['. */
7472 amount = get_indent_lnum(trypos->lnum);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007473 goto theend;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007474 }
7475
7476 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 * Are we inside parentheses or braces?
7478 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007479 if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007480 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007481 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 || trypos != NULL)
7483 {
7484 if (trypos != NULL && tryposBrace != NULL)
7485 {
7486 /* Both an unmatched '(' and '{' is found. Use the one which is
7487 * closer to the current cursor position, set the other to NULL. */
7488 if (trypos->lnum != tryposBrace->lnum
7489 ? trypos->lnum < tryposBrace->lnum
7490 : trypos->col < tryposBrace->col)
7491 trypos = NULL;
7492 else
7493 tryposBrace = NULL;
7494 }
7495
7496 if (trypos != NULL)
7497 {
7498 /*
7499 * If the matching paren is more than one line away, use the indent of
7500 * a previous non-empty line that matches the same paren.
7501 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007502 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007504 /* Line up with the start of the matching paren line. */
7505 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7506 }
7507 else
7508 {
7509 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007510 our_paren_pos = *trypos;
7511 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007513 l = skipwhite(ml_get(lnum));
7514 if (cin_nocode(l)) /* skip comment lines */
7515 continue;
7516 if (cin_ispreproc_cont(&l, &lnum))
7517 continue; /* ignore #define, #if, etc. */
7518 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007520 /* Skip a comment or raw string. XXX */
7521 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007522 {
7523 lnum = trypos->lnum + 1;
7524 continue;
7525 }
7526
7527 /* XXX */
7528 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007529 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007530 && trypos->lnum == our_paren_pos.lnum
7531 && trypos->col == our_paren_pos.col)
7532 {
7533 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007535 if (theline[0] == ')')
7536 {
7537 if (our_paren_pos.lnum != lnum
7538 && cur_amount > amount)
7539 cur_amount = amount;
7540 amount = -1;
7541 }
7542 break;
7543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 }
7545 }
7546
7547 /*
7548 * Line up with line where the matching paren is. XXX
7549 * If the line starts with a '(' or the indent for unclosed
7550 * parentheses is zero, line up with the unclosed parentheses.
7551 */
7552 if (amount == -1)
7553 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007554 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007555 int is_if_for_while = 0;
7556
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007557 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007558 {
7559 /* Look for the outermost opening parenthesis on this line
7560 * and check whether it belongs to an "if", "for" or "while". */
7561
7562 pos_T cursor_save = curwin->w_cursor;
7563 pos_T outermost;
7564 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007565
7566 trypos = &our_paren_pos;
7567 do {
7568 outermost = *trypos;
7569 curwin->w_cursor.lnum = outermost.lnum;
7570 curwin->w_cursor.col = outermost.col;
7571
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007572 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007573 } while (trypos && trypos->lnum == outermost.lnum);
7574
7575 curwin->w_cursor = cursor_save;
7576
7577 line = ml_get(outermost.lnum);
7578
7579 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007580 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007581 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007582
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007583 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007584 look = skipwhite(look);
7585 if (*look == '(')
7586 {
7587 linenr_T save_lnum = curwin->w_cursor.lnum;
7588 char_u *line;
7589 int look_col;
7590
7591 /* Ignore a '(' in front of the line that has a match before
7592 * our matching '('. */
7593 curwin->w_cursor.lnum = our_paren_pos.lnum;
7594 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007595 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007596 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007597 if ((trypos = findmatchlimit(NULL, ')', 0,
7598 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007599 != NULL
7600 && trypos->lnum == our_paren_pos.lnum
7601 && trypos->col < our_paren_pos.col)
7602 ignore_paren_col = trypos->col + 1;
7603
7604 curwin->w_cursor.lnum = save_lnum;
7605 look = ml_get(our_paren_pos.lnum) + look_col;
7606 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007607 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7608 && is_if_for_while == 0)
7609 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007610 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 {
7612 /*
7613 * If we're looking at a close paren, line up right there;
7614 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007615 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 * the last nonwhite character of the line, use either the
7617 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007618 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 * lines).
7620 */
7621 if (theline[0] != ')')
7622 {
7623 cur_amount = MAXCOL;
7624 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007625 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 && cin_ends_in(l, (char_u *)"(", NULL))
7627 {
7628 /* look for opening unmatched paren, indent one level
7629 * for each additional level */
7630 n = 1;
7631 for (col = 0; col < our_paren_pos.col; ++col)
7632 {
7633 switch (l[col])
7634 {
7635 case '(':
7636 case '{': ++n;
7637 break;
7638
7639 case ')':
7640 case '}': if (n > 1)
7641 --n;
7642 break;
7643 }
7644 }
7645
7646 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007647 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007649 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 our_paren_pos.col++;
7651 else
7652 {
7653 col = our_paren_pos.col + 1;
7654 while (vim_iswhite(l[col]))
7655 col++;
7656 if (l[col] != NUL) /* In case of trailing space */
7657 our_paren_pos.col = col;
7658 else
7659 our_paren_pos.col++;
7660 }
7661 }
7662
7663 /*
7664 * Find how indented the paren is, or the character after it
7665 * if we did the above "if".
7666 */
7667 if (our_paren_pos.col > 0)
7668 {
7669 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7670 if (cur_amount > (int)col)
7671 cur_amount = col;
7672 }
7673 }
7674
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007675 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 {
7677 /* Line up with the start of the matching paren line. */
7678 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007679 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7680 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007681 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 {
7683 if (cur_amount != MAXCOL)
7684 amount = cur_amount;
7685 }
7686 else
7687 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007688 /* Add b_ind_unclosed2 for each '(' before our matching one,
7689 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007691 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 {
7693 --our_paren_pos.col;
7694 switch (*ml_get_pos(&our_paren_pos))
7695 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007696 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 col = our_paren_pos.col;
7698 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007699 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 col = MAXCOL;
7701 break;
7702 }
7703 }
7704
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007705 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 * braces */
7707 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007708 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 else
7710 {
7711 curwin->w_cursor.lnum = our_paren_pos.lnum;
7712 curwin->w_cursor.col = col;
Bram Moolenaar81439a62014-07-02 18:27:48 +02007713 if (find_match_paren_after_brace(curbuf->b_ind_maxparen)
7714 != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007715 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007717 {
7718 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007719 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007720 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007721 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 }
7724 /*
7725 * For a line starting with ')' use the minimum of the two
7726 * positions, to avoid giving it more indent than the previous
7727 * lines:
7728 * func_long_name( if (x
7729 * arg && yy
7730 * ) ^ not here ) ^ not here
7731 */
7732 if (cur_amount < amount)
7733 amount = cur_amount;
7734 }
7735 }
7736
7737 /* add extra indent for a comment */
7738 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007739 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 else
7742 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007743 /*
7744 * We are inside braces, there is a { before this line at the position
7745 * stored in tryposBrace.
Bram Moolenaar04d17ae2014-08-06 17:44:14 +02007746 * Make a copy of tryposBrace, it may point to pos_copy inside
7747 * find_start_brace(), which may be changed somewhere.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007748 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007749 tryposCopy = *tryposBrace;
7750 tryposBrace = &tryposCopy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 trypos = tryposBrace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 ourscope = trypos->lnum;
7753 start = ml_get(ourscope);
7754
7755 /*
7756 * Now figure out how indented the line is in general.
7757 * If the brace was at the start of the line, we use that;
7758 * otherwise, check out the indentation of the line as
7759 * a whole and then add the "imaginary indent" to that.
7760 */
7761 look = skipwhite(start);
7762 if (*look == '{')
7763 {
7764 getvcol(curwin, trypos, &col, NULL, NULL);
7765 amount = col;
7766 if (*start == '{')
7767 start_brace = BRACE_IN_COL0;
7768 else
7769 start_brace = BRACE_AT_START;
7770 }
7771 else
7772 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007773 /* That opening brace might have been on a continuation
7774 * line. if so, find the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 curwin->w_cursor.lnum = ourscope;
7776
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007777 /* Position the cursor over the rightmost paren, so that
7778 * matching it will take us back to the start of the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 lnum = ourscope;
7780 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007781 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7782 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 lnum = trypos->lnum;
7784
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007785 /* It could have been something like
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 * case 1: if (asdf &&
7787 * ldfd) {
7788 * }
7789 */
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007790 if ((curbuf->b_ind_js || curbuf->b_ind_keep_case_label)
7791 && cin_iscase(skipwhite(ml_get_curline()), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 amount = get_indent();
Bram Moolenaare7eb7892014-07-02 17:02:36 +02007793 else if (curbuf->b_ind_js)
7794 amount = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007796 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797
7798 start_brace = BRACE_AT_END;
7799 }
7800
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007801 /* For Javascript check if the line starts with "key:". */
7802 if (curbuf->b_ind_js)
7803 js_cur_has_key = cin_has_js_key(theline);
7804
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007806 * If we're looking at a closing brace, that's where
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 * we want to be. otherwise, add the amount of room
7808 * that an indent is supposed to be.
7809 */
7810 if (theline[0] == '}')
7811 {
7812 /*
7813 * they may want closing braces to line up with something
7814 * other than the open brace. indulge them, if so.
7815 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007816 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 }
7818 else
7819 {
7820 /*
7821 * If we're looking at an "else", try to find an "if"
7822 * to match it with.
7823 * If we're looking at a "while", try to find a "do"
7824 * to match it with.
7825 */
7826 lookfor = LOOKFOR_INITIAL;
7827 if (cin_iselse(theline))
7828 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007829 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 lookfor = LOOKFOR_DO;
7831 if (lookfor != LOOKFOR_INITIAL)
7832 {
7833 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007834 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 {
7836 amount = get_indent(); /* XXX */
7837 goto theend;
7838 }
7839 }
7840
7841 /*
7842 * We get here if we are not on an "while-of-do" or "else" (or
7843 * failed to find a matching "if").
7844 * Search backwards for something to line up with.
7845 * First set amount for when we don't find anything.
7846 */
7847
7848 /*
7849 * if the '{' is _really_ at the left margin, use the imaginary
7850 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007851 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 */
7853
7854 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7855 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007856 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007857 lookfor_cpp_namespace = TRUE;
7858 }
7859 else if (start_brace == BRACE_AT_START &&
7860 lookfor_cpp_namespace) /* '{' is at start */
7861 {
7862
7863 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 }
7865 else
7866 {
7867 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007868 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007869 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007870
7871 l = skipwhite(ml_get_curline());
7872 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007873 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 else
7876 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007877 /* Compensate for adding b_ind_open_extra later. */
7878 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 if (amount < 0)
7880 amount = 0;
7881 }
7882 }
7883
7884 lookfor_break = FALSE;
7885
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007886 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 {
7888 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007889 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 }
7891 else if (cin_isscopedecl(theline)) /* private:, ... */
7892 {
7893 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007894 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 }
7896 else
7897 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007898 if (curbuf->b_ind_case_break && cin_isbreak(theline))
7899 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 lookfor_break = TRUE;
7901
7902 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007903 /* b_ind_level from start of block */
7904 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 }
7906 scope_amount = amount;
7907 whilelevel = 0;
7908
7909 /*
7910 * Search backwards. If we find something we recognize, line up
7911 * with that.
7912 *
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02007913 * If we're looking at an open brace, indent
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 * the usual amount relative to the conditional
7915 * that opens the block.
7916 */
7917 curwin->w_cursor = cur_curpos;
7918 for (;;)
7919 {
7920 curwin->w_cursor.lnum--;
7921 curwin->w_cursor.col = 0;
7922
7923 /*
7924 * If we went all the way back to the start of our scope, line
7925 * up with it.
7926 */
7927 if (curwin->w_cursor.lnum <= ourscope)
7928 {
7929 /* we reached end of scope:
7930 * if looking for a enum or structure initialization
7931 * go further back:
7932 * if it is an initializer (enum xxx or xxx =), then
7933 * don't add ind_continuation, otherwise it is a variable
7934 * declaration:
7935 * int x,
7936 * here; <-- add ind_continuation
7937 */
7938 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7939 {
7940 if (curwin->w_cursor.lnum == 0
7941 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007942 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007944 /* nothing found (abuse curbuf->b_ind_maxparen as
7945 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 * initialization) */
7947 if (cont_amount > 0)
7948 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007949 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 amount += ind_continuation;
7951 break;
7952 }
7953
7954 l = ml_get_curline();
7955
7956 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007957 * If we're in a comment or raw string now, skip to
7958 * the start of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007960 trypos = ind_find_start_CORS();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 if (trypos != NULL)
7962 {
7963 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007964 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 continue;
7966 }
7967
7968 /*
7969 * Skip preprocessor directives and blank lines.
7970 */
7971 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7972 continue;
7973
7974 if (cin_nocode(l))
7975 continue;
7976
7977 terminated = cin_isterminated(l, FALSE, TRUE);
7978
7979 /*
7980 * If we are at top level and the line looks like a
7981 * function declaration, we are done
7982 * (it's a variable declaration).
7983 */
7984 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007985 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 {
7987 /* if the line is terminated with another ','
7988 * it is a continued variable initialization.
7989 * don't add extra indent.
7990 * TODO: does not work, if a function
7991 * declaration is split over multiple lines:
7992 * cin_isfuncdecl returns FALSE then.
7993 */
7994 if (terminated == ',')
7995 break;
7996
7997 /* if it es a enum declaration or an assignment,
7998 * we are done.
7999 */
8000 if (terminated != ';' && cin_isinit())
8001 break;
8002
8003 /* nothing useful found */
8004 if (terminated == 0 || terminated == '{')
8005 continue;
8006 }
8007
8008 if (terminated != ';')
8009 {
8010 /* Skip parens and braces. Position the cursor
8011 * over the rightmost paren, so that matching it
8012 * will take us back to the start of the line.
8013 */ /* XXX */
8014 trypos = NULL;
8015 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008016 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008017 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018
8019 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008020 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021
8022 if (trypos != NULL)
8023 {
8024 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008025 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 continue;
8027 }
8028 }
8029
8030 /* it's a variable declaration, add indentation
8031 * like in
8032 * int a,
8033 * b;
8034 */
8035 if (cont_amount > 0)
8036 amount = cont_amount;
8037 else
8038 amount += ind_continuation;
8039 }
8040 else if (lookfor == LOOKFOR_UNTERM)
8041 {
8042 if (cont_amount > 0)
8043 amount = cont_amount;
8044 else
8045 amount += ind_continuation;
8046 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02008047 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02008048 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02008049 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008050 && lookfor != LOOKFOR_CPP_BASECLASS
8051 && lookfor != LOOKFOR_COMMA)
Bram Moolenaare79d1532011-10-04 18:03:47 +02008052 {
8053 amount = scope_amount;
8054 if (theline[0] == '{')
8055 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008056 amount += curbuf->b_ind_open_extra;
8057 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008058 }
8059 }
8060
8061 if (lookfor_cpp_namespace)
8062 {
8063 /*
8064 * Looking for C++ namespace, need to look further
8065 * back.
8066 */
8067 if (curwin->w_cursor.lnum == ourscope)
8068 continue;
8069
8070 if (curwin->w_cursor.lnum == 0
8071 || curwin->w_cursor.lnum
8072 < ourscope - FIND_NAMESPACE_LIM)
8073 break;
8074
8075 l = ml_get_curline();
8076
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008077 /* If we're in a comment or raw string now, skip
8078 * to the start of it. */
8079 trypos = ind_find_start_CORS();
Bram Moolenaare79d1532011-10-04 18:03:47 +02008080 if (trypos != NULL)
8081 {
8082 curwin->w_cursor.lnum = trypos->lnum + 1;
8083 curwin->w_cursor.col = 0;
8084 continue;
8085 }
8086
8087 /* Skip preprocessor directives and blank lines. */
8088 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
8089 continue;
8090
8091 /* Finally the actual check for "namespace". */
8092 if (cin_is_cpp_namespace(l))
8093 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008094 amount += curbuf->b_ind_cpp_namespace
8095 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02008096 break;
8097 }
8098
8099 if (cin_nocode(l))
8100 continue;
8101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 }
8103 break;
8104 }
8105
8106 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008107 * If we're in a comment or raw string now, skip to the start
8108 * of it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 */ /* XXX */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008110 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 {
8112 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008113 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 continue;
8115 }
8116
8117 l = ml_get_curline();
8118
8119 /*
8120 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00008121 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008123 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 if (iscase || cin_isscopedecl(l))
8125 {
8126 /* we are only looking for cpp base class
8127 * declaration/initialization any longer */
8128 if (lookfor == LOOKFOR_CPP_BASECLASS)
8129 break;
8130
8131 /* When looking for a "do" we are not interested in
8132 * labels. */
8133 if (whilelevel > 0)
8134 continue;
8135
8136 /*
8137 * case xx:
8138 * c = 99 + <- this indent plus continuation
8139 *-> here;
8140 */
8141 if (lookfor == LOOKFOR_UNTERM
8142 || lookfor == LOOKFOR_ENUM_OR_INIT)
8143 {
8144 if (cont_amount > 0)
8145 amount = cont_amount;
8146 else
8147 amount += ind_continuation;
8148 break;
8149 }
8150
8151 /*
8152 * case xx: <- line up with this case
8153 * x = 333;
8154 * case yy:
8155 */
8156 if ( (iscase && lookfor == LOOKFOR_CASE)
8157 || (iscase && lookfor_break)
8158 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
8159 {
8160 /*
8161 * Check that this case label is not for another
8162 * switch()
8163 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008164 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008165 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 {
8167 amount = get_indent(); /* XXX */
8168 break;
8169 }
8170 continue;
8171 }
8172
8173 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
8174
8175 /*
8176 * case xx: if (cond) <- line up with this if
8177 * y = y + 1;
8178 * -> s = 99;
8179 *
8180 * case xx:
8181 * if (cond) <- line up with this line
8182 * y = y + 1;
8183 * -> s = 99;
8184 */
8185 if (lookfor == LOOKFOR_TERM)
8186 {
8187 if (n)
8188 amount = n;
8189
8190 if (!lookfor_break)
8191 break;
8192 }
8193
8194 /*
8195 * case xx: x = x + 1; <- line up with this x
8196 * -> y = y + 1;
8197 *
8198 * case xx: if (cond) <- line up with this if
8199 * -> y = y + 1;
8200 */
8201 if (n)
8202 {
8203 amount = n;
8204 l = after_label(ml_get_curline());
8205 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008206 {
8207 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008208 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008209 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008210 amount += curbuf->b_ind_level
8211 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00008212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 break;
8214 }
8215
8216 /*
8217 * Try to get the indent of a statement before the switch
8218 * label. If nothing is found, line up relative to the
8219 * switch label.
8220 * break; <- may line up with this line
8221 * case xx:
8222 * -> y = 1;
8223 */
8224 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008225 ? curbuf->b_ind_case_code
8226 : curbuf->b_ind_scopedecl_code);
8227 lookfor = curbuf->b_ind_case_break
8228 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 continue;
8230 }
8231
8232 /*
8233 * Looking for a switch() label or C++ scope declaration,
8234 * ignore other lines, skip {}-blocks.
8235 */
8236 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
8237 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008238 if (find_last_paren(l, '{', '}')
8239 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008240 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008242 curwin->w_cursor.col = 0;
8243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 continue;
8245 }
8246
8247 /*
8248 * Ignore jump labels with nothing after them.
8249 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008250 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 {
8252 l = after_label(ml_get_curline());
8253 if (l == NULL || cin_nocode(l))
8254 continue;
8255 }
8256
8257 /*
8258 * Ignore #defines, #if, etc.
8259 * Ignore comment and empty lines.
8260 * (need to get the line again, cin_islabel() may have
8261 * unlocked it)
8262 */
8263 l = ml_get_curline();
8264 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)
8265 || cin_nocode(l))
8266 continue;
8267
8268 /*
8269 * Are we at the start of a cpp base class declaration or
8270 * constructor initialization?
8271 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008272 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008273 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008274 {
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008275 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008276 l = ml_get_curline();
8277 }
8278 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 {
8280 if (lookfor == LOOKFOR_UNTERM)
8281 {
8282 if (cont_amount > 0)
8283 amount = cont_amount;
8284 else
8285 amount += ind_continuation;
8286 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008287 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008289 /* Need to find start of the declaration. */
8290 lookfor = LOOKFOR_UNTERM;
8291 ind_continuation = 0;
8292 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 }
8294 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008295 /* XXX */
Bram Moolenaar4032cfd2015-05-04 17:50:33 +02008296 amount = get_baseclass_amount(
8297 cache_cpp_baseclass.lpos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 break;
8299 }
8300 else if (lookfor == LOOKFOR_CPP_BASECLASS)
8301 {
8302 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00008303 * declaration or initialization before the opening brace.
8304 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 if (cin_isterminated(l, TRUE, FALSE))
8306 break;
8307 else
8308 continue;
8309 }
8310
8311 /*
8312 * What happens next depends on the line being terminated.
8313 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00008314 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 * 123,
8316 * sizeof
8317 * here
8318 * Otherwise check whether it is a enumeration or structure
8319 * initialisation (not indented) or a variable declaration
8320 * (indented).
8321 */
8322 terminated = cin_isterminated(l, FALSE, TRUE);
8323
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008324 if (js_cur_has_key)
8325 {
8326 js_cur_has_key = 0; /* only check the first line */
8327 if (curbuf->b_ind_js && terminated == ',')
8328 {
8329 /* For Javascript we might be inside an object:
8330 * key: something, <- align with this
8331 * key: something
8332 * or:
8333 * key: something + <- align with this
8334 * something,
8335 * key: something
8336 */
8337 lookfor = LOOKFOR_JS_KEY;
8338 }
8339 }
8340 if (lookfor == LOOKFOR_JS_KEY && cin_has_js_key(l))
8341 {
8342 amount = get_indent();
8343 break;
8344 }
Bram Moolenaardcefba92015-03-20 19:06:06 +01008345 if (lookfor == LOOKFOR_COMMA)
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008346 {
Bram Moolenaardcefba92015-03-20 19:06:06 +01008347 if (tryposBrace != NULL && tryposBrace->lnum
8348 >= curwin->w_cursor.lnum)
8349 break;
8350 if (terminated == ',')
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008351 /* line below current line is the one that starts a
8352 * (possibly broken) line ending in a comma. */
8353 break;
Bram Moolenaardcefba92015-03-20 19:06:06 +01008354 else
8355 {
8356 amount = get_indent();
8357 if (curwin->w_cursor.lnum - 1 == ourscope)
8358 /* line above is start of the scope, thus current
8359 * line is the one that stars a (possibly broken)
8360 * line ending in a comma. */
8361 break;
8362 }
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008363 }
8364
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
8366 && terminated == ','))
8367 {
Bram Moolenaar089af182015-10-07 11:41:49 +02008368 if (lookfor != LOOKFOR_ENUM_OR_INIT &&
8369 (*skipwhite(l) == '[' || l[STRLEN(l) - 1] == '['))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008370 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 /*
8372 * if we're in the middle of a paren thing,
8373 * go back to the line that starts it so
8374 * we can get the right prevailing indent
8375 * if ( foo &&
8376 * bar )
8377 */
8378 /*
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008379 * Position the cursor over the rightmost paren, so that
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 * matching it will take us back to the start of the line.
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008381 * Ignore a match before the start of the block.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 */
8383 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008384 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008385 if (trypos != NULL && (trypos->lnum < tryposBrace->lnum
8386 || (trypos->lnum == tryposBrace->lnum
8387 && trypos->col < tryposBrace->col)))
8388 trypos = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389
8390 /*
8391 * If we are looking for ',', we also look for matching
8392 * braces.
8393 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008394 if (trypos == NULL && terminated == ','
8395 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008396 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397
8398 if (trypos != NULL)
8399 {
8400 /*
8401 * Check if we are on a case label now. This is
8402 * handled above.
8403 * case xx: if ( asdf &&
8404 * asdf)
8405 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008406 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008408 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 {
8410 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008411 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 continue;
8413 }
8414 }
8415
8416 /*
8417 * Skip over continuation lines to find the one to get the
8418 * indent from
8419 * char *usethis = "bla\
8420 * bla",
8421 * here;
8422 */
8423 if (terminated == ',')
8424 {
8425 while (curwin->w_cursor.lnum > 1)
8426 {
8427 l = ml_get(curwin->w_cursor.lnum - 1);
8428 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8429 break;
8430 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008431 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 }
8433 }
8434
8435 /*
8436 * Get indent and pointer to text for current line,
8437 * ignoring any jump label. XXX
8438 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008439 if (curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008440 cur_amount = get_indent();
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008441 else
8442 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 /*
8444 * If this is just above the line we are indenting, and it
8445 * starts with a '{', line it up with this line.
8446 * while (not)
8447 * -> {
8448 * }
8449 */
8450 if (terminated != ',' && lookfor != LOOKFOR_TERM
8451 && theline[0] == '{')
8452 {
8453 amount = cur_amount;
8454 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008455 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 * doesn't start with a '{', which must have a match
8457 * in the same line (scope is the same). Probably:
8458 * { 1, 2 },
8459 * -> { 3, 4 }
8460 */
8461 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008462 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008464 if (curbuf->b_ind_cpp_baseclass && !curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 {
8466 /* have to look back, whether it is a cpp base
8467 * class declaration or initialization */
8468 lookfor = LOOKFOR_CPP_BASECLASS;
8469 continue;
8470 }
8471 break;
8472 }
8473
8474 /*
8475 * Check if we are after an "if", "while", etc.
8476 * Also allow " } else".
8477 */
8478 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8479 {
8480 /*
8481 * Found an unterminated line after an if (), line up
8482 * with the last one.
8483 * if (cond)
8484 * 100 +
8485 * -> here;
8486 */
8487 if (lookfor == LOOKFOR_UNTERM
8488 || lookfor == LOOKFOR_ENUM_OR_INIT)
8489 {
8490 if (cont_amount > 0)
8491 amount = cont_amount;
8492 else
8493 amount += ind_continuation;
8494 break;
8495 }
8496
8497 /*
8498 * If this is just above the line we are indenting, we
8499 * are finished.
8500 * while (not)
8501 * -> here;
8502 * Otherwise this indent can be used when the line
8503 * before this is terminated.
8504 * yyy;
8505 * if (stat)
8506 * while (not)
8507 * xxx;
8508 * -> here;
8509 */
8510 amount = cur_amount;
8511 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008512 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 if (lookfor != LOOKFOR_TERM)
8514 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008515 amount += curbuf->b_ind_level
8516 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 break;
8518 }
8519
8520 /*
8521 * Special trick: when expecting the while () after a
8522 * do, line up with the while()
8523 * do
8524 * x = 1;
8525 * -> here
8526 */
8527 l = skipwhite(ml_get_curline());
8528 if (cin_isdo(l))
8529 {
8530 if (whilelevel == 0)
8531 break;
8532 --whilelevel;
8533 }
8534
8535 /*
8536 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008537 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 * Need to use the scope of this "else". XXX
8539 * If whilelevel != 0 continue looking for a "do {".
8540 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008541 if (cin_iselse(l) && whilelevel == 0)
8542 {
8543 /* If we're looking at "} else", let's make sure we
8544 * find the opening brace of the enclosing scope,
8545 * not the one from "if () {". */
8546 if (*l == '}')
8547 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008548 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008549
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008550 if ((trypos = find_start_brace()) == NULL
8551 || find_match(LOOKFOR_IF, trypos->lnum)
8552 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008553 break;
8554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 }
8556
8557 /*
8558 * If we're below an unterminated line that is not an
8559 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008560 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 * the line before this one.
8562 */
8563 else
8564 {
8565 /*
8566 * Found two unterminated lines on a row, line up with
8567 * the last one.
8568 * c = 99 +
8569 * 100 +
8570 * -> here;
8571 */
8572 if (lookfor == LOOKFOR_UNTERM)
8573 {
8574 /* When line ends in a comma add extra indent */
8575 if (terminated == ',')
8576 amount += ind_continuation;
8577 break;
8578 }
8579
8580 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8581 {
8582 /* Found two lines ending in ',', lineup with the
8583 * lowest one, but check for cpp base class
8584 * declaration/initialization, if it is an
8585 * opening brace or we are looking just for
8586 * enumerations/initializations. */
8587 if (terminated == ',')
8588 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008589 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 break;
8591
8592 lookfor = LOOKFOR_CPP_BASECLASS;
8593 continue;
8594 }
8595
8596 /* Ignore unterminated lines in between, but
8597 * reduce indent. */
8598 if (amount > cur_amount)
8599 amount = cur_amount;
8600 }
8601 else
8602 {
8603 /*
8604 * Found first unterminated line on a row, may
8605 * line up with this line, remember its indent
8606 * 100 +
8607 * -> here;
8608 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008609 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 amount = cur_amount;
Bram Moolenaar089af182015-10-07 11:41:49 +02008611
8612 n = (int)STRLEN(l);
8613 if (terminated == ',' && (*skipwhite(l) == ']'
8614 || (n >=2 && l[n - 2] == ']')))
Bram Moolenaardcefba92015-03-20 19:06:06 +01008615 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616
8617 /*
8618 * If previous line ends in ',', check whether we
8619 * are in an initialization or enum
8620 * struct xxx =
8621 * {
8622 * sizeof a,
8623 * 124 };
8624 * or a normal possible continuation line.
8625 * but only, of no other statement has been found
8626 * yet.
8627 */
8628 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8629 {
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008630 if (curbuf->b_ind_js)
8631 {
8632 /* Search for a line ending in a comma
8633 * and line up with the line below it
8634 * (could be the current line).
8635 * some = [
8636 * 1, <- line up here
8637 * 2,
8638 * some = [
8639 * 3 + <- line up here
8640 * 4 *
8641 * 5,
8642 * 6,
8643 */
Bram Moolenaardcefba92015-03-20 19:06:06 +01008644 if (cin_iscomment(skipwhite(l)))
8645 break;
8646 lookfor = LOOKFOR_COMMA;
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008647 trypos = find_match_char('[',
8648 curbuf->b_ind_maxparen);
8649 if (trypos != NULL)
8650 {
8651 if (trypos->lnum
8652 == curwin->w_cursor.lnum - 1)
8653 {
8654 /* Current line is first inside
8655 * [], line up with it. */
8656 break;
8657 }
8658 ourscope = trypos->lnum;
8659 }
8660 }
8661 else
8662 {
8663 lookfor = LOOKFOR_ENUM_OR_INIT;
8664 cont_amount = cin_first_id_amount();
8665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 }
8667 else
8668 {
8669 if (lookfor == LOOKFOR_INITIAL
8670 && *l != NUL
8671 && l[STRLEN(l) - 1] == '\\')
8672 /* XXX */
8673 cont_amount = cin_get_equal_amount(
8674 curwin->w_cursor.lnum);
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008675 if (lookfor != LOOKFOR_TERM
Bram Moolenaardcefba92015-03-20 19:06:06 +01008676 && lookfor != LOOKFOR_JS_KEY
8677 && lookfor != LOOKFOR_COMMA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 lookfor = LOOKFOR_UNTERM;
8679 }
8680 }
8681 }
8682 }
8683
8684 /*
8685 * Check if we are after a while (cond);
8686 * If so: Ignore until the matching "do".
8687 */
Bram Moolenaar9f4fe7c2014-07-03 22:57:55 +02008688 else if (cin_iswhileofdo_end(terminated)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 {
8690 /*
8691 * Found an unterminated line after a while ();, line up
8692 * with the last one.
8693 * while (cond);
8694 * 100 + <- line up with this one
8695 * -> here;
8696 */
8697 if (lookfor == LOOKFOR_UNTERM
8698 || lookfor == LOOKFOR_ENUM_OR_INIT)
8699 {
8700 if (cont_amount > 0)
8701 amount = cont_amount;
8702 else
8703 amount += ind_continuation;
8704 break;
8705 }
8706
8707 if (whilelevel == 0)
8708 {
8709 lookfor = LOOKFOR_TERM;
8710 amount = get_indent(); /* XXX */
8711 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008712 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 }
8714 ++whilelevel;
8715 }
8716
8717 /*
8718 * We are after a "normal" statement.
8719 * If we had another statement we can stop now and use the
8720 * indent of that other statement.
8721 * Otherwise the indent of the current statement may be used,
8722 * search backwards for the next "normal" statement.
8723 */
8724 else
8725 {
8726 /*
8727 * Skip single break line, if before a switch label. It
8728 * may be lined up with the case label.
8729 */
8730 if (lookfor == LOOKFOR_NOBREAK
8731 && cin_isbreak(skipwhite(ml_get_curline())))
8732 {
8733 lookfor = LOOKFOR_ANY;
8734 continue;
8735 }
8736
8737 /*
8738 * Handle "do {" line.
8739 */
8740 if (whilelevel > 0)
8741 {
8742 l = cin_skipcomment(ml_get_curline());
8743 if (cin_isdo(l))
8744 {
8745 amount = get_indent(); /* XXX */
8746 --whilelevel;
8747 continue;
8748 }
8749 }
8750
8751 /*
8752 * Found a terminated line above an unterminated line. Add
8753 * the amount for a continuation line.
8754 * x = 1;
8755 * y = foo +
8756 * -> here;
8757 * or
8758 * int x = 1;
8759 * int foo,
8760 * -> here;
8761 */
8762 if (lookfor == LOOKFOR_UNTERM
8763 || lookfor == LOOKFOR_ENUM_OR_INIT)
8764 {
8765 if (cont_amount > 0)
8766 amount = cont_amount;
8767 else
8768 amount += ind_continuation;
8769 break;
8770 }
8771
8772 /*
8773 * Found a terminated line above a terminated line or "if"
8774 * etc. line. Use the amount of the line below us.
8775 * x = 1; x = 1;
8776 * if (asdf) y = 2;
8777 * while (asdf) ->here;
8778 * here;
8779 * ->foo;
8780 */
8781 if (lookfor == LOOKFOR_TERM)
8782 {
8783 if (!lookfor_break && whilelevel == 0)
8784 break;
8785 }
8786
8787 /*
8788 * First line above the one we're indenting is terminated.
8789 * To know what needs to be done look further backward for
8790 * a terminated line.
8791 */
8792 else
8793 {
8794 /*
8795 * position the cursor over the rightmost paren, so
8796 * that matching it will take us back to the start of
8797 * the line. Helps for:
8798 * func(asdr,
8799 * asdfasdf);
8800 * here;
8801 */
8802term_again:
8803 l = ml_get_curline();
8804 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008805 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008806 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 {
8808 /*
8809 * Check if we are on a case label now. This is
8810 * handled above.
8811 * case xx: if ( asdf &&
8812 * asdf)
8813 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008814 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008816 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 {
8818 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008819 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 continue;
8821 }
8822 }
8823
8824 /* When aligning with the case statement, don't align
8825 * with a statement after it.
8826 * case 1: { <-- don't use this { position
8827 * stat;
8828 * }
8829 * case 2:
8830 * stat;
8831 * }
8832 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008833 iscase = (curbuf->b_ind_keep_case_label
8834 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835
8836 /*
8837 * Get indent and pointer to text for current line,
8838 * ignoring any jump label.
8839 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008840 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841
8842 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008843 amount += curbuf->b_ind_open_extra;
8844 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008845 l = skipwhite(l);
8846 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008847 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8849
8850 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008851 * When a terminated line starts with "else" skip to
8852 * the matching "if":
8853 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008854 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008855 * Need to use the scope of this "else". XXX
8856 * If whilelevel != 0 continue looking for a "do {".
8857 */
8858 if (lookfor == LOOKFOR_TERM
8859 && *l != '}'
8860 && cin_iselse(l)
8861 && whilelevel == 0)
8862 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008863 if ((trypos = find_start_brace()) == NULL
8864 || find_match(LOOKFOR_IF, trypos->lnum)
8865 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008866 break;
8867 continue;
8868 }
8869
8870 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 * If we're at the end of a block, skip to the start of
8872 * that block.
8873 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008874 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008875 if (find_last_paren(l, '{', '}') /* XXX */
8876 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008878 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 /* if not "else {" check for terminated again */
8880 /* but skip block for "} else {" */
8881 l = cin_skipcomment(ml_get_curline());
8882 if (*l == '}' || !cin_iselse(l))
8883 goto term_again;
8884 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008885 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 }
8887 }
8888 }
8889 }
8890 }
8891 }
8892
8893 /* add extra indent for a comment */
8894 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008895 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008896
8897 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008898 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
8899 amount -= curbuf->b_ind_jump_label;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008900
8901 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008903
8904 /*
8905 * ok -- we're not inside any sort of structure at all!
8906 *
8907 * This means we're at the top level, and everything should
8908 * basically just match where the previous line is, except
8909 * for the lines immediately following a function declaration,
8910 * which are K&R-style parameters and need to be indented.
8911 *
8912 * if our line starts with an open brace, forget about any
8913 * prevailing indent and make sure it looks like the start
8914 * of a function
8915 */
8916
8917 if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008919 amount = curbuf->b_ind_first_open;
8920 goto theend;
8921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008923 /*
8924 * If the NEXT line is a function declaration, the current
8925 * line needs to be indented as a function type spec.
8926 * Don't do this if the current line looks like a comment or if the
8927 * current line is terminated, ie. ends in ';', or if the current line
8928 * contains { or }: "void f() {\n if (1)"
8929 */
8930 if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8931 && !cin_nocode(theline)
8932 && vim_strchr(theline, '{') == NULL
8933 && vim_strchr(theline, '}') == NULL
8934 && !cin_ends_in(theline, (char_u *)":", NULL)
8935 && !cin_ends_in(theline, (char_u *)",", NULL)
8936 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
8937 cur_curpos.lnum + 1)
8938 && !cin_isterminated(theline, FALSE, TRUE))
8939 {
8940 amount = curbuf->b_ind_func_type;
8941 goto theend;
8942 }
8943
8944 /* search backwards until we find something we recognize */
8945 amount = 0;
8946 curwin->w_cursor = cur_curpos;
8947 while (curwin->w_cursor.lnum > 1)
8948 {
8949 curwin->w_cursor.lnum--;
8950 curwin->w_cursor.col = 0;
8951
8952 l = ml_get_curline();
8953
8954 /*
8955 * If we're in a comment or raw string now, skip to the start
8956 * of it.
8957 */ /* XXX */
8958 if ((trypos = ind_find_start_CORS()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008960 curwin->w_cursor.lnum = trypos->lnum + 1;
8961 curwin->w_cursor.col = 0;
8962 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 }
8964
8965 /*
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008966 * Are we at the start of a cpp base class declaration or
8967 * constructor initialization?
8968 */ /* XXX */
8969 n = FALSE;
8970 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008972 n = cin_is_cpp_baseclass(&cache_cpp_baseclass);
8973 l = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 }
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008975 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008977 /* XXX */
8978 amount = get_baseclass_amount(cache_cpp_baseclass.lpos.col);
8979 break;
8980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02008982 /*
8983 * Skip preprocessor directives and blank lines.
8984 */
8985 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
8986 continue;
8987
8988 if (cin_nocode(l))
8989 continue;
8990
8991 /*
8992 * If the previous line ends in ',', use one level of
8993 * indentation:
8994 * int foo,
8995 * bar;
8996 * do this before checking for '}' in case of eg.
8997 * enum foobar
8998 * {
8999 * ...
9000 * } foo,
9001 * bar;
9002 */
9003 n = 0;
9004 if (cin_ends_in(l, (char_u *)",", NULL)
9005 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
9006 {
9007 /* take us back to opening paren */
9008 if (find_last_paren(l, '(', ')')
9009 && (trypos = find_match_paren(
9010 curbuf->b_ind_maxparen)) != NULL)
9011 curwin->w_cursor = *trypos;
9012
9013 /* For a line ending in ',' that is a continuation line go
9014 * back to the first line with a backslash:
9015 * char *foo = "bla\
9016 * bla",
9017 * here;
9018 */
9019 while (n == 0 && curwin->w_cursor.lnum > 1)
9020 {
9021 l = ml_get(curwin->w_cursor.lnum - 1);
9022 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
9023 break;
9024 --curwin->w_cursor.lnum;
9025 curwin->w_cursor.col = 0;
9026 }
9027
9028 amount = get_indent(); /* XXX */
9029
9030 if (amount == 0)
9031 amount = cin_first_id_amount();
9032 if (amount == 0)
9033 amount = ind_continuation;
9034 break;
9035 }
9036
9037 /*
9038 * If the line looks like a function declaration, and we're
9039 * not in a comment, put it the left margin.
9040 */
9041 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
9042 break;
9043 l = ml_get_curline();
9044
9045 /*
9046 * Finding the closing '}' of a previous function. Put
9047 * current line at the left margin. For when 'cino' has "fs".
9048 */
9049 if (*skipwhite(l) == '}')
9050 break;
9051
9052 /* (matching {)
9053 * If the previous line ends on '};' (maybe followed by
9054 * comments) align at column 0. For example:
9055 * char *string_array[] = { "foo",
9056 * / * x * / "b};ar" }; / * foobar * /
9057 */
9058 if (cin_ends_in(l, (char_u *)"};", NULL))
9059 break;
9060
9061 /*
9062 * If the previous line ends on '[' we are probably in an
9063 * array constant:
9064 * something = [
9065 * 234, <- extra indent
9066 */
9067 if (cin_ends_in(l, (char_u *)"[", NULL))
9068 {
9069 amount = get_indent() + ind_continuation;
9070 break;
9071 }
9072
9073 /*
9074 * Find a line only has a semicolon that belongs to a previous
9075 * line ending in '}', e.g. before an #endif. Don't increase
9076 * indent then.
9077 */
9078 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
9079 {
9080 pos_T curpos_save = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081
9082 while (curwin->w_cursor.lnum > 1)
9083 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009084 look = ml_get(--curwin->w_cursor.lnum);
9085 if (!(cin_nocode(look) || cin_ispreproc_cont(
9086 &look, &curwin->w_cursor.lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 break;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009088 }
9089 if (curwin->w_cursor.lnum > 0
9090 && cin_ends_in(look, (char_u *)"}", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009093 curwin->w_cursor = curpos_save;
9094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009096 /*
9097 * If the PREVIOUS line is a function declaration, the current
9098 * line (and the ones that follow) needs to be indented as
9099 * parameters.
9100 */
9101 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
9102 {
9103 amount = curbuf->b_ind_param;
9104 break;
9105 }
9106
9107 /*
9108 * If the previous line ends in ';' and the line before the
9109 * previous line ends in ',' or '\', ident to column zero:
9110 * int foo,
9111 * bar;
9112 * indent_to_0 here;
9113 */
9114 if (cin_ends_in(l, (char_u *)";", NULL))
9115 {
9116 l = ml_get(curwin->w_cursor.lnum - 1);
9117 if (cin_ends_in(l, (char_u *)",", NULL)
9118 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
9119 break;
9120 l = ml_get_curline();
9121 }
9122
9123 /*
9124 * Doesn't look like anything interesting -- so just
9125 * use the indent of this line.
9126 *
9127 * Position the cursor over the rightmost paren, so that
9128 * matching it will take us back to the start of the line.
9129 */
9130 find_last_paren(l, '(', ')');
9131
9132 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
9133 curwin->w_cursor = *trypos;
9134 amount = get_indent(); /* XXX */
9135 break;
9136 }
9137
9138 /* add extra indent for a comment */
9139 if (cin_iscomment(theline))
9140 amount += curbuf->b_ind_comment;
9141
9142 /* add extra indent if the previous line ended in a backslash:
9143 * "asdfasdf\
9144 * here";
9145 * char *foo = "asdf\
9146 * here";
9147 */
9148 if (cur_curpos.lnum > 1)
9149 {
9150 l = ml_get(cur_curpos.lnum - 1);
9151 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
9152 {
9153 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
9154 if (cur_amount > 0)
9155 amount = cur_amount;
9156 else if (cur_amount == 0)
9157 amount += ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 }
9159 }
9160
9161theend:
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02009162 if (amount < 0)
9163 amount = 0;
9164
9165laterend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 /* put the cursor back where it belongs */
9167 curwin->w_cursor = cur_curpos;
9168
9169 vim_free(linecopy);
9170
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 return amount;
9172}
9173
9174 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009175find_match(lookfor, ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 int lookfor;
9177 linenr_T ourscope;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178{
9179 char_u *look;
9180 pos_T *theirscope;
9181 char_u *mightbeif;
9182 int elselevel;
9183 int whilelevel;
9184
9185 if (lookfor == LOOKFOR_IF)
9186 {
9187 elselevel = 1;
9188 whilelevel = 0;
9189 }
9190 else
9191 {
9192 elselevel = 0;
9193 whilelevel = 1;
9194 }
9195
9196 curwin->w_cursor.col = 0;
9197
9198 while (curwin->w_cursor.lnum > ourscope + 1)
9199 {
9200 curwin->w_cursor.lnum--;
9201 curwin->w_cursor.col = 0;
9202
9203 look = cin_skipcomment(ml_get_curline());
9204 if (cin_iselse(look)
9205 || cin_isif(look)
9206 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009207 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 {
9209 /*
9210 * if we've gone outside the braces entirely,
9211 * we must be out of scope...
9212 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009213 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 if (theirscope == NULL)
9215 break;
9216
9217 /*
9218 * and if the brace enclosing this is further
9219 * back than the one enclosing the else, we're
9220 * out of luck too.
9221 */
9222 if (theirscope->lnum < ourscope)
9223 break;
9224
9225 /*
9226 * and if they're enclosed in a *deeper* brace,
9227 * then we can ignore it because it's in a
9228 * different scope...
9229 */
9230 if (theirscope->lnum > ourscope)
9231 continue;
9232
9233 /*
9234 * if it was an "else" (that's not an "else if")
9235 * then we need to go back to another if, so
9236 * increment elselevel
9237 */
9238 look = cin_skipcomment(ml_get_curline());
9239 if (cin_iselse(look))
9240 {
9241 mightbeif = cin_skipcomment(look + 4);
9242 if (!cin_isif(mightbeif))
9243 ++elselevel;
9244 continue;
9245 }
9246
9247 /*
9248 * if it was a "while" then we need to go back to
9249 * another "do", so increment whilelevel. XXX
9250 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01009251 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 {
9253 ++whilelevel;
9254 continue;
9255 }
9256
9257 /* If it's an "if" decrement elselevel */
9258 look = cin_skipcomment(ml_get_curline());
9259 if (cin_isif(look))
9260 {
9261 elselevel--;
9262 /*
9263 * When looking for an "if" ignore "while"s that
9264 * get in the way.
9265 */
9266 if (elselevel == 0 && lookfor == LOOKFOR_IF)
9267 whilelevel = 0;
9268 }
9269
9270 /* If it's a "do" decrement whilelevel */
9271 if (cin_isdo(look))
9272 whilelevel--;
9273
9274 /*
9275 * if we've used up all the elses, then
9276 * this must be the if that we want!
9277 * match the indent level of that if.
9278 */
9279 if (elselevel <= 0 && whilelevel <= 0)
9280 {
9281 return OK;
9282 }
9283 }
9284 }
9285 return FAIL;
9286}
9287
9288# if defined(FEAT_EVAL) || defined(PROTO)
9289/*
9290 * Get indent level from 'indentexpr'.
9291 */
9292 int
9293get_expr_indent()
9294{
9295 int indent;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009296 pos_T save_pos;
9297 colnr_T save_curswant;
9298 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009300 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
9301 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009303 /* Save and restore cursor position and curswant, in case it was changed
9304 * via :normal commands */
9305 save_pos = curwin->w_cursor;
9306 save_curswant = curwin->w_curswant;
9307 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009309 if (use_sandbox)
9310 ++sandbox;
9311 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 indent = eval_to_number(curbuf->b_p_inde);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00009313 if (use_sandbox)
9314 --sandbox;
9315 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316
9317 /* Restore the cursor position so that 'indentexpr' doesn't need to.
9318 * Pretend to be in Insert mode, allow cursor past end of line for "o"
9319 * command. */
9320 save_State = State;
9321 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01009322 curwin->w_cursor = save_pos;
9323 curwin->w_curswant = save_curswant;
9324 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 check_cursor();
9326 State = save_State;
9327
9328 /* If there is an error, just keep the current indent. */
9329 if (indent < 0)
9330 indent = get_indent();
9331
9332 return indent;
9333}
9334# endif
9335
9336#endif /* FEAT_CINDENT */
9337
9338#if defined(FEAT_LISP) || defined(PROTO)
9339
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009340static int lisp_match(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341
9342 static int
9343lisp_match(p)
9344 char_u *p;
9345{
9346 char_u buf[LSIZE];
9347 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01009348 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349
9350 while (*word != NUL)
9351 {
9352 (void)copy_option_part(&word, buf, LSIZE, ",");
9353 len = (int)STRLEN(buf);
9354 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
9355 return TRUE;
9356 }
9357 return FALSE;
9358}
9359
9360/*
9361 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
9362 * The incompatible newer method is quite a bit better at indenting
9363 * code in lisp-like languages than the traditional one; it's still
9364 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
9365 *
9366 * TODO:
9367 * Findmatch() should be adapted for lisp, also to make showmatch
9368 * work correctly: now (v5.3) it seems all C/C++ oriented:
9369 * - it does not recognize the #\( and #\) notations as character literals
9370 * - it doesn't know about comments starting with a semicolon
9371 * - it incorrectly interprets '(' as a character literal
9372 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009373 * Update from Sergey Khorev:
9374 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 */
9376 int
9377get_lisp_indent()
9378{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009379 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 int amount;
9381 char_u *that;
9382 colnr_T col;
9383 colnr_T firsttry;
9384 int parencount, quotecount;
9385 int vi_lisp;
9386
9387 /* Set vi_lisp to use the vi-compatible method */
9388 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
9389
9390 realpos = curwin->w_cursor;
9391 curwin->w_cursor.col = 0;
9392
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009393 if ((pos = findmatch(NULL, '(')) == NULL)
9394 pos = findmatch(NULL, '[');
9395 else
9396 {
9397 paren = *pos;
9398 pos = findmatch(NULL, '[');
9399 if (pos == NULL || ltp(pos, &paren))
9400 pos = &paren;
9401 }
9402 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 {
9404 /* Extra trick: Take the indent of the first previous non-white
9405 * line that is at the same () level. */
9406 amount = -1;
9407 parencount = 0;
9408
9409 while (--curwin->w_cursor.lnum >= pos->lnum)
9410 {
9411 if (linewhite(curwin->w_cursor.lnum))
9412 continue;
9413 for (that = ml_get_curline(); *that != NUL; ++that)
9414 {
9415 if (*that == ';')
9416 {
9417 while (*(that + 1) != NUL)
9418 ++that;
9419 continue;
9420 }
9421 if (*that == '\\')
9422 {
9423 if (*(that + 1) != NUL)
9424 ++that;
9425 continue;
9426 }
9427 if (*that == '"' && *(that + 1) != NUL)
9428 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009429 while (*++that && *that != '"')
9430 {
9431 /* skipping escaped characters in the string */
9432 if (*that == '\\')
9433 {
9434 if (*++that == NUL)
9435 break;
9436 if (that[1] == NUL)
9437 {
9438 ++that;
9439 break;
9440 }
9441 }
9442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009444 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009446 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 --parencount;
9448 }
9449 if (parencount == 0)
9450 {
9451 amount = get_indent();
9452 break;
9453 }
9454 }
9455
9456 if (amount == -1)
9457 {
9458 curwin->w_cursor.lnum = pos->lnum;
9459 curwin->w_cursor.col = pos->col;
9460 col = pos->col;
9461
9462 that = ml_get_curline();
9463
9464 if (vi_lisp && get_indent() == 0)
9465 amount = 2;
9466 else
9467 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009468 char_u *line = that;
9469
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 amount = 0;
9471 while (*that && col)
9472 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009473 amount += lbr_chartabsize_adv(line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 col--;
9475 }
9476
9477 /*
9478 * Some keywords require "body" indenting rules (the
9479 * non-standard-lisp ones are Scheme special forms):
9480 *
9481 * (let ((a 1)) instead (let ((a 1))
9482 * (...)) of (...))
9483 */
9484
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009485 if (!vi_lisp && (*that == '(' || *that == '[')
9486 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 amount += 2;
9488 else
9489 {
9490 that++;
9491 amount++;
9492 firsttry = amount;
9493
9494 while (vim_iswhite(*that))
9495 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009496 amount += lbr_chartabsize(line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 ++that;
9498 }
9499
9500 if (*that && *that != ';') /* not a comment line */
9501 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009502 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009504 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 firsttry++;
9506
9507 parencount = 0;
9508 quotecount = 0;
9509
9510 if (vi_lisp
9511 || (*that != '"'
9512 && *that != '\''
9513 && *that != '#'
9514 && (*that < '0' || *that > '9')))
9515 {
9516 while (*that
9517 && (!vim_iswhite(*that)
9518 || quotecount
9519 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009520 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 && !quotecount
9522 && !parencount
9523 && vi_lisp)))
9524 {
9525 if (*that == '"')
9526 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009527 if ((*that == '(' || *that == '[')
9528 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009530 if ((*that == ')' || *that == ']')
9531 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 --parencount;
9533 if (*that == '\\' && *(that+1) != NUL)
Bram Moolenaar597a4222014-06-25 14:39:50 +02009534 amount += lbr_chartabsize_adv(
9535 line, &that, (colnr_T)amount);
9536 amount += lbr_chartabsize_adv(
9537 line, &that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 }
9539 }
9540 while (vim_iswhite(*that))
9541 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009542 amount += lbr_chartabsize(
9543 line, that, (colnr_T)amount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 that++;
9545 }
9546 if (!*that || *that == ';')
9547 amount = firsttry;
9548 }
9549 }
9550 }
9551 }
9552 }
9553 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009554 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555
9556 curwin->w_cursor = realpos;
9557
9558 return amount;
9559}
9560#endif /* FEAT_LISP */
9561
9562 void
9563prepare_to_exit()
9564{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009565#if defined(SIGHUP) && defined(SIG_IGN)
9566 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9567 * makes Vim exit and then handling SIGHUP causes various reentrance
9568 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009569 signal(SIGHUP, SIG_IGN);
9570#endif
9571
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572#ifdef FEAT_GUI
9573 if (gui.in_use)
9574 {
9575 gui.dying = TRUE;
9576 out_trash(); /* trash any pending output */
9577 }
9578 else
9579#endif
9580 {
9581 windgoto((int)Rows - 1, 0);
9582
9583 /*
9584 * Switch terminal mode back now, so messages end up on the "normal"
9585 * screen (if there are two screens).
9586 */
9587 settmode(TMODE_COOK);
9588#ifdef WIN3264
9589 if (can_end_termcap_mode(FALSE) == TRUE)
9590#endif
9591 stoptermcap();
9592 out_flush();
9593 }
9594}
9595
9596/*
9597 * Preserve files and exit.
9598 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009599 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9600 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 */
9602 void
9603preserve_exit()
9604{
9605 buf_T *buf;
9606
9607 prepare_to_exit();
9608
Bram Moolenaar4770d092006-01-12 23:22:24 +00009609 /* Setting this will prevent free() calls. That avoids calling free()
9610 * recursively when free() was invoked with a bad pointer. */
9611 really_exiting = TRUE;
9612
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 out_str(IObuff);
9614 screen_start(); /* don't know where cursor is now */
9615 out_flush();
9616
9617 ml_close_notmod(); /* close all not-modified buffers */
9618
9619 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9620 {
9621 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9622 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009623 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 screen_start(); /* don't know where cursor is now */
9625 out_flush();
9626 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9627 break;
9628 }
9629 }
9630
9631 ml_close_all(FALSE); /* close all memfiles, without deleting */
9632
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009633 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634
9635 getout(1);
9636}
9637
9638/*
9639 * return TRUE if "fname" exists.
9640 */
9641 int
9642vim_fexists(fname)
9643 char_u *fname;
9644{
9645 struct stat st;
9646
9647 if (mch_stat((char *)fname, &st))
9648 return FALSE;
9649 return TRUE;
9650}
9651
9652/*
9653 * Check for CTRL-C pressed, but only once in a while.
9654 * Should be used instead of ui_breakcheck() for functions that check for
9655 * each line in the file. Calling ui_breakcheck() each time takes too much
9656 * time, because it can be a system call.
9657 */
9658
9659#ifndef BREAKCHECK_SKIP
9660# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9661# define BREAKCHECK_SKIP 200
9662# else
9663# define BREAKCHECK_SKIP 32
9664# endif
9665#endif
9666
9667static int breakcheck_count = 0;
9668
9669 void
9670line_breakcheck()
9671{
9672 if (++breakcheck_count >= BREAKCHECK_SKIP)
9673 {
9674 breakcheck_count = 0;
9675 ui_breakcheck();
9676 }
9677}
9678
9679/*
9680 * Like line_breakcheck() but check 10 times less often.
9681 */
9682 void
9683fast_breakcheck()
9684{
9685 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9686 {
9687 breakcheck_count = 0;
9688 ui_breakcheck();
9689 }
9690}
9691
9692/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009693 * Invoke expand_wildcards() for one pattern.
9694 * Expand items like "%:h" before the expansion.
9695 * Returns OK or FAIL.
9696 */
9697 int
9698expand_wildcards_eval(pat, num_file, file, flags)
9699 char_u **pat; /* pointer to input pattern */
9700 int *num_file; /* resulting number of files */
9701 char_u ***file; /* array of resulting files */
9702 int flags; /* EW_DIR, etc. */
9703{
9704 int ret = FAIL;
9705 char_u *eval_pat = NULL;
9706 char_u *exp_pat = *pat;
9707 char_u *ignored_msg;
9708 int usedlen;
9709
9710 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9711 {
9712 ++emsg_off;
9713 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9714 NULL, &ignored_msg, NULL);
9715 --emsg_off;
9716 if (eval_pat != NULL)
9717 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9718 }
9719
9720 if (exp_pat != NULL)
9721 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9722
9723 if (eval_pat != NULL)
9724 {
9725 vim_free(exp_pat);
9726 vim_free(eval_pat);
9727 }
9728
9729 return ret;
9730}
9731
9732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9734 * 'wildignore'.
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009735 * Returns OK or FAIL. When FAIL then "num_files" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 */
9737 int
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009738expand_wildcards(num_pat, pat, num_files, files, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 int num_pat; /* number of input patterns */
9740 char_u **pat; /* array of input patterns */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009741 int *num_files; /* resulting number of files */
9742 char_u ***files; /* array of resulting files */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 int flags; /* EW_DIR, etc. */
9744{
9745 int retval;
9746 int i, j;
9747 char_u *p;
9748 int non_suf_match; /* number without matching suffix */
9749
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009750 retval = gen_expand_wildcards(num_pat, pat, num_files, files, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751
9752 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009753 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754 return retval;
9755
9756#ifdef FEAT_WILDIGN
9757 /*
9758 * Remove names that match 'wildignore'.
9759 */
9760 if (*p_wig)
9761 {
9762 char_u *ffname;
9763
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009764 /* check all files in (*files)[] */
9765 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009767 ffname = FullName_save((*files)[i], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 if (ffname == NULL) /* out of memory */
9769 break;
9770# ifdef VMS
9771 vms_remove_version(ffname);
9772# endif
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009773 if (match_file_list(p_wig, (*files)[i], ffname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009775 /* remove this matching files from the list */
9776 vim_free((*files)[i]);
9777 for (j = i; j + 1 < *num_files; ++j)
9778 (*files)[j] = (*files)[j + 1];
9779 --*num_files;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 --i;
9781 }
9782 vim_free(ffname);
9783 }
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009784
9785 /* If the number of matches is now zero, we fail. */
9786 if (*num_files == 0)
9787 {
9788 vim_free(*files);
9789 *files = NULL;
9790 return FAIL;
9791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 }
9793#endif
9794
9795 /*
9796 * Move the names where 'suffixes' match to the end.
9797 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009798 if (*num_files > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 {
9800 non_suf_match = 0;
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009801 for (i = 0; i < *num_files; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802 {
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009803 if (!match_suffix((*files)[i]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 {
9805 /*
9806 * Move the name without matching suffix to the front
9807 * of the list.
9808 */
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009809 p = (*files)[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 for (j = i; j > non_suf_match; --j)
Bram Moolenaar7b256fe2015-09-15 19:05:59 +02009811 (*files)[j] = (*files)[j - 1];
9812 (*files)[non_suf_match++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 }
9814 }
9815 }
9816
9817 return retval;
9818}
9819
9820/*
9821 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9822 */
9823 int
9824match_suffix(fname)
9825 char_u *fname;
9826{
9827 int fnamelen, setsuflen;
9828 char_u *setsuf;
9829#define MAXSUFLEN 30 /* maximum length of a file suffix */
9830 char_u suf_buf[MAXSUFLEN];
9831
9832 fnamelen = (int)STRLEN(fname);
9833 setsuflen = 0;
9834 for (setsuf = p_su; *setsuf; )
9835 {
9836 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009837 if (setsuflen == 0)
9838 {
9839 char_u *tail = gettail(fname);
9840
9841 /* empty entry: match name without a '.' */
9842 if (vim_strchr(tail, '.') == NULL)
9843 {
9844 setsuflen = 1;
9845 break;
9846 }
9847 }
9848 else
9849 {
9850 if (fnamelen >= setsuflen
9851 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9852 (size_t)setsuflen) == 0)
9853 break;
9854 setsuflen = 0;
9855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 }
9857 return (setsuflen != 0);
9858}
9859
9860#if !defined(NO_EXPANDPATH) || defined(PROTO)
9861
9862# ifdef VIM_BACKTICK
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01009863static int vim_backtick(char_u *p);
9864static int expand_backtick(garray_T *gap, char_u *pat, int flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865# endif
9866
9867# if defined(MSDOS) || defined(FEAT_GUI_W16) || defined(WIN3264)
9868/*
9869 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9870 * it's shared between these systems.
9871 */
9872# if defined(DJGPP) || defined(PROTO)
9873# define _cdecl /* DJGPP doesn't have this */
9874# else
9875# ifdef __BORLANDC__
9876# define _cdecl _RTLENTRYF
9877# endif
9878# endif
9879
9880/*
9881 * comparison function for qsort in dos_expandpath()
9882 */
9883 static int _cdecl
9884pstrcmp(const void *a, const void *b)
9885{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009886 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887}
9888
9889# ifndef WIN3264
9890 static void
9891namelowcpy(
9892 char_u *d,
9893 char_u *s)
9894{
9895# ifdef DJGPP
9896 if (USE_LONG_FNAME) /* don't lower case on Windows 95/NT systems */
9897 while (*s)
9898 *d++ = *s++;
9899 else
9900# endif
9901 while (*s)
9902 *d++ = TOLOWER_LOC(*s++);
9903 *d = NUL;
9904}
9905# endif
9906
9907/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009908 * Recursively expand one path component into all matching files and/or
9909 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910 * Return the number of matches found.
9911 * "path" has backslashes before chars that are not to be expanded, starting
9912 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009913 * Return the number of matches found.
9914 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915 */
9916 static int
9917dos_expandpath(
9918 garray_T *gap,
9919 char_u *path,
9920 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009921 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009922 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009924 char_u *buf;
9925 char_u *path_end;
9926 char_u *p, *s, *e;
9927 int start_len = gap->ga_len;
9928 char_u *pat;
9929 regmatch_T regmatch;
9930 int starts_with_dot;
9931 int matches;
9932 int len;
9933 int starstar = FALSE;
9934 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935#ifdef WIN3264
9936 WIN32_FIND_DATA fb;
9937 HANDLE hFind = (HANDLE)0;
9938# ifdef FEAT_MBYTE
9939 WIN32_FIND_DATAW wfb;
9940 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9941# endif
9942#else
9943 struct ffblk fb;
9944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009946 int ok;
9947
9948 /* Expanding "**" may take a long time, check for CTRL-C. */
9949 if (stardepth > 0)
9950 {
9951 ui_breakcheck();
9952 if (got_int)
9953 return 0;
9954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955
Bram Moolenaar7314efd2015-10-31 15:32:52 +01009956 /* Make room for file name. When doing encoding conversion the actual
9957 * length may be quite a bit longer, thus use the maximum possible length. */
9958 buf = alloc((int)MAXPATHL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959 if (buf == NULL)
9960 return 0;
9961
9962 /*
9963 * Find the first part in the path name that contains a wildcard or a ~1.
9964 * Copy it into buf, including the preceding characters.
9965 */
9966 p = buf;
9967 s = buf;
9968 e = NULL;
9969 path_end = path;
9970 while (*path_end != NUL)
9971 {
9972 /* May ignore a wildcard that has a backslash before it; it will
9973 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9974 if (path_end >= path + wildoff && rem_backslash(path_end))
9975 *p++ = *path_end++;
9976 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9977 {
9978 if (e != NULL)
9979 break;
9980 s = p + 1;
9981 }
9982 else if (path_end >= path + wildoff
9983 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9984 e = p;
9985#ifdef FEAT_MBYTE
9986 if (has_mbyte)
9987 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009988 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989 STRNCPY(p, path_end, len);
9990 p += len;
9991 path_end += len;
9992 }
9993 else
9994#endif
9995 *p++ = *path_end++;
9996 }
9997 e = p;
9998 *e = NUL;
9999
10000 /* now we have one wildcard component between s and e */
10001 /* Remove backslashes between "wildoff" and the start of the wildcard
10002 * component. */
10003 for (p = buf + wildoff; p < s; ++p)
10004 if (rem_backslash(p))
10005 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010006 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007 --e;
10008 --s;
10009 }
10010
Bram Moolenaar231334e2005-07-25 20:46:57 +000010011 /* Check for "**" between "s" and "e". */
10012 for (p = s; p < e; ++p)
10013 if (p[0] == '*' && p[1] == '*')
10014 starstar = TRUE;
10015
Bram Moolenaard82103e2016-01-17 17:04:05 +010010016 starts_with_dot = *s == '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10018 if (pat == NULL)
10019 {
10020 vim_free(buf);
10021 return 0;
10022 }
10023
10024 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010025 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010026 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 regmatch.rm_ic = TRUE; /* Always ignore case */
10028 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010029 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +020010030 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 vim_free(pat);
10032
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010033 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034 {
10035 vim_free(buf);
10036 return 0;
10037 }
10038
10039 /* remember the pattern or file name being looked for */
10040 matchname = vim_strsave(s);
10041
Bram Moolenaar231334e2005-07-25 20:46:57 +000010042 /* If "**" is by itself, this is the first time we encounter it and more
10043 * is following then find matches without any directory. */
10044 if (!didstar && stardepth < 100 && starstar && e - s == 2
10045 && *path_end == '/')
10046 {
10047 STRCPY(s, path_end + 1);
10048 ++stardepth;
10049 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10050 --stardepth;
10051 }
10052
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 /* Scan all files in the directory with "dir/ *.*" */
10054 STRCPY(s, "*.*");
10055#ifdef WIN3264
10056# ifdef FEAT_MBYTE
10057 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
10058 {
10059 /* The active codepage differs from 'encoding'. Attempt using the
10060 * wide function. If it fails because it is not implemented fall back
10061 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010062 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 if (wn != NULL)
10064 {
10065 hFind = FindFirstFileW(wn, &wfb);
10066 if (hFind == INVALID_HANDLE_VALUE
10067 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
10068 {
10069 vim_free(wn);
10070 wn = NULL;
10071 }
10072 }
10073 }
10074
10075 if (wn == NULL)
10076# endif
10077 hFind = FindFirstFile(buf, &fb);
10078 ok = (hFind != INVALID_HANDLE_VALUE);
10079#else
10080 /* If we are expanding wildcards we try both files and directories */
10081 ok = (findfirst((char *)buf, &fb,
10082 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
10083#endif
10084
10085 while (ok)
10086 {
10087#ifdef WIN3264
10088# ifdef FEAT_MBYTE
10089 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010090 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091 else
10092# endif
10093 p = (char_u *)fb.cFileName;
10094#else
10095 p = (char_u *)fb.ff_name;
10096#endif
10097 /* Ignore entries starting with a dot, unless when asked for. Accept
10098 * all entries found with "matchname". */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010099 if ((p[0] != '.' || starts_with_dot
10100 || ((flags & EW_DODOT)
10101 && p[1] != NUL && (p[1] != '.' || p[2] != NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010103 || (regmatch.regprog != NULL
10104 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010105 || ((flags & EW_NOTWILD)
10106 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 {
10108#ifdef WIN3264
10109 STRCPY(s, p);
10110#else
10111 namelowcpy(s, p);
10112#endif
10113 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010114
10115 if (starstar && stardepth < 100)
10116 {
10117 /* For "**" in the pattern first go deeper in the tree to
10118 * find matches. */
10119 STRCPY(buf + len, "/**");
10120 STRCPY(buf + len + 3, path_end);
10121 ++stardepth;
10122 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
10123 --stardepth;
10124 }
10125
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 STRCPY(buf + len, path_end);
10127 if (mch_has_exp_wildcard(path_end))
10128 {
10129 /* need to expand another component of the path */
10130 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010131 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132 }
10133 else
10134 {
10135 /* no more wildcards, check if there is a match */
10136 /* remove backslashes for the remaining components only */
10137 if (*path_end != 0)
10138 backslash_halve(buf + len + 1);
10139 if (mch_getperm(buf) >= 0) /* add existing file */
10140 addfile(gap, buf, flags);
10141 }
10142 }
10143
10144#ifdef WIN3264
10145# ifdef FEAT_MBYTE
10146 if (wn != NULL)
10147 {
10148 vim_free(p);
10149 ok = FindNextFileW(hFind, &wfb);
10150 }
10151 else
10152# endif
10153 ok = FindNextFile(hFind, &fb);
10154#else
10155 ok = (findnext(&fb) == 0);
10156#endif
10157
10158 /* If no more matches and no match was used, try expanding the name
10159 * itself. Finds the long name of a short filename. */
10160 if (!ok && matchname != NULL && gap->ga_len == start_len)
10161 {
10162 STRCPY(s, matchname);
10163#ifdef WIN3264
10164 FindClose(hFind);
10165# ifdef FEAT_MBYTE
10166 if (wn != NULL)
10167 {
10168 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +000010169 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 if (wn != NULL)
10171 hFind = FindFirstFileW(wn, &wfb);
10172 }
10173 if (wn == NULL)
10174# endif
10175 hFind = FindFirstFile(buf, &fb);
10176 ok = (hFind != INVALID_HANDLE_VALUE);
10177#else
10178 ok = (findfirst((char *)buf, &fb,
10179 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
10180#endif
10181 vim_free(matchname);
10182 matchname = NULL;
10183 }
10184 }
10185
10186#ifdef WIN3264
10187 FindClose(hFind);
10188# ifdef FEAT_MBYTE
10189 vim_free(wn);
10190# endif
10191#endif
10192 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010193 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194 vim_free(matchname);
10195
10196 matches = gap->ga_len - start_len;
10197 if (matches > 0)
10198 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
10199 sizeof(char_u *), pstrcmp);
10200 return matches;
10201}
10202
10203 int
10204mch_expandpath(
10205 garray_T *gap,
10206 char_u *path,
10207 int flags) /* EW_* flags */
10208{
Bram Moolenaar231334e2005-07-25 20:46:57 +000010209 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210}
10211# endif /* MSDOS || FEAT_GUI_W16 || WIN3264 */
10212
Bram Moolenaar231334e2005-07-25 20:46:57 +000010213#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
10214 || defined(PROTO)
10215/*
10216 * Unix style wildcard expansion code.
10217 * It's here because it's used both for Unix and Mac.
10218 */
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010219static int pstrcmp(const void *, const void *);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010220
10221 static int
10222pstrcmp(a, b)
10223 const void *a, *b;
10224{
10225 return (pathcmp(*(char **)a, *(char **)b, -1));
10226}
10227
10228/*
10229 * Recursively expand one path component into all matching files and/or
10230 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
10231 * "path" has backslashes before chars that are not to be expanded, starting
10232 * at "path + wildoff".
10233 * Return the number of matches found.
10234 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
10235 */
10236 int
10237unix_expandpath(gap, path, wildoff, flags, didstar)
10238 garray_T *gap;
10239 char_u *path;
10240 int wildoff;
10241 int flags; /* EW_* flags */
10242 int didstar; /* expanded "**" once already */
10243{
10244 char_u *buf;
10245 char_u *path_end;
10246 char_u *p, *s, *e;
10247 int start_len = gap->ga_len;
10248 char_u *pat;
10249 regmatch_T regmatch;
10250 int starts_with_dot;
10251 int matches;
10252 int len;
10253 int starstar = FALSE;
10254 static int stardepth = 0; /* depth for "**" expansion */
10255
10256 DIR *dirp;
10257 struct dirent *dp;
10258
10259 /* Expanding "**" may take a long time, check for CTRL-C. */
10260 if (stardepth > 0)
10261 {
10262 ui_breakcheck();
10263 if (got_int)
10264 return 0;
10265 }
10266
10267 /* make room for file name */
10268 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
10269 if (buf == NULL)
10270 return 0;
10271
10272 /*
10273 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010274 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +000010275 * Copy it into "buf", including the preceding characters.
10276 */
10277 p = buf;
10278 s = buf;
10279 e = NULL;
10280 path_end = path;
10281 while (*path_end != NUL)
10282 {
10283 /* May ignore a wildcard that has a backslash before it; it will
10284 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
10285 if (path_end >= path + wildoff && rem_backslash(path_end))
10286 *p++ = *path_end++;
10287 else if (*path_end == '/')
10288 {
10289 if (e != NULL)
10290 break;
10291 s = p + 1;
10292 }
10293 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +020010294 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010295 || (!p_fic && (flags & EW_ICASE)
10296 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010297 e = p;
10298#ifdef FEAT_MBYTE
10299 if (has_mbyte)
10300 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010301 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010302 STRNCPY(p, path_end, len);
10303 p += len;
10304 path_end += len;
10305 }
10306 else
10307#endif
10308 *p++ = *path_end++;
10309 }
10310 e = p;
10311 *e = NUL;
10312
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010313 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +000010314 /* Remove backslashes between "wildoff" and the start of the wildcard
10315 * component. */
10316 for (p = buf + wildoff; p < s; ++p)
10317 if (rem_backslash(p))
10318 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010319 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010320 --e;
10321 --s;
10322 }
10323
10324 /* Check for "**" between "s" and "e". */
10325 for (p = s; p < e; ++p)
10326 if (p[0] == '*' && p[1] == '*')
10327 starstar = TRUE;
10328
10329 /* convert the file pattern to a regexp pattern */
Bram Moolenaard82103e2016-01-17 17:04:05 +010010330 starts_with_dot = *s == '.';
Bram Moolenaar231334e2005-07-25 20:46:57 +000010331 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
10332 if (pat == NULL)
10333 {
10334 vim_free(buf);
10335 return 0;
10336 }
10337
10338 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +010010339 if (flags & EW_ICASE)
10340 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
10341 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +010010342 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010343 if (flags & (EW_NOERROR | EW_NOTWILD))
10344 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010345 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010346 if (flags & (EW_NOERROR | EW_NOTWILD))
10347 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +000010348 vim_free(pat);
10349
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010350 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010351 {
10352 vim_free(buf);
10353 return 0;
10354 }
10355
10356 /* If "**" is by itself, this is the first time we encounter it and more
10357 * is following then find matches without any directory. */
10358 if (!didstar && stardepth < 100 && starstar && e - s == 2
10359 && *path_end == '/')
10360 {
10361 STRCPY(s, path_end + 1);
10362 ++stardepth;
10363 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
10364 --stardepth;
10365 }
10366
10367 /* open the directory for scanning */
10368 *s = NUL;
10369 dirp = opendir(*buf == NUL ? "." : (char *)buf);
10370
10371 /* Find all matching entries */
10372 if (dirp != NULL)
10373 {
10374 for (;;)
10375 {
10376 dp = readdir(dirp);
10377 if (dp == NULL)
10378 break;
Bram Moolenaard82103e2016-01-17 17:04:05 +010010379 if ((dp->d_name[0] != '.' || starts_with_dot
10380 || ((flags & EW_DODOT)
10381 && dp->d_name[1] != NUL
10382 && (dp->d_name[1] != '.' || dp->d_name[2] != NUL)))
Bram Moolenaar1f5965b2012-01-10 18:37:58 +010010383 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
10384 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +020010385 || ((flags & EW_NOTWILD)
10386 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000010387 {
10388 STRCPY(s, dp->d_name);
10389 len = STRLEN(buf);
10390
10391 if (starstar && stardepth < 100)
10392 {
10393 /* For "**" in the pattern first go deeper in the tree to
10394 * find matches. */
10395 STRCPY(buf + len, "/**");
10396 STRCPY(buf + len + 3, path_end);
10397 ++stardepth;
10398 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
10399 --stardepth;
10400 }
10401
10402 STRCPY(buf + len, path_end);
10403 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
10404 {
10405 /* need to expand another component of the path */
10406 /* remove backslashes for the remaining components only */
10407 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
10408 }
10409 else
10410 {
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010411 struct stat sb;
10412
Bram Moolenaar231334e2005-07-25 20:46:57 +000010413 /* no more wildcards, check if there is a match */
10414 /* remove backslashes for the remaining components only */
10415 if (*path_end != NUL)
10416 backslash_halve(buf + len + 1);
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010417 /* add existing file or symbolic link */
Bram Moolenaarab11a592015-03-06 22:00:11 +010010418 if ((flags & EW_ALLLINKS) ? mch_lstat((char *)buf, &sb) >= 0
Bram Moolenaard8b77f72015-03-05 21:21:19 +010010419 : mch_getperm(buf) >= 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +000010420 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010421#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +000010422 size_t precomp_len = STRLEN(buf)+1;
10423 char_u *precomp_buf =
10424 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +000010425
Bram Moolenaar231334e2005-07-25 20:46:57 +000010426 if (precomp_buf)
10427 {
10428 mch_memmove(buf, precomp_buf, precomp_len);
10429 vim_free(precomp_buf);
10430 }
10431#endif
10432 addfile(gap, buf, flags);
10433 }
10434 }
10435 }
10436 }
10437
10438 closedir(dirp);
10439 }
10440
10441 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010442 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010443
10444 matches = gap->ga_len - start_len;
10445 if (matches > 0)
10446 qsort(((char_u **)gap->ga_data) + start_len, matches,
10447 sizeof(char_u *), pstrcmp);
10448 return matches;
10449}
10450#endif
10451
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010452#if defined(FEAT_SEARCHPATH)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010453static int find_previous_pathsep(char_u *path, char_u **psep);
10454static int is_unique(char_u *maybe_unique, garray_T *gap, int i);
10455static void expand_path_option(char_u *curdir, garray_T *gap);
10456static char_u *get_path_cutoff(char_u *fname, garray_T *gap);
10457static void uniquefy_paths(garray_T *gap, char_u *pattern);
10458static int expand_in_path(garray_T *gap, char_u *pattern, int flags);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010459
10460/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010461 * Moves "*psep" back to the previous path separator in "path".
10462 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010463 */
10464 static int
10465find_previous_pathsep(path, psep)
10466 char_u *path;
10467 char_u **psep;
10468{
10469 /* skip the current separator */
10470 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010471 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010472
10473 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010474 while (*psep > path)
10475 {
10476 if (vim_ispathsep(**psep))
10477 return OK;
10478 mb_ptr_back(path, *psep);
10479 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010480
10481 return FAIL;
10482}
10483
10484/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010485 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10486 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010487 */
10488 static int
10489is_unique(maybe_unique, gap, i)
10490 char_u *maybe_unique;
10491 garray_T *gap;
10492 int i;
10493{
10494 int j;
10495 int candidate_len;
10496 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010497 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010498 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010499
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010500 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010501 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010502 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010503 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010504
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010505 candidate_len = (int)STRLEN(maybe_unique);
10506 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010507 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010508 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010509
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010510 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010511 if (fnamecmp(maybe_unique, rival) == 0
10512 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010513 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010514 }
10515
Bram Moolenaar162bd912010-07-28 22:29:10 +020010516 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010517}
10518
10519/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010520 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010521 * paths are expanded to their equivalent fullpath. This includes the "."
10522 * (relative to current buffer directory) and empty path (relative to current
10523 * directory) notations.
10524 *
10525 * TODO: handle upward search (;) and path limiter (**N) notations by
10526 * expanding each into their equivalent path(s).
10527 */
10528 static void
10529expand_path_option(curdir, gap)
10530 char_u *curdir;
10531 garray_T *gap;
10532{
10533 char_u *path_option = *curbuf->b_p_path == NUL
10534 ? p_path : curbuf->b_p_path;
10535 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010536 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010537 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010538
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010539 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010540 return;
10541
10542 while (*path_option != NUL)
10543 {
10544 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10545
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010546 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010547 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010548 /* Relative to current buffer:
10549 * "/path/file" + "." -> "/path/"
10550 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010551 if (curbuf->b_ffname == NULL)
10552 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010553 p = gettail(curbuf->b_ffname);
10554 len = (int)(p - curbuf->b_ffname);
10555 if (len + (int)STRLEN(buf) >= MAXPATHL)
10556 continue;
10557 if (buf[1] == NUL)
10558 buf[len] = NUL;
10559 else
10560 STRMOVE(buf + len, buf + 2);
10561 mch_memmove(buf, curbuf->b_ffname, len);
10562 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010563 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010564 else if (buf[0] == NUL)
10565 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010566 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010567 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010568 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010569 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010570 else if (!mch_isFullName(buf))
10571 {
10572 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010573 len = (int)STRLEN(curdir);
10574 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010575 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010576 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010577 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010578 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010579 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010580 }
10581
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010582 if (ga_grow(gap, 1) == FAIL)
10583 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010584
10585# if defined(MSWIN) || defined(MSDOS)
10586 /* Avoid the path ending in a backslash, it fails when a comma is
10587 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010588 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010589 if (buf[len - 1] == '\\')
10590 buf[len - 1] = '/';
10591# endif
10592
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010593 p = vim_strsave(buf);
10594 if (p == NULL)
10595 break;
10596 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010597 }
10598
10599 vim_free(buf);
10600}
10601
10602/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010603 * Returns a pointer to the file or directory name in "fname" that matches the
10604 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010605 *
10606 * path: /foo/bar/baz
10607 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010608 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010609 */
10610 static char_u *
10611get_path_cutoff(fname, gap)
10612 char_u *fname;
10613 garray_T *gap;
10614{
10615 int i;
10616 int maxlen = 0;
10617 char_u **path_part = (char_u **)gap->ga_data;
10618 char_u *cutoff = NULL;
10619
10620 for (i = 0; i < gap->ga_len; i++)
10621 {
10622 int j = 0;
10623
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010624 while ((fname[j] == path_part[i][j]
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +020010625# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010626 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10627#endif
10628 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010629 j++;
10630 if (j > maxlen)
10631 {
10632 maxlen = j;
10633 cutoff = &fname[j];
10634 }
10635 }
10636
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010637 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010638 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010639 while (vim_ispathsep(*cutoff))
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010640 mb_ptr_adv(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010641
10642 return cutoff;
10643}
10644
10645/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010646 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10647 * that they are unique with respect to each other while conserving the part
10648 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010649 */
10650 static void
10651uniquefy_paths(gap, pattern)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010652 garray_T *gap;
10653 char_u *pattern;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010654{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010655 int i;
10656 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010657 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010658 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010659 char_u *pat;
10660 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010661 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010662 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010663 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010664 char_u **in_curdir = NULL;
10665 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010666
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010667 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010668 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010669
10670 /*
10671 * We need to prepend a '*' at the beginning of file_pattern so that the
10672 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010673 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010674 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010675 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010676 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010677 if (file_pattern == NULL)
10678 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010679 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010680 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010681 STRCAT(file_pattern, pattern);
10682 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10683 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010684 if (pat == NULL)
10685 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010686
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010687 regmatch.rm_ic = TRUE; /* always ignore case */
10688 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10689 vim_free(pat);
10690 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010691 return;
10692
Bram Moolenaar162bd912010-07-28 22:29:10 +020010693 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010694 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010695 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010696 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010697
10698 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010699 if (in_curdir == NULL)
10700 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010701
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010702 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010703 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010704 char_u *path = fnames[i];
10705 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010706 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010707 char_u *pathsep_p;
10708 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010709
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010710 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010711 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010712 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010713 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010714 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010715
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010716 /* Shorten the filename while maintaining its uniqueness */
10717 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010718
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010719 /* we start at the end of the path */
10720 pathsep_p = path + len - 1;
10721
10722 while (find_previous_pathsep(path, &pathsep_p))
10723 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10724 && is_unique(pathsep_p + 1, gap, i)
10725 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10726 {
10727 sort_again = TRUE;
10728 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10729 break;
10730 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010731
10732 if (mch_isFullName(path))
10733 {
10734 /*
10735 * Last resort: shorten relative to curdir if possible.
10736 * 'possible' means:
10737 * 1. It is under the current directory.
10738 * 2. The result is actually shorter than the original.
10739 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010740 * Before curdir After
10741 * /foo/bar/file.txt /foo/bar ./file.txt
10742 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10743 * /file.txt / /file.txt
10744 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010745 */
10746 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010747 if (short_name != NULL && short_name > path + 1
10748#if defined(MSWIN) || defined(MSDOS)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010749 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010750 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010751 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010752 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010753 && !vim_ispathsep(*short_name)
10754#endif
10755 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010756 {
10757 STRCPY(path, ".");
10758 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010759 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010760 }
10761 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010762 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010763 }
10764
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010765 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010766 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010767 {
10768 char_u *rel_path;
10769 char_u *path = in_curdir[i];
10770
10771 if (path == NULL)
10772 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010773
10774 /* If the {filename} is not unique, change it to ./{filename}.
10775 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010776 short_name = shorten_fname(path, curdir);
10777 if (short_name == NULL)
10778 short_name = path;
10779 if (is_unique(short_name, gap, i))
10780 {
10781 STRCPY(fnames[i], short_name);
10782 continue;
10783 }
10784
10785 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10786 if (rel_path == NULL)
10787 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010788 STRCPY(rel_path, ".");
10789 add_pathsep(rel_path);
10790 STRCAT(rel_path, short_name);
10791
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010792 vim_free(fnames[i]);
10793 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010794 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010795 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010796 }
10797
Bram Moolenaar162bd912010-07-28 22:29:10 +020010798theend:
10799 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010800 if (in_curdir != NULL)
10801 {
10802 for (i = 0; i < gap->ga_len; i++)
10803 vim_free(in_curdir[i]);
10804 vim_free(in_curdir);
10805 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010806 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010807 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010808
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010809 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010810 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010811}
10812
10813/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010814 * Calls globpath() with 'path' values for the given pattern and stores the
10815 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010816 * Returns the total number of matches.
10817 */
10818 static int
10819expand_in_path(gap, pattern, flags)
10820 garray_T *gap;
10821 char_u *pattern;
10822 int flags; /* EW_* flags */
10823{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010824 char_u *curdir;
10825 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010826 char_u *paths = NULL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010827
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010828 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010829 return 0;
10830 mch_dirname(curdir, MAXPATHL);
10831
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010832 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010833 expand_path_option(curdir, &path_ga);
10834 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010835 if (path_ga.ga_len == 0)
10836 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010837
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010838 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010839 ga_clear_strings(&path_ga);
10840 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010841 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010842
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010843 globpath(paths, pattern, gap, (flags & EW_ICASE) ? WILD_ICASE : 0);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010844 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010845
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010846 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010847}
10848#endif
10849
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010850#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10851/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010852 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10853 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010854 */
10855 void
10856remove_duplicates(gap)
10857 garray_T *gap;
10858{
10859 int i;
10860 int j;
10861 char_u **fnames = (char_u **)gap->ga_data;
10862
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010863 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010864 for (i = gap->ga_len - 1; i > 0; --i)
10865 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10866 {
10867 vim_free(fnames[i]);
10868 for (j = i + 1; j < gap->ga_len; ++j)
10869 fnames[j - 1] = fnames[j];
10870 --gap->ga_len;
10871 }
10872}
10873#endif
10874
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010875static int has_env_var(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010876
10877/*
10878 * Return TRUE if "p" contains what looks like an environment variable.
10879 * Allowing for escaping.
10880 */
10881 static int
10882has_env_var(p)
10883 char_u *p;
10884{
10885 for ( ; *p; mb_ptr_adv(p))
10886 {
10887 if (*p == '\\' && p[1] != NUL)
10888 ++p;
10889 else if (vim_strchr((char_u *)
Bram Moolenaare7fedb62015-12-31 19:07:19 +010010890#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010891 "$%"
10892#else
10893 "$"
10894#endif
10895 , *p) != NULL)
10896 return TRUE;
10897 }
10898 return FALSE;
10899}
10900
10901#ifdef SPECIAL_WILDCHAR
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010010902static int has_special_wildchar(char_u *p);
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010903
10904/*
10905 * Return TRUE if "p" contains a special wildcard character.
10906 * Allowing for escaping.
10907 */
10908 static int
10909has_special_wildchar(p)
10910 char_u *p;
10911{
10912 for ( ; *p; mb_ptr_adv(p))
10913 {
10914 if (*p == '\\' && p[1] != NUL)
10915 ++p;
10916 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10917 return TRUE;
10918 }
10919 return FALSE;
10920}
10921#endif
10922
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923/*
10924 * Generic wildcard expansion code.
10925 *
10926 * Characters in "pat" that should not be expanded must be preceded with a
10927 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10928 *
10929 * Return FAIL when no single file was found. In this case "num_file" is not
10930 * set, and "file" may contain an error message.
10931 * Return OK when some files found. "num_file" is set to the number of
10932 * matches, "file" to the array of matches. Call FreeWild() later.
10933 */
10934 int
10935gen_expand_wildcards(num_pat, pat, num_file, file, flags)
10936 int num_pat; /* number of input patterns */
10937 char_u **pat; /* array of input patterns */
10938 int *num_file; /* resulting number of files */
10939 char_u ***file; /* array of resulting files */
10940 int flags; /* EW_* flags */
10941{
10942 int i;
10943 garray_T ga;
10944 char_u *p;
10945 static int recursive = FALSE;
10946 int add_pat;
Bram Moolenaar3f188932015-08-25 13:57:04 +020010947 int retval = OK;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010948#if defined(FEAT_SEARCHPATH)
10949 int did_expand_in_path = FALSE;
10950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951
10952 /*
10953 * expand_env() is called to expand things like "~user". If this fails,
10954 * it calls ExpandOne(), which brings us back here. In this case, always
10955 * call the machine specific expansion function, if possible. Otherwise,
10956 * return FAIL.
10957 */
10958 if (recursive)
10959#ifdef SPECIAL_WILDCHAR
10960 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10961#else
10962 return FAIL;
10963#endif
10964
10965#ifdef SPECIAL_WILDCHAR
10966 /*
10967 * If there are any special wildcard characters which we cannot handle
10968 * here, call machine specific function for all the expansion. This
10969 * avoids starting the shell for each argument separately.
10970 * For `=expr` do use the internal function.
10971 */
10972 for (i = 0; i < num_pat; i++)
10973 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010974 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975# ifdef VIM_BACKTICK
10976 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10977# endif
10978 )
10979 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10980 }
10981#endif
10982
10983 recursive = TRUE;
10984
10985 /*
10986 * The matching file names are stored in a growarray. Init it empty.
10987 */
10988 ga_init2(&ga, (int)sizeof(char_u *), 30);
10989
10990 for (i = 0; i < num_pat; ++i)
10991 {
10992 add_pat = -1;
10993 p = pat[i];
10994
10995#ifdef VIM_BACKTICK
10996 if (vim_backtick(p))
Bram Moolenaar3f188932015-08-25 13:57:04 +020010997 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998 add_pat = expand_backtick(&ga, p, flags);
Bram Moolenaar3f188932015-08-25 13:57:04 +020010999 if (add_pat == -1)
11000 retval = FAIL;
11001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002 else
11003#endif
11004 {
11005 /*
11006 * First expand environment variables, "~/" and "~user/".
11007 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011008 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000011010 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011 if (p == NULL)
11012 p = pat[i];
11013#ifdef UNIX
11014 /*
11015 * On Unix, if expand_env() can't expand an environment
11016 * variable, use the shell to do that. Discard previously
11017 * found file names and start all over again.
11018 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020011019 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020 {
11021 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000011022 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 i = mch_expand_wildcards(num_pat, pat, num_file, file,
Bram Moolenaare4df1642014-08-29 12:58:44 +020011024 flags|EW_KEEPDOLLAR);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025 recursive = FALSE;
11026 return i;
11027 }
11028#endif
11029 }
11030
11031 /*
11032 * If there are wildcards: Expand file names and add each match to
11033 * the list. If there is no match, and EW_NOTFOUND is given, add
11034 * the pattern.
11035 * If there are no wildcards: Add the file name if it exists or
11036 * when EW_NOTFOUND is given.
11037 */
11038 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011039 {
11040#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011041 if ((flags & EW_PATH)
11042 && !mch_isFullName(p)
11043 && !(p[0] == '.'
11044 && (vim_ispathsep(p[1])
11045 || (p[1] == '.' && vim_ispathsep(p[2]))))
11046 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011047 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011048 /* :find completion where 'path' is used.
11049 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011050 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011051 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011052 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011053 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020011054 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020011055 else
11056#endif
11057 add_pat = mch_expandpath(&ga, p, flags);
11058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059 }
11060
11061 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
11062 {
11063 char_u *t = backslash_halve_save(p);
11064
11065#if defined(MACOS_CLASSIC)
11066 slash_to_colon(t);
11067#endif
11068 /* When EW_NOTFOUND is used, always add files and dirs. Makes
11069 * "vim c:/" work. */
11070 if (flags & EW_NOTFOUND)
11071 addfile(&ga, t, flags | EW_DIR | EW_FILE);
11072 else if (mch_getperm(t) >= 0)
11073 addfile(&ga, t, flags);
11074 vim_free(t);
11075 }
11076
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011077#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020011078 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020011079 uniquefy_paths(&ga, p);
11080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081 if (p != pat[i])
11082 vim_free(p);
11083 }
11084
11085 *num_file = ga.ga_len;
11086 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
11087
11088 recursive = FALSE;
11089
Bram Moolenaar336bd622016-01-17 18:23:58 +010011090 return ((flags & EW_EMPTYOK) || ga.ga_data != NULL) ? retval : FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091}
11092
11093# ifdef VIM_BACKTICK
11094
11095/*
11096 * Return TRUE if we can expand this backtick thing here.
11097 */
11098 static int
11099vim_backtick(p)
11100 char_u *p;
11101{
11102 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
11103}
11104
11105/*
11106 * Expand an item in `backticks` by executing it as a command.
11107 * Currently only works when pat[] starts and ends with a `.
Bram Moolenaar3f188932015-08-25 13:57:04 +020011108 * Returns number of file names found, -1 if an error is encountered.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109 */
11110 static int
11111expand_backtick(gap, pat, flags)
11112 garray_T *gap;
11113 char_u *pat;
11114 int flags; /* EW_* flags */
11115{
11116 char_u *p;
11117 char_u *cmd;
11118 char_u *buffer;
11119 int cnt = 0;
11120 int i;
11121
11122 /* Create the command: lop off the backticks. */
11123 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
11124 if (cmd == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011125 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011126
11127#ifdef FEAT_EVAL
11128 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011129 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130 else
11131#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011132 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011133 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134 vim_free(cmd);
11135 if (buffer == NULL)
Bram Moolenaar3f188932015-08-25 13:57:04 +020011136 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137
11138 cmd = buffer;
11139 while (*cmd != NUL)
11140 {
11141 cmd = skipwhite(cmd); /* skip over white space */
11142 p = cmd;
11143 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
11144 ++p;
11145 /* add an entry if it is not empty */
11146 if (p > cmd)
11147 {
11148 i = *p;
11149 *p = NUL;
11150 addfile(gap, cmd, flags);
11151 *p = i;
11152 ++cnt;
11153 }
11154 cmd = p;
11155 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
11156 ++cmd;
11157 }
11158
11159 vim_free(buffer);
11160 return cnt;
11161}
11162# endif /* VIM_BACKTICK */
11163
11164/*
11165 * Add a file to a file list. Accepted flags:
11166 * EW_DIR add directories
11167 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011168 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169 * EW_NOTFOUND add even when it doesn't exist
11170 * EW_ADDSLASH add slash after directory name
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011171 * EW_ALLLINKS add symlink also when the referred file does not exist
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172 */
11173 void
11174addfile(gap, f, flags)
11175 garray_T *gap;
11176 char_u *f; /* filename */
11177 int flags;
11178{
11179 char_u *p;
11180 int isdir;
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011181 struct stat sb;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011182
Bram Moolenaard8b77f72015-03-05 21:21:19 +010011183 /* if the file/dir/link doesn't exist, may not add it */
11184 if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS)
Bram Moolenaarab11a592015-03-06 22:00:11 +010011185 ? mch_lstat((char *)f, &sb) < 0 : mch_getperm(f) < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186 return;
11187
11188#ifdef FNAME_ILLEGAL
11189 /* if the file/dir contains illegal characters, don't add it */
11190 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
11191 return;
11192#endif
11193
11194 isdir = mch_isdir(f);
11195 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
11196 return;
11197
Bram Moolenaarb5971142015-03-21 17:32:19 +010011198 /* If the file isn't executable, may not add it. Do accept directories.
11199 * When invoked from expand_shellcmd() do not use $PATH. */
11200 if (!isdir && (flags & EW_EXEC)
11201 && !mch_can_exe(f, NULL, !(flags & EW_SHELLCMD)))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011202 return;
11203
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204 /* Make room for another item in the file list. */
11205 if (ga_grow(gap, 1) == FAIL)
11206 return;
11207
11208 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
11209 if (p == NULL)
11210 return;
11211
11212 STRCPY(p, f);
11213#ifdef BACKSLASH_IN_FILENAME
11214 slash_adjust(p);
11215#endif
11216 /*
11217 * Append a slash or backslash after directory names if none is present.
11218 */
11219#ifndef DONT_ADD_PATHSEP_TO_DIR
11220 if (isdir && (flags & EW_ADDSLASH))
11221 add_pathsep(p);
11222#endif
11223 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224}
11225#endif /* !NO_EXPANDPATH */
11226
11227#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
11228
11229#ifndef SEEK_SET
11230# define SEEK_SET 0
11231#endif
11232#ifndef SEEK_END
11233# define SEEK_END 2
11234#endif
11235
11236/*
11237 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011238 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
11239 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240 * Returns an allocated string, or NULL for error.
11241 */
11242 char_u *
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011243get_cmd_output(cmd, infile, flags, ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244 char_u *cmd;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011245 char_u *infile; /* optional input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246 int flags; /* can be SHELL_SILENT */
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011247 int *ret_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248{
11249 char_u *tempname;
11250 char_u *command;
11251 char_u *buffer = NULL;
11252 int len;
11253 int i = 0;
11254 FILE *fd;
11255
11256 if (check_restricted() || check_secure())
11257 return NULL;
11258
11259 /* get a name for the temp file */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020011260 if ((tempname = vim_tempname('o', FALSE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261 {
11262 EMSG(_(e_notmp));
11263 return NULL;
11264 }
11265
11266 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011267 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011268 if (command == NULL)
11269 goto done;
11270
11271 /*
11272 * Call the shell to execute the command (errors are ignored).
11273 * Don't check timestamps here.
11274 */
11275 ++no_check_timestamps;
11276 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
11277 --no_check_timestamps;
11278
11279 vim_free(command);
11280
11281 /*
11282 * read the names from the file into memory
11283 */
11284# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000011285 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286 fd = mch_fopen((char *)tempname, "r");
11287# else
11288 fd = mch_fopen((char *)tempname, READBIN);
11289# endif
11290
11291 if (fd == NULL)
11292 {
11293 EMSG2(_(e_notopen), tempname);
11294 goto done;
11295 }
11296
11297 fseek(fd, 0L, SEEK_END);
11298 len = ftell(fd); /* get size of temp file */
11299 fseek(fd, 0L, SEEK_SET);
11300
11301 buffer = alloc(len + 1);
11302 if (buffer != NULL)
11303 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
11304 fclose(fd);
11305 mch_remove(tempname);
11306 if (buffer == NULL)
11307 goto done;
11308#ifdef VMS
11309 len = i; /* VMS doesn't give us what we asked for... */
11310#endif
11311 if (i != len)
11312 {
11313 EMSG2(_(e_notread), tempname);
11314 vim_free(buffer);
11315 buffer = NULL;
11316 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011317 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011318 {
11319 /* Change NUL into SOH, otherwise the string is truncated. */
11320 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020011321 if (buffer[i] == NUL)
11322 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011323
Bram Moolenaar162bd912010-07-28 22:29:10 +020011324 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020011325 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020011326 else
11327 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011328
11329done:
11330 vim_free(tempname);
11331 return buffer;
11332}
11333#endif
11334
11335/*
11336 * Free the list of files returned by expand_wildcards() or other expansion
11337 * functions.
11338 */
11339 void
11340FreeWild(count, files)
11341 int count;
11342 char_u **files;
11343{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000011344 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345 return;
11346#if defined(__EMX__) && defined(__ALWAYS_HAS_TRAILING_NULL_POINTER) /* XXX */
11347 /*
11348 * Is this still OK for when other functions than expand_wildcards() have
11349 * been used???
11350 */
11351 _fnexplodefree((char **)files);
11352#else
11353 while (count--)
11354 vim_free(files[count]);
11355 vim_free(files);
11356#endif
11357}
11358
11359/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020011360 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361 * Don't do this when still processing a command or a mapping.
11362 * Don't do this when inside a ":normal" command.
11363 */
11364 int
11365goto_im()
11366{
11367 return (p_im && stuff_empty() && typebuf_typed());
11368}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011369
11370/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020011371 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020011372 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
11373 * - Remove any argument. E.g., "csh -f" -> "csh".
11374 * But don't allow a space in the path, so that this works:
11375 * "/usr/bin/csh --rcfile ~/.cshrc"
11376 * But don't do that for Windows, it's common to have a space in the path.
11377 */
11378 char_u *
11379get_isolated_shell_name()
11380{
11381 char_u *p;
11382
11383#ifdef WIN3264
11384 p = gettail(p_sh);
11385 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
11386#else
11387 p = skiptowhite(p_sh);
11388 if (*p == NUL)
11389 {
11390 /* No white space, use the tail. */
11391 p = vim_strsave(gettail(p_sh));
11392 }
11393 else
11394 {
11395 char_u *p1, *p2;
11396
11397 /* Find the last path separator before the space. */
11398 p1 = p_sh;
11399 for (p2 = p_sh; p2 < p; mb_ptr_adv(p2))
11400 if (vim_ispathsep(*p2))
11401 p1 = p2 + 1;
11402 p = vim_strnsave(p1, (int)(p - p1));
11403 }
11404#endif
11405 return p;
11406}