blob: fa43657a4723adb99e28e4591a0cdf31f9f6c516 [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 Moolenaar071d4272004-06-13 20:20:40 +000017static char_u *vim_version_dir __ARGS((char_u *vimdir));
18static char_u *remove_tail __ARGS((char_u *p, char_u *pend, char_u *name));
Bram Moolenaar06ae70d2013-06-17 19:26:36 +020019#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar01b626c2013-06-16 22:49:14 +020020static void init_users __ARGS((void));
Bram Moolenaar06ae70d2013-06-17 19:26:36 +020021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022static int copy_indent __ARGS((int size, char_u *src));
23
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{
35 return get_indent_str(ml_get_curline(), (int)curbuf->b_p_ts);
36}
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{
45 return get_indent_str(ml_get(lnum), (int)curbuf->b_p_ts);
46}
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{
58 return get_indent_str(ml_get_buf(buf, lnum, FALSE), (int)buf->b_p_ts);
59}
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 Moolenaar071d4272004-06-13 20:20:40 +000067get_indent_str(ptr, ts)
68 char_u *ptr;
69 int ts;
70{
71 int count = 0;
72
73 for ( ; *ptr; ++ptr)
74 {
75 if (*ptr == TAB) /* count a tab for what it is worth */
76 count += ts - (count % ts);
77 else if (*ptr == ' ')
78 ++count; /* count a space for one */
79 else
80 break;
81 }
Bram Moolenaar4399ef42005-02-12 14:29:27 +000082 return count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000083}
84
85/*
86 * Set the indent of the current line.
87 * Leaves the cursor on the first non-blank in the line.
88 * Caller must take care of undo.
89 * "flags":
90 * SIN_CHANGED: call changed_bytes() if the line was changed.
91 * SIN_INSERT: insert the indent in front of the line.
92 * SIN_UNDO: save line for undo before changing it.
93 * Returns TRUE if the line was changed.
94 */
95 int
96set_indent(size, flags)
Bram Moolenaar5002c292007-07-24 13:26:15 +000097 int size; /* measured in spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +000098 int flags;
99{
100 char_u *p;
101 char_u *newline;
102 char_u *oldline;
103 char_u *s;
104 int todo;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000105 int ind_len; /* measured in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106 int line_len;
107 int doit = FALSE;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000108 int ind_done = 0; /* measured in spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109 int tab_pad;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000110 int retval = FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000111 int orig_char_len = -1; /* number of initial whitespace chars when
Bram Moolenaar5002c292007-07-24 13:26:15 +0000112 'et' and 'pi' are both set */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113
114 /*
115 * First check if there is anything to do and compute the number of
116 * characters needed for the indent.
117 */
118 todo = size;
119 ind_len = 0;
120 p = oldline = ml_get_curline();
121
122 /* Calculate the buffer size for the new indent, and check to see if it
123 * isn't already set */
124
Bram Moolenaar5002c292007-07-24 13:26:15 +0000125 /* if 'expandtab' isn't set: use TABs; if both 'expandtab' and
126 * 'preserveindent' are set count the number of characters at the
127 * beginning of the line to be copied */
128 if (!curbuf->b_p_et || (!(flags & SIN_INSERT) && curbuf->b_p_pi))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129 {
130 /* If 'preserveindent' is set then reuse as much as possible of
131 * the existing indent structure for the new indent */
132 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
133 {
134 ind_done = 0;
135
136 /* count as many characters as we can use */
137 while (todo > 0 && vim_iswhite(*p))
138 {
139 if (*p == TAB)
140 {
141 tab_pad = (int)curbuf->b_p_ts
142 - (ind_done % (int)curbuf->b_p_ts);
143 /* stop if this tab will overshoot the target */
144 if (todo < tab_pad)
145 break;
146 todo -= tab_pad;
147 ++ind_len;
148 ind_done += tab_pad;
149 }
150 else
151 {
152 --todo;
153 ++ind_len;
154 ++ind_done;
155 }
156 ++p;
157 }
158
Bram Moolenaar5002c292007-07-24 13:26:15 +0000159 /* Set initial number of whitespace chars to copy if we are
160 * preserving indent but expandtab is set */
161 if (curbuf->b_p_et)
162 orig_char_len = ind_len;
163
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 /* Fill to next tabstop with a tab, if possible */
165 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000166 if (todo >= tab_pad && orig_char_len == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 {
168 doit = TRUE;
169 todo -= tab_pad;
170 ++ind_len;
171 /* ind_done += tab_pad; */
172 }
173 }
174
175 /* count tabs required for indent */
176 while (todo >= (int)curbuf->b_p_ts)
177 {
178 if (*p != TAB)
179 doit = TRUE;
180 else
181 ++p;
182 todo -= (int)curbuf->b_p_ts;
183 ++ind_len;
184 /* ind_done += (int)curbuf->b_p_ts; */
185 }
186 }
187 /* count spaces required for indent */
188 while (todo > 0)
189 {
190 if (*p != ' ')
191 doit = TRUE;
192 else
193 ++p;
194 --todo;
195 ++ind_len;
196 /* ++ind_done; */
197 }
198
199 /* Return if the indent is OK already. */
200 if (!doit && !vim_iswhite(*p) && !(flags & SIN_INSERT))
201 return FALSE;
202
203 /* Allocate memory for the new line. */
204 if (flags & SIN_INSERT)
205 p = oldline;
206 else
207 p = skipwhite(p);
208 line_len = (int)STRLEN(p) + 1;
Bram Moolenaar5002c292007-07-24 13:26:15 +0000209
210 /* If 'preserveindent' and 'expandtab' are both set keep the original
211 * characters and allocate accordingly. We will fill the rest with spaces
212 * after the if (!curbuf->b_p_et) below. */
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000213 if (orig_char_len != -1)
Bram Moolenaar5002c292007-07-24 13:26:15 +0000214 {
215 newline = alloc(orig_char_len + size - ind_done + line_len);
216 if (newline == NULL)
217 return FALSE;
Bram Moolenaar4d64b782007-08-14 20:16:42 +0000218 todo = size - ind_done;
219 ind_len = orig_char_len + todo; /* Set total length of indent in
220 * characters, which may have been
221 * undercounted until now */
Bram Moolenaar5002c292007-07-24 13:26:15 +0000222 p = oldline;
223 s = newline;
224 while (orig_char_len > 0)
225 {
226 *s++ = *p++;
227 orig_char_len--;
228 }
Bram Moolenaar913626c2008-01-03 11:43:42 +0000229
Bram Moolenaar5002c292007-07-24 13:26:15 +0000230 /* Skip over any additional white space (useful when newindent is less
231 * than old) */
232 while (vim_iswhite(*p))
Bram Moolenaar913626c2008-01-03 11:43:42 +0000233 ++p;
Bram Moolenaarcc00b952007-08-11 12:32:57 +0000234
Bram Moolenaar5002c292007-07-24 13:26:15 +0000235 }
236 else
237 {
238 todo = size;
239 newline = alloc(ind_len + line_len);
240 if (newline == NULL)
241 return FALSE;
242 s = newline;
243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244
245 /* Put the characters in the new line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 /* if 'expandtab' isn't set: use TABs */
247 if (!curbuf->b_p_et)
248 {
249 /* If 'preserveindent' is set then reuse as much as possible of
250 * the existing indent structure for the new indent */
251 if (!(flags & SIN_INSERT) && curbuf->b_p_pi)
252 {
253 p = oldline;
254 ind_done = 0;
255
256 while (todo > 0 && vim_iswhite(*p))
257 {
258 if (*p == TAB)
259 {
260 tab_pad = (int)curbuf->b_p_ts
261 - (ind_done % (int)curbuf->b_p_ts);
262 /* stop if this tab will overshoot the target */
263 if (todo < tab_pad)
264 break;
265 todo -= tab_pad;
266 ind_done += tab_pad;
267 }
268 else
269 {
270 --todo;
271 ++ind_done;
272 }
273 *s++ = *p++;
274 }
275
276 /* Fill to next tabstop with a tab, if possible */
277 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
278 if (todo >= tab_pad)
279 {
280 *s++ = TAB;
281 todo -= tab_pad;
282 }
283
284 p = skipwhite(p);
285 }
286
287 while (todo >= (int)curbuf->b_p_ts)
288 {
289 *s++ = TAB;
290 todo -= (int)curbuf->b_p_ts;
291 }
292 }
293 while (todo > 0)
294 {
295 *s++ = ' ';
296 --todo;
297 }
298 mch_memmove(s, p, (size_t)line_len);
299
300 /* Replace the line (unless undo fails). */
301 if (!(flags & SIN_UNDO) || u_savesub(curwin->w_cursor.lnum) == OK)
302 {
303 ml_replace(curwin->w_cursor.lnum, newline, FALSE);
304 if (flags & SIN_CHANGED)
305 changed_bytes(curwin->w_cursor.lnum, 0);
Bram Moolenaar2c019c82013-10-06 17:46:56 +0200306 /* Correct saved cursor position if it is in this line. */
307 if (saved_cursor.lnum == curwin->w_cursor.lnum)
308 {
309 if (saved_cursor.col >= (colnr_T)(p - oldline))
310 /* cursor was after the indent, adjust for the number of
311 * bytes added/removed */
312 saved_cursor.col += ind_len - (colnr_T)(p - oldline);
313 else if (saved_cursor.col >= (colnr_T)(s - newline))
314 /* cursor was in the indent, and is now after it, put it back
315 * at the start of the indent (replacing spaces with TAB) */
316 saved_cursor.col = (colnr_T)(s - newline);
317 }
Bram Moolenaar5409c052005-03-18 20:27:04 +0000318 retval = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 }
320 else
321 vim_free(newline);
322
323 curwin->w_cursor.col = ind_len;
Bram Moolenaar5409c052005-03-18 20:27:04 +0000324 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325}
326
327/*
328 * Copy the indent from ptr to the current line (and fill to size)
329 * Leaves the cursor on the first non-blank in the line.
330 * Returns TRUE if the line was changed.
331 */
332 static int
333copy_indent(size, src)
334 int size;
335 char_u *src;
336{
337 char_u *p = NULL;
338 char_u *line = NULL;
339 char_u *s;
340 int todo;
341 int ind_len;
342 int line_len = 0;
343 int tab_pad;
344 int ind_done;
345 int round;
346
347 /* Round 1: compute the number of characters needed for the indent
348 * Round 2: copy the characters. */
349 for (round = 1; round <= 2; ++round)
350 {
351 todo = size;
352 ind_len = 0;
353 ind_done = 0;
354 s = src;
355
356 /* Count/copy the usable portion of the source line */
357 while (todo > 0 && vim_iswhite(*s))
358 {
359 if (*s == TAB)
360 {
361 tab_pad = (int)curbuf->b_p_ts
362 - (ind_done % (int)curbuf->b_p_ts);
363 /* Stop if this tab will overshoot the target */
364 if (todo < tab_pad)
365 break;
366 todo -= tab_pad;
367 ind_done += tab_pad;
368 }
369 else
370 {
371 --todo;
372 ++ind_done;
373 }
374 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000375 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 *p++ = *s;
377 ++s;
378 }
379
380 /* Fill to next tabstop with a tab, if possible */
381 tab_pad = (int)curbuf->b_p_ts - (ind_done % (int)curbuf->b_p_ts);
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200382 if (todo >= tab_pad && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 {
384 todo -= tab_pad;
385 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000386 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 *p++ = TAB;
388 }
389
390 /* Add tabs required for indent */
Bram Moolenaarc42e7ed2011-09-07 19:58:09 +0200391 while (todo >= (int)curbuf->b_p_ts && !curbuf->b_p_et)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 {
393 todo -= (int)curbuf->b_p_ts;
394 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000395 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 *p++ = TAB;
397 }
398
399 /* Count/add spaces required for indent */
400 while (todo > 0)
401 {
402 --todo;
403 ++ind_len;
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000404 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 *p++ = ' ';
406 }
407
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000408 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 {
410 /* Allocate memory for the result: the copied indent, new indent
411 * and the rest of the line. */
412 line_len = (int)STRLEN(ml_get_curline()) + 1;
413 line = alloc(ind_len + line_len);
414 if (line == NULL)
415 return FALSE;
416 p = line;
417 }
418 }
419
420 /* Append the original line */
421 mch_memmove(p, ml_get_curline(), (size_t)line_len);
422
423 /* Replace the line */
424 ml_replace(curwin->w_cursor.lnum, line, FALSE);
425
426 /* Put the cursor after the indent. */
427 curwin->w_cursor.col = ind_len;
428 return TRUE;
429}
430
431/*
432 * Return the indent of the current line after a number. Return -1 if no
433 * number was found. Used for 'n' in 'formatoptions': numbered list.
Bram Moolenaar86b68352004-12-27 21:59:20 +0000434 * Since a pattern is used it can actually handle more than numbers.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 */
436 int
437get_number_indent(lnum)
438 linenr_T lnum;
439{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 colnr_T col;
441 pos_T pos;
442
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200443 regmatch_T regmatch;
444 int lead_len = 0; /* length of comment leader */
445
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 if (lnum > curbuf->b_ml.ml_line_count)
447 return -1;
Bram Moolenaar86b68352004-12-27 21:59:20 +0000448 pos.lnum = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200449
450#ifdef FEAT_COMMENTS
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200451 /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */
452 if ((State & INSERT) || has_format_option(FO_Q_COMS))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200453 lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE);
Bram Moolenaar86b68352004-12-27 21:59:20 +0000454#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200455 regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC);
456 if (regmatch.regprog != NULL)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200457 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200458 regmatch.rm_ic = FALSE;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200459
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200460 /* vim_regexec() expects a pointer to a line. This lets us
461 * start matching for the flp beyond any comment leader... */
462 if (vim_regexec(&regmatch, ml_get(lnum) + lead_len, (colnr_T)0))
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200463 {
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200464 pos.lnum = lnum;
465 pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200466#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar96b7ca52012-06-29 15:04:49 +0200467 pos.coladd = 0;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200468#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200469 }
Bram Moolenaar473de612013-06-08 18:19:48 +0200470 vim_regfree(regmatch.regprog);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200471 }
Bram Moolenaar86b68352004-12-27 21:59:20 +0000472
473 if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 getvcol(curwin, &pos, &col, NULL, NULL);
476 return (int)col;
477}
478
479#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
480
481static int cin_is_cinword __ARGS((char_u *line));
482
483/*
484 * Return TRUE if the string "line" starts with a word from 'cinwords'.
485 */
486 static int
487cin_is_cinword(line)
488 char_u *line;
489{
490 char_u *cinw;
491 char_u *cinw_buf;
492 int cinw_len;
493 int retval = FALSE;
494 int len;
495
496 cinw_len = (int)STRLEN(curbuf->b_p_cinw) + 1;
497 cinw_buf = alloc((unsigned)cinw_len);
498 if (cinw_buf != NULL)
499 {
500 line = skipwhite(line);
501 for (cinw = curbuf->b_p_cinw; *cinw; )
502 {
503 len = copy_option_part(&cinw, cinw_buf, cinw_len, ",");
504 if (STRNCMP(line, cinw_buf, len) == 0
505 && (!vim_iswordc(line[len]) || !vim_iswordc(line[len - 1])))
506 {
507 retval = TRUE;
508 break;
509 }
510 }
511 vim_free(cinw_buf);
512 }
513 return retval;
514}
515#endif
516
517/*
518 * open_line: Add a new line below or above the current line.
519 *
520 * For VREPLACE mode, we only add a new line when we get to the end of the
521 * file, otherwise we just start replacing the next line.
522 *
523 * Caller must take care of undo. Since VREPLACE may affect any number of
524 * lines however, it may call u_save_cursor() again when starting to change a
525 * new line.
526 * "flags": OPENLINE_DELSPACES delete spaces after cursor
527 * OPENLINE_DO_COM format comments
528 * OPENLINE_KEEPTRAIL keep trailing spaces
529 * OPENLINE_MARKFIX adjust mark positions after the line break
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200530 * OPENLINE_COM_LIST format comments with list or 2nd line indent
531 *
532 * "second_line_indent": indent for after ^^D in Insert mode or if flag
533 * OPENLINE_COM_LIST
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 *
535 * Return TRUE for success, FALSE for failure
536 */
537 int
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200538open_line(dir, flags, second_line_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 int dir; /* FORWARD or BACKWARD */
540 int flags;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200541 int second_line_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542{
543 char_u *saved_line; /* copy of the original line */
544 char_u *next_line = NULL; /* copy of the next line */
545 char_u *p_extra = NULL; /* what goes to next line */
546 int less_cols = 0; /* less columns for mark in new line */
547 int less_cols_off = 0; /* columns to skip for mark adjust */
548 pos_T old_cursor; /* old cursor position */
549 int newcol = 0; /* new cursor column */
550 int newindent = 0; /* auto-indent of the new line */
551 int n;
552 int trunc_line = FALSE; /* truncate current line afterwards */
553 int retval = FALSE; /* return value, default is FAIL */
554#ifdef FEAT_COMMENTS
555 int extra_len = 0; /* length of p_extra string */
556 int lead_len; /* length of comment leader */
557 char_u *lead_flags; /* position in 'comments' for comment leader */
558 char_u *leader = NULL; /* copy of comment leader */
559#endif
560 char_u *allocated = NULL; /* allocated memory */
561#if defined(FEAT_SMARTINDENT) || defined(FEAT_VREPLACE) || defined(FEAT_LISP) \
562 || defined(FEAT_CINDENT) || defined(FEAT_COMMENTS)
563 char_u *p;
564#endif
565 int saved_char = NUL; /* init for GCC */
566#if defined(FEAT_SMARTINDENT) || defined(FEAT_COMMENTS)
567 pos_T *pos;
568#endif
569#ifdef FEAT_SMARTINDENT
570 int do_si = (!p_paste && curbuf->b_p_si
571# ifdef FEAT_CINDENT
572 && !curbuf->b_p_cin
573# endif
574 );
575 int no_si = FALSE; /* reset did_si afterwards */
576 int first_char = NUL; /* init for GCC */
577#endif
578#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
579 int vreplace_mode;
580#endif
581 int did_append; /* appended a new line */
582 int saved_pi = curbuf->b_p_pi; /* copy of preserveindent setting */
583
584 /*
585 * make a copy of the current line so we can mess with it
586 */
587 saved_line = vim_strsave(ml_get_curline());
588 if (saved_line == NULL) /* out of memory! */
589 return FALSE;
590
591#ifdef FEAT_VREPLACE
592 if (State & VREPLACE_FLAG)
593 {
594 /*
595 * With VREPLACE we make a copy of the next line, which we will be
596 * starting to replace. First make the new line empty and let vim play
597 * with the indenting and comment leader to its heart's content. Then
598 * we grab what it ended up putting on the new line, put back the
599 * original line, and call ins_char() to put each new character onto
600 * the line, replacing what was there before and pushing the right
601 * stuff onto the replace stack. -- webb.
602 */
603 if (curwin->w_cursor.lnum < orig_line_count)
604 next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1));
605 else
606 next_line = vim_strsave((char_u *)"");
607 if (next_line == NULL) /* out of memory! */
608 goto theend;
609
610 /*
611 * In VREPLACE mode, a NL replaces the rest of the line, and starts
612 * replacing the next line, so push all of the characters left on the
613 * line onto the replace stack. We'll push any other characters that
614 * might be replaced at the start of the next line (due to autoindent
615 * etc) a bit later.
616 */
617 replace_push(NUL); /* Call twice because BS over NL expects it */
618 replace_push(NUL);
619 p = saved_line + curwin->w_cursor.col;
620 while (*p != NUL)
Bram Moolenaar2c994e82008-01-02 16:49:36 +0000621 {
622#ifdef FEAT_MBYTE
623 if (has_mbyte)
624 p += replace_push_mb(p);
625 else
626#endif
627 replace_push(*p++);
628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 saved_line[curwin->w_cursor.col] = NUL;
630 }
631#endif
632
633 if ((State & INSERT)
634#ifdef FEAT_VREPLACE
635 && !(State & VREPLACE_FLAG)
636#endif
637 )
638 {
639 p_extra = saved_line + curwin->w_cursor.col;
640#ifdef FEAT_SMARTINDENT
641 if (do_si) /* need first char after new line break */
642 {
643 p = skipwhite(p_extra);
644 first_char = *p;
645 }
646#endif
647#ifdef FEAT_COMMENTS
648 extra_len = (int)STRLEN(p_extra);
649#endif
650 saved_char = *p_extra;
651 *p_extra = NUL;
652 }
653
654 u_clearline(); /* cannot do "U" command when adding lines */
655#ifdef FEAT_SMARTINDENT
656 did_si = FALSE;
657#endif
658 ai_col = 0;
659
660 /*
661 * If we just did an auto-indent, then we didn't type anything on
662 * the prior line, and it should be truncated. Do this even if 'ai' is not
663 * set because automatically inserting a comment leader also sets did_ai.
664 */
665 if (dir == FORWARD && did_ai)
666 trunc_line = TRUE;
667
668 /*
669 * If 'autoindent' and/or 'smartindent' is set, try to figure out what
670 * indent to use for the new line.
671 */
672 if (curbuf->b_p_ai
673#ifdef FEAT_SMARTINDENT
674 || do_si
675#endif
676 )
677 {
678 /*
679 * count white space on current line
680 */
681 newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +0200682 if (newindent == 0 && !(flags & OPENLINE_COM_LIST))
683 newindent = second_line_indent; /* for ^^D command in insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684
685#ifdef FEAT_SMARTINDENT
686 /*
687 * Do smart indenting.
688 * In insert/replace mode (only when dir == FORWARD)
689 * we may move some text to the next line. If it starts with '{'
690 * don't add an indent. Fixes inserting a NL before '{' in line
691 * "if (condition) {"
692 */
693 if (!trunc_line && do_si && *saved_line != NUL
694 && (p_extra == NULL || first_char != '{'))
695 {
696 char_u *ptr;
697 char_u last_char;
698
699 old_cursor = curwin->w_cursor;
700 ptr = saved_line;
701# ifdef FEAT_COMMENTS
702 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200703 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 else
705 lead_len = 0;
706# endif
707 if (dir == FORWARD)
708 {
709 /*
710 * Skip preprocessor directives, unless they are
711 * recognised as comments.
712 */
713 if (
714# ifdef FEAT_COMMENTS
715 lead_len == 0 &&
716# endif
717 ptr[0] == '#')
718 {
719 while (ptr[0] == '#' && curwin->w_cursor.lnum > 1)
720 ptr = ml_get(--curwin->w_cursor.lnum);
721 newindent = get_indent();
722 }
723# ifdef FEAT_COMMENTS
724 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200725 lead_len = get_leader_len(ptr, NULL, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 else
727 lead_len = 0;
728 if (lead_len > 0)
729 {
730 /*
731 * This case gets the following right:
732 * \*
733 * * A comment (read '\' as '/').
734 * *\
735 * #define IN_THE_WAY
736 * This should line up here;
737 */
738 p = skipwhite(ptr);
739 if (p[0] == '/' && p[1] == '*')
740 p++;
741 if (p[0] == '*')
742 {
743 for (p++; *p; p++)
744 {
745 if (p[0] == '/' && p[-1] == '*')
746 {
747 /*
748 * End of C comment, indent should line up
749 * with the line containing the start of
750 * the comment
751 */
752 curwin->w_cursor.col = (colnr_T)(p - ptr);
753 if ((pos = findmatch(NULL, NUL)) != NULL)
754 {
755 curwin->w_cursor.lnum = pos->lnum;
756 newindent = get_indent();
757 }
758 }
759 }
760 }
761 }
762 else /* Not a comment line */
763# endif
764 {
765 /* Find last non-blank in line */
766 p = ptr + STRLEN(ptr) - 1;
767 while (p > ptr && vim_iswhite(*p))
768 --p;
769 last_char = *p;
770
771 /*
772 * find the character just before the '{' or ';'
773 */
774 if (last_char == '{' || last_char == ';')
775 {
776 if (p > ptr)
777 --p;
778 while (p > ptr && vim_iswhite(*p))
779 --p;
780 }
781 /*
782 * Try to catch lines that are split over multiple
783 * lines. eg:
784 * if (condition &&
785 * condition) {
786 * Should line up here!
787 * }
788 */
789 if (*p == ')')
790 {
791 curwin->w_cursor.col = (colnr_T)(p - ptr);
792 if ((pos = findmatch(NULL, '(')) != NULL)
793 {
794 curwin->w_cursor.lnum = pos->lnum;
795 newindent = get_indent();
796 ptr = ml_get_curline();
797 }
798 }
799 /*
800 * If last character is '{' do indent, without
801 * checking for "if" and the like.
802 */
803 if (last_char == '{')
804 {
805 did_si = TRUE; /* do indent */
806 no_si = TRUE; /* don't delete it when '{' typed */
807 }
808 /*
809 * Look for "if" and the like, use 'cinwords'.
810 * Don't do this if the previous line ended in ';' or
811 * '}'.
812 */
813 else if (last_char != ';' && last_char != '}'
814 && cin_is_cinword(ptr))
815 did_si = TRUE;
816 }
817 }
818 else /* dir == BACKWARD */
819 {
820 /*
821 * Skip preprocessor directives, unless they are
822 * recognised as comments.
823 */
824 if (
825# ifdef FEAT_COMMENTS
826 lead_len == 0 &&
827# endif
828 ptr[0] == '#')
829 {
830 int was_backslashed = FALSE;
831
832 while ((ptr[0] == '#' || was_backslashed) &&
833 curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
834 {
835 if (*ptr && ptr[STRLEN(ptr) - 1] == '\\')
836 was_backslashed = TRUE;
837 else
838 was_backslashed = FALSE;
839 ptr = ml_get(++curwin->w_cursor.lnum);
840 }
841 if (was_backslashed)
842 newindent = 0; /* Got to end of file */
843 else
844 newindent = get_indent();
845 }
846 p = skipwhite(ptr);
847 if (*p == '}') /* if line starts with '}': do indent */
848 did_si = TRUE;
849 else /* can delete indent when '{' typed */
850 can_si_back = TRUE;
851 }
852 curwin->w_cursor = old_cursor;
853 }
854 if (do_si)
855 can_si = TRUE;
856#endif /* FEAT_SMARTINDENT */
857
858 did_ai = TRUE;
859 }
860
861#ifdef FEAT_COMMENTS
862 /*
863 * Find out if the current line starts with a comment leader.
864 * This may then be inserted in front of the new line.
865 */
866 end_comment_pending = NUL;
867 if (flags & OPENLINE_DO_COM)
Bram Moolenaar81340392012-06-06 16:12:59 +0200868 lead_len = get_leader_len(saved_line, &lead_flags, dir == BACKWARD, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 else
870 lead_len = 0;
871 if (lead_len > 0)
872 {
873 char_u *lead_repl = NULL; /* replaces comment leader */
874 int lead_repl_len = 0; /* length of *lead_repl */
875 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
876 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
877 char_u *comment_end = NULL; /* where lead_end has been found */
878 int extra_space = FALSE; /* append extra space */
879 int current_flag;
880 int require_blank = FALSE; /* requires blank after middle */
881 char_u *p2;
882
883 /*
884 * If the comment leader has the start, middle or end flag, it may not
885 * be used or may be replaced with the middle leader.
886 */
887 for (p = lead_flags; *p && *p != ':'; ++p)
888 {
889 if (*p == COM_BLANK)
890 {
891 require_blank = TRUE;
892 continue;
893 }
894 if (*p == COM_START || *p == COM_MIDDLE)
895 {
896 current_flag = *p;
897 if (*p == COM_START)
898 {
899 /*
900 * Doing "O" on a start of comment does not insert leader.
901 */
902 if (dir == BACKWARD)
903 {
904 lead_len = 0;
905 break;
906 }
907
908 /* find start of middle part */
909 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
910 require_blank = FALSE;
911 }
912
913 /*
914 * Isolate the strings of the middle and end leader.
915 */
916 while (*p && p[-1] != ':') /* find end of middle flags */
917 {
918 if (*p == COM_BLANK)
919 require_blank = TRUE;
920 ++p;
921 }
922 (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ",");
923
924 while (*p && p[-1] != ':') /* find end of end flags */
925 {
926 /* Check whether we allow automatic ending of comments */
927 if (*p == COM_AUTO_END)
928 end_comment_pending = -1; /* means we want to set it */
929 ++p;
930 }
931 n = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
932
933 if (end_comment_pending == -1) /* we can set it now */
934 end_comment_pending = lead_end[n - 1];
935
936 /*
937 * If the end of the comment is in the same line, don't use
938 * the comment leader.
939 */
940 if (dir == FORWARD)
941 {
942 for (p = saved_line + lead_len; *p; ++p)
943 if (STRNCMP(p, lead_end, n) == 0)
944 {
945 comment_end = p;
946 lead_len = 0;
947 break;
948 }
949 }
950
951 /*
952 * Doing "o" on a start of comment inserts the middle leader.
953 */
954 if (lead_len > 0)
955 {
956 if (current_flag == COM_START)
957 {
958 lead_repl = lead_middle;
959 lead_repl_len = (int)STRLEN(lead_middle);
960 }
961
962 /*
963 * If we have hit RETURN immediately after the start
964 * comment leader, then put a space after the middle
965 * comment leader on the next line.
966 */
967 if (!vim_iswhite(saved_line[lead_len - 1])
968 && ((p_extra != NULL
969 && (int)curwin->w_cursor.col == lead_len)
970 || (p_extra == NULL
971 && saved_line[lead_len] == NUL)
972 || require_blank))
973 extra_space = TRUE;
974 }
975 break;
976 }
977 if (*p == COM_END)
978 {
979 /*
980 * Doing "o" on the end of a comment does not insert leader.
981 * Remember where the end is, might want to use it to find the
982 * start (for C-comments).
983 */
984 if (dir == FORWARD)
985 {
986 comment_end = skipwhite(saved_line);
987 lead_len = 0;
988 break;
989 }
990
991 /*
992 * Doing "O" on the end of a comment inserts the middle leader.
993 * Find the string for the middle leader, searching backwards.
994 */
995 while (p > curbuf->b_p_com && *p != ',')
996 --p;
997 for (lead_repl = p; lead_repl > curbuf->b_p_com
998 && lead_repl[-1] != ':'; --lead_repl)
999 ;
1000 lead_repl_len = (int)(p - lead_repl);
1001
1002 /* We can probably always add an extra space when doing "O" on
1003 * the comment-end */
1004 extra_space = TRUE;
1005
1006 /* Check whether we allow automatic ending of comments */
1007 for (p2 = p; *p2 && *p2 != ':'; p2++)
1008 {
1009 if (*p2 == COM_AUTO_END)
1010 end_comment_pending = -1; /* means we want to set it */
1011 }
1012 if (end_comment_pending == -1)
1013 {
1014 /* Find last character in end-comment string */
1015 while (*p2 && *p2 != ',')
1016 p2++;
1017 end_comment_pending = p2[-1];
1018 }
1019 break;
1020 }
1021 if (*p == COM_FIRST)
1022 {
1023 /*
1024 * Comment leader for first line only: Don't repeat leader
1025 * when using "O", blank out leader when using "o".
1026 */
1027 if (dir == BACKWARD)
1028 lead_len = 0;
1029 else
1030 {
1031 lead_repl = (char_u *)"";
1032 lead_repl_len = 0;
1033 }
1034 break;
1035 }
1036 }
1037 if (lead_len)
1038 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001039 /* allocate buffer (may concatenate p_extra later) */
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001040 leader = alloc(lead_len + lead_repl_len + extra_space + extra_len
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02001041 + (second_line_indent > 0 ? second_line_indent : 0) + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 allocated = leader; /* remember to free it later */
1043
1044 if (leader == NULL)
1045 lead_len = 0;
1046 else
1047 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001048 vim_strncpy(leader, saved_line, lead_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049
1050 /*
1051 * Replace leader with lead_repl, right or left adjusted
1052 */
1053 if (lead_repl != NULL)
1054 {
1055 int c = 0;
1056 int off = 0;
1057
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001058 for (p = lead_flags; *p != NUL && *p != ':'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 {
1060 if (*p == COM_RIGHT || *p == COM_LEFT)
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001061 c = *p++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 else if (VIM_ISDIGIT(*p) || *p == '-')
1063 off = getdigits(&p);
Bram Moolenaard7d5b472009-11-11 16:30:08 +00001064 else
1065 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 }
1067 if (c == COM_RIGHT) /* right adjusted leader */
1068 {
1069 /* find last non-white in the leader to line up with */
1070 for (p = leader + lead_len - 1; p > leader
1071 && vim_iswhite(*p); --p)
1072 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 ++p;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001074
1075#ifdef FEAT_MBYTE
1076 /* Compute the length of the replaced characters in
1077 * screen characters, not bytes. */
1078 {
1079 int repl_size = vim_strnsize(lead_repl,
1080 lead_repl_len);
1081 int old_size = 0;
1082 char_u *endp = p;
1083 int l;
1084
1085 while (old_size < repl_size && p > leader)
1086 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001087 mb_ptr_back(leader, p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001088 old_size += ptr2cells(p);
1089 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001090 l = lead_repl_len - (int)(endp - p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001091 if (l != 0)
1092 mch_memmove(endp + l, endp,
1093 (size_t)((leader + lead_len) - endp));
1094 lead_len += l;
1095 }
1096#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 if (p < leader + lead_repl_len)
1098 p = leader;
1099 else
1100 p -= lead_repl_len;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1103 if (p + lead_repl_len > leader + lead_len)
1104 p[lead_repl_len] = NUL;
1105
1106 /* blank-out any other chars from the old leader. */
1107 while (--p >= leader)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001108 {
1109#ifdef FEAT_MBYTE
1110 int l = mb_head_off(leader, p);
1111
1112 if (l > 1)
1113 {
1114 p -= l;
1115 if (ptr2cells(p) > 1)
1116 {
1117 p[1] = ' ';
1118 --l;
1119 }
1120 mch_memmove(p + 1, p + l + 1,
1121 (size_t)((leader + lead_len) - (p + l + 1)));
1122 lead_len -= l;
1123 *p = ' ';
1124 }
1125 else
1126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 if (!vim_iswhite(*p))
1128 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 }
1131 else /* left adjusted leader */
1132 {
1133 p = skipwhite(leader);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001134#ifdef FEAT_MBYTE
1135 /* Compute the length of the replaced characters in
1136 * screen characters, not bytes. Move the part that is
1137 * not to be overwritten. */
1138 {
1139 int repl_size = vim_strnsize(lead_repl,
1140 lead_repl_len);
1141 int i;
1142 int l;
1143
1144 for (i = 0; p[i] != NUL && i < lead_len; i += l)
1145 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001146 l = (*mb_ptr2len)(p + i);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001147 if (vim_strnsize(p, i + l) > repl_size)
1148 break;
1149 }
1150 if (i != lead_repl_len)
1151 {
1152 mch_memmove(p + lead_repl_len, p + i,
Bram Moolenaar2d7ff052009-11-17 15:08:26 +00001153 (size_t)(lead_len - i - (p - leader)));
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001154 lead_len += lead_repl_len - i;
1155 }
1156 }
1157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 mch_memmove(p, lead_repl, (size_t)lead_repl_len);
1159
1160 /* Replace any remaining non-white chars in the old
1161 * leader by spaces. Keep Tabs, the indent must
1162 * remain the same. */
1163 for (p += lead_repl_len; p < leader + lead_len; ++p)
1164 if (!vim_iswhite(*p))
1165 {
1166 /* Don't put a space before a TAB. */
1167 if (p + 1 < leader + lead_len && p[1] == TAB)
1168 {
1169 --lead_len;
1170 mch_memmove(p, p + 1,
1171 (leader + lead_len) - p);
1172 }
1173 else
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001174 {
1175#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001176 int l = (*mb_ptr2len)(p);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001177
1178 if (l > 1)
1179 {
1180 if (ptr2cells(p) > 1)
1181 {
1182 /* Replace a double-wide char with
1183 * two spaces */
1184 --l;
1185 *p++ = ' ';
1186 }
1187 mch_memmove(p + 1, p + l,
1188 (leader + lead_len) - p);
1189 lead_len -= l - 1;
1190 }
1191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 *p = ' ';
Bram Moolenaar21cf8232004-07-16 20:18:37 +00001193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 }
1195 *p = NUL;
1196 }
1197
1198 /* Recompute the indent, it may have changed. */
1199 if (curbuf->b_p_ai
1200#ifdef FEAT_SMARTINDENT
1201 || do_si
1202#endif
1203 )
1204 newindent = get_indent_str(leader, (int)curbuf->b_p_ts);
1205
1206 /* Add the indent offset */
1207 if (newindent + off < 0)
1208 {
1209 off = -newindent;
1210 newindent = 0;
1211 }
1212 else
1213 newindent += off;
1214
1215 /* Correct trailing spaces for the shift, so that
1216 * alignment remains equal. */
1217 while (off > 0 && lead_len > 0
1218 && leader[lead_len - 1] == ' ')
1219 {
1220 /* Don't do it when there is a tab before the space */
1221 if (vim_strchr(skipwhite(leader), '\t') != NULL)
1222 break;
1223 --lead_len;
1224 --off;
1225 }
1226
1227 /* If the leader ends in white space, don't add an
1228 * extra space */
1229 if (lead_len > 0 && vim_iswhite(leader[lead_len - 1]))
1230 extra_space = FALSE;
1231 leader[lead_len] = NUL;
1232 }
1233
1234 if (extra_space)
1235 {
1236 leader[lead_len++] = ' ';
1237 leader[lead_len] = NUL;
1238 }
1239
1240 newcol = lead_len;
1241
1242 /*
1243 * if a new indent will be set below, remove the indent that
1244 * is in the comment leader
1245 */
1246 if (newindent
1247#ifdef FEAT_SMARTINDENT
1248 || did_si
1249#endif
1250 )
1251 {
1252 while (lead_len && vim_iswhite(*leader))
1253 {
1254 --lead_len;
1255 --newcol;
1256 ++leader;
1257 }
1258 }
1259
1260 }
1261#ifdef FEAT_SMARTINDENT
1262 did_si = can_si = FALSE;
1263#endif
1264 }
1265 else if (comment_end != NULL)
1266 {
1267 /*
1268 * We have finished a comment, so we don't use the leader.
1269 * If this was a C-comment and 'ai' or 'si' is set do a normal
1270 * indent to align with the line containing the start of the
1271 * comment.
1272 */
1273 if (comment_end[0] == '*' && comment_end[1] == '/' &&
1274 (curbuf->b_p_ai
1275#ifdef FEAT_SMARTINDENT
1276 || do_si
1277#endif
1278 ))
1279 {
1280 old_cursor = curwin->w_cursor;
1281 curwin->w_cursor.col = (colnr_T)(comment_end - saved_line);
1282 if ((pos = findmatch(NULL, NUL)) != NULL)
1283 {
1284 curwin->w_cursor.lnum = pos->lnum;
1285 newindent = get_indent();
1286 }
1287 curwin->w_cursor = old_cursor;
1288 }
1289 }
1290 }
1291#endif
1292
1293 /* (State == INSERT || State == REPLACE), only when dir == FORWARD */
1294 if (p_extra != NULL)
1295 {
1296 *p_extra = saved_char; /* restore char that NUL replaced */
1297
1298 /*
1299 * When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first
1300 * non-blank.
1301 *
1302 * When in REPLACE mode, put the deleted blanks on the replace stack,
1303 * preceded by a NUL, so they can be put back when a BS is entered.
1304 */
1305 if (REPLACE_NORMAL(State))
1306 replace_push(NUL); /* end of extra blanks */
1307 if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES))
1308 {
1309 while ((*p_extra == ' ' || *p_extra == '\t')
1310#ifdef FEAT_MBYTE
1311 && (!enc_utf8
1312 || !utf_iscomposing(utf_ptr2char(p_extra + 1)))
1313#endif
1314 )
1315 {
1316 if (REPLACE_NORMAL(State))
1317 replace_push(*p_extra);
1318 ++p_extra;
1319 ++less_cols_off;
1320 }
1321 }
1322 if (*p_extra != NUL)
1323 did_ai = FALSE; /* append some text, don't truncate now */
1324
1325 /* columns for marks adjusted for removed columns */
1326 less_cols = (int)(p_extra - saved_line);
1327 }
1328
1329 if (p_extra == NULL)
1330 p_extra = (char_u *)""; /* append empty line */
1331
1332#ifdef FEAT_COMMENTS
1333 /* concatenate leader and p_extra, if there is a leader */
1334 if (lead_len)
1335 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001336 if (flags & OPENLINE_COM_LIST && second_line_indent > 0)
1337 {
1338 int i;
Bram Moolenaar36105782012-06-14 20:59:25 +02001339 int padding = second_line_indent
1340 - (newindent + (int)STRLEN(leader));
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001341
1342 /* Here whitespace is inserted after the comment char.
1343 * Below, set_indent(newindent, SIN_INSERT) will insert the
1344 * whitespace needed before the comment char. */
1345 for (i = 0; i < padding; i++)
1346 {
1347 STRCAT(leader, " ");
Bram Moolenaar5fb9ec52012-07-25 16:10:03 +02001348 less_cols--;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001349 newcol++;
1350 }
1351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 STRCAT(leader, p_extra);
1353 p_extra = leader;
1354 did_ai = TRUE; /* So truncating blanks works with comments */
1355 less_cols -= lead_len;
1356 }
1357 else
1358 end_comment_pending = NUL; /* turns out there was no leader */
1359#endif
1360
1361 old_cursor = curwin->w_cursor;
1362 if (dir == BACKWARD)
1363 --curwin->w_cursor.lnum;
1364#ifdef FEAT_VREPLACE
1365 if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count)
1366#endif
1367 {
1368 if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE)
1369 == FAIL)
1370 goto theend;
1371 /* Postpone calling changed_lines(), because it would mess up folding
1372 * with markers. */
1373 mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L);
1374 did_append = TRUE;
1375 }
1376#ifdef FEAT_VREPLACE
1377 else
1378 {
1379 /*
1380 * In VREPLACE mode we are starting to replace the next line.
1381 */
1382 curwin->w_cursor.lnum++;
1383 if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed)
1384 {
1385 /* In case we NL to a new line, BS to the previous one, and NL
1386 * again, we don't want to save the new line for undo twice.
1387 */
1388 (void)u_save_cursor(); /* errors are ignored! */
1389 vr_lines_changed++;
1390 }
1391 ml_replace(curwin->w_cursor.lnum, p_extra, TRUE);
1392 changed_bytes(curwin->w_cursor.lnum, 0);
1393 curwin->w_cursor.lnum--;
1394 did_append = FALSE;
1395 }
1396#endif
1397
1398 if (newindent
1399#ifdef FEAT_SMARTINDENT
1400 || did_si
1401#endif
1402 )
1403 {
1404 ++curwin->w_cursor.lnum;
1405#ifdef FEAT_SMARTINDENT
1406 if (did_si)
1407 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01001408 int sw = (int)get_sw_value(curbuf);
Bram Moolenaar14f24742012-08-08 18:01:05 +02001409
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 if (p_sr)
Bram Moolenaar14f24742012-08-08 18:01:05 +02001411 newindent -= newindent % sw;
1412 newindent += sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 }
1414#endif
Bram Moolenaar5002c292007-07-24 13:26:15 +00001415 /* Copy the indent */
1416 if (curbuf->b_p_ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 {
1418 (void)copy_indent(newindent, saved_line);
1419
1420 /*
1421 * Set the 'preserveindent' option so that any further screwing
1422 * with the line doesn't entirely destroy our efforts to preserve
1423 * it. It gets restored at the function end.
1424 */
1425 curbuf->b_p_pi = TRUE;
1426 }
1427 else
1428 (void)set_indent(newindent, SIN_INSERT);
1429 less_cols -= curwin->w_cursor.col;
1430
1431 ai_col = curwin->w_cursor.col;
1432
1433 /*
1434 * In REPLACE mode, for each character in the new indent, there must
1435 * be a NUL on the replace stack, for when it is deleted with BS
1436 */
1437 if (REPLACE_NORMAL(State))
1438 for (n = 0; n < (int)curwin->w_cursor.col; ++n)
1439 replace_push(NUL);
1440 newcol += curwin->w_cursor.col;
1441#ifdef FEAT_SMARTINDENT
1442 if (no_si)
1443 did_si = FALSE;
1444#endif
1445 }
1446
1447#ifdef FEAT_COMMENTS
1448 /*
1449 * In REPLACE mode, for each character in the extra leader, there must be
1450 * a NUL on the replace stack, for when it is deleted with BS.
1451 */
1452 if (REPLACE_NORMAL(State))
1453 while (lead_len-- > 0)
1454 replace_push(NUL);
1455#endif
1456
1457 curwin->w_cursor = old_cursor;
1458
1459 if (dir == FORWARD)
1460 {
1461 if (trunc_line || (State & INSERT))
1462 {
1463 /* truncate current line at cursor */
1464 saved_line[curwin->w_cursor.col] = NUL;
1465 /* Remove trailing white space, unless OPENLINE_KEEPTRAIL used. */
1466 if (trunc_line && !(flags & OPENLINE_KEEPTRAIL))
1467 truncate_spaces(saved_line);
1468 ml_replace(curwin->w_cursor.lnum, saved_line, FALSE);
1469 saved_line = NULL;
1470 if (did_append)
1471 {
1472 changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col,
1473 curwin->w_cursor.lnum + 1, 1L);
1474 did_append = FALSE;
1475
1476 /* Move marks after the line break to the new line. */
1477 if (flags & OPENLINE_MARKFIX)
1478 mark_col_adjust(curwin->w_cursor.lnum,
1479 curwin->w_cursor.col + less_cols_off,
1480 1L, (long)-less_cols);
1481 }
1482 else
1483 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
1484 }
1485
1486 /*
1487 * Put the cursor on the new line. Careful: the scrollup() above may
1488 * have moved w_cursor, we must use old_cursor.
1489 */
1490 curwin->w_cursor.lnum = old_cursor.lnum + 1;
1491 }
1492 if (did_append)
1493 changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L);
1494
1495 curwin->w_cursor.col = newcol;
1496#ifdef FEAT_VIRTUALEDIT
1497 curwin->w_cursor.coladd = 0;
1498#endif
1499
1500#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1501 /*
1502 * In VREPLACE mode, we are handling the replace stack ourselves, so stop
1503 * fixthisline() from doing it (via change_indent()) by telling it we're in
1504 * normal INSERT mode.
1505 */
1506 if (State & VREPLACE_FLAG)
1507 {
1508 vreplace_mode = State; /* So we know to put things right later */
1509 State = INSERT;
1510 }
1511 else
1512 vreplace_mode = 0;
1513#endif
1514#ifdef FEAT_LISP
1515 /*
1516 * May do lisp indenting.
1517 */
1518 if (!p_paste
1519# ifdef FEAT_COMMENTS
1520 && leader == NULL
1521# endif
1522 && curbuf->b_p_lisp
1523 && curbuf->b_p_ai)
1524 {
1525 fixthisline(get_lisp_indent);
1526 p = ml_get_curline();
1527 ai_col = (colnr_T)(skipwhite(p) - p);
1528 }
1529#endif
1530#ifdef FEAT_CINDENT
1531 /*
1532 * May do indenting after opening a new line.
1533 */
1534 if (!p_paste
1535 && (curbuf->b_p_cin
1536# ifdef FEAT_EVAL
1537 || *curbuf->b_p_inde != NUL
1538# endif
1539 )
1540 && in_cinkeys(dir == FORWARD
1541 ? KEY_OPEN_FORW
1542 : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)))
1543 {
1544 do_c_expr_indent();
1545 p = ml_get_curline();
1546 ai_col = (colnr_T)(skipwhite(p) - p);
1547 }
1548#endif
1549#if defined(FEAT_VREPLACE) && (defined(FEAT_LISP) || defined(FEAT_CINDENT))
1550 if (vreplace_mode != 0)
1551 State = vreplace_mode;
1552#endif
1553
1554#ifdef FEAT_VREPLACE
1555 /*
1556 * Finally, VREPLACE gets the stuff on the new line, then puts back the
1557 * original line, and inserts the new stuff char by char, pushing old stuff
1558 * onto the replace stack (via ins_char()).
1559 */
1560 if (State & VREPLACE_FLAG)
1561 {
1562 /* Put new line in p_extra */
1563 p_extra = vim_strsave(ml_get_curline());
1564 if (p_extra == NULL)
1565 goto theend;
1566
1567 /* Put back original line */
1568 ml_replace(curwin->w_cursor.lnum, next_line, FALSE);
1569
1570 /* Insert new stuff into line again */
1571 curwin->w_cursor.col = 0;
1572#ifdef FEAT_VIRTUALEDIT
1573 curwin->w_cursor.coladd = 0;
1574#endif
1575 ins_bytes(p_extra); /* will call changed_bytes() */
1576 vim_free(p_extra);
1577 next_line = NULL;
1578 }
1579#endif
1580
1581 retval = TRUE; /* success! */
1582theend:
1583 curbuf->b_p_pi = saved_pi;
1584 vim_free(saved_line);
1585 vim_free(next_line);
1586 vim_free(allocated);
1587 return retval;
1588}
1589
1590#if defined(FEAT_COMMENTS) || defined(PROTO)
1591/*
Bram Moolenaar2c019c82013-10-06 17:46:56 +02001592 * get_leader_len() returns the length in bytes of the prefix of the given
1593 * string which introduces a comment. If this string is not a comment then
1594 * 0 is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 * When "flags" is not NULL, it is set to point to the flags of the recognized
1596 * comment leader.
1597 * "backward" must be true for the "O" command.
Bram Moolenaar81340392012-06-06 16:12:59 +02001598 * If "include_space" is set, include trailing whitespace while calculating the
1599 * length.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 */
1601 int
Bram Moolenaar81340392012-06-06 16:12:59 +02001602get_leader_len(line, flags, backward, include_space)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 char_u *line;
1604 char_u **flags;
1605 int backward;
Bram Moolenaar81340392012-06-06 16:12:59 +02001606 int include_space;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607{
1608 int i, j;
Bram Moolenaar81340392012-06-06 16:12:59 +02001609 int result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 int got_com = FALSE;
1611 int found_one;
1612 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1613 char_u *string; /* pointer to comment string */
1614 char_u *list;
Bram Moolenaara4271d52011-05-10 13:38:27 +02001615 int middle_match_len = 0;
1616 char_u *prev_list;
Bram Moolenaar05da4282011-05-10 14:44:11 +02001617 char_u *saved_flags = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618
Bram Moolenaar81340392012-06-06 16:12:59 +02001619 result = i = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 while (vim_iswhite(line[i])) /* leading white space is ignored */
1621 ++i;
1622
1623 /*
1624 * Repeat to match several nested comment strings.
1625 */
Bram Moolenaara4271d52011-05-10 13:38:27 +02001626 while (line[i] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 {
1628 /*
1629 * scan through the 'comments' option for a match
1630 */
1631 found_one = FALSE;
1632 for (list = curbuf->b_p_com; *list; )
1633 {
Bram Moolenaara4271d52011-05-10 13:38:27 +02001634 /* Get one option part into part_buf[]. Advance "list" to next
1635 * one. Put "string" at start of string. */
1636 if (!got_com && flags != NULL)
1637 *flags = list; /* remember where flags started */
1638 prev_list = list;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1640 string = vim_strchr(part_buf, ':');
1641 if (string == NULL) /* missing ':', ignore this part */
1642 continue;
1643 *string++ = NUL; /* isolate flags from string */
1644
Bram Moolenaara4271d52011-05-10 13:38:27 +02001645 /* If we found a middle match previously, use that match when this
1646 * is not a middle or end. */
1647 if (middle_match_len != 0
1648 && vim_strchr(part_buf, COM_MIDDLE) == NULL
1649 && vim_strchr(part_buf, COM_END) == NULL)
1650 break;
1651
1652 /* When we already found a nested comment, only accept further
1653 * nested comments. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 if (got_com && vim_strchr(part_buf, COM_NEST) == NULL)
1655 continue;
1656
Bram Moolenaara4271d52011-05-10 13:38:27 +02001657 /* When 'O' flag present and using "O" command skip this one. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 if (backward && vim_strchr(part_buf, COM_NOBACK) != NULL)
1659 continue;
1660
Bram Moolenaara4271d52011-05-10 13:38:27 +02001661 /* Line contents and string must match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 * When string starts with white space, must have some white space
1663 * (but the amount does not need to match, there might be a mix of
Bram Moolenaara4271d52011-05-10 13:38:27 +02001664 * TABs and spaces). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 if (vim_iswhite(string[0]))
1666 {
1667 if (i == 0 || !vim_iswhite(line[i - 1]))
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001668 continue; /* missing white space */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 while (vim_iswhite(string[0]))
1670 ++string;
1671 }
1672 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1673 ;
1674 if (string[j] != NUL)
Bram Moolenaara4271d52011-05-10 13:38:27 +02001675 continue; /* string doesn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676
Bram Moolenaara4271d52011-05-10 13:38:27 +02001677 /* When 'b' flag used, there must be white space or an
1678 * end-of-line after the string in the line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 if (vim_strchr(part_buf, COM_BLANK) != NULL
1680 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1681 continue;
1682
Bram Moolenaara4271d52011-05-10 13:38:27 +02001683 /* We have found a match, stop searching unless this is a middle
1684 * comment. The middle comment can be a substring of the end
1685 * comment in which case it's better to return the length of the
1686 * end comment and its flags. Thus we keep searching with middle
1687 * and end matches and use an end match if it matches better. */
1688 if (vim_strchr(part_buf, COM_MIDDLE) != NULL)
1689 {
1690 if (middle_match_len == 0)
1691 {
1692 middle_match_len = j;
1693 saved_flags = prev_list;
1694 }
1695 continue;
1696 }
1697 if (middle_match_len != 0 && j > middle_match_len)
1698 /* Use this match instead of the middle match, since it's a
1699 * longer thus better match. */
1700 middle_match_len = 0;
1701
1702 if (middle_match_len == 0)
1703 i += j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 found_one = TRUE;
1705 break;
1706 }
1707
Bram Moolenaara4271d52011-05-10 13:38:27 +02001708 if (middle_match_len != 0)
1709 {
1710 /* Use the previously found middle match after failing to find a
1711 * match with an end. */
1712 if (!got_com && flags != NULL)
1713 *flags = saved_flags;
1714 i += middle_match_len;
1715 found_one = TRUE;
1716 }
1717
1718 /* No match found, stop scanning. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 if (!found_one)
1720 break;
1721
Bram Moolenaar81340392012-06-06 16:12:59 +02001722 result = i;
1723
Bram Moolenaara4271d52011-05-10 13:38:27 +02001724 /* Include any trailing white space. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 while (vim_iswhite(line[i]))
1726 ++i;
1727
Bram Moolenaar81340392012-06-06 16:12:59 +02001728 if (include_space)
1729 result = i;
1730
Bram Moolenaara4271d52011-05-10 13:38:27 +02001731 /* If this comment doesn't nest, stop here. */
1732 got_com = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 if (vim_strchr(part_buf, COM_NEST) == NULL)
1734 break;
1735 }
Bram Moolenaar81340392012-06-06 16:12:59 +02001736 return result;
1737}
Bram Moolenaara4271d52011-05-10 13:38:27 +02001738
Bram Moolenaar81340392012-06-06 16:12:59 +02001739/*
1740 * Return the offset at which the last comment in line starts. If there is no
1741 * comment in the whole line, -1 is returned.
1742 *
1743 * When "flags" is not null, it is set to point to the flags describing the
1744 * recognized comment leader.
1745 */
1746 int
1747get_last_leader_offset(line, flags)
1748 char_u *line;
1749 char_u **flags;
1750{
1751 int result = -1;
1752 int i, j;
1753 int lower_check_bound = 0;
1754 char_u *string;
1755 char_u *com_leader;
1756 char_u *com_flags;
1757 char_u *list;
1758 int found_one;
1759 char_u part_buf[COM_MAX_LEN]; /* buffer for one option part */
1760
1761 /*
1762 * Repeat to match several nested comment strings.
1763 */
1764 i = (int)STRLEN(line);
1765 while (--i >= lower_check_bound)
1766 {
1767 /*
1768 * scan through the 'comments' option for a match
1769 */
1770 found_one = FALSE;
1771 for (list = curbuf->b_p_com; *list; )
1772 {
1773 char_u *flags_save = list;
1774
1775 /*
1776 * Get one option part into part_buf[]. Advance list to next one.
1777 * put string at start of string.
1778 */
1779 (void)copy_option_part(&list, part_buf, COM_MAX_LEN, ",");
1780 string = vim_strchr(part_buf, ':');
1781 if (string == NULL) /* If everything is fine, this cannot actually
1782 * happen. */
1783 {
1784 continue;
1785 }
1786 *string++ = NUL; /* Isolate flags from string. */
1787 com_leader = string;
1788
1789 /*
1790 * Line contents and string must match.
1791 * When string starts with white space, must have some white space
1792 * (but the amount does not need to match, there might be a mix of
1793 * TABs and spaces).
1794 */
1795 if (vim_iswhite(string[0]))
1796 {
1797 if (i == 0 || !vim_iswhite(line[i - 1]))
1798 continue;
1799 while (vim_iswhite(string[0]))
1800 ++string;
1801 }
1802 for (j = 0; string[j] != NUL && string[j] == line[i + j]; ++j)
1803 /* do nothing */;
1804 if (string[j] != NUL)
1805 continue;
1806
1807 /*
1808 * When 'b' flag used, there must be white space or an
1809 * end-of-line after the string in the line.
1810 */
1811 if (vim_strchr(part_buf, COM_BLANK) != NULL
1812 && !vim_iswhite(line[i + j]) && line[i + j] != NUL)
1813 {
1814 continue;
1815 }
1816
1817 /*
1818 * We have found a match, stop searching.
1819 */
1820 found_one = TRUE;
1821
1822 if (flags)
1823 *flags = flags_save;
1824 com_flags = flags_save;
1825
1826 break;
1827 }
1828
1829 if (found_one)
1830 {
1831 char_u part_buf2[COM_MAX_LEN]; /* buffer for one option part */
1832 int len1, len2, off;
1833
1834 result = i;
1835 /*
1836 * If this comment nests, continue searching.
1837 */
1838 if (vim_strchr(part_buf, COM_NEST) != NULL)
1839 continue;
1840
1841 lower_check_bound = i;
1842
1843 /* Let's verify whether the comment leader found is a substring
1844 * of other comment leaders. If it is, let's adjust the
1845 * lower_check_bound so that we make sure that we have determined
1846 * the comment leader correctly.
1847 */
1848
1849 while (vim_iswhite(*com_leader))
1850 ++com_leader;
1851 len1 = (int)STRLEN(com_leader);
1852
1853 for (list = curbuf->b_p_com; *list; )
1854 {
1855 char_u *flags_save = list;
1856
1857 (void)copy_option_part(&list, part_buf2, COM_MAX_LEN, ",");
1858 if (flags_save == com_flags)
1859 continue;
1860 string = vim_strchr(part_buf2, ':');
1861 ++string;
1862 while (vim_iswhite(*string))
1863 ++string;
1864 len2 = (int)STRLEN(string);
1865 if (len2 == 0)
1866 continue;
1867
1868 /* Now we have to verify whether string ends with a substring
1869 * beginning the com_leader. */
1870 for (off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;)
1871 {
1872 --off;
1873 if (!STRNCMP(string + off, com_leader, len2 - off))
1874 {
1875 if (i - off < lower_check_bound)
1876 lower_check_bound = i - off;
1877 }
1878 }
1879 }
1880 }
1881 }
1882 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883}
1884#endif
1885
1886/*
1887 * Return the number of window lines occupied by buffer line "lnum".
1888 */
1889 int
1890plines(lnum)
1891 linenr_T lnum;
1892{
1893 return plines_win(curwin, lnum, TRUE);
1894}
1895
1896 int
1897plines_win(wp, lnum, winheight)
1898 win_T *wp;
1899 linenr_T lnum;
1900 int winheight; /* when TRUE limit to window height */
1901{
1902#if defined(FEAT_DIFF) || defined(PROTO)
1903 /* Check for filler lines above this buffer line. When folded the result
1904 * is one line anyway. */
1905 return plines_win_nofill(wp, lnum, winheight) + diff_check_fill(wp, lnum);
1906}
1907
1908 int
1909plines_nofill(lnum)
1910 linenr_T lnum;
1911{
1912 return plines_win_nofill(curwin, lnum, TRUE);
1913}
1914
1915 int
1916plines_win_nofill(wp, lnum, winheight)
1917 win_T *wp;
1918 linenr_T lnum;
1919 int winheight; /* when TRUE limit to window height */
1920{
1921#endif
1922 int lines;
1923
1924 if (!wp->w_p_wrap)
1925 return 1;
1926
1927#ifdef FEAT_VERTSPLIT
1928 if (wp->w_width == 0)
1929 return 1;
1930#endif
1931
1932#ifdef FEAT_FOLDING
1933 /* A folded lines is handled just like an empty line. */
1934 /* NOTE: Caller must handle lines that are MAYBE folded. */
1935 if (lineFolded(wp, lnum) == TRUE)
1936 return 1;
1937#endif
1938
1939 lines = plines_win_nofold(wp, lnum);
1940 if (winheight > 0 && lines > wp->w_height)
1941 return (int)wp->w_height;
1942 return lines;
1943}
1944
1945/*
1946 * Return number of window lines physical line "lnum" will occupy in window
1947 * "wp". Does not care about folding, 'wrap' or 'diff'.
1948 */
1949 int
1950plines_win_nofold(wp, lnum)
1951 win_T *wp;
1952 linenr_T lnum;
1953{
1954 char_u *s;
1955 long col;
1956 int width;
1957
1958 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
1959 if (*s == NUL) /* empty line */
1960 return 1;
1961 col = win_linetabsize(wp, s, (colnr_T)MAXCOL);
1962
1963 /*
1964 * If list mode is on, then the '$' at the end of the line may take up one
1965 * extra column.
1966 */
1967 if (wp->w_p_list && lcs_eol != NUL)
1968 col += 1;
1969
1970 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02001971 * Add column offset for 'number', 'relativenumber' and 'foldcolumn'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 */
1973 width = W_WIDTH(wp) - win_col_off(wp);
1974 if (width <= 0)
1975 return 32000;
1976 if (col <= width)
1977 return 1;
1978 col -= width;
1979 width += win_col_off2(wp);
1980 return (col + (width - 1)) / width + 1;
1981}
1982
1983/*
1984 * Like plines_win(), but only reports the number of physical screen lines
1985 * used from the start of the line to the given column number.
1986 */
1987 int
1988plines_win_col(wp, lnum, column)
1989 win_T *wp;
1990 linenr_T lnum;
1991 long column;
1992{
1993 long col;
1994 char_u *s;
1995 int lines = 0;
1996 int width;
1997
1998#ifdef FEAT_DIFF
1999 /* Check for filler lines above this buffer line. When folded the result
2000 * is one line anyway. */
2001 lines = diff_check_fill(wp, lnum);
2002#endif
2003
2004 if (!wp->w_p_wrap)
2005 return lines + 1;
2006
2007#ifdef FEAT_VERTSPLIT
2008 if (wp->w_width == 0)
2009 return lines + 1;
2010#endif
2011
2012 s = ml_get_buf(wp->w_buffer, lnum, FALSE);
2013
2014 col = 0;
2015 while (*s != NUL && --column >= 0)
2016 {
2017 col += win_lbr_chartabsize(wp, s, (colnr_T)col, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002018 mb_ptr_adv(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 }
2020
2021 /*
2022 * If *s is a TAB, and the TAB is not displayed as ^I, and we're not in
2023 * INSERT mode, then col must be adjusted so that it represents the last
2024 * screen position of the TAB. This only fixes an error when the TAB wraps
2025 * from one screen line to the next (when 'columns' is not a multiple of
2026 * 'ts') -- webb.
2027 */
2028 if (*s == TAB && (State & NORMAL) && (!wp->w_p_list || lcs_tab1))
2029 col += win_lbr_chartabsize(wp, s, (colnr_T)col, NULL) - 1;
2030
2031 /*
Bram Moolenaar64486672010-05-16 15:46:46 +02002032 * Add column offset for 'number', 'relativenumber', 'foldcolumn', etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 */
2034 width = W_WIDTH(wp) - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00002035 if (width <= 0)
2036 return 9999;
2037
2038 lines += 1;
2039 if (col > width)
2040 lines += (col - width) / (width + win_col_off2(wp)) + 1;
2041 return lines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042}
2043
2044 int
2045plines_m_win(wp, first, last)
2046 win_T *wp;
2047 linenr_T first, last;
2048{
2049 int count = 0;
2050
2051 while (first <= last)
2052 {
2053#ifdef FEAT_FOLDING
2054 int x;
2055
2056 /* Check if there are any really folded lines, but also included lines
2057 * that are maybe folded. */
2058 x = foldedCount(wp, first, NULL);
2059 if (x > 0)
2060 {
2061 ++count; /* count 1 for "+-- folded" line */
2062 first += x;
2063 }
2064 else
2065#endif
2066 {
2067#ifdef FEAT_DIFF
2068 if (first == wp->w_topline)
2069 count += plines_win_nofill(wp, first, TRUE) + wp->w_topfill;
2070 else
2071#endif
2072 count += plines_win(wp, first, TRUE);
2073 ++first;
2074 }
2075 }
2076 return (count);
2077}
2078
2079#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) || defined(PROTO)
2080/*
2081 * Insert string "p" at the cursor position. Stops at a NUL byte.
2082 * Handles Replace mode and multi-byte characters.
2083 */
2084 void
2085ins_bytes(p)
2086 char_u *p;
2087{
2088 ins_bytes_len(p, (int)STRLEN(p));
2089}
2090#endif
2091
2092#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2093 || defined(FEAT_COMMENTS) || defined(FEAT_MBYTE) || defined(PROTO)
2094/*
2095 * Insert string "p" with length "len" at the cursor position.
2096 * Handles Replace mode and multi-byte characters.
2097 */
2098 void
2099ins_bytes_len(p, len)
2100 char_u *p;
2101 int len;
2102{
2103 int i;
2104# ifdef FEAT_MBYTE
2105 int n;
2106
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002107 if (has_mbyte)
2108 for (i = 0; i < len; i += n)
2109 {
2110 if (enc_utf8)
2111 /* avoid reading past p[len] */
2112 n = utfc_ptr2len_len(p + i, len - i);
2113 else
2114 n = (*mb_ptr2len)(p + i);
2115 ins_char_bytes(p + i, n);
2116 }
2117 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118# endif
Bram Moolenaar176dd1e2008-06-21 14:30:28 +00002119 for (i = 0; i < len; ++i)
2120 ins_char(p[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121}
2122#endif
2123
2124/*
2125 * Insert or replace a single character at the cursor position.
2126 * When in REPLACE or VREPLACE mode, replace any existing character.
2127 * Caller must have prepared for undo.
2128 * For multi-byte characters we get the whole character, the caller must
2129 * convert bytes to a character.
2130 */
2131 void
2132ins_char(c)
2133 int c;
2134{
2135#if defined(FEAT_MBYTE) || defined(PROTO)
Bram Moolenaar9a920d82012-06-01 15:21:02 +02002136 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 int n;
2138
2139 n = (*mb_char2bytes)(c, buf);
2140
2141 /* When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte.
2142 * Happens for CTRL-Vu9900. */
2143 if (buf[0] == 0)
2144 buf[0] = '\n';
2145
2146 ins_char_bytes(buf, n);
2147}
2148
2149 void
2150ins_char_bytes(buf, charlen)
2151 char_u *buf;
2152 int charlen;
2153{
2154 int c = buf[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155#endif
2156 int newlen; /* nr of bytes inserted */
2157 int oldlen; /* nr of bytes deleted (0 when not replacing) */
2158 char_u *p;
2159 char_u *newp;
2160 char_u *oldp;
2161 int linelen; /* length of old line including NUL */
2162 colnr_T col;
2163 linenr_T lnum = curwin->w_cursor.lnum;
2164 int i;
2165
2166#ifdef FEAT_VIRTUALEDIT
2167 /* Break tabs if needed. */
2168 if (virtual_active() && curwin->w_cursor.coladd > 0)
2169 coladvance_force(getviscol());
2170#endif
2171
2172 col = curwin->w_cursor.col;
2173 oldp = ml_get(lnum);
2174 linelen = (int)STRLEN(oldp) + 1;
2175
2176 /* The lengths default to the values for when not replacing. */
2177 oldlen = 0;
2178#ifdef FEAT_MBYTE
2179 newlen = charlen;
2180#else
2181 newlen = 1;
2182#endif
2183
2184 if (State & REPLACE_FLAG)
2185 {
2186#ifdef FEAT_VREPLACE
2187 if (State & VREPLACE_FLAG)
2188 {
2189 colnr_T new_vcol = 0; /* init for GCC */
2190 colnr_T vcol;
2191 int old_list;
2192#ifndef FEAT_MBYTE
2193 char_u buf[2];
2194#endif
2195
2196 /*
2197 * Disable 'list' temporarily, unless 'cpo' contains the 'L' flag.
2198 * Returns the old value of list, so when finished,
2199 * curwin->w_p_list should be set back to this.
2200 */
2201 old_list = curwin->w_p_list;
2202 if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
2203 curwin->w_p_list = FALSE;
2204
2205 /*
2206 * In virtual replace mode each character may replace one or more
2207 * characters (zero if it's a TAB). Count the number of bytes to
2208 * be deleted to make room for the new character, counting screen
2209 * cells. May result in adding spaces to fill a gap.
2210 */
2211 getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL);
2212#ifndef FEAT_MBYTE
2213 buf[0] = c;
2214 buf[1] = NUL;
2215#endif
2216 new_vcol = vcol + chartabsize(buf, vcol);
2217 while (oldp[col + oldlen] != NUL && vcol < new_vcol)
2218 {
2219 vcol += chartabsize(oldp + col + oldlen, vcol);
2220 /* Don't need to remove a TAB that takes us to the right
2221 * position. */
2222 if (vcol > new_vcol && oldp[col + oldlen] == TAB)
2223 break;
2224#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002225 oldlen += (*mb_ptr2len)(oldp + col + oldlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226#else
2227 ++oldlen;
2228#endif
2229 /* Deleted a bit too much, insert spaces. */
2230 if (vcol > new_vcol)
2231 newlen += vcol - new_vcol;
2232 }
2233 curwin->w_p_list = old_list;
2234 }
2235 else
2236#endif
2237 if (oldp[col] != NUL)
2238 {
2239 /* normal replace */
2240#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002241 oldlen = (*mb_ptr2len)(oldp + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242#else
2243 oldlen = 1;
2244#endif
2245 }
2246
2247
2248 /* Push the replaced bytes onto the replace stack, so that they can be
2249 * put back when BS is used. The bytes of a multi-byte character are
2250 * done the other way around, so that the first byte is popped off
2251 * first (it tells the byte length of the character). */
2252 replace_push(NUL);
2253 for (i = 0; i < oldlen; ++i)
2254 {
2255#ifdef FEAT_MBYTE
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002256 if (has_mbyte)
2257 i += replace_push_mb(oldp + col + i) - 1;
2258 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259#endif
Bram Moolenaar2c994e82008-01-02 16:49:36 +00002260 replace_push(oldp[col + i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 }
2262 }
2263
2264 newp = alloc_check((unsigned)(linelen + newlen - oldlen));
2265 if (newp == NULL)
2266 return;
2267
2268 /* Copy bytes before the cursor. */
2269 if (col > 0)
2270 mch_memmove(newp, oldp, (size_t)col);
2271
2272 /* Copy bytes after the changed character(s). */
2273 p = newp + col;
2274 mch_memmove(p + newlen, oldp + col + oldlen,
2275 (size_t)(linelen - col - oldlen));
2276
2277 /* Insert or overwrite the new character. */
2278#ifdef FEAT_MBYTE
2279 mch_memmove(p, buf, charlen);
2280 i = charlen;
2281#else
2282 *p = c;
2283 i = 1;
2284#endif
2285
2286 /* Fill with spaces when necessary. */
2287 while (i < newlen)
2288 p[i++] = ' ';
2289
2290 /* Replace the line in the buffer. */
2291 ml_replace(lnum, newp, FALSE);
2292
2293 /* mark the buffer as changed and prepare for displaying */
2294 changed_bytes(lnum, col);
2295
2296 /*
2297 * If we're in Insert or Replace mode and 'showmatch' is set, then briefly
2298 * show the match for right parens and braces.
2299 */
2300 if (p_sm && (State & INSERT)
2301 && msg_silent == 0
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002302#ifdef FEAT_INS_EXPAND
2303 && !ins_compl_active()
2304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 )
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002306 {
2307#ifdef FEAT_MBYTE
2308 if (has_mbyte)
2309 showmatch(mb_ptr2char(buf));
2310 else
2311#endif
2312 showmatch(c);
2313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314
2315#ifdef FEAT_RIGHTLEFT
2316 if (!p_ri || (State & REPLACE_FLAG))
2317#endif
2318 {
2319 /* Normal insert: move cursor right */
2320#ifdef FEAT_MBYTE
2321 curwin->w_cursor.col += charlen;
2322#else
2323 ++curwin->w_cursor.col;
2324#endif
2325 }
2326 /*
2327 * TODO: should try to update w_row here, to avoid recomputing it later.
2328 */
2329}
2330
2331/*
2332 * Insert a string at the cursor position.
2333 * Note: Does NOT handle Replace mode.
2334 * Caller must have prepared for undo.
2335 */
2336 void
2337ins_str(s)
2338 char_u *s;
2339{
2340 char_u *oldp, *newp;
2341 int newlen = (int)STRLEN(s);
2342 int oldlen;
2343 colnr_T col;
2344 linenr_T lnum = curwin->w_cursor.lnum;
2345
2346#ifdef FEAT_VIRTUALEDIT
2347 if (virtual_active() && curwin->w_cursor.coladd > 0)
2348 coladvance_force(getviscol());
2349#endif
2350
2351 col = curwin->w_cursor.col;
2352 oldp = ml_get(lnum);
2353 oldlen = (int)STRLEN(oldp);
2354
2355 newp = alloc_check((unsigned)(oldlen + newlen + 1));
2356 if (newp == NULL)
2357 return;
2358 if (col > 0)
2359 mch_memmove(newp, oldp, (size_t)col);
2360 mch_memmove(newp + col, s, (size_t)newlen);
2361 mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1));
2362 ml_replace(lnum, newp, FALSE);
2363 changed_bytes(lnum, col);
2364 curwin->w_cursor.col += newlen;
2365}
2366
2367/*
2368 * Delete one character under the cursor.
2369 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2370 * Caller must have prepared for undo.
2371 *
2372 * return FAIL for failure, OK otherwise
2373 */
2374 int
2375del_char(fixpos)
2376 int fixpos;
2377{
2378#ifdef FEAT_MBYTE
2379 if (has_mbyte)
2380 {
2381 /* Make sure the cursor is at the start of a character. */
2382 mb_adjust_cursor();
2383 if (*ml_get_cursor() == NUL)
2384 return FAIL;
2385 return del_chars(1L, fixpos);
2386 }
2387#endif
Bram Moolenaare3226be2005-12-18 22:10:00 +00002388 return del_bytes(1L, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389}
2390
2391#if defined(FEAT_MBYTE) || defined(PROTO)
2392/*
2393 * Like del_bytes(), but delete characters instead of bytes.
2394 */
2395 int
2396del_chars(count, fixpos)
2397 long count;
2398 int fixpos;
2399{
2400 long bytes = 0;
2401 long i;
2402 char_u *p;
2403 int l;
2404
2405 p = ml_get_cursor();
2406 for (i = 0; i < count && *p != NUL; ++i)
2407 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002408 l = (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 bytes += l;
2410 p += l;
2411 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00002412 return del_bytes(bytes, fixpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413}
2414#endif
2415
2416/*
2417 * Delete "count" bytes under the cursor.
2418 * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line.
2419 * Caller must have prepared for undo.
2420 *
2421 * return FAIL for failure, OK otherwise
2422 */
2423 int
Bram Moolenaarca003e12006-03-17 23:19:38 +00002424del_bytes(count, fixpos_arg, use_delcombine)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 long count;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002426 int fixpos_arg;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002427 int use_delcombine UNUSED; /* 'delcombine' option applies */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428{
2429 char_u *oldp, *newp;
2430 colnr_T oldlen;
2431 linenr_T lnum = curwin->w_cursor.lnum;
2432 colnr_T col = curwin->w_cursor.col;
2433 int was_alloced;
2434 long movelen;
Bram Moolenaarca003e12006-03-17 23:19:38 +00002435 int fixpos = fixpos_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436
2437 oldp = ml_get(lnum);
2438 oldlen = (int)STRLEN(oldp);
2439
2440 /*
2441 * Can't do anything when the cursor is on the NUL after the line.
2442 */
2443 if (col >= oldlen)
2444 return FAIL;
2445
2446#ifdef FEAT_MBYTE
2447 /* If 'delcombine' is set and deleting (less than) one character, only
2448 * delete the last combining character. */
Bram Moolenaare3226be2005-12-18 22:10:00 +00002449 if (p_deco && use_delcombine && enc_utf8
2450 && utfc_ptr2len(oldp + col) >= count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002452 int cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 int n;
2454
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002455 (void)utfc_ptr2char(oldp + col, cc);
2456 if (cc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 {
2458 /* Find the last composing char, there can be several. */
2459 n = col;
2460 do
2461 {
2462 col = n;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002463 count = utf_ptr2len(oldp + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 n += count;
2465 } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n));
2466 fixpos = 0;
2467 }
2468 }
2469#endif
2470
2471 /*
2472 * When count is too big, reduce it.
2473 */
2474 movelen = (long)oldlen - (long)col - count + 1; /* includes trailing NUL */
2475 if (movelen <= 1)
2476 {
2477 /*
2478 * If we just took off the last character of a non-blank line, and
Bram Moolenaarca003e12006-03-17 23:19:38 +00002479 * fixpos is TRUE, we don't want to end up positioned at the NUL,
2480 * unless "restart_edit" is set or 'virtualedit' contains "onemore".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 */
Bram Moolenaarca003e12006-03-17 23:19:38 +00002482 if (col > 0 && fixpos && restart_edit == 0
2483#ifdef FEAT_VIRTUALEDIT
2484 && (ve_flags & VE_ONEMORE) == 0
2485#endif
2486 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 {
2488 --curwin->w_cursor.col;
2489#ifdef FEAT_VIRTUALEDIT
2490 curwin->w_cursor.coladd = 0;
2491#endif
2492#ifdef FEAT_MBYTE
2493 if (has_mbyte)
2494 curwin->w_cursor.col -=
2495 (*mb_head_off)(oldp, oldp + curwin->w_cursor.col);
2496#endif
2497 }
2498 count = oldlen - col;
2499 movelen = 1;
2500 }
2501
2502 /*
2503 * If the old line has been allocated the deletion can be done in the
2504 * existing line. Otherwise a new line has to be allocated
Bram Moolenaare21877a2008-02-13 09:58:14 +00002505 * Can't do this when using Netbeans, because we would need to invoke
2506 * netbeans_removed(), which deallocates the line. Let ml_replace() take
Bram Moolenaar1a509df2010-08-01 17:59:57 +02002507 * care of notifying Netbeans.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002510 if (netbeans_active())
Bram Moolenaare21877a2008-02-13 09:58:14 +00002511 was_alloced = FALSE;
2512 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513#endif
Bram Moolenaare21877a2008-02-13 09:58:14 +00002514 was_alloced = ml_line_alloced(); /* check if oldp was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 if (was_alloced)
2516 newp = oldp; /* use same allocated memory */
2517 else
2518 { /* need to allocate a new line */
2519 newp = alloc((unsigned)(oldlen + 1 - count));
2520 if (newp == NULL)
2521 return FAIL;
2522 mch_memmove(newp, oldp, (size_t)col);
2523 }
2524 mch_memmove(newp + col, oldp + col + count, (size_t)movelen);
2525 if (!was_alloced)
2526 ml_replace(lnum, newp, FALSE);
2527
2528 /* mark the buffer as changed and prepare for displaying */
2529 changed_bytes(lnum, curwin->w_cursor.col);
2530
2531 return OK;
2532}
2533
2534/*
2535 * Delete from cursor to end of line.
2536 * Caller must have prepared for undo.
2537 *
2538 * return FAIL for failure, OK otherwise
2539 */
2540 int
2541truncate_line(fixpos)
2542 int fixpos; /* if TRUE fix the cursor position when done */
2543{
2544 char_u *newp;
2545 linenr_T lnum = curwin->w_cursor.lnum;
2546 colnr_T col = curwin->w_cursor.col;
2547
2548 if (col == 0)
2549 newp = vim_strsave((char_u *)"");
2550 else
2551 newp = vim_strnsave(ml_get(lnum), col);
2552
2553 if (newp == NULL)
2554 return FAIL;
2555
2556 ml_replace(lnum, newp, FALSE);
2557
2558 /* mark the buffer as changed and prepare for displaying */
2559 changed_bytes(lnum, curwin->w_cursor.col);
2560
2561 /*
2562 * If "fixpos" is TRUE we don't want to end up positioned at the NUL.
2563 */
2564 if (fixpos && curwin->w_cursor.col > 0)
2565 --curwin->w_cursor.col;
2566
2567 return OK;
2568}
2569
2570/*
2571 * Delete "nlines" lines at the cursor.
2572 * Saves the lines for undo first if "undo" is TRUE.
2573 */
2574 void
2575del_lines(nlines, undo)
2576 long nlines; /* number of lines to delete */
2577 int undo; /* if TRUE, prepare for undo */
2578{
2579 long n;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002580 linenr_T first = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581
2582 if (nlines <= 0)
2583 return;
2584
2585 /* save the deleted lines for undo */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002586 if (undo && u_savedel(first, nlines) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 return;
2588
2589 for (n = 0; n < nlines; )
2590 {
2591 if (curbuf->b_ml.ml_flags & ML_EMPTY) /* nothing to delete */
2592 break;
2593
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002594 ml_delete(first, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 ++n;
2596
2597 /* If we delete the last line in the file, stop */
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002598 if (first > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 break;
2600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002602 /* Correct the cursor position before calling deleted_lines_mark(), it may
2603 * trigger a callback to display the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 curwin->w_cursor.col = 0;
2605 check_cursor_lnum();
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002606
2607 /* adjust marks, mark the buffer as changed and prepare for displaying */
2608 deleted_lines_mark(first, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609}
2610
2611 int
2612gchar_pos(pos)
2613 pos_T *pos;
2614{
2615 char_u *ptr = ml_get_pos(pos);
2616
2617#ifdef FEAT_MBYTE
2618 if (has_mbyte)
2619 return (*mb_ptr2char)(ptr);
2620#endif
2621 return (int)*ptr;
2622}
2623
2624 int
2625gchar_cursor()
2626{
2627#ifdef FEAT_MBYTE
2628 if (has_mbyte)
2629 return (*mb_ptr2char)(ml_get_cursor());
2630#endif
2631 return (int)*ml_get_cursor();
2632}
2633
2634/*
2635 * Write a character at the current cursor position.
2636 * It is directly written into the block.
2637 */
2638 void
2639pchar_cursor(c)
2640 int c;
2641{
2642 *(ml_get_buf(curbuf, curwin->w_cursor.lnum, TRUE)
2643 + curwin->w_cursor.col) = c;
2644}
2645
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646/*
2647 * When extra == 0: Return TRUE if the cursor is before or on the first
2648 * non-blank in the line.
2649 * When extra == 1: Return TRUE if the cursor is before the first non-blank in
2650 * the line.
2651 */
2652 int
2653inindent(extra)
2654 int extra;
2655{
2656 char_u *ptr;
2657 colnr_T col;
2658
2659 for (col = 0, ptr = ml_get_curline(); vim_iswhite(*ptr); ++col)
2660 ++ptr;
2661 if (col >= curwin->w_cursor.col + extra)
2662 return TRUE;
2663 else
2664 return FALSE;
2665}
2666
2667/*
2668 * Skip to next part of an option argument: Skip space and comma.
2669 */
2670 char_u *
2671skip_to_option_part(p)
2672 char_u *p;
2673{
2674 if (*p == ',')
2675 ++p;
2676 while (*p == ' ')
2677 ++p;
2678 return p;
2679}
2680
2681/*
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002682 * Call this function when something in the current buffer is changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 *
2684 * Most often called through changed_bytes() and changed_lines(), which also
2685 * mark the area of the display to be redrawn.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002686 *
2687 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 */
2689 void
2690changed()
2691{
2692#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2693 /* The text of the preediting area is inserted, but this doesn't
2694 * mean a change of the buffer yet. That is delayed until the
2695 * text is committed. (this means preedit becomes empty) */
2696 if (im_is_preediting() && !xim_changed_while_preediting)
2697 return;
2698 xim_changed_while_preediting = FALSE;
2699#endif
2700
2701 if (!curbuf->b_changed)
2702 {
2703 int save_msg_scroll = msg_scroll;
2704
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002705 /* Give a warning about changing a read-only file. This may also
2706 * check-out the file, thus change "curbuf"! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 change_warning(0);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002708
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 /* Create a swap file if that is wanted.
2710 * Don't do this for "nofile" and "nowrite" buffer types. */
2711 if (curbuf->b_may_swap
2712#ifdef FEAT_QUICKFIX
2713 && !bt_dontwrite(curbuf)
2714#endif
2715 )
2716 {
2717 ml_open_file(curbuf);
2718
2719 /* The ml_open_file() can cause an ATTENTION message.
2720 * Wait two seconds, to make sure the user reads this unexpected
2721 * message. Since we could be anywhere, call wait_return() now,
2722 * and don't let the emsg() set msg_scroll. */
2723 if (need_wait_return && emsg_silent == 0)
2724 {
2725 out_flush();
2726 ui_delay(2000L, TRUE);
2727 wait_return(TRUE);
2728 msg_scroll = save_msg_scroll;
2729 }
2730 }
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002731 changed_int();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 }
2733 ++curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734}
2735
Bram Moolenaarfc2d5bd2010-05-15 17:06:53 +02002736/*
2737 * Internal part of changed(), no user interaction.
2738 */
2739 void
2740changed_int()
2741{
2742 curbuf->b_changed = TRUE;
2743 ml_setflags(curbuf);
2744#ifdef FEAT_WINDOWS
2745 check_status(curbuf);
2746 redraw_tabline = TRUE;
2747#endif
2748#ifdef FEAT_TITLE
2749 need_maketitle = TRUE; /* set window title later */
2750#endif
2751}
2752
Bram Moolenaardba8a912005-04-24 22:08:39 +00002753static void changedOneline __ARGS((buf_T *buf, linenr_T lnum));
2754static void changed_lines_buf __ARGS((buf_T *buf, linenr_T lnum, linenr_T lnume, long xtra));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755static void changed_common __ARGS((linenr_T lnum, colnr_T col, linenr_T lnume, long xtra));
2756
2757/*
2758 * Changed bytes within a single line for the current buffer.
2759 * - marks the windows on this buffer to be redisplayed
2760 * - marks the buffer changed by calling changed()
2761 * - invalidates cached values
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002762 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 */
2764 void
2765changed_bytes(lnum, col)
2766 linenr_T lnum;
2767 colnr_T col;
2768{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002769 changedOneline(curbuf, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 changed_common(lnum, col, lnum + 1, 0L);
Bram Moolenaardba8a912005-04-24 22:08:39 +00002771
2772#ifdef FEAT_DIFF
2773 /* Diff highlighting in other diff windows may need to be updated too. */
2774 if (curwin->w_p_diff)
2775 {
2776 win_T *wp;
2777 linenr_T wlnum;
2778
2779 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2780 if (wp->w_p_diff && wp != curwin)
2781 {
2782 redraw_win_later(wp, VALID);
2783 wlnum = diff_lnum_win(lnum, wp);
2784 if (wlnum > 0)
2785 changedOneline(wp->w_buffer, wlnum);
2786 }
2787 }
2788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789}
2790
2791 static void
Bram Moolenaardba8a912005-04-24 22:08:39 +00002792changedOneline(buf, lnum)
2793 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 linenr_T lnum;
2795{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002796 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 {
2798 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002799 if (lnum < buf->b_mod_top)
2800 buf->b_mod_top = lnum;
2801 else if (lnum >= buf->b_mod_bot)
2802 buf->b_mod_bot = lnum + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 }
2804 else
2805 {
2806 /* set the area that must be redisplayed to one line */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002807 buf->b_mod_set = TRUE;
2808 buf->b_mod_top = lnum;
2809 buf->b_mod_bot = lnum + 1;
2810 buf->b_mod_xlines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 }
2812}
2813
2814/*
2815 * Appended "count" lines below line "lnum" in the current buffer.
2816 * Must be called AFTER the change and after mark_adjust().
2817 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2818 */
2819 void
2820appended_lines(lnum, count)
2821 linenr_T lnum;
2822 long count;
2823{
2824 changed_lines(lnum + 1, 0, lnum + 1, count);
2825}
2826
2827/*
2828 * Like appended_lines(), but adjust marks first.
2829 */
2830 void
2831appended_lines_mark(lnum, count)
2832 linenr_T lnum;
2833 long count;
2834{
2835 mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L);
2836 changed_lines(lnum + 1, 0, lnum + 1, count);
2837}
2838
2839/*
2840 * Deleted "count" lines at line "lnum" in the current buffer.
2841 * Must be called AFTER the change and after mark_adjust().
2842 * Takes care of marking the buffer to be redrawn and sets the changed flag.
2843 */
2844 void
2845deleted_lines(lnum, count)
2846 linenr_T lnum;
2847 long count;
2848{
2849 changed_lines(lnum, 0, lnum + count, -count);
2850}
2851
2852/*
2853 * Like deleted_lines(), but adjust marks first.
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00002854 * Make sure the cursor is on a valid line before calling, a GUI callback may
2855 * be triggered to display the cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 */
2857 void
2858deleted_lines_mark(lnum, count)
2859 linenr_T lnum;
2860 long count;
2861{
2862 mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count);
2863 changed_lines(lnum, 0, lnum + count, -count);
2864}
2865
2866/*
2867 * Changed lines for the current buffer.
2868 * Must be called AFTER the change and after mark_adjust().
2869 * - mark the buffer changed by calling changed()
2870 * - mark the windows on this buffer to be redisplayed
2871 * - invalidate cached values
2872 * "lnum" is the first line that needs displaying, "lnume" the first line
2873 * below the changed lines (BEFORE the change).
2874 * When only inserting lines, "lnum" and "lnume" are equal.
2875 * Takes care of calling changed() and updating b_mod_*.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002876 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 */
2878 void
2879changed_lines(lnum, col, lnume, xtra)
2880 linenr_T lnum; /* first line with change */
2881 colnr_T col; /* column in first line with change */
2882 linenr_T lnume; /* line below last changed line */
2883 long xtra; /* number of extra lines (negative when deleting) */
2884{
Bram Moolenaardba8a912005-04-24 22:08:39 +00002885 changed_lines_buf(curbuf, lnum, lnume, xtra);
2886
2887#ifdef FEAT_DIFF
2888 if (xtra == 0 && curwin->w_p_diff)
2889 {
2890 /* When the number of lines doesn't change then mark_adjust() isn't
2891 * called and other diff buffers still need to be marked for
2892 * displaying. */
2893 win_T *wp;
2894 linenr_T wlnum;
2895
2896 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2897 if (wp->w_p_diff && wp != curwin)
2898 {
2899 redraw_win_later(wp, VALID);
2900 wlnum = diff_lnum_win(lnum, wp);
2901 if (wlnum > 0)
2902 changed_lines_buf(wp->w_buffer, wlnum,
2903 lnume - lnum + wlnum, 0L);
2904 }
2905 }
2906#endif
2907
2908 changed_common(lnum, col, lnume, xtra);
2909}
2910
2911 static void
2912changed_lines_buf(buf, lnum, lnume, xtra)
2913 buf_T *buf;
2914 linenr_T lnum; /* first line with change */
2915 linenr_T lnume; /* line below last changed line */
2916 long xtra; /* number of extra lines (negative when deleting) */
2917{
2918 if (buf->b_mod_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 {
2920 /* find the maximum area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002921 if (lnum < buf->b_mod_top)
2922 buf->b_mod_top = lnum;
2923 if (lnum < buf->b_mod_bot)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 {
2925 /* adjust old bot position for xtra lines */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002926 buf->b_mod_bot += xtra;
2927 if (buf->b_mod_bot < lnum)
2928 buf->b_mod_bot = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 }
Bram Moolenaardba8a912005-04-24 22:08:39 +00002930 if (lnume + xtra > buf->b_mod_bot)
2931 buf->b_mod_bot = lnume + xtra;
2932 buf->b_mod_xlines += xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 }
2934 else
2935 {
2936 /* set the area that must be redisplayed */
Bram Moolenaardba8a912005-04-24 22:08:39 +00002937 buf->b_mod_set = TRUE;
2938 buf->b_mod_top = lnum;
2939 buf->b_mod_bot = lnume + xtra;
2940 buf->b_mod_xlines = xtra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942}
2943
Bram Moolenaarb0b50882010-07-07 18:26:28 +02002944/*
2945 * Common code for when a change is was made.
2946 * See changed_lines() for the arguments.
2947 * Careful: may trigger autocommands that reload the buffer.
2948 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 static void
2950changed_common(lnum, col, lnume, xtra)
2951 linenr_T lnum;
2952 colnr_T col;
2953 linenr_T lnume;
2954 long xtra;
2955{
2956 win_T *wp;
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00002957#ifdef FEAT_WINDOWS
2958 tabpage_T *tp;
2959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 int i;
2961#ifdef FEAT_JUMPLIST
2962 int cols;
2963 pos_T *p;
2964 int add;
2965#endif
2966
2967 /* mark the buffer as modified */
2968 changed();
2969
2970 /* set the '. mark */
2971 if (!cmdmod.keepjumps)
2972 {
2973 curbuf->b_last_change.lnum = lnum;
2974 curbuf->b_last_change.col = col;
2975
2976#ifdef FEAT_JUMPLIST
2977 /* Create a new entry if a new undo-able change was started or we
2978 * don't have an entry yet. */
2979 if (curbuf->b_new_change || curbuf->b_changelistlen == 0)
2980 {
2981 if (curbuf->b_changelistlen == 0)
2982 add = TRUE;
2983 else
2984 {
2985 /* Don't create a new entry when the line number is the same
2986 * as the last one and the column is not too far away. Avoids
2987 * creating many entries for typing "xxxxx". */
2988 p = &curbuf->b_changelist[curbuf->b_changelistlen - 1];
2989 if (p->lnum != lnum)
2990 add = TRUE;
2991 else
2992 {
2993 cols = comp_textwidth(FALSE);
2994 if (cols == 0)
2995 cols = 79;
2996 add = (p->col + cols < col || col + cols < p->col);
2997 }
2998 }
2999 if (add)
3000 {
3001 /* This is the first of a new sequence of undo-able changes
3002 * and it's at some distance of the last change. Use a new
3003 * position in the changelist. */
3004 curbuf->b_new_change = FALSE;
3005
3006 if (curbuf->b_changelistlen == JUMPLISTSIZE)
3007 {
3008 /* changelist is full: remove oldest entry */
3009 curbuf->b_changelistlen = JUMPLISTSIZE - 1;
3010 mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1,
3011 sizeof(pos_T) * (JUMPLISTSIZE - 1));
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003012 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 {
3014 /* Correct position in changelist for other windows on
3015 * this buffer. */
3016 if (wp->w_buffer == curbuf && wp->w_changelistidx > 0)
3017 --wp->w_changelistidx;
3018 }
3019 }
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003020 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 {
3022 /* For other windows, if the position in the changelist is
3023 * at the end it stays at the end. */
3024 if (wp->w_buffer == curbuf
3025 && wp->w_changelistidx == curbuf->b_changelistlen)
3026 ++wp->w_changelistidx;
3027 }
3028 ++curbuf->b_changelistlen;
3029 }
3030 }
3031 curbuf->b_changelist[curbuf->b_changelistlen - 1] =
3032 curbuf->b_last_change;
3033 /* The current window is always after the last change, so that "g,"
3034 * takes you back to it. */
3035 curwin->w_changelistidx = curbuf->b_changelistlen;
3036#endif
3037 }
3038
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00003039 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 {
3041 if (wp->w_buffer == curbuf)
3042 {
3043 /* Mark this window to be redrawn later. */
3044 if (wp->w_redr_type < VALID)
3045 wp->w_redr_type = VALID;
3046
3047 /* Check if a change in the buffer has invalidated the cached
3048 * values for the cursor. */
3049#ifdef FEAT_FOLDING
3050 /*
3051 * Update the folds for this window. Can't postpone this, because
3052 * a following operator might work on the whole fold: ">>dd".
3053 */
3054 foldUpdate(wp, lnum, lnume + xtra - 1);
3055
3056 /* The change may cause lines above or below the change to become
3057 * included in a fold. Set lnum/lnume to the first/last line that
3058 * might be displayed differently.
3059 * Set w_cline_folded here as an efficient way to update it when
3060 * inserting lines just above a closed fold. */
3061 i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL);
3062 if (wp->w_cursor.lnum == lnum)
3063 wp->w_cline_folded = i;
3064 i = hasFoldingWin(wp, lnume, NULL, &lnume, FALSE, NULL);
3065 if (wp->w_cursor.lnum == lnume)
3066 wp->w_cline_folded = i;
3067
3068 /* If the changed line is in a range of previously folded lines,
3069 * compare with the first line in that range. */
3070 if (wp->w_cursor.lnum <= lnum)
3071 {
3072 i = find_wl_entry(wp, lnum);
3073 if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum)
3074 changed_line_abv_curs_win(wp);
3075 }
3076#endif
3077
3078 if (wp->w_cursor.lnum > lnum)
3079 changed_line_abv_curs_win(wp);
3080 else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col)
3081 changed_cline_bef_curs_win(wp);
3082 if (wp->w_botline >= lnum)
3083 {
3084 /* Assume that botline doesn't change (inserted lines make
3085 * other lines scroll down below botline). */
3086 approximate_botline_win(wp);
3087 }
3088
3089 /* Check if any w_lines[] entries have become invalid.
3090 * For entries below the change: Correct the lnums for
3091 * inserted/deleted lines. Makes it possible to stop displaying
3092 * after the change. */
3093 for (i = 0; i < wp->w_lines_valid; ++i)
3094 if (wp->w_lines[i].wl_valid)
3095 {
3096 if (wp->w_lines[i].wl_lnum >= lnum)
3097 {
3098 if (wp->w_lines[i].wl_lnum < lnume)
3099 {
3100 /* line included in change */
3101 wp->w_lines[i].wl_valid = FALSE;
3102 }
3103 else if (xtra != 0)
3104 {
3105 /* line below change */
3106 wp->w_lines[i].wl_lnum += xtra;
3107#ifdef FEAT_FOLDING
3108 wp->w_lines[i].wl_lastlnum += xtra;
3109#endif
3110 }
3111 }
3112#ifdef FEAT_FOLDING
3113 else if (wp->w_lines[i].wl_lastlnum >= lnum)
3114 {
3115 /* change somewhere inside this range of folded lines,
3116 * may need to be redrawn */
3117 wp->w_lines[i].wl_valid = FALSE;
3118 }
3119#endif
3120 }
Bram Moolenaar3234cc62009-11-03 17:47:12 +00003121
3122#ifdef FEAT_FOLDING
3123 /* Take care of side effects for setting w_topline when folds have
3124 * changed. Esp. when the buffer was changed in another window. */
3125 if (hasAnyFolding(wp))
3126 set_topline(wp, wp->w_topline);
3127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 }
3129 }
3130
3131 /* Call update_screen() later, which checks out what needs to be redrawn,
3132 * since it notices b_mod_set and then uses b_mod_*. */
3133 if (must_redraw < VALID)
3134 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003135
3136#ifdef FEAT_AUTOCMD
3137 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003138 if (lnum <= curwin->w_cursor.lnum
3139 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003140 last_cursormoved.lnum = 0;
3141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142}
3143
3144/*
3145 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3146 */
3147 void
3148unchanged(buf, ff)
3149 buf_T *buf;
3150 int ff; /* also reset 'fileformat' */
3151{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003152 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 {
3154 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003155 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 if (ff)
3157 save_file_ff(buf);
3158#ifdef FEAT_WINDOWS
3159 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003160 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161#endif
3162#ifdef FEAT_TITLE
3163 need_maketitle = TRUE; /* set window title later */
3164#endif
3165 }
3166 ++buf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167#ifdef FEAT_NETBEANS_INTG
3168 netbeans_unmodified(buf);
3169#endif
3170}
3171
3172#if defined(FEAT_WINDOWS) || defined(PROTO)
3173/*
3174 * check_status: called when the status bars for the buffer 'buf'
3175 * need to be updated
3176 */
3177 void
3178check_status(buf)
3179 buf_T *buf;
3180{
3181 win_T *wp;
3182
3183 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3184 if (wp->w_buffer == buf && wp->w_status_height)
3185 {
3186 wp->w_redr_status = TRUE;
3187 if (must_redraw < VALID)
3188 must_redraw = VALID;
3189 }
3190}
3191#endif
3192
3193/*
3194 * If the file is readonly, give a warning message with the first change.
3195 * Don't do this for autocommands.
3196 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003197 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003199 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 */
3201 void
3202change_warning(col)
3203 int col; /* column for message; non-zero when in insert
3204 mode and 'showmode' is on */
3205{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003206 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3207
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 if (curbuf->b_did_warn == FALSE
3209 && curbufIsChanged() == 0
3210#ifdef FEAT_AUTOCMD
3211 && !autocmd_busy
3212#endif
3213 && curbuf->b_p_ro)
3214 {
3215#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003216 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003218 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 if (!curbuf->b_p_ro)
3220 return;
3221#endif
3222 /*
3223 * Do what msg() does, but with a column offset if the warning should
3224 * be after the mode message.
3225 */
3226 msg_start();
3227 if (msg_row == Rows - 1)
3228 msg_col = col;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003229 msg_source(hl_attr(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003230 MSG_PUTS_ATTR(_(w_readonly), hl_attr(HLF_W) | MSG_HIST);
3231#ifdef FEAT_EVAL
3232 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 msg_clr_eos();
3235 (void)msg_end();
3236 if (msg_silent == 0 && !silent_mode)
3237 {
3238 out_flush();
3239 ui_delay(1000L, TRUE); /* give the user time to think about it */
3240 }
3241 curbuf->b_did_warn = TRUE;
3242 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3243 if (msg_row < Rows - 1)
3244 showmode();
3245 }
3246}
3247
3248/*
3249 * Ask for a reply from the user, a 'y' or a 'n'.
3250 * No other characters are accepted, the message is repeated until a valid
3251 * reply is entered or CTRL-C is hit.
3252 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3253 * from any buffers but directly from the user.
3254 *
3255 * return the 'y' or 'n'
3256 */
3257 int
3258ask_yesno(str, direct)
3259 char_u *str;
3260 int direct;
3261{
3262 int r = ' ';
3263 int save_State = State;
3264
3265 if (exiting) /* put terminal in raw mode for this question */
3266 settmode(TMODE_RAW);
3267 ++no_wait_return;
3268#ifdef USE_ON_FLY_SCROLL
3269 dont_scroll = TRUE; /* disallow scrolling here */
3270#endif
3271 State = CONFIRM; /* mouse behaves like with :confirm */
3272#ifdef FEAT_MOUSE
3273 setmouse(); /* disables mouse for xterm */
3274#endif
3275 ++no_mapping;
3276 ++allow_keys; /* no mapping here, but recognize keys */
3277
3278 while (r != 'y' && r != 'n')
3279 {
3280 /* same highlighting as for wait_return */
3281 smsg_attr(hl_attr(HLF_R), (char_u *)"%s (y/n)?", str);
3282 if (direct)
3283 r = get_keystroke();
3284 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003285 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 if (r == Ctrl_C || r == ESC)
3287 r = 'n';
3288 msg_putchar(r); /* show what you typed */
3289 out_flush();
3290 }
3291 --no_wait_return;
3292 State = save_State;
3293#ifdef FEAT_MOUSE
3294 setmouse();
3295#endif
3296 --no_mapping;
3297 --allow_keys;
3298
3299 return r;
3300}
3301
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003302#if defined(FEAT_MOUSE) || defined(PROTO)
3303/*
3304 * Return TRUE if "c" is a mouse key.
3305 */
3306 int
3307is_mouse_key(c)
3308 int c;
3309{
3310 return c == K_LEFTMOUSE
3311 || c == K_LEFTMOUSE_NM
3312 || c == K_LEFTDRAG
3313 || c == K_LEFTRELEASE
3314 || c == K_LEFTRELEASE_NM
3315 || c == K_MIDDLEMOUSE
3316 || c == K_MIDDLEDRAG
3317 || c == K_MIDDLERELEASE
3318 || c == K_RIGHTMOUSE
3319 || c == K_RIGHTDRAG
3320 || c == K_RIGHTRELEASE
3321 || c == K_MOUSEDOWN
3322 || c == K_MOUSEUP
3323 || c == K_MOUSELEFT
3324 || c == K_MOUSERIGHT
3325 || c == K_X1MOUSE
3326 || c == K_X1DRAG
3327 || c == K_X1RELEASE
3328 || c == K_X2MOUSE
3329 || c == K_X2DRAG
3330 || c == K_X2RELEASE;
3331}
3332#endif
3333
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334/*
3335 * Get a key stroke directly from the user.
3336 * Ignores mouse clicks and scrollbar events, except a click for the left
3337 * button (used at the more prompt).
3338 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3339 * Disadvantage: typeahead is ignored.
3340 * Translates the interrupt character for unix to ESC.
3341 */
3342 int
3343get_keystroke()
3344{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003345 char_u *buf = NULL;
3346 int buflen = 150;
3347 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 int len = 0;
3349 int n;
3350 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003351 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352
3353 mapped_ctrl_c = FALSE; /* mappings are not used here */
3354 for (;;)
3355 {
3356 cursor_on();
3357 out_flush();
3358
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003359 /* Leave some room for check_termcode() to insert a key code into (max
3360 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3361 * bytes. */
3362 maxlen = (buflen - 6 - len) / 3;
3363 if (buf == NULL)
3364 buf = alloc(buflen);
3365 else if (maxlen < 10)
3366 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003367 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003368 * escape sequence. */
3369 buflen += 100;
3370 buf = vim_realloc(buf, buflen);
3371 maxlen = (buflen - 6 - len) / 3;
3372 }
3373 if (buf == NULL)
3374 {
3375 do_outofmem_msg((long_u)buflen);
3376 return ESC; /* panic! */
3377 }
3378
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003380 * terminal code to complete. */
3381 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 if (n > 0)
3383 {
3384 /* Replace zero and CSI by a special key code. */
3385 n = fix_input_buffer(buf + len, n, FALSE);
3386 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003387 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003389 else if (len > 0)
3390 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391
Bram Moolenaar4395a712006-09-05 18:57:57 +00003392 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003393 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003394 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003396
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003397 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003398 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003399 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003400 {
3401 /* Redrawing was postponed, do it now. */
3402 update_screen(0);
3403 setcursor(); /* put cursor back where it belongs */
3404 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003405 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003406 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003407 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003409 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 continue;
3411
3412 /* Handle modifier and/or special key code. */
3413 n = buf[0];
3414 if (n == K_SPECIAL)
3415 {
3416 n = TO_SPECIAL(buf[1], buf[2]);
3417 if (buf[1] == KS_MODIFIER
3418 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003419#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003420 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003421#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003422#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 || n == K_VER_SCROLLBAR
3424 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425#endif
3426 )
3427 {
3428 if (buf[1] == KS_MODIFIER)
3429 mod_mask = buf[2];
3430 len -= 3;
3431 if (len > 0)
3432 mch_memmove(buf, buf + 3, (size_t)len);
3433 continue;
3434 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003435 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 }
3437#ifdef FEAT_MBYTE
3438 if (has_mbyte)
3439 {
3440 if (MB_BYTE2LEN(n) > len)
3441 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003442 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 n = (*mb_ptr2char)(buf);
3444 }
3445#endif
3446#ifdef UNIX
3447 if (n == intr_char)
3448 n = ESC;
3449#endif
3450 break;
3451 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003452 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453
3454 mapped_ctrl_c = save_mapped_ctrl_c;
3455 return n;
3456}
3457
3458/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003459 * Get a number from the user.
3460 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 */
3462 int
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003463get_number(colon, mouse_used)
3464 int colon; /* allow colon to abort */
3465 int *mouse_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466{
3467 int n = 0;
3468 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003469 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003471 if (mouse_used != NULL)
3472 *mouse_used = FALSE;
3473
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 /* When not printing messages, the user won't know what to type, return a
3475 * zero (as if CR was hit). */
3476 if (msg_silent != 0)
3477 return 0;
3478
3479#ifdef USE_ON_FLY_SCROLL
3480 dont_scroll = TRUE; /* disallow scrolling here */
3481#endif
3482 ++no_mapping;
3483 ++allow_keys; /* no mapping here, but recognize keys */
3484 for (;;)
3485 {
3486 windgoto(msg_row, msg_col);
3487 c = safe_vgetc();
3488 if (VIM_ISDIGIT(c))
3489 {
3490 n = n * 10 + c - '0';
3491 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003492 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 }
3494 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3495 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003496 if (typed > 0)
3497 {
3498 MSG_PUTS("\b \b");
3499 --typed;
3500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003503#ifdef FEAT_MOUSE
3504 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3505 {
3506 *mouse_used = TRUE;
3507 n = mouse_row + 1;
3508 break;
3509 }
3510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 else if (n == 0 && c == ':' && colon)
3512 {
3513 stuffcharReadbuff(':');
3514 if (!exmode_active)
3515 cmdline_row = msg_row;
3516 skip_redraw = TRUE; /* skip redraw once */
3517 do_redraw = FALSE;
3518 break;
3519 }
3520 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3521 break;
3522 }
3523 --no_mapping;
3524 --allow_keys;
3525 return n;
3526}
3527
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003528/*
3529 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003530 * When "mouse_used" is not NULL allow using the mouse and in that case return
3531 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003532 */
3533 int
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003534prompt_for_number(mouse_used)
3535 int *mouse_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003536{
3537 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003538 int save_cmdline_row;
3539 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003540
3541 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003542 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003543 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003544 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003545 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003546
Bram Moolenaar203335e2006-09-03 14:35:42 +00003547 /* Set the state such that text can be selected/copied/pasted and we still
3548 * get mouse events. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003549 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003550 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003551 save_State = State;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003552 State = CMDLINE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003553
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003554 i = get_number(TRUE, mouse_used);
3555 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003556 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003557 /* don't call wait_return() now */
3558 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003559 cmdline_row = msg_row - 1;
3560 need_wait_return = FALSE;
3561 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003562 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003563 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003564 else
3565 cmdline_row = save_cmdline_row;
3566 State = save_State;
3567
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003568 return i;
3569}
3570
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 void
3572msgmore(n)
3573 long n;
3574{
3575 long pn;
3576
3577 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3579 return;
3580
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003581 /* We don't want to overwrite another important message, but do overwrite
3582 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3583 * then "put" reports the last action. */
3584 if (keep_msg != NULL && !keep_msg_more)
3585 return;
3586
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 if (n > 0)
3588 pn = n;
3589 else
3590 pn = -n;
3591
3592 if (pn > p_report)
3593 {
3594 if (pn == 1)
3595 {
3596 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003597 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3598 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003600 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3601 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 }
3603 else
3604 {
3605 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003606 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3607 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003609 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3610 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 }
3612 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003613 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 if (msg(msg_buf))
3615 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003616 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003617 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 }
3619 }
3620}
3621
3622/*
3623 * flush map and typeahead buffers and give a warning for an error
3624 */
3625 void
3626beep_flush()
3627{
3628 if (emsg_silent == 0)
3629 {
3630 flush_buffers(FALSE);
3631 vim_beep();
3632 }
3633}
3634
3635/*
3636 * give a warning for an error
3637 */
3638 void
3639vim_beep()
3640{
3641 if (emsg_silent == 0)
3642 {
3643 if (p_vb
3644#ifdef FEAT_GUI
3645 /* While the GUI is starting up the termcap is set for the GUI
3646 * but the output still goes to a terminal. */
3647 && !(gui.in_use && gui.starting)
3648#endif
3649 )
3650 {
3651 out_str(T_VB);
3652 }
3653 else
3654 {
3655#ifdef MSDOS
3656 /*
3657 * The number of beeps outputted is reduced to avoid having to wait
3658 * for all the beeps to finish. This is only a problem on systems
3659 * where the beeps don't overlap.
3660 */
3661 if (beep_count == 0 || beep_count == 10)
3662 {
3663 out_char(BELL);
3664 beep_count = 1;
3665 }
3666 else
3667 ++beep_count;
3668#else
3669 out_char(BELL);
3670#endif
3671 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003672
3673 /* When 'verbose' is set and we are sourcing a script or executing a
3674 * function give the user a hint where the beep comes from. */
3675 if (vim_strchr(p_debug, 'e') != NULL)
3676 {
3677 msg_source(hl_attr(HLF_W));
3678 msg_attr((char_u *)_("Beep!"), hl_attr(HLF_W));
3679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 }
3681}
3682
3683/*
3684 * To get the "real" home directory:
3685 * - get value of $HOME
3686 * For Unix:
3687 * - go to that directory
3688 * - do mch_dirname() to get the real name of that directory.
3689 * This also works with mounts and links.
3690 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3691 */
3692static char_u *homedir = NULL;
3693
3694 void
3695init_homedir()
3696{
3697 char_u *var;
3698
Bram Moolenaar05159a02005-02-26 23:04:13 +00003699 /* In case we are called a second time (when 'encoding' changes). */
3700 vim_free(homedir);
3701 homedir = NULL;
3702
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703#ifdef VMS
3704 var = mch_getenv((char_u *)"SYS$LOGIN");
3705#else
3706 var = mch_getenv((char_u *)"HOME");
3707#endif
3708
3709 if (var != NULL && *var == NUL) /* empty is same as not set */
3710 var = NULL;
3711
3712#ifdef WIN3264
3713 /*
3714 * Weird but true: $HOME may contain an indirect reference to another
3715 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3716 * when $HOME is being set.
3717 */
3718 if (var != NULL && *var == '%')
3719 {
3720 char_u *p;
3721 char_u *exp;
3722
3723 p = vim_strchr(var + 1, '%');
3724 if (p != NULL)
3725 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003726 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 exp = mch_getenv(NameBuff);
3728 if (exp != NULL && *exp != NUL
3729 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3730 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003731 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 var = NameBuff;
3733 /* Also set $HOME, it's needed for _viminfo. */
3734 vim_setenv((char_u *)"HOME", NameBuff);
3735 }
3736 }
3737 }
3738
3739 /*
3740 * Typically, $HOME is not defined on Windows, unless the user has
3741 * specifically defined it for Vim's sake. However, on Windows NT
3742 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3743 * each user. Try constructing $HOME from these.
3744 */
3745 if (var == NULL)
3746 {
3747 char_u *homedrive, *homepath;
3748
3749 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3750 homepath = mch_getenv((char_u *)"HOMEPATH");
Bram Moolenaar6f977012010-01-06 17:53:38 +01003751 if (homepath == NULL || *homepath == NUL)
3752 homepath = "\\";
3753 if (homedrive != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3755 {
3756 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3757 if (NameBuff[0] != NUL)
3758 {
3759 var = NameBuff;
3760 /* Also set $HOME, it's needed for _viminfo. */
3761 vim_setenv((char_u *)"HOME", NameBuff);
3762 }
3763 }
3764 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003765
3766# if defined(FEAT_MBYTE)
3767 if (enc_utf8 && var != NULL)
3768 {
3769 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003770 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003771
3772 /* Convert from active codepage to UTF-8. Other conversions are
3773 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003774 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003775 if (pp != NULL)
3776 {
3777 homedir = pp;
3778 return;
3779 }
3780 }
3781# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782#endif
3783
3784#if defined(OS2) || defined(MSDOS) || defined(MSWIN)
3785 /*
3786 * Default home dir is C:/
3787 * Best assumption we can make in such a situation.
3788 */
3789 if (var == NULL)
3790 var = "C:/";
3791#endif
3792 if (var != NULL)
3793 {
3794#ifdef UNIX
3795 /*
3796 * Change to the directory and get the actual path. This resolves
3797 * links. Don't do it when we can't return.
3798 */
3799 if (mch_dirname(NameBuff, MAXPATHL) == OK
3800 && mch_chdir((char *)NameBuff) == 0)
3801 {
3802 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3803 var = IObuff;
3804 if (mch_chdir((char *)NameBuff) != 0)
3805 EMSG(_(e_prev_dir));
3806 }
3807#endif
3808 homedir = vim_strsave(var);
3809 }
3810}
3811
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003812#if defined(EXITFREE) || defined(PROTO)
3813 void
3814free_homedir()
3815{
3816 vim_free(homedir);
3817}
Bram Moolenaar24305862012-08-15 14:05:05 +02003818
3819# ifdef FEAT_CMDL_COMPL
3820 void
3821free_users()
3822{
3823 ga_clear_strings(&ga_users);
3824}
3825# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003826#endif
3827
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003829 * Call expand_env() and store the result in an allocated string.
3830 * This is not very memory efficient, this expects the result to be freed
3831 * again soon.
3832 */
3833 char_u *
3834expand_env_save(src)
3835 char_u *src;
3836{
3837 return expand_env_save_opt(src, FALSE);
3838}
3839
3840/*
3841 * Idem, but when "one" is TRUE handle the string as one file name, only
3842 * expand "~" at the start.
3843 */
3844 char_u *
3845expand_env_save_opt(src, one)
3846 char_u *src;
3847 int one;
3848{
3849 char_u *p;
3850
3851 p = alloc(MAXPATHL);
3852 if (p != NULL)
3853 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3854 return p;
3855}
3856
3857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 * Expand environment variable with path name.
3859 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003860 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 * If anything fails no expansion is done and dst equals src.
3862 */
3863 void
3864expand_env(src, dst, dstlen)
3865 char_u *src; /* input string e.g. "$HOME/vim.hlp" */
3866 char_u *dst; /* where to put the result */
3867 int dstlen; /* maximum length of the result */
3868{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003869 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870}
3871
3872 void
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003873expand_env_esc(srcp, dst, dstlen, esc, one, startstr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003874 char_u *srcp; /* input string e.g. "$HOME/vim.hlp" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 char_u *dst; /* where to put the result */
3876 int dstlen; /* maximum length of the result */
3877 int esc; /* escape spaces in expanded variables */
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003878 int one; /* "srcp" is one file name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003879 char_u *startstr; /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003881 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 char_u *tail;
3883 int c;
3884 char_u *var;
3885 int copy_char;
3886 int mustfree; /* var was allocated, need to free it later */
3887 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003888 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003890 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003891 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003892
3893 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 --dstlen; /* leave one char space for "\," */
3895 while (*src && dstlen > 0)
3896 {
3897 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003898 if ((*src == '$'
3899#ifdef VMS
3900 && at_start
3901#endif
3902 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3904 || *src == '%'
3905#endif
3906 || (*src == '~' && at_start))
3907 {
3908 mustfree = FALSE;
3909
3910 /*
3911 * The variable name is copied into dst temporarily, because it may
3912 * be a string in read-only memory and a NUL needs to be appended.
3913 */
3914 if (*src != '~') /* environment var */
3915 {
3916 tail = src + 1;
3917 var = dst;
3918 c = dstlen - 1;
3919
3920#ifdef UNIX
3921 /* Unix has ${var-name} type environment vars */
3922 if (*tail == '{' && !vim_isIDc('{'))
3923 {
3924 tail++; /* ignore '{' */
3925 while (c-- > 0 && *tail && *tail != '}')
3926 *var++ = *tail++;
3927 }
3928 else
3929#endif
3930 {
3931 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
3932#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3933 || (*src == '%' && *tail != '%')
3934#endif
3935 ))
3936 {
3937#ifdef OS2 /* env vars only in uppercase */
3938 *var++ = TOUPPER_LOC(*tail);
3939 tail++; /* toupper() may be a macro! */
3940#else
3941 *var++ = *tail++;
3942#endif
3943 }
3944 }
3945
3946#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX)
3947# ifdef UNIX
3948 if (src[1] == '{' && *tail != '}')
3949# else
3950 if (*src == '%' && *tail != '%')
3951# endif
3952 var = NULL;
3953 else
3954 {
3955# ifdef UNIX
3956 if (src[1] == '{')
3957# else
3958 if (*src == '%')
3959#endif
3960 ++tail;
3961#endif
3962 *var = NUL;
3963 var = vim_getenv(dst, &mustfree);
3964#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX)
3965 }
3966#endif
3967 }
3968 /* home directory */
3969 else if ( src[1] == NUL
3970 || vim_ispathsep(src[1])
3971 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
3972 {
3973 var = homedir;
3974 tail = src + 1;
3975 }
3976 else /* user directory */
3977 {
3978#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
3979 /*
3980 * Copy ~user to dst[], so we can put a NUL after it.
3981 */
3982 tail = src;
3983 var = dst;
3984 c = dstlen - 1;
3985 while ( c-- > 0
3986 && *tail
3987 && vim_isfilec(*tail)
3988 && !vim_ispathsep(*tail))
3989 *var++ = *tail++;
3990 *var = NUL;
3991# ifdef UNIX
3992 /*
3993 * If the system supports getpwnam(), use it.
3994 * Otherwise, or if getpwnam() fails, the shell is used to
3995 * expand ~user. This is slower and may fail if the shell
3996 * does not support ~user (old versions of /bin/sh).
3997 */
3998# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
3999 {
4000 struct passwd *pw;
4001
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004002 /* Note: memory allocated by getpwnam() is never freed.
4003 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 pw = getpwnam((char *)dst + 1);
4005 if (pw != NULL)
4006 var = (char_u *)pw->pw_dir;
4007 else
4008 var = NULL;
4009 }
4010 if (var == NULL)
4011# endif
4012 {
4013 expand_T xpc;
4014
4015 ExpandInit(&xpc);
4016 xpc.xp_context = EXPAND_FILES;
4017 var = ExpandOne(&xpc, dst, NULL,
4018 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 mustfree = TRUE;
4020 }
4021
4022# else /* !UNIX, thus VMS */
4023 /*
4024 * USER_HOME is a comma-separated list of
4025 * directories to search for the user account in.
4026 */
4027 {
4028 char_u test[MAXPATHL], paths[MAXPATHL];
4029 char_u *path, *next_path, *ptr;
4030 struct stat st;
4031
4032 STRCPY(paths, USER_HOME);
4033 next_path = paths;
4034 while (*next_path)
4035 {
4036 for (path = next_path; *next_path && *next_path != ',';
4037 next_path++);
4038 if (*next_path)
4039 *next_path++ = NUL;
4040 STRCPY(test, path);
4041 STRCAT(test, "/");
4042 STRCAT(test, dst + 1);
4043 if (mch_stat(test, &st) == 0)
4044 {
4045 var = alloc(STRLEN(test) + 1);
4046 STRCPY(var, test);
4047 mustfree = TRUE;
4048 break;
4049 }
4050 }
4051 }
4052# endif /* UNIX */
4053#else
4054 /* cannot expand user's home directory, so don't try */
4055 var = NULL;
4056 tail = (char_u *)""; /* for gcc */
4057#endif /* UNIX || VMS */
4058 }
4059
4060#ifdef BACKSLASH_IN_FILENAME
4061 /* If 'shellslash' is set change backslashes to forward slashes.
4062 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4063 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4064 {
4065 char_u *p = vim_strsave(var);
4066
4067 if (p != NULL)
4068 {
4069 if (mustfree)
4070 vim_free(var);
4071 var = p;
4072 mustfree = TRUE;
4073 forward_slash(var);
4074 }
4075 }
4076#endif
4077
4078 /* If "var" contains white space, escape it with a backslash.
4079 * Required for ":e ~/tt" when $HOME includes a space. */
4080 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4081 {
4082 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4083
4084 if (p != NULL)
4085 {
4086 if (mustfree)
4087 vim_free(var);
4088 var = p;
4089 mustfree = TRUE;
4090 }
4091 }
4092
4093 if (var != NULL && *var != NUL
4094 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4095 {
4096 STRCPY(dst, var);
4097 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004098 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 /* if var[] ends in a path separator and tail[] starts
4100 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004101 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4103 && dst[-1] != ':'
4104#endif
4105 && vim_ispathsep(*tail))
4106 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004107 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 src = tail;
4109 copy_char = FALSE;
4110 }
4111 if (mustfree)
4112 vim_free(var);
4113 }
4114
4115 if (copy_char) /* copy at least one char */
4116 {
4117 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004118 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004119 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4120 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 */
4122 at_start = FALSE;
4123 if (src[0] == '\\' && src[1] != NUL)
4124 {
4125 *dst++ = *src++;
4126 --dstlen;
4127 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004128 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 at_start = TRUE;
4130 *dst++ = *src++;
4131 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004132
4133 if (startstr != NULL && src - startstr_len >= srcp
4134 && STRNCMP(src - startstr_len, startstr, startstr_len) == 0)
4135 at_start = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 }
4137 }
4138 *dst = NUL;
4139}
4140
4141/*
4142 * Vim's version of getenv().
4143 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004144 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004145 * "mustfree" is set to TRUE when returned is allocated, it must be
4146 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 */
4148 char_u *
4149vim_getenv(name, mustfree)
4150 char_u *name;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004151 int *mustfree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152{
4153 char_u *p;
4154 char_u *pend;
4155 int vimruntime;
4156
4157#if defined(OS2) || defined(MSDOS) || defined(MSWIN)
4158 /* use "C:/" when $HOME is not set */
4159 if (STRCMP(name, "HOME") == 0)
4160 return homedir;
4161#endif
4162
4163 p = mch_getenv(name);
4164 if (p != NULL && *p == NUL) /* empty is the same as not set */
4165 p = NULL;
4166
4167 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004168 {
4169#if defined(FEAT_MBYTE) && defined(WIN3264)
4170 if (enc_utf8)
4171 {
4172 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004173 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004174
4175 /* Convert from active codepage to UTF-8. Other conversions are
4176 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004177 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004178 if (pp != NULL)
4179 {
4180 p = pp;
4181 *mustfree = TRUE;
4182 }
4183 }
4184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187
4188 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4189 if (!vimruntime && STRCMP(name, "VIM") != 0)
4190 return NULL;
4191
4192 /*
4193 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4194 * Don't do this when default_vimruntime_dir is non-empty.
4195 */
4196 if (vimruntime
4197#ifdef HAVE_PATHDEF
4198 && *default_vimruntime_dir == NUL
4199#endif
4200 )
4201 {
4202 p = mch_getenv((char_u *)"VIM");
4203 if (p != NULL && *p == NUL) /* empty is the same as not set */
4204 p = NULL;
4205 if (p != NULL)
4206 {
4207 p = vim_version_dir(p);
4208 if (p != NULL)
4209 *mustfree = TRUE;
4210 else
4211 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004212
4213#if defined(FEAT_MBYTE) && defined(WIN3264)
4214 if (enc_utf8)
4215 {
4216 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004217 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004218
4219 /* Convert from active codepage to UTF-8. Other conversions
4220 * are not done, because they would fail for non-ASCII
4221 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004222 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004223 if (pp != NULL)
4224 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004225 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004226 vim_free(p);
4227 p = pp;
4228 *mustfree = TRUE;
4229 }
4230 }
4231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 }
4233 }
4234
4235 /*
4236 * When expanding $VIM or $VIMRUNTIME fails, try using:
4237 * - the directory name from 'helpfile' (unless it contains '$')
4238 * - the executable name from argv[0]
4239 */
4240 if (p == NULL)
4241 {
4242 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4243 p = p_hf;
4244#ifdef USE_EXE_NAME
4245 /*
4246 * Use the name of the executable, obtained from argv[0].
4247 */
4248 else
4249 p = exe_name;
4250#endif
4251 if (p != NULL)
4252 {
4253 /* remove the file name */
4254 pend = gettail(p);
4255
4256 /* remove "doc/" from 'helpfile', if present */
4257 if (p == p_hf)
4258 pend = remove_tail(p, pend, (char_u *)"doc");
4259
4260#ifdef USE_EXE_NAME
4261# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004262 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 if (p == exe_name)
4264 {
4265 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004266 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004268 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4269 if (pend1 != pend)
4270 {
4271 pnew = alloc((unsigned)(pend1 - p) + 15);
4272 if (pnew != NULL)
4273 {
4274 STRNCPY(pnew, p, (pend1 - p));
4275 STRCPY(pnew + (pend1 - p), "Resources/vim");
4276 p = pnew;
4277 pend = p + STRLEN(p);
4278 }
4279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 }
4281# endif
4282 /* remove "src/" from exe_name, if present */
4283 if (p == exe_name)
4284 pend = remove_tail(p, pend, (char_u *)"src");
4285#endif
4286
4287 /* for $VIM, remove "runtime/" or "vim54/", if present */
4288 if (!vimruntime)
4289 {
4290 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4291 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4292 }
4293
4294 /* remove trailing path separator */
4295#ifndef MACOS_CLASSIC
4296 /* With MacOS path (with colons) the final colon is required */
Bram Moolenaare21877a2008-02-13 09:58:14 +00004297 /* to avoid confusion between absolute and relative path */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004298 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 --pend;
4300#endif
4301
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004302#ifdef MACOS_X
4303 if (p == exe_name || p == p_hf)
4304#endif
4305 /* check that the result is a directory name */
4306 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307
4308 if (p != NULL && !mch_isdir(p))
4309 {
4310 vim_free(p);
4311 p = NULL;
4312 }
4313 else
4314 {
4315#ifdef USE_EXE_NAME
4316 /* may add "/vim54" or "/runtime" if it exists */
4317 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4318 {
4319 vim_free(p);
4320 p = pend;
4321 }
4322#endif
4323 *mustfree = TRUE;
4324 }
4325 }
4326 }
4327
4328#ifdef HAVE_PATHDEF
4329 /* When there is a pathdef.c file we can use default_vim_dir and
4330 * default_vimruntime_dir */
4331 if (p == NULL)
4332 {
4333 /* Only use default_vimruntime_dir when it is not empty */
4334 if (vimruntime && *default_vimruntime_dir != NUL)
4335 {
4336 p = default_vimruntime_dir;
4337 *mustfree = FALSE;
4338 }
4339 else if (*default_vim_dir != NUL)
4340 {
4341 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4342 *mustfree = TRUE;
4343 else
4344 {
4345 p = default_vim_dir;
4346 *mustfree = FALSE;
4347 }
4348 }
4349 }
4350#endif
4351
4352 /*
4353 * Set the environment variable, so that the new value can be found fast
4354 * next time, and others can also use it (e.g. Perl).
4355 */
4356 if (p != NULL)
4357 {
4358 if (vimruntime)
4359 {
4360 vim_setenv((char_u *)"VIMRUNTIME", p);
4361 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 }
4363 else
4364 {
4365 vim_setenv((char_u *)"VIM", p);
4366 didset_vim = TRUE;
4367 }
4368 }
4369 return p;
4370}
4371
4372/*
4373 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4374 * Return NULL if not, return its name in allocated memory otherwise.
4375 */
4376 static char_u *
4377vim_version_dir(vimdir)
4378 char_u *vimdir;
4379{
4380 char_u *p;
4381
4382 if (vimdir == NULL || *vimdir == NUL)
4383 return NULL;
4384 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4385 if (p != NULL && mch_isdir(p))
4386 return p;
4387 vim_free(p);
4388 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4389 if (p != NULL && mch_isdir(p))
4390 return p;
4391 vim_free(p);
4392 return NULL;
4393}
4394
4395/*
4396 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4397 * the length of "name/". Otherwise return "pend".
4398 */
4399 static char_u *
4400remove_tail(p, pend, name)
4401 char_u *p;
4402 char_u *pend;
4403 char_u *name;
4404{
4405 int len = (int)STRLEN(name) + 1;
4406 char_u *newend = pend - len;
4407
4408 if (newend >= p
4409 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004410 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 return newend;
4412 return pend;
4413}
4414
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 * Our portable version of setenv.
4417 */
4418 void
4419vim_setenv(name, val)
4420 char_u *name;
4421 char_u *val;
4422{
4423#ifdef HAVE_SETENV
4424 mch_setenv((char *)name, (char *)val, 1);
4425#else
4426 char_u *envbuf;
4427
4428 /*
4429 * Putenv does not copy the string, it has to remain
4430 * valid. The allocated memory will never be freed.
4431 */
4432 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4433 if (envbuf != NULL)
4434 {
4435 sprintf((char *)envbuf, "%s=%s", name, val);
4436 putenv((char *)envbuf);
4437 }
4438#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004439#ifdef FEAT_GETTEXT
4440 /*
4441 * When setting $VIMRUNTIME adjust the directory to find message
4442 * translations to $VIMRUNTIME/lang.
4443 */
4444 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4445 {
4446 char_u *buf = concat_str(val, (char_u *)"/lang");
4447
4448 if (buf != NULL)
4449 {
4450 bindtextdomain(VIMPACKAGE, (char *)buf);
4451 vim_free(buf);
4452 }
4453 }
4454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455}
4456
4457#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4458/*
4459 * Function given to ExpandGeneric() to obtain an environment variable name.
4460 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 char_u *
4462get_env_name(xp, idx)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004463 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 int idx;
4465{
4466# if defined(AMIGA) || defined(__MRC__) || defined(__SC__)
4467 /*
4468 * No environ[] on the Amiga and on the Mac (using MPW).
4469 */
4470 return NULL;
4471# else
4472# ifndef __WIN32__
4473 /* Borland C++ 5.2 has this in a header file. */
4474 extern char **environ;
4475# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004476# define ENVNAMELEN 100
4477 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 char_u *str;
4479 int n;
4480
4481 str = (char_u *)environ[idx];
4482 if (str == NULL)
4483 return NULL;
4484
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004485 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 {
4487 if (str[n] == '=' || str[n] == NUL)
4488 break;
4489 name[n] = str[n];
4490 }
4491 name[n] = NUL;
4492 return name;
4493# endif
4494}
Bram Moolenaar24305862012-08-15 14:05:05 +02004495
4496/*
4497 * Find all user names for user completion.
4498 * Done only once and then cached.
4499 */
4500 static void
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004501init_users()
4502{
Bram Moolenaar24305862012-08-15 14:05:05 +02004503 static int lazy_init_done = FALSE;
4504
4505 if (lazy_init_done)
4506 return;
4507
4508 lazy_init_done = TRUE;
4509 ga_init2(&ga_users, sizeof(char_u *), 20);
4510
4511# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4512 {
4513 char_u* user;
4514 struct passwd* pw;
4515
4516 setpwent();
4517 while ((pw = getpwent()) != NULL)
4518 /* pw->pw_name shouldn't be NULL but just in case... */
4519 if (pw->pw_name != NULL)
4520 {
4521 if (ga_grow(&ga_users, 1) == FAIL)
4522 break;
4523 user = vim_strsave((char_u*)pw->pw_name);
4524 if (user == NULL)
4525 break;
4526 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4527 }
4528 endpwent();
4529 }
4530# endif
4531}
4532
4533/*
4534 * Function given to ExpandGeneric() to obtain an user names.
4535 */
4536 char_u*
4537get_users(xp, idx)
4538 expand_T *xp UNUSED;
4539 int idx;
4540{
4541 init_users();
4542 if (idx < ga_users.ga_len)
4543 return ((char_u **)ga_users.ga_data)[idx];
4544 return NULL;
4545}
4546
4547/*
4548 * Check whether name matches a user name. Return:
4549 * 0 if name does not match any user name.
4550 * 1 if name partially matches the beginning of a user name.
4551 * 2 is name fully matches a user name.
4552 */
4553int match_user(name)
4554 char_u* name;
4555{
4556 int i;
4557 int n = (int)STRLEN(name);
4558 int result = 0;
4559
4560 init_users();
4561 for (i = 0; i < ga_users.ga_len; i++)
4562 {
4563 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4564 return 2; /* full match */
4565 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4566 result = 1; /* partial match */
4567 }
4568 return result;
4569}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570#endif
4571
4572/*
4573 * Replace home directory by "~" in each space or comma separated file name in
4574 * 'src'.
4575 * If anything fails (except when out of space) dst equals src.
4576 */
4577 void
4578home_replace(buf, src, dst, dstlen, one)
4579 buf_T *buf; /* when not NULL, check for help files */
4580 char_u *src; /* input file name */
4581 char_u *dst; /* where to put the result */
4582 int dstlen; /* maximum length of the result */
4583 int one; /* if TRUE, only replace one file name, include
4584 spaces and commas in the file name. */
4585{
4586 size_t dirlen = 0, envlen = 0;
4587 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004588 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 char_u *p;
4590
4591 if (src == NULL)
4592 {
4593 *dst = NUL;
4594 return;
4595 }
4596
4597 /*
4598 * If the file is a help file, remove the path completely.
4599 */
4600 if (buf != NULL && buf->b_help)
4601 {
4602 STRCPY(dst, gettail(src));
4603 return;
4604 }
4605
4606 /*
4607 * We check both the value of the $HOME environment variable and the
4608 * "real" home directory.
4609 */
4610 if (homedir != NULL)
4611 dirlen = STRLEN(homedir);
4612
4613#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004614 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004616 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4617#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004618 /* Empty is the same as not set. */
4619 if (homedir_env != NULL && *homedir_env == NUL)
4620 homedir_env = NULL;
4621
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004622#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004623 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004624 {
4625 int usedlen = 0;
4626 int flen;
4627 char_u *fbuf = NULL;
4628
4629 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004630 (void)modify_fname((char_u *)":p", &usedlen,
4631 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004632 flen = (int)STRLEN(homedir_env);
4633 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4634 /* Remove the trailing / that is added to a directory. */
4635 homedir_env[flen - 1] = NUL;
4636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637#endif
4638
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 if (homedir_env != NULL)
4640 envlen = STRLEN(homedir_env);
4641
4642 if (!one)
4643 src = skipwhite(src);
4644 while (*src && dstlen > 0)
4645 {
4646 /*
4647 * Here we are at the beginning of a file name.
4648 * First, check to see if the beginning of the file name matches
4649 * $HOME or the "real" home directory. Check that there is a '/'
4650 * after the match (so that if e.g. the file is "/home/pieter/bla",
4651 * and the home directory is "/home/piet", the file does not end up
4652 * as "~er/bla" (which would seem to indicate the file "bla" in user
4653 * er's home directory)).
4654 */
4655 p = homedir;
4656 len = dirlen;
4657 for (;;)
4658 {
4659 if ( len
4660 && fnamencmp(src, p, len) == 0
4661 && (vim_ispathsep(src[len])
4662 || (!one && (src[len] == ',' || src[len] == ' '))
4663 || src[len] == NUL))
4664 {
4665 src += len;
4666 if (--dstlen > 0)
4667 *dst++ = '~';
4668
4669 /*
4670 * If it's just the home directory, add "/".
4671 */
4672 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4673 *dst++ = '/';
4674 break;
4675 }
4676 if (p == homedir_env)
4677 break;
4678 p = homedir_env;
4679 len = envlen;
4680 }
4681
4682 /* if (!one) skip to separator: space or comma */
4683 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4684 *dst++ = *src++;
4685 /* skip separator */
4686 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4687 *dst++ = *src++;
4688 }
4689 /* if (dstlen == 0) out of space, what to do??? */
4690
4691 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004692
4693 if (homedir_env != homedir_env_orig)
4694 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695}
4696
4697/*
4698 * Like home_replace, store the replaced string in allocated memory.
4699 * When something fails, NULL is returned.
4700 */
4701 char_u *
4702home_replace_save(buf, src)
4703 buf_T *buf; /* when not NULL, check for help files */
4704 char_u *src; /* input file name */
4705{
4706 char_u *dst;
4707 unsigned len;
4708
4709 len = 3; /* space for "~/" and trailing NUL */
4710 if (src != NULL) /* just in case */
4711 len += (unsigned)STRLEN(src);
4712 dst = alloc(len);
4713 if (dst != NULL)
4714 home_replace(buf, src, dst, len, TRUE);
4715 return dst;
4716}
4717
4718/*
4719 * Compare two file names and return:
4720 * FPC_SAME if they both exist and are the same file.
4721 * FPC_SAMEX if they both don't exist and have the same file name.
4722 * FPC_DIFF if they both exist and are different files.
4723 * FPC_NOTX if they both don't exist.
4724 * FPC_DIFFX if one of them doesn't exist.
4725 * For the first name environment variables are expanded
4726 */
4727 int
4728fullpathcmp(s1, s2, checkname)
4729 char_u *s1, *s2;
4730 int checkname; /* when both don't exist, check file names */
4731{
4732#ifdef UNIX
4733 char_u exp1[MAXPATHL];
4734 char_u full1[MAXPATHL];
4735 char_u full2[MAXPATHL];
4736 struct stat st1, st2;
4737 int r1, r2;
4738
4739 expand_env(s1, exp1, MAXPATHL);
4740 r1 = mch_stat((char *)exp1, &st1);
4741 r2 = mch_stat((char *)s2, &st2);
4742 if (r1 != 0 && r2 != 0)
4743 {
4744 /* if mch_stat() doesn't work, may compare the names */
4745 if (checkname)
4746 {
4747 if (fnamecmp(exp1, s2) == 0)
4748 return FPC_SAMEX;
4749 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4750 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4751 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4752 return FPC_SAMEX;
4753 }
4754 return FPC_NOTX;
4755 }
4756 if (r1 != 0 || r2 != 0)
4757 return FPC_DIFFX;
4758 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4759 return FPC_SAME;
4760 return FPC_DIFF;
4761#else
4762 char_u *exp1; /* expanded s1 */
4763 char_u *full1; /* full path of s1 */
4764 char_u *full2; /* full path of s2 */
4765 int retval = FPC_DIFF;
4766 int r1, r2;
4767
4768 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4769 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4770 {
4771 full1 = exp1 + MAXPATHL;
4772 full2 = full1 + MAXPATHL;
4773
4774 expand_env(s1, exp1, MAXPATHL);
4775 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4776 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4777
4778 /* If vim_FullName() fails, the file probably doesn't exist. */
4779 if (r1 != OK && r2 != OK)
4780 {
4781 if (checkname && fnamecmp(exp1, s2) == 0)
4782 retval = FPC_SAMEX;
4783 else
4784 retval = FPC_NOTX;
4785 }
4786 else if (r1 != OK || r2 != OK)
4787 retval = FPC_DIFFX;
4788 else if (fnamecmp(full1, full2))
4789 retval = FPC_DIFF;
4790 else
4791 retval = FPC_SAME;
4792 vim_free(exp1);
4793 }
4794 return retval;
4795#endif
4796}
4797
4798/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004799 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004800 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004801 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 */
4803 char_u *
4804gettail(fname)
4805 char_u *fname;
4806{
4807 char_u *p1, *p2;
4808
4809 if (fname == NULL)
4810 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01004811 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01004813 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 p1 = p2 + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004815 mb_ptr_adv(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 }
4817 return p1;
4818}
4819
Bram Moolenaar31710262010-08-13 13:36:15 +02004820#if defined(FEAT_SEARCHPATH)
4821static char_u *gettail_dir __ARGS((char_u *fname));
4822
4823/*
4824 * Return the end of the directory name, on the first path
4825 * separator:
4826 * "/path/file", "/path/dir/", "/path//dir", "/file"
4827 * ^ ^ ^ ^
4828 */
4829 static char_u *
4830gettail_dir(fname)
4831 char_u *fname;
4832{
4833 char_u *dir_end = fname;
4834 char_u *next_dir_end = fname;
4835 int look_for_sep = TRUE;
4836 char_u *p;
4837
4838 for (p = fname; *p != NUL; )
4839 {
4840 if (vim_ispathsep(*p))
4841 {
4842 if (look_for_sep)
4843 {
4844 next_dir_end = p;
4845 look_for_sep = FALSE;
4846 }
4847 }
4848 else
4849 {
4850 if (!look_for_sep)
4851 dir_end = next_dir_end;
4852 look_for_sep = TRUE;
4853 }
4854 mb_ptr_adv(p);
4855 }
4856 return dir_end;
4857}
4858#endif
4859
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004861 * Get pointer to tail of "fname", including path separators. Putting a NUL
4862 * here leaves the directory name. Takes care of "c:/" and "//".
4863 * Always returns a valid pointer.
4864 */
4865 char_u *
4866gettail_sep(fname)
4867 char_u *fname;
4868{
4869 char_u *p;
4870 char_u *t;
4871
4872 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4873 t = gettail(fname);
4874 while (t > p && after_pathsep(fname, t))
4875 --t;
4876#ifdef VMS
4877 /* path separator is part of the path */
4878 ++t;
4879#endif
4880 return t;
4881}
4882
4883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 * get the next path component (just after the next path separator).
4885 */
4886 char_u *
4887getnextcomp(fname)
4888 char_u *fname;
4889{
4890 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004891 mb_ptr_adv(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 if (*fname)
4893 ++fname;
4894 return fname;
4895}
4896
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897/*
4898 * Get a pointer to one character past the head of a path name.
4899 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4900 * If there is no head, path is returned.
4901 */
4902 char_u *
4903get_past_head(path)
4904 char_u *path;
4905{
4906 char_u *retval;
4907
4908#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4909 /* may skip "c:" */
4910 if (isalpha(path[0]) && path[1] == ':')
4911 retval = path + 2;
4912 else
4913 retval = path;
4914#else
4915# if defined(AMIGA)
4916 /* may skip "label:" */
4917 retval = vim_strchr(path, ':');
4918 if (retval == NULL)
4919 retval = path;
4920# else /* Unix */
4921 retval = path;
4922# endif
4923#endif
4924
4925 while (vim_ispathsep(*retval))
4926 ++retval;
4927
4928 return retval;
4929}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930
4931/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01004932 * Return TRUE if 'c' is a path separator.
4933 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 */
4935 int
4936vim_ispathsep(c)
4937 int c;
4938{
Bram Moolenaare60acc12011-05-10 16:41:25 +02004939#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02004941#else
4942# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004944# else
4945# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
4947 return (c == ':' || c == '[' || c == ']' || c == '/'
4948 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02004949# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004951# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02004953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954}
4955
Bram Moolenaar69c35002013-11-04 02:54:12 +01004956/*
4957 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
4958 */
4959 int
4960vim_ispathsep_nocolon(c)
4961 int c;
4962{
4963 return vim_ispathsep(c)
4964#ifdef BACKSLASH_IN_FILENAME
4965 && c != ':'
4966#endif
4967 ;
4968}
4969
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970#if defined(FEAT_SEARCHPATH) || defined(PROTO)
4971/*
4972 * return TRUE if 'c' is a path list separator.
4973 */
4974 int
4975vim_ispathlistsep(c)
4976 int c;
4977{
4978#ifdef UNIX
4979 return (c == ':');
4980#else
Bram Moolenaar25394022007-05-10 19:06:20 +00004981 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982#endif
4983}
4984#endif
4985
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004986#if defined(FEAT_GUI_TABLINE) || defined(FEAT_WINDOWS) \
4987 || defined(FEAT_EVAL) || defined(PROTO)
4988/*
4989 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
4990 * It's done in-place.
4991 */
4992 void
4993shorten_dir(str)
4994 char_u *str;
4995{
4996 char_u *tail, *s, *d;
4997 int skip = FALSE;
4998
4999 tail = gettail(str);
5000 d = str;
5001 for (s = str; ; ++s)
5002 {
5003 if (s >= tail) /* copy the whole tail */
5004 {
5005 *d++ = *s;
5006 if (*s == NUL)
5007 break;
5008 }
5009 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5010 {
5011 *d++ = *s;
5012 skip = FALSE;
5013 }
5014 else if (!skip)
5015 {
5016 *d++ = *s; /* copy next char */
5017 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5018 skip = TRUE;
5019# ifdef FEAT_MBYTE
5020 if (has_mbyte)
5021 {
5022 int l = mb_ptr2len(s);
5023
5024 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005025 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005026 }
5027# endif
5028 }
5029 }
5030}
5031#endif
5032
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005033/*
5034 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5035 * Also returns TRUE if there is no directory name.
5036 * "fname" must be writable!.
5037 */
5038 int
5039dir_of_file_exists(fname)
5040 char_u *fname;
5041{
5042 char_u *p;
5043 int c;
5044 int retval;
5045
5046 p = gettail_sep(fname);
5047 if (p == fname)
5048 return TRUE;
5049 c = *p;
5050 *p = NUL;
5051 retval = mch_isdir(fname);
5052 *p = c;
5053 return retval;
5054}
5055
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005057 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5058 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 */
5060 int
5061vim_fnamecmp(x, y)
5062 char_u *x, *y;
5063{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005064#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005066#else
5067 if (p_fic)
5068 return MB_STRICMP(x, y);
5069 return STRCMP(x, y);
5070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071}
5072
5073 int
5074vim_fnamencmp(x, y, len)
5075 char_u *x, *y;
5076 size_t len;
5077{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005078#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005079 char_u *px = x;
5080 char_u *py = y;
5081 int cx = NUL;
5082 int cy = NUL;
5083
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005084 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005086 cx = PTR2CHAR(px);
5087 cy = PTR2CHAR(py);
5088 if (cx == NUL || cy == NUL
5089 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5090 && !(cx == '/' && cy == '\\')
5091 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005093 len -= MB_PTR2LEN(px);
5094 px += MB_PTR2LEN(px);
5095 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 }
5097 if (len == 0)
5098 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005099 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005100#else
5101 if (p_fic)
5102 return MB_STRNICMP(x, y, len);
5103 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005105}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106
5107/*
5108 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005109 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 */
5111 char_u *
5112concat_fnames(fname1, fname2, sep)
5113 char_u *fname1;
5114 char_u *fname2;
5115 int sep;
5116{
5117 char_u *dest;
5118
5119 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5120 if (dest != NULL)
5121 {
5122 STRCPY(dest, fname1);
5123 if (sep)
5124 add_pathsep(dest);
5125 STRCAT(dest, fname2);
5126 }
5127 return dest;
5128}
5129
Bram Moolenaard6754642005-01-17 22:18:45 +00005130/*
5131 * Concatenate two strings and return the result in allocated memory.
5132 * Returns NULL when out of memory.
5133 */
5134 char_u *
5135concat_str(str1, str2)
5136 char_u *str1;
5137 char_u *str2;
5138{
5139 char_u *dest;
5140 size_t l = STRLEN(str1);
5141
5142 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5143 if (dest != NULL)
5144 {
5145 STRCPY(dest, str1);
5146 STRCPY(dest + l, str2);
5147 }
5148 return dest;
5149}
Bram Moolenaard6754642005-01-17 22:18:45 +00005150
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151/*
5152 * Add a path separator to a file name, unless it already ends in a path
5153 * separator.
5154 */
5155 void
5156add_pathsep(p)
5157 char_u *p;
5158{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005159 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 STRCAT(p, PATHSEPSTR);
5161}
5162
5163/*
5164 * FullName_save - Make an allocated copy of a full file name.
5165 * Returns NULL when out of memory.
5166 */
5167 char_u *
5168FullName_save(fname, force)
5169 char_u *fname;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005170 int force; /* force expansion, even when it already looks
5171 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172{
5173 char_u *buf;
5174 char_u *new_fname = NULL;
5175
5176 if (fname == NULL)
5177 return NULL;
5178
5179 buf = alloc((unsigned)MAXPATHL);
5180 if (buf != NULL)
5181 {
5182 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5183 new_fname = vim_strsave(buf);
5184 else
5185 new_fname = vim_strsave(fname);
5186 vim_free(buf);
5187 }
5188 return new_fname;
5189}
5190
5191#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5192
5193static char_u *skip_string __ARGS((char_u *p));
5194
5195/*
5196 * Find the start of a comment, not knowing if we are in a comment right now.
5197 * Search starts at w_cursor.lnum and goes backwards.
5198 */
5199 pos_T *
5200find_start_comment(ind_maxcomment) /* XXX */
5201 int ind_maxcomment;
5202{
5203 pos_T *pos;
5204 char_u *line;
5205 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005206 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005208 for (;;)
5209 {
5210 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5211 if (pos == NULL)
5212 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005214 /*
5215 * Check if the comment start we found is inside a string.
5216 * If it is then restrict the search to below this line and try again.
5217 */
5218 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005219 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005220 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005221 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005222 break;
5223 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5224 if (cur_maxcomment <= 0)
5225 {
5226 pos = NULL;
5227 break;
5228 }
5229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 return pos;
5231}
5232
5233/*
5234 * Skip to the end of a "string" and a 'c' character.
5235 * If there is no string or character, return argument unmodified.
5236 */
5237 static char_u *
5238skip_string(p)
5239 char_u *p;
5240{
5241 int i;
5242
5243 /*
5244 * We loop, because strings may be concatenated: "date""time".
5245 */
5246 for ( ; ; ++p)
5247 {
5248 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5249 {
5250 if (!p[1]) /* ' at end of line */
5251 break;
5252 i = 2;
5253 if (p[1] == '\\') /* '\n' or '\000' */
5254 {
5255 ++i;
5256 while (vim_isdigit(p[i - 1])) /* '\000' */
5257 ++i;
5258 }
5259 if (p[i] == '\'') /* check for trailing ' */
5260 {
5261 p += i;
5262 continue;
5263 }
5264 }
5265 else if (p[0] == '"') /* start of string */
5266 {
5267 for (++p; p[0]; ++p)
5268 {
5269 if (p[0] == '\\' && p[1] != NUL)
5270 ++p;
5271 else if (p[0] == '"') /* end of string */
5272 break;
5273 }
5274 if (p[0] == '"')
5275 continue;
5276 }
5277 break; /* no string found */
5278 }
5279 if (!*p)
5280 --p; /* backup from NUL */
5281 return p;
5282}
5283#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5284
5285#if defined(FEAT_CINDENT) || defined(PROTO)
5286
5287/*
5288 * Do C or expression indenting on the current line.
5289 */
5290 void
5291do_c_expr_indent()
5292{
5293# ifdef FEAT_EVAL
5294 if (*curbuf->b_p_inde != NUL)
5295 fixthisline(get_expr_indent);
5296 else
5297# endif
5298 fixthisline(get_c_indent);
5299}
5300
5301/*
5302 * Functions for C-indenting.
5303 * Most of this originally comes from Eric Fischer.
5304 */
5305/*
5306 * Below "XXX" means that this function may unlock the current line.
5307 */
5308
5309static char_u *cin_skipcomment __ARGS((char_u *));
5310static int cin_nocode __ARGS((char_u *));
5311static pos_T *find_line_comment __ARGS((void));
5312static int cin_islabel_skip __ARGS((char_u **));
5313static int cin_isdefault __ARGS((char_u *));
5314static char_u *after_label __ARGS((char_u *l));
5315static int get_indent_nolabel __ARGS((linenr_T lnum));
5316static int skip_label __ARGS((linenr_T, char_u **pp, int ind_maxcomment));
5317static int cin_first_id_amount __ARGS((void));
5318static int cin_get_equal_amount __ARGS((linenr_T lnum));
5319static int cin_ispreproc __ARGS((char_u *));
5320static int cin_ispreproc_cont __ARGS((char_u **pp, linenr_T *lnump));
5321static int cin_iscomment __ARGS((char_u *));
5322static int cin_islinecomment __ARGS((char_u *));
5323static int cin_isterminated __ARGS((char_u *, int, int));
5324static int cin_isinit __ARGS((void));
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005325static int cin_isfuncdecl __ARGS((char_u **, linenr_T, linenr_T, int, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326static int cin_isif __ARGS((char_u *));
5327static int cin_iselse __ARGS((char_u *));
5328static int cin_isdo __ARGS((char_u *));
5329static int cin_iswhileofdo __ARGS((char_u *, linenr_T, int));
Bram Moolenaarb345d492012-04-09 20:42:26 +02005330static int cin_is_if_for_while_before_offset __ARGS((char_u *line, int *poffset));
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005331static int cin_iswhileofdo_end __ARGS((int terminated, int ind_maxparen, int ind_maxcomment));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332static int cin_isbreak __ARGS((char_u *));
Bram Moolenaare7c56862007-08-04 10:14:52 +00005333static int cin_is_cpp_baseclass __ARGS((colnr_T *col));
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005334static int get_baseclass_amount __ARGS((int col, int ind_maxparen, int ind_maxcomment, int ind_cpp_baseclass));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335static int cin_ends_in __ARGS((char_u *, char_u *, char_u *));
Bram Moolenaar75342212013-03-07 13:13:52 +01005336static int cin_starts_with __ARGS((char_u *s, char *word));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337static int cin_skip2pos __ARGS((pos_T *trypos));
5338static pos_T *find_start_brace __ARGS((int));
5339static pos_T *find_match_paren __ARGS((int, int));
5340static int corr_ind_maxparen __ARGS((int ind_maxparen, pos_T *startpos));
5341static int find_last_paren __ARGS((char_u *l, int start, int end));
5342static int find_match __ARGS((int lookfor, linenr_T ourscope, int ind_maxparen, int ind_maxcomment));
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005343static int cin_is_cpp_namespace __ARGS((char_u *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344
5345/*
5346 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005347 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 */
5349 static char_u *
5350cin_skipcomment(s)
5351 char_u *s;
5352{
5353 while (*s)
5354 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005355 char_u *prev_s = s;
5356
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005358
5359 /* Perl/shell # comment comment continues until eol. Require a space
5360 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005361 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005362 {
5363 s += STRLEN(s);
5364 break;
5365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 if (*s != '/')
5367 break;
5368 ++s;
5369 if (*s == '/') /* slash-slash comment continues till eol */
5370 {
5371 s += STRLEN(s);
5372 break;
5373 }
5374 if (*s != '*')
5375 break;
5376 for (++s; *s; ++s) /* skip slash-star comment */
5377 if (s[0] == '*' && s[1] == '/')
5378 {
5379 s += 2;
5380 break;
5381 }
5382 }
5383 return s;
5384}
5385
5386/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005387 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 * not considered code.
5389 */
5390 static int
5391cin_nocode(s)
5392 char_u *s;
5393{
5394 return *cin_skipcomment(s) == NUL;
5395}
5396
5397/*
5398 * Check previous lines for a "//" line comment, skipping over blank lines.
5399 */
5400 static pos_T *
5401find_line_comment() /* XXX */
5402{
5403 static pos_T pos;
5404 char_u *line;
5405 char_u *p;
5406
5407 pos = curwin->w_cursor;
5408 while (--pos.lnum > 0)
5409 {
5410 line = ml_get(pos.lnum);
5411 p = skipwhite(line);
5412 if (cin_islinecomment(p))
5413 {
5414 pos.col = (int)(p - line);
5415 return &pos;
5416 }
5417 if (*p != NUL)
5418 break;
5419 }
5420 return NULL;
5421}
5422
5423/*
5424 * Check if string matches "label:"; move to character after ':' if true.
5425 */
5426 static int
5427cin_islabel_skip(s)
5428 char_u **s;
5429{
5430 if (!vim_isIDc(**s)) /* need at least one ID character */
5431 return FALSE;
5432
5433 while (vim_isIDc(**s))
5434 (*s)++;
5435
5436 *s = cin_skipcomment(*s);
5437
5438 /* "::" is not a label, it's C++ */
5439 return (**s == ':' && *++*s != ':');
5440}
5441
5442/*
5443 * Recognize a label: "label:".
5444 * Note: curwin->w_cursor must be where we are looking for the label.
5445 */
5446 int
5447cin_islabel(ind_maxcomment) /* XXX */
5448 int ind_maxcomment;
5449{
5450 char_u *s;
5451
5452 s = cin_skipcomment(ml_get_curline());
5453
5454 /*
5455 * Exclude "default" from labels, since it should be indented
5456 * like a switch label. Same for C++ scope declarations.
5457 */
5458 if (cin_isdefault(s))
5459 return FALSE;
5460 if (cin_isscopedecl(s))
5461 return FALSE;
5462
5463 if (cin_islabel_skip(&s))
5464 {
5465 /*
5466 * Only accept a label if the previous line is terminated or is a case
5467 * label.
5468 */
5469 pos_T cursor_save;
5470 pos_T *trypos;
5471 char_u *line;
5472
5473 cursor_save = curwin->w_cursor;
5474 while (curwin->w_cursor.lnum > 1)
5475 {
5476 --curwin->w_cursor.lnum;
5477
5478 /*
5479 * If we're in a comment now, skip to the start of the comment.
5480 */
5481 curwin->w_cursor.col = 0;
5482 if ((trypos = find_start_comment(ind_maxcomment)) != NULL) /* XXX */
5483 curwin->w_cursor = *trypos;
5484
5485 line = ml_get_curline();
5486 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5487 continue;
5488 if (*(line = cin_skipcomment(line)) == NUL)
5489 continue;
5490
5491 curwin->w_cursor = cursor_save;
5492 if (cin_isterminated(line, TRUE, FALSE)
5493 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005494 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 || (cin_islabel_skip(&line) && cin_nocode(line)))
5496 return TRUE;
5497 return FALSE;
5498 }
5499 curwin->w_cursor = cursor_save;
5500 return TRUE; /* label at start of file??? */
5501 }
5502 return FALSE;
5503}
5504
5505/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005506 * Recognize structure initialization and enumerations:
5507 * "[typedef] [static|public|protected|private] enum"
5508 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 */
5510 static int
5511cin_isinit(void)
5512{
5513 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005514 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515
5516 s = cin_skipcomment(ml_get_curline());
5517
Bram Moolenaar75342212013-03-07 13:13:52 +01005518 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 s = cin_skipcomment(s + 7);
5520
Bram Moolenaar75342212013-03-07 13:13:52 +01005521 for (;;)
5522 {
5523 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005524
Bram Moolenaar75342212013-03-07 13:13:52 +01005525 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5526 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005527 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005528 if (cin_starts_with(s, skip[i]))
5529 {
5530 s = cin_skipcomment(s + l);
5531 l = 0;
5532 break;
5533 }
5534 }
5535 if (l != 0)
5536 break;
5537 }
5538
5539 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 return TRUE;
5541
5542 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5543 return TRUE;
5544
5545 return FALSE;
5546}
5547
5548/*
5549 * Recognize a switch label: "case .*:" or "default:".
5550 */
5551 int
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005552cin_iscase(s, strict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 char_u *s;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005554 int strict; /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555{
5556 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005557 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 {
5559 for (s += 4; *s; ++s)
5560 {
5561 s = cin_skipcomment(s);
5562 if (*s == ':')
5563 {
5564 if (s[1] == ':') /* skip over "::" for C++ */
5565 ++s;
5566 else
5567 return TRUE;
5568 }
5569 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005570 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5572 return FALSE; /* stop at comment */
5573 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005574 {
5575 /* JS etc. */
5576 if (strict)
5577 return FALSE; /* stop at string */
5578 else
5579 return TRUE;
5580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 }
5582 return FALSE;
5583 }
5584
5585 if (cin_isdefault(s))
5586 return TRUE;
5587 return FALSE;
5588}
5589
5590/*
5591 * Recognize a "default" switch label.
5592 */
5593 static int
5594cin_isdefault(s)
5595 char_u *s;
5596{
5597 return (STRNCMP(s, "default", 7) == 0
5598 && *(s = cin_skipcomment(s + 7)) == ':'
5599 && s[1] != ':');
5600}
5601
5602/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005603 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 */
5605 int
5606cin_isscopedecl(s)
5607 char_u *s;
5608{
5609 int i;
5610
5611 s = cin_skipcomment(s);
5612 if (STRNCMP(s, "public", 6) == 0)
5613 i = 6;
5614 else if (STRNCMP(s, "protected", 9) == 0)
5615 i = 9;
5616 else if (STRNCMP(s, "private", 7) == 0)
5617 i = 7;
5618 else
5619 return FALSE;
5620 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5621}
5622
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005623/* Maximum number of lines to search back for a "namespace" line. */
5624#define FIND_NAMESPACE_LIM 20
5625
5626/*
5627 * Recognize a "namespace" scope declaration.
5628 */
5629 static int
5630cin_is_cpp_namespace(s)
5631 char_u *s;
5632{
5633 char_u *p;
5634 int has_name = FALSE;
5635
5636 s = cin_skipcomment(s);
5637 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5638 {
5639 p = cin_skipcomment(skipwhite(s + 9));
5640 while (*p != NUL)
5641 {
5642 if (vim_iswhite(*p))
5643 {
5644 has_name = TRUE; /* found end of a name */
5645 p = cin_skipcomment(skipwhite(p));
5646 }
5647 else if (*p == '{')
5648 {
5649 break;
5650 }
5651 else if (vim_iswordc(*p))
5652 {
5653 if (has_name)
5654 return FALSE; /* word character after skipping past name */
5655 ++p;
5656 }
5657 else
5658 {
5659 return FALSE;
5660 }
5661 }
5662 return TRUE;
5663 }
5664 return FALSE;
5665}
5666
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667/*
5668 * Return a pointer to the first non-empty non-comment character after a ':'.
5669 * Return NULL if not found.
5670 * case 234: a = b;
5671 * ^
5672 */
5673 static char_u *
5674after_label(l)
5675 char_u *l;
5676{
5677 for ( ; *l; ++l)
5678 {
5679 if (*l == ':')
5680 {
5681 if (l[1] == ':') /* skip over "::" for C++ */
5682 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005683 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 break;
5685 }
5686 else if (*l == '\'' && l[1] && l[2] == '\'')
5687 l += 2; /* skip over 'x' */
5688 }
5689 if (*l == NUL)
5690 return NULL;
5691 l = cin_skipcomment(l + 1);
5692 if (*l == NUL)
5693 return NULL;
5694 return l;
5695}
5696
5697/*
5698 * Get indent of line "lnum", skipping a label.
5699 * Return 0 if there is nothing after the label.
5700 */
5701 static int
5702get_indent_nolabel(lnum) /* XXX */
5703 linenr_T lnum;
5704{
5705 char_u *l;
5706 pos_T fp;
5707 colnr_T col;
5708 char_u *p;
5709
5710 l = ml_get(lnum);
5711 p = after_label(l);
5712 if (p == NULL)
5713 return 0;
5714
5715 fp.col = (colnr_T)(p - l);
5716 fp.lnum = lnum;
5717 getvcol(curwin, &fp, &col, NULL, NULL);
5718 return (int)col;
5719}
5720
5721/*
5722 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005723 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 * label: if (asdf && asdfasdf)
5725 * ^
5726 */
5727 static int
5728skip_label(lnum, pp, ind_maxcomment)
5729 linenr_T lnum;
5730 char_u **pp;
5731 int ind_maxcomment;
5732{
5733 char_u *l;
5734 int amount;
5735 pos_T cursor_save;
5736
5737 cursor_save = curwin->w_cursor;
5738 curwin->w_cursor.lnum = lnum;
5739 l = ml_get_curline();
5740 /* XXX */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005741 if (cin_iscase(l, FALSE) || cin_isscopedecl(l)
5742 || cin_islabel(ind_maxcomment))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 {
5744 amount = get_indent_nolabel(lnum);
5745 l = after_label(ml_get_curline());
5746 if (l == NULL) /* just in case */
5747 l = ml_get_curline();
5748 }
5749 else
5750 {
5751 amount = get_indent();
5752 l = ml_get_curline();
5753 }
5754 *pp = l;
5755
5756 curwin->w_cursor = cursor_save;
5757 return amount;
5758}
5759
5760/*
5761 * Return the indent of the first variable name after a type in a declaration.
5762 * int a, indent of "a"
5763 * static struct foo b, indent of "b"
5764 * enum bla c, indent of "c"
5765 * Returns zero when it doesn't look like a declaration.
5766 */
5767 static int
5768cin_first_id_amount()
5769{
5770 char_u *line, *p, *s;
5771 int len;
5772 pos_T fp;
5773 colnr_T col;
5774
5775 line = ml_get_curline();
5776 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005777 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 if (len == 6 && STRNCMP(p, "static", 6) == 0)
5779 {
5780 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005781 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 }
5783 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
5784 p = skipwhite(p + 6);
5785 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
5786 p = skipwhite(p + 4);
5787 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
5788 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
5789 {
5790 s = skipwhite(p + len);
5791 if ((STRNCMP(s, "int", 3) == 0 && vim_iswhite(s[3]))
5792 || (STRNCMP(s, "long", 4) == 0 && vim_iswhite(s[4]))
5793 || (STRNCMP(s, "short", 5) == 0 && vim_iswhite(s[5]))
5794 || (STRNCMP(s, "char", 4) == 0 && vim_iswhite(s[4])))
5795 p = s;
5796 }
5797 for (len = 0; vim_isIDc(p[len]); ++len)
5798 ;
5799 if (len == 0 || !vim_iswhite(p[len]) || cin_nocode(p))
5800 return 0;
5801
5802 p = skipwhite(p + len);
5803 fp.lnum = curwin->w_cursor.lnum;
5804 fp.col = (colnr_T)(p - line);
5805 getvcol(curwin, &fp, &col, NULL, NULL);
5806 return (int)col;
5807}
5808
5809/*
5810 * Return the indent of the first non-blank after an equal sign.
5811 * char *foo = "here";
5812 * Return zero if no (useful) equal sign found.
5813 * Return -1 if the line above "lnum" ends in a backslash.
5814 * foo = "asdf\
5815 * asdf\
5816 * here";
5817 */
5818 static int
5819cin_get_equal_amount(lnum)
5820 linenr_T lnum;
5821{
5822 char_u *line;
5823 char_u *s;
5824 colnr_T col;
5825 pos_T fp;
5826
5827 if (lnum > 1)
5828 {
5829 line = ml_get(lnum - 1);
5830 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
5831 return -1;
5832 }
5833
5834 line = s = ml_get(lnum);
5835 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
5836 {
5837 if (cin_iscomment(s)) /* ignore comments */
5838 s = cin_skipcomment(s);
5839 else
5840 ++s;
5841 }
5842 if (*s != '=')
5843 return 0;
5844
5845 s = skipwhite(s + 1);
5846 if (cin_nocode(s))
5847 return 0;
5848
5849 if (*s == '"') /* nice alignment for continued strings */
5850 ++s;
5851
5852 fp.lnum = lnum;
5853 fp.col = (colnr_T)(s - line);
5854 getvcol(curwin, &fp, &col, NULL, NULL);
5855 return (int)col;
5856}
5857
5858/*
5859 * Recognize a preprocessor statement: Any line that starts with '#'.
5860 */
5861 static int
5862cin_ispreproc(s)
5863 char_u *s;
5864{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02005865 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 return TRUE;
5867 return FALSE;
5868}
5869
5870/*
5871 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
5872 * continuation line of a preprocessor statement. Decrease "*lnump" to the
5873 * start and return the line in "*pp".
5874 */
5875 static int
5876cin_ispreproc_cont(pp, lnump)
5877 char_u **pp;
5878 linenr_T *lnump;
5879{
5880 char_u *line = *pp;
5881 linenr_T lnum = *lnump;
5882 int retval = FALSE;
5883
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00005884 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 {
5886 if (cin_ispreproc(line))
5887 {
5888 retval = TRUE;
5889 *lnump = lnum;
5890 break;
5891 }
5892 if (lnum == 1)
5893 break;
5894 line = ml_get(--lnum);
5895 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
5896 break;
5897 }
5898
5899 if (lnum != *lnump)
5900 *pp = ml_get(*lnump);
5901 return retval;
5902}
5903
5904/*
5905 * Recognize the start of a C or C++ comment.
5906 */
5907 static int
5908cin_iscomment(p)
5909 char_u *p;
5910{
5911 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
5912}
5913
5914/*
5915 * Recognize the start of a "//" comment.
5916 */
5917 static int
5918cin_islinecomment(p)
5919 char_u *p;
5920{
5921 return (p[0] == '/' && p[1] == '/');
5922}
5923
5924/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005925 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
5926 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02005928 * If a line begins with an "else", only consider it terminated if no unmatched
5929 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 * Return the character terminating the line (ending char's have precedence if
5931 * both apply in order to determine initializations).
5932 */
5933 static int
5934cin_isterminated(s, incl_open, incl_comma)
5935 char_u *s;
5936 int incl_open; /* include '{' at the end as terminator */
5937 int incl_comma; /* recognize a trailing comma */
5938{
Bram Moolenaar496f9512011-05-19 16:35:09 +02005939 char_u found_start = 0;
5940 unsigned n_open = 0;
5941 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942
5943 s = cin_skipcomment(s);
5944
5945 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
5946 found_start = *s;
5947
Bram Moolenaar496f9512011-05-19 16:35:09 +02005948 if (!found_start)
5949 is_else = cin_iselse(s);
5950
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 while (*s)
5952 {
5953 /* skip over comments, "" strings and 'c'haracters */
5954 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005955 if (*s == '}' && n_open > 0)
5956 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02005957 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005958 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 && cin_nocode(s + 1))
5960 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005961 else if (*s == '{')
5962 {
5963 if (incl_open && cin_nocode(s + 1))
5964 return *s;
5965 else
5966 ++n_open;
5967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968
5969 if (*s)
5970 s++;
5971 }
5972 return found_start;
5973}
5974
5975/*
5976 * Recognize the basic picture of a function declaration -- it needs to
5977 * have an open paren somewhere and a close paren at the end of the line and
5978 * no semicolons anywhere.
5979 * When a line ends in a comma we continue looking in the next line.
5980 * "sp" points to a string with the line. When looking at other lines it must
5981 * be restored to the line. When it's NULL fetch lines here.
5982 * "lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005983 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 */
5985 static int
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005986cin_isfuncdecl(sp, first_lnum, min_lnum, ind_maxparen, ind_maxcomment)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 char_u **sp;
5988 linenr_T first_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005989 linenr_T min_lnum;
5990 int ind_maxparen;
5991 int ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992{
5993 char_u *s;
5994 linenr_T lnum = first_lnum;
5995 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005996 pos_T *trypos;
5997 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998
5999 if (sp == NULL)
6000 s = ml_get(lnum);
6001 else
6002 s = *sp;
6003
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006004 if (find_last_paren(s, '(', ')')
6005 && (trypos = find_match_paren(ind_maxparen, ind_maxcomment)) != NULL)
6006 {
6007 lnum = trypos->lnum;
6008 if (lnum < min_lnum)
6009 return FALSE;
6010
6011 s = ml_get(lnum);
6012 }
6013
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006014 /* Ignore line starting with #. */
6015 if (cin_ispreproc(s))
6016 return FALSE;
6017
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6019 {
6020 if (cin_iscomment(s)) /* ignore comments */
6021 s = cin_skipcomment(s);
6022 else
6023 ++s;
6024 }
6025 if (*s != '(')
6026 return FALSE; /* ';', ' or " before any () or no '(' */
6027
6028 while (*s && *s != ';' && *s != '\'' && *s != '"')
6029 {
6030 if (*s == ')' && cin_nocode(s + 1))
6031 {
6032 /* ')' at the end: may have found a match
6033 * Check for he previous line not to end in a backslash:
6034 * #if defined(x) && \
6035 * defined(y)
6036 */
6037 lnum = first_lnum - 1;
6038 s = ml_get(lnum);
6039 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6040 retval = TRUE;
6041 goto done;
6042 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006043 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006045 int comma = (*s == ',');
6046
6047 /* ',' at the end: continue looking in the next line.
6048 * At the end: check for ',' in the next line, for this style:
6049 * func(arg1
6050 * , arg2) */
6051 for (;;)
6052 {
6053 if (lnum >= curbuf->b_ml.ml_line_count)
6054 break;
6055 s = ml_get(++lnum);
6056 if (!cin_ispreproc(s))
6057 break;
6058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 if (lnum >= curbuf->b_ml.ml_line_count)
6060 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006061 /* Require a comma at end of the line or a comma or ')' at the
6062 * start of next line. */
6063 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006064 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006065 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006066 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 }
6068 else if (cin_iscomment(s)) /* ignore comments */
6069 s = cin_skipcomment(s);
6070 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006071 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006073 just_started = FALSE;
6074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 }
6076
6077done:
6078 if (lnum != first_lnum && sp != NULL)
6079 *sp = ml_get(first_lnum);
6080
6081 return retval;
6082}
6083
6084 static int
6085cin_isif(p)
6086 char_u *p;
6087{
6088 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
6089}
6090
6091 static int
6092cin_iselse(p)
6093 char_u *p;
6094{
6095 if (*p == '}') /* accept "} else" */
6096 p = cin_skipcomment(p + 1);
6097 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6098}
6099
6100 static int
6101cin_isdo(p)
6102 char_u *p;
6103{
6104 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6105}
6106
6107/*
6108 * Check if this is a "while" that should have a matching "do".
6109 * We only accept a "while (condition) ;", with only white space between the
6110 * ')' and ';'. The condition may be spread over several lines.
6111 */
6112 static int
6113cin_iswhileofdo(p, lnum, ind_maxparen) /* XXX */
6114 char_u *p;
6115 linenr_T lnum;
6116 int ind_maxparen;
6117{
6118 pos_T cursor_save;
6119 pos_T *trypos;
6120 int retval = FALSE;
6121
6122 p = cin_skipcomment(p);
6123 if (*p == '}') /* accept "} while (cond);" */
6124 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006125 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 {
6127 cursor_save = curwin->w_cursor;
6128 curwin->w_cursor.lnum = lnum;
6129 curwin->w_cursor.col = 0;
6130 p = ml_get_curline();
6131 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6132 {
6133 ++p;
6134 ++curwin->w_cursor.col;
6135 }
6136 if ((trypos = findmatchlimit(NULL, 0, 0, ind_maxparen)) != NULL
6137 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6138 retval = TRUE;
6139 curwin->w_cursor = cursor_save;
6140 }
6141 return retval;
6142}
6143
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006144/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006145 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006146 * Return 0 if there is none.
6147 * Otherwise return !0 and update "*poffset" to point to the place where the
6148 * string was found.
6149 */
6150 static int
Bram Moolenaarb345d492012-04-09 20:42:26 +02006151cin_is_if_for_while_before_offset(line, poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006152 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006153 int *poffset;
6154{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006155 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006156
6157 if (offset-- < 2)
6158 return 0;
6159 while (offset > 2 && vim_iswhite(line[offset]))
6160 --offset;
6161
6162 offset -= 1;
6163 if (!STRNCMP(line + offset, "if", 2))
6164 goto probablyFound;
6165
6166 if (offset >= 1)
6167 {
6168 offset -= 1;
6169 if (!STRNCMP(line + offset, "for", 3))
6170 goto probablyFound;
6171
6172 if (offset >= 2)
6173 {
6174 offset -= 2;
6175 if (!STRNCMP(line + offset, "while", 5))
6176 goto probablyFound;
6177 }
6178 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006179 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006180
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006181probablyFound:
6182 if (!offset || !vim_isIDc(line[offset - 1]))
6183 {
6184 *poffset = offset;
6185 return 1;
6186 }
6187 return 0;
6188}
6189
6190/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006191 * Return TRUE if we are at the end of a do-while.
6192 * do
6193 * nothing;
6194 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006195 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006196 * Adjust the cursor to the line with "while".
6197 */
6198 static int
6199cin_iswhileofdo_end(terminated, ind_maxparen, ind_maxcomment)
6200 int terminated;
6201 int ind_maxparen;
6202 int ind_maxcomment;
6203{
6204 char_u *line;
6205 char_u *p;
6206 char_u *s;
6207 pos_T *trypos;
6208 int i;
6209
6210 if (terminated != ';') /* there must be a ';' at the end */
6211 return FALSE;
6212
6213 p = line = ml_get_curline();
6214 while (*p != NUL)
6215 {
6216 p = cin_skipcomment(p);
6217 if (*p == ')')
6218 {
6219 s = skipwhite(p + 1);
6220 if (*s == ';' && cin_nocode(s + 1))
6221 {
6222 /* Found ");" at end of the line, now check there is "while"
6223 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006224 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006225 curwin->w_cursor.col = i;
6226 trypos = find_match_paren(ind_maxparen, ind_maxcomment);
6227 if (trypos != NULL)
6228 {
6229 s = cin_skipcomment(ml_get(trypos->lnum));
6230 if (*s == '}') /* accept "} while (cond);" */
6231 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006232 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006233 {
6234 curwin->w_cursor.lnum = trypos->lnum;
6235 return TRUE;
6236 }
6237 }
6238
6239 /* Searching may have made "line" invalid, get it again. */
6240 line = ml_get_curline();
6241 p = line + i;
6242 }
6243 }
6244 if (*p != NUL)
6245 ++p;
6246 }
6247 return FALSE;
6248}
6249
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 static int
6251cin_isbreak(p)
6252 char_u *p;
6253{
6254 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6255}
6256
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006257/*
6258 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 * constructor-initialization. eg:
6260 *
6261 * class MyClass :
6262 * baseClass <-- here
6263 * class MyClass : public baseClass,
6264 * anotherBaseClass <-- here (should probably lineup ??)
6265 * MyClass::MyClass(...) :
6266 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006267 *
6268 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 */
6270 static int
Bram Moolenaare7c56862007-08-04 10:14:52 +00006271cin_is_cpp_baseclass(col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006272 colnr_T *col; /* return: column to align with */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273{
6274 char_u *s;
6275 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006276 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006277 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278
6279 *col = 0;
6280
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006281 s = skipwhite(line);
6282 if (*s == '#') /* skip #define FOO x ? (x) : x */
6283 return FALSE;
6284 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 if (*s == NUL)
6286 return FALSE;
6287
6288 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6289
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006290 /* Search for a line starting with '#', empty, ending in ';' or containing
6291 * '{' or '}' and start below it. This handles the following situations:
6292 * a = cond ?
6293 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006294 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006295 * func::foo()
6296 * : something
6297 * {}
6298 * Foo::Foo (int one, int two)
6299 * : something(4),
6300 * somethingelse(3)
6301 * {}
6302 */
6303 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006305 line = ml_get(lnum - 1);
6306 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006307 if (*s == '#' || *s == NUL)
6308 break;
6309 while (*s != NUL)
6310 {
6311 s = cin_skipcomment(s);
6312 if (*s == '{' || *s == '}'
6313 || (*s == ';' && cin_nocode(s + 1)))
6314 break;
6315 if (*s != NUL)
6316 ++s;
6317 }
6318 if (*s != NUL)
6319 break;
6320 --lnum;
6321 }
6322
Bram Moolenaare7c56862007-08-04 10:14:52 +00006323 line = ml_get(lnum);
6324 s = cin_skipcomment(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006325 for (;;)
6326 {
6327 if (*s == NUL)
6328 {
6329 if (lnum == curwin->w_cursor.lnum)
6330 break;
6331 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006332 line = ml_get(++lnum);
6333 s = cin_skipcomment(line);
6334 if (*s == NUL)
6335 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006336 }
6337
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006338 if (s[0] == '"')
6339 s = skip_string(s) + 1;
6340 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 {
6342 if (s[1] == ':')
6343 {
6344 /* skip double colon. It can't be a constructor
6345 * initialization any more */
6346 lookfor_ctor_init = FALSE;
6347 s = cin_skipcomment(s + 2);
6348 }
6349 else if (lookfor_ctor_init || class_or_struct)
6350 {
6351 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006352 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 cpp_base_class = TRUE;
6354 lookfor_ctor_init = class_or_struct = FALSE;
6355 *col = 0;
6356 s = cin_skipcomment(s + 1);
6357 }
6358 else
6359 s = cin_skipcomment(s + 1);
6360 }
6361 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6362 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6363 {
6364 class_or_struct = TRUE;
6365 lookfor_ctor_init = FALSE;
6366
6367 if (*s == 'c')
6368 s = cin_skipcomment(s + 5);
6369 else
6370 s = cin_skipcomment(s + 6);
6371 }
6372 else
6373 {
6374 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6375 {
6376 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6377 }
6378 else if (s[0] == ')')
6379 {
6380 /* Constructor-initialization is assumed if we come across
6381 * something like "):" */
6382 class_or_struct = FALSE;
6383 lookfor_ctor_init = TRUE;
6384 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006385 else if (s[0] == '?')
6386 {
6387 /* Avoid seeing '() :' after '?' as constructor init. */
6388 return FALSE;
6389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 else if (!vim_isIDc(s[0]))
6391 {
6392 /* if it is not an identifier, we are wrong */
6393 class_or_struct = FALSE;
6394 lookfor_ctor_init = FALSE;
6395 }
6396 else if (*col == 0)
6397 {
6398 /* it can't be a constructor-initialization any more */
6399 lookfor_ctor_init = FALSE;
6400
6401 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006402 if (cpp_base_class)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 *col = (colnr_T)(s - line);
6404 }
6405
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006406 /* When the line ends in a comma don't align with it. */
6407 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
6408 *col = 0;
6409
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 s = cin_skipcomment(s + 1);
6411 }
6412 }
6413
6414 return cpp_base_class;
6415}
6416
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006417 static int
6418get_baseclass_amount(col, ind_maxparen, ind_maxcomment, ind_cpp_baseclass)
6419 int col;
6420 int ind_maxparen;
6421 int ind_maxcomment;
6422 int ind_cpp_baseclass;
6423{
6424 int amount;
6425 colnr_T vcol;
6426 pos_T *trypos;
6427
6428 if (col == 0)
6429 {
6430 amount = get_indent();
6431 if (find_last_paren(ml_get_curline(), '(', ')')
6432 && (trypos = find_match_paren(ind_maxparen,
6433 ind_maxcomment)) != NULL)
6434 amount = get_indent_lnum(trypos->lnum); /* XXX */
6435 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
6436 amount += ind_cpp_baseclass;
6437 }
6438 else
6439 {
6440 curwin->w_cursor.col = col;
6441 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6442 amount = (int)vcol;
6443 }
6444 if (amount < ind_cpp_baseclass)
6445 amount = ind_cpp_baseclass;
6446 return amount;
6447}
6448
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449/*
6450 * Return TRUE if string "s" ends with the string "find", possibly followed by
6451 * white space and comments. Skip strings and comments.
6452 * Ignore "ignore" after "find" if it's not NULL.
6453 */
6454 static int
6455cin_ends_in(s, find, ignore)
6456 char_u *s;
6457 char_u *find;
6458 char_u *ignore;
6459{
6460 char_u *p = s;
6461 char_u *r;
6462 int len = (int)STRLEN(find);
6463
6464 while (*p != NUL)
6465 {
6466 p = cin_skipcomment(p);
6467 if (STRNCMP(p, find, len) == 0)
6468 {
6469 r = skipwhite(p + len);
6470 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6471 r = skipwhite(r + STRLEN(ignore));
6472 if (cin_nocode(r))
6473 return TRUE;
6474 }
6475 if (*p != NUL)
6476 ++p;
6477 }
6478 return FALSE;
6479}
6480
6481/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006482 * Return TRUE when "s" starts with "word" and then a non-ID character.
6483 */
6484 static int
6485cin_starts_with(s, word)
6486 char_u *s;
6487 char *word;
6488{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006489 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006490
6491 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6492}
6493
6494/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 * Skip strings, chars and comments until at or past "trypos".
6496 * Return the column found.
6497 */
6498 static int
6499cin_skip2pos(trypos)
6500 pos_T *trypos;
6501{
6502 char_u *line;
6503 char_u *p;
6504
6505 p = line = ml_get(trypos->lnum);
6506 while (*p && (colnr_T)(p - line) < trypos->col)
6507 {
6508 if (cin_iscomment(p))
6509 p = cin_skipcomment(p);
6510 else
6511 {
6512 p = skip_string(p);
6513 ++p;
6514 }
6515 }
6516 return (int)(p - line);
6517}
6518
6519/*
6520 * Find the '{' at the start of the block we are in.
6521 * Return NULL if no match found.
6522 * Ignore a '{' that is in a comment, makes indenting the next three lines
6523 * work. */
6524/* foo() */
6525/* { */
6526/* } */
6527
6528 static pos_T *
6529find_start_brace(ind_maxcomment) /* XXX */
6530 int ind_maxcomment;
6531{
6532 pos_T cursor_save;
6533 pos_T *trypos;
6534 pos_T *pos;
6535 static pos_T pos_copy;
6536
6537 cursor_save = curwin->w_cursor;
6538 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6539 {
6540 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6541 trypos = &pos_copy;
6542 curwin->w_cursor = *trypos;
6543 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006544 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
6546 && (pos = find_start_comment(ind_maxcomment)) == NULL) /* XXX */
6547 break;
6548 if (pos != NULL)
6549 curwin->w_cursor.lnum = pos->lnum;
6550 }
6551 curwin->w_cursor = cursor_save;
6552 return trypos;
6553}
6554
6555/*
6556 * Find the matching '(', failing if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006557 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 */
6559 static pos_T *
6560find_match_paren(ind_maxparen, ind_maxcomment) /* XXX */
6561 int ind_maxparen;
6562 int ind_maxcomment;
6563{
6564 pos_T cursor_save;
6565 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006566 static pos_T pos_copy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567
6568 cursor_save = curwin->w_cursor;
6569 if ((trypos = findmatchlimit(NULL, '(', 0, ind_maxparen)) != NULL)
6570 {
6571 /* check if the ( is in a // comment */
6572 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
6573 trypos = NULL;
6574 else
6575 {
6576 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6577 trypos = &pos_copy;
6578 curwin->w_cursor = *trypos;
6579 if (find_start_comment(ind_maxcomment) != NULL) /* XXX */
6580 trypos = NULL;
6581 }
6582 }
6583 curwin->w_cursor = cursor_save;
6584 return trypos;
6585}
6586
6587/*
6588 * Return ind_maxparen corrected for the difference in line number between the
6589 * cursor position and "startpos". This makes sure that searching for a
6590 * matching paren above the cursor line doesn't find a match because of
6591 * looking a few lines further.
6592 */
6593 static int
6594corr_ind_maxparen(ind_maxparen, startpos)
6595 int ind_maxparen;
6596 pos_T *startpos;
6597{
6598 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6599
6600 if (n > 0 && n < ind_maxparen / 2)
6601 return ind_maxparen - (int)n;
6602 return ind_maxparen;
6603}
6604
6605/*
6606 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006607 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 */
6609 static int
6610find_last_paren(l, start, end)
6611 char_u *l;
6612 int start, end;
6613{
6614 int i;
6615 int retval = FALSE;
6616 int open_count = 0;
6617
6618 curwin->w_cursor.col = 0; /* default is start of line */
6619
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006620 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 {
6622 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6623 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6624 if (l[i] == start)
6625 ++open_count;
6626 else if (l[i] == end)
6627 {
6628 if (open_count > 0)
6629 --open_count;
6630 else
6631 {
6632 curwin->w_cursor.col = i;
6633 retval = TRUE;
6634 }
6635 }
6636 }
6637 return retval;
6638}
6639
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006640/*
6641 * Parse 'cinoptions' and set the values in "curbuf".
6642 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
6643 */
6644 void
6645parse_cino(buf)
6646 buf_T *buf;
6647{
6648 char_u *p;
6649 char_u *l;
6650 char_u *digits;
6651 int n;
6652 int divider;
6653 int fraction = 0;
6654 int sw = (int)get_sw_value(buf);
6655
6656 /*
6657 * Set the default values.
6658 */
6659 /* Spaces from a block's opening brace the prevailing indent for that
6660 * block should be. */
6661 buf->b_ind_level = sw;
6662
6663 /* Spaces from the edge of the line an open brace that's at the end of a
6664 * line is imagined to be. */
6665 buf->b_ind_open_imag = 0;
6666
6667 /* Spaces from the prevailing indent for a line that is not preceded by
6668 * an opening brace. */
6669 buf->b_ind_no_brace = 0;
6670
6671 /* Column where the first { of a function should be located }. */
6672 buf->b_ind_first_open = 0;
6673
6674 /* Spaces from the prevailing indent a leftmost open brace should be
6675 * located. */
6676 buf->b_ind_open_extra = 0;
6677
6678 /* Spaces from the matching open brace (real location for one at the left
6679 * edge; imaginary location from one that ends a line) the matching close
6680 * brace should be located. */
6681 buf->b_ind_close_extra = 0;
6682
6683 /* Spaces from the edge of the line an open brace sitting in the leftmost
6684 * column is imagined to be. */
6685 buf->b_ind_open_left_imag = 0;
6686
6687 /* Spaces jump labels should be shifted to the left if N is non-negative,
6688 * otherwise the jump label will be put to column 1. */
6689 buf->b_ind_jump_label = -1;
6690
6691 /* Spaces from the switch() indent a "case xx" label should be located. */
6692 buf->b_ind_case = sw;
6693
6694 /* Spaces from the "case xx:" code after a switch() should be located. */
6695 buf->b_ind_case_code = sw;
6696
6697 /* Lineup break at end of case in switch() with case label. */
6698 buf->b_ind_case_break = 0;
6699
6700 /* Spaces from the class declaration indent a scope declaration label
6701 * should be located. */
6702 buf->b_ind_scopedecl = sw;
6703
6704 /* Spaces from the scope declaration label code should be located. */
6705 buf->b_ind_scopedecl_code = sw;
6706
6707 /* Amount K&R-style parameters should be indented. */
6708 buf->b_ind_param = sw;
6709
6710 /* Amount a function type spec should be indented. */
6711 buf->b_ind_func_type = sw;
6712
6713 /* Amount a cpp base class declaration or constructor initialization
6714 * should be indented. */
6715 buf->b_ind_cpp_baseclass = sw;
6716
6717 /* additional spaces beyond the prevailing indent a continuation line
6718 * should be located. */
6719 buf->b_ind_continuation = sw;
6720
6721 /* Spaces from the indent of the line with an unclosed parentheses. */
6722 buf->b_ind_unclosed = sw * 2;
6723
6724 /* Spaces from the indent of the line with an unclosed parentheses, which
6725 * itself is also unclosed. */
6726 buf->b_ind_unclosed2 = sw;
6727
6728 /* Suppress ignoring spaces from the indent of a line starting with an
6729 * unclosed parentheses. */
6730 buf->b_ind_unclosed_noignore = 0;
6731
6732 /* If the opening paren is the last nonwhite character on the line, and
6733 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
6734 * context (for very long lines). */
6735 buf->b_ind_unclosed_wrapped = 0;
6736
6737 /* Suppress ignoring white space when lining up with the character after
6738 * an unclosed parentheses. */
6739 buf->b_ind_unclosed_whiteok = 0;
6740
6741 /* Indent a closing parentheses under the line start of the matching
6742 * opening parentheses. */
6743 buf->b_ind_matching_paren = 0;
6744
6745 /* Indent a closing parentheses under the previous line. */
6746 buf->b_ind_paren_prev = 0;
6747
6748 /* Extra indent for comments. */
6749 buf->b_ind_comment = 0;
6750
6751 /* Spaces from the comment opener when there is nothing after it. */
6752 buf->b_ind_in_comment = 3;
6753
6754 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
6755 * after the comment opener. */
6756 buf->b_ind_in_comment2 = 0;
6757
6758 /* Max lines to search for an open paren. */
6759 buf->b_ind_maxparen = 20;
6760
6761 /* Max lines to search for an open comment. */
6762 buf->b_ind_maxcomment = 70;
6763
6764 /* Handle braces for java code. */
6765 buf->b_ind_java = 0;
6766
6767 /* Not to confuse JS object properties with labels. */
6768 buf->b_ind_js = 0;
6769
6770 /* Handle blocked cases correctly. */
6771 buf->b_ind_keep_case_label = 0;
6772
6773 /* Handle C++ namespace. */
6774 buf->b_ind_cpp_namespace = 0;
6775
6776 /* Handle continuation lines containing conditions of if(), for() and
6777 * while(). */
6778 buf->b_ind_if_for_while = 0;
6779
6780 for (p = buf->b_p_cino; *p; )
6781 {
6782 l = p++;
6783 if (*p == '-')
6784 ++p;
6785 digits = p; /* remember where the digits start */
6786 n = getdigits(&p);
6787 divider = 0;
6788 if (*p == '.') /* ".5s" means a fraction */
6789 {
6790 fraction = atol((char *)++p);
6791 while (VIM_ISDIGIT(*p))
6792 {
6793 ++p;
6794 if (divider)
6795 divider *= 10;
6796 else
6797 divider = 10;
6798 }
6799 }
6800 if (*p == 's') /* "2s" means two times 'shiftwidth' */
6801 {
6802 if (p == digits)
6803 n = sw; /* just "s" is one 'shiftwidth' */
6804 else
6805 {
6806 n *= sw;
6807 if (divider)
6808 n += (sw * fraction + divider / 2) / divider;
6809 }
6810 ++p;
6811 }
6812 if (l[1] == '-')
6813 n = -n;
6814
6815 /* When adding an entry here, also update the default 'cinoptions' in
6816 * doc/indent.txt, and add explanation for it! */
6817 switch (*l)
6818 {
6819 case '>': buf->b_ind_level = n; break;
6820 case 'e': buf->b_ind_open_imag = n; break;
6821 case 'n': buf->b_ind_no_brace = n; break;
6822 case 'f': buf->b_ind_first_open = n; break;
6823 case '{': buf->b_ind_open_extra = n; break;
6824 case '}': buf->b_ind_close_extra = n; break;
6825 case '^': buf->b_ind_open_left_imag = n; break;
6826 case 'L': buf->b_ind_jump_label = n; break;
6827 case ':': buf->b_ind_case = n; break;
6828 case '=': buf->b_ind_case_code = n; break;
6829 case 'b': buf->b_ind_case_break = n; break;
6830 case 'p': buf->b_ind_param = n; break;
6831 case 't': buf->b_ind_func_type = n; break;
6832 case '/': buf->b_ind_comment = n; break;
6833 case 'c': buf->b_ind_in_comment = n; break;
6834 case 'C': buf->b_ind_in_comment2 = n; break;
6835 case 'i': buf->b_ind_cpp_baseclass = n; break;
6836 case '+': buf->b_ind_continuation = n; break;
6837 case '(': buf->b_ind_unclosed = n; break;
6838 case 'u': buf->b_ind_unclosed2 = n; break;
6839 case 'U': buf->b_ind_unclosed_noignore = n; break;
6840 case 'W': buf->b_ind_unclosed_wrapped = n; break;
6841 case 'w': buf->b_ind_unclosed_whiteok = n; break;
6842 case 'm': buf->b_ind_matching_paren = n; break;
6843 case 'M': buf->b_ind_paren_prev = n; break;
6844 case ')': buf->b_ind_maxparen = n; break;
6845 case '*': buf->b_ind_maxcomment = n; break;
6846 case 'g': buf->b_ind_scopedecl = n; break;
6847 case 'h': buf->b_ind_scopedecl_code = n; break;
6848 case 'j': buf->b_ind_java = n; break;
6849 case 'J': buf->b_ind_js = n; break;
6850 case 'l': buf->b_ind_keep_case_label = n; break;
6851 case '#': buf->b_ind_hash_comment = n; break;
6852 case 'N': buf->b_ind_cpp_namespace = n; break;
6853 case 'k': buf->b_ind_if_for_while = n; break;
6854 }
6855 if (*p == ',')
6856 ++p;
6857 }
6858}
6859
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 int
6861get_c_indent()
6862{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 pos_T cur_curpos;
6864 int amount;
6865 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00006866 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 colnr_T col;
6868 char_u *theline;
6869 char_u *linecopy;
6870 pos_T *trypos;
6871 pos_T *tryposBrace = NULL;
6872 pos_T our_paren_pos;
6873 char_u *start;
6874 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00006875#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876#define BRACE_AT_START 2 /* '{' is at start of line */
6877#define BRACE_AT_END 3 /* '{' is at end of line */
6878 linenr_T ourscope;
6879 char_u *l;
6880 char_u *look;
6881 char_u terminated;
6882 int lookfor;
6883#define LOOKFOR_INITIAL 0
6884#define LOOKFOR_IF 1
6885#define LOOKFOR_DO 2
6886#define LOOKFOR_CASE 3
6887#define LOOKFOR_ANY 4
6888#define LOOKFOR_TERM 5
6889#define LOOKFOR_UNTERM 6
6890#define LOOKFOR_SCOPEDECL 7
6891#define LOOKFOR_NOBREAK 8
6892#define LOOKFOR_CPP_BASECLASS 9
6893#define LOOKFOR_ENUM_OR_INIT 10
6894
6895 int whilelevel;
6896 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 int n;
6898 int iscase;
6899 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006900 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006902 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02006903 int added_to_amount = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006905 /* make a copy, value is changed below */
6906 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907
6908 /* remember where the cursor was when we started */
6909 cur_curpos = curwin->w_cursor;
6910
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006911 /* if we are at line 1 0 is fine, right? */
6912 if (cur_curpos.lnum == 1)
6913 return 0;
6914
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 /* Get a copy of the current contents of the line.
6916 * This is required, because only the most recent line obtained with
6917 * ml_get is valid! */
6918 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
6919 if (linecopy == NULL)
6920 return 0;
6921
6922 /*
6923 * In insert mode and the cursor is on a ')' truncate the line at the
6924 * cursor position. We don't want to line up with the matching '(' when
6925 * inserting new stuff.
6926 * For unknown reasons the cursor might be past the end of the line, thus
6927 * check for that.
6928 */
6929 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006930 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 && linecopy[curwin->w_cursor.col] == ')')
6932 linecopy[curwin->w_cursor.col] = NUL;
6933
6934 theline = skipwhite(linecopy);
6935
6936 /* move the cursor to the start of the line */
6937
6938 curwin->w_cursor.col = 0;
6939
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006940 original_line_islabel = cin_islabel(curbuf->b_ind_maxcomment); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006941
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 /*
6943 * #defines and so on always go at the left when included in 'cinkeys'.
6944 */
6945 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006946 amount = curbuf->b_ind_hash_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947
6948 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006949 * Is it a non-case label? Then that goes at the left margin too unless:
6950 * - JS flag is set.
6951 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006953 else if (original_line_islabel && !curbuf->b_ind_js
6954 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 {
6956 amount = 0;
6957 }
6958
6959 /*
6960 * If we're inside a "//" comment and there is a "//" comment in a
6961 * previous line, lineup with that one.
6962 */
6963 else if (cin_islinecomment(theline)
6964 && (trypos = find_line_comment()) != NULL) /* XXX */
6965 {
6966 /* find how indented the line beginning the comment is */
6967 getvcol(curwin, trypos, &col, NULL, NULL);
6968 amount = col;
6969 }
6970
6971 /*
6972 * If we're inside a comment and not looking at the start of the
6973 * comment, try using the 'comments' option.
6974 */
6975 else if (!cin_iscomment(theline)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006976 && (trypos = find_start_comment(curbuf->b_ind_maxcomment)) != NULL)
6977 /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 {
6979 int lead_start_len = 2;
6980 int lead_middle_len = 1;
6981 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
6982 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
6983 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6984 char_u *p;
6985 int start_align = 0;
6986 int start_off = 0;
6987 int done = FALSE;
6988
6989 /* find how indented the line beginning the comment is */
6990 getvcol(curwin, trypos, &col, NULL, NULL);
6991 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02006992 *lead_start = NUL;
6993 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994
6995 p = curbuf->b_p_com;
6996 while (*p != NUL)
6997 {
6998 int align = 0;
6999 int off = 0;
7000 int what = 0;
7001
7002 while (*p != NUL && *p != ':')
7003 {
7004 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7005 what = *p++;
7006 else if (*p == COM_LEFT || *p == COM_RIGHT)
7007 align = *p++;
7008 else if (VIM_ISDIGIT(*p) || *p == '-')
7009 off = getdigits(&p);
7010 else
7011 ++p;
7012 }
7013
7014 if (*p == ':')
7015 ++p;
7016 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7017 if (what == COM_START)
7018 {
7019 STRCPY(lead_start, lead_end);
7020 lead_start_len = (int)STRLEN(lead_start);
7021 start_off = off;
7022 start_align = align;
7023 }
7024 else if (what == COM_MIDDLE)
7025 {
7026 STRCPY(lead_middle, lead_end);
7027 lead_middle_len = (int)STRLEN(lead_middle);
7028 }
7029 else if (what == COM_END)
7030 {
7031 /* If our line starts with the middle comment string, line it
7032 * up with the comment opener per the 'comments' option. */
7033 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7034 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7035 {
7036 done = TRUE;
7037 if (curwin->w_cursor.lnum > 1)
7038 {
7039 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007040 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 * the middle comment string matches in the previous
7042 * line, use the indent of that line. XXX */
7043 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7044 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7045 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7046 else if (STRNCMP(look, lead_middle,
7047 lead_middle_len) == 0)
7048 {
7049 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7050 break;
7051 }
7052 /* If the start comment string doesn't match with the
7053 * start of the comment, skip this entry. XXX */
7054 else if (STRNCMP(ml_get(trypos->lnum) + trypos->col,
7055 lead_start, lead_start_len) != 0)
7056 continue;
7057 }
7058 if (start_off != 0)
7059 amount += start_off;
7060 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007061 amount += vim_strsize(lead_start)
7062 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 break;
7064 }
7065
7066 /* If our line starts with the end comment string, line it up
7067 * with the middle comment */
7068 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7069 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7070 {
7071 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7072 /* XXX */
7073 if (off != 0)
7074 amount += off;
7075 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007076 amount += vim_strsize(lead_start)
7077 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 done = TRUE;
7079 break;
7080 }
7081 }
7082 }
7083
7084 /* If our line starts with an asterisk, line up with the
7085 * asterisk in the comment opener; otherwise, line up
7086 * with the first character of the comment text.
7087 */
7088 if (done)
7089 ;
7090 else if (theline[0] == '*')
7091 amount += 1;
7092 else
7093 {
7094 /*
7095 * If we are more than one line away from the comment opener, take
7096 * the indent of the previous non-empty line. If 'cino' has "CO"
7097 * and we are just below the comment opener and there are any
7098 * white characters after it line up with the text after it;
7099 * otherwise, add the amount specified by "c" in 'cino'
7100 */
7101 amount = -1;
7102 for (lnum = cur_curpos.lnum - 1; lnum > trypos->lnum; --lnum)
7103 {
7104 if (linewhite(lnum)) /* skip blank lines */
7105 continue;
7106 amount = get_indent_lnum(lnum); /* XXX */
7107 break;
7108 }
7109 if (amount == -1) /* use the comment opener */
7110 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007111 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 {
7113 start = ml_get(trypos->lnum);
7114 look = start + trypos->col + 2; /* skip / and * */
7115 if (*look != NUL) /* if something after it */
7116 trypos->col = (colnr_T)(skipwhite(look) - start);
7117 }
7118 getvcol(curwin, trypos, &col, NULL, NULL);
7119 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007120 if (curbuf->b_ind_in_comment2 || *look == NUL)
7121 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 }
7123 }
7124 }
7125
7126 /*
7127 * Are we inside parentheses or braces?
7128 */ /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007129 else if (((trypos = find_match_paren(curbuf->b_ind_maxparen,
7130 curbuf->b_ind_maxcomment)) != NULL
7131 && curbuf->b_ind_java == 0)
7132 || (tryposBrace =
7133 find_start_brace(curbuf->b_ind_maxcomment)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 || trypos != NULL)
7135 {
7136 if (trypos != NULL && tryposBrace != NULL)
7137 {
7138 /* Both an unmatched '(' and '{' is found. Use the one which is
7139 * closer to the current cursor position, set the other to NULL. */
7140 if (trypos->lnum != tryposBrace->lnum
7141 ? trypos->lnum < tryposBrace->lnum
7142 : trypos->col < tryposBrace->col)
7143 trypos = NULL;
7144 else
7145 tryposBrace = NULL;
7146 }
7147
7148 if (trypos != NULL)
7149 {
7150 /*
7151 * If the matching paren is more than one line away, use the indent of
7152 * a previous non-empty line that matches the same paren.
7153 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007154 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007156 /* Line up with the start of the matching paren line. */
7157 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7158 }
7159 else
7160 {
7161 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007162 our_paren_pos = *trypos;
7163 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007165 l = skipwhite(ml_get(lnum));
7166 if (cin_nocode(l)) /* skip comment lines */
7167 continue;
7168 if (cin_ispreproc_cont(&l, &lnum))
7169 continue; /* ignore #define, #if, etc. */
7170 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007172 /* Skip a comment. XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007173 if ((trypos = find_start_comment(curbuf->b_ind_maxcomment))
7174 != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007175 {
7176 lnum = trypos->lnum + 1;
7177 continue;
7178 }
7179
7180 /* XXX */
7181 if ((trypos = find_match_paren(
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007182 corr_ind_maxparen(curbuf->b_ind_maxparen, &cur_curpos),
7183 curbuf->b_ind_maxcomment)) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007184 && trypos->lnum == our_paren_pos.lnum
7185 && trypos->col == our_paren_pos.col)
7186 {
7187 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007189 if (theline[0] == ')')
7190 {
7191 if (our_paren_pos.lnum != lnum
7192 && cur_amount > amount)
7193 cur_amount = amount;
7194 amount = -1;
7195 }
7196 break;
7197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 }
7199 }
7200
7201 /*
7202 * Line up with line where the matching paren is. XXX
7203 * If the line starts with a '(' or the indent for unclosed
7204 * parentheses is zero, line up with the unclosed parentheses.
7205 */
7206 if (amount == -1)
7207 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007208 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007209 int is_if_for_while = 0;
7210
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007211 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007212 {
7213 /* Look for the outermost opening parenthesis on this line
7214 * and check whether it belongs to an "if", "for" or "while". */
7215
7216 pos_T cursor_save = curwin->w_cursor;
7217 pos_T outermost;
7218 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007219
7220 trypos = &our_paren_pos;
7221 do {
7222 outermost = *trypos;
7223 curwin->w_cursor.lnum = outermost.lnum;
7224 curwin->w_cursor.col = outermost.col;
7225
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007226 trypos = find_match_paren(curbuf->b_ind_maxparen,
7227 curbuf->b_ind_maxcomment);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007228 } while (trypos && trypos->lnum == outermost.lnum);
7229
7230 curwin->w_cursor = cursor_save;
7231
7232 line = ml_get(outermost.lnum);
7233
7234 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007235 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007236 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007237
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007238 amount = skip_label(our_paren_pos.lnum, &look,
7239 curbuf->b_ind_maxcomment);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007240 look = skipwhite(look);
7241 if (*look == '(')
7242 {
7243 linenr_T save_lnum = curwin->w_cursor.lnum;
7244 char_u *line;
7245 int look_col;
7246
7247 /* Ignore a '(' in front of the line that has a match before
7248 * our matching '('. */
7249 curwin->w_cursor.lnum = our_paren_pos.lnum;
7250 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007251 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007252 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007253 if ((trypos = findmatchlimit(NULL, ')', 0,
7254 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007255 != NULL
7256 && trypos->lnum == our_paren_pos.lnum
7257 && trypos->col < our_paren_pos.col)
7258 ignore_paren_col = trypos->col + 1;
7259
7260 curwin->w_cursor.lnum = save_lnum;
7261 look = ml_get(our_paren_pos.lnum) + look_col;
7262 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007263 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7264 && is_if_for_while == 0)
7265 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007266 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 {
7268 /*
7269 * If we're looking at a close paren, line up right there;
7270 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007271 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 * the last nonwhite character of the line, use either the
7273 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007274 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 * lines).
7276 */
7277 if (theline[0] != ')')
7278 {
7279 cur_amount = MAXCOL;
7280 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007281 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 && cin_ends_in(l, (char_u *)"(", NULL))
7283 {
7284 /* look for opening unmatched paren, indent one level
7285 * for each additional level */
7286 n = 1;
7287 for (col = 0; col < our_paren_pos.col; ++col)
7288 {
7289 switch (l[col])
7290 {
7291 case '(':
7292 case '{': ++n;
7293 break;
7294
7295 case ')':
7296 case '}': if (n > 1)
7297 --n;
7298 break;
7299 }
7300 }
7301
7302 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007303 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007305 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 our_paren_pos.col++;
7307 else
7308 {
7309 col = our_paren_pos.col + 1;
7310 while (vim_iswhite(l[col]))
7311 col++;
7312 if (l[col] != NUL) /* In case of trailing space */
7313 our_paren_pos.col = col;
7314 else
7315 our_paren_pos.col++;
7316 }
7317 }
7318
7319 /*
7320 * Find how indented the paren is, or the character after it
7321 * if we did the above "if".
7322 */
7323 if (our_paren_pos.col > 0)
7324 {
7325 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7326 if (cur_amount > (int)col)
7327 cur_amount = col;
7328 }
7329 }
7330
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007331 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 {
7333 /* Line up with the start of the matching paren line. */
7334 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007335 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7336 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007337 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 {
7339 if (cur_amount != MAXCOL)
7340 amount = cur_amount;
7341 }
7342 else
7343 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007344 /* Add b_ind_unclosed2 for each '(' before our matching one,
7345 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007347 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 {
7349 --our_paren_pos.col;
7350 switch (*ml_get_pos(&our_paren_pos))
7351 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007352 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 col = our_paren_pos.col;
7354 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007355 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 col = MAXCOL;
7357 break;
7358 }
7359 }
7360
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007361 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 * braces */
7363 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007364 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 else
7366 {
7367 curwin->w_cursor.lnum = our_paren_pos.lnum;
7368 curwin->w_cursor.col = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007369 if (find_match_paren(curbuf->b_ind_maxparen,
7370 curbuf->b_ind_maxcomment) != NULL)
7371 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007373 {
7374 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007375 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007376 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007377 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 }
7380 /*
7381 * For a line starting with ')' use the minimum of the two
7382 * positions, to avoid giving it more indent than the previous
7383 * lines:
7384 * func_long_name( if (x
7385 * arg && yy
7386 * ) ^ not here ) ^ not here
7387 */
7388 if (cur_amount < amount)
7389 amount = cur_amount;
7390 }
7391 }
7392
7393 /* add extra indent for a comment */
7394 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007395 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 }
7397
7398 /*
7399 * Are we at least inside braces, then?
7400 */
7401 else
7402 {
7403 trypos = tryposBrace;
7404
7405 ourscope = trypos->lnum;
7406 start = ml_get(ourscope);
7407
7408 /*
7409 * Now figure out how indented the line is in general.
7410 * If the brace was at the start of the line, we use that;
7411 * otherwise, check out the indentation of the line as
7412 * a whole and then add the "imaginary indent" to that.
7413 */
7414 look = skipwhite(start);
7415 if (*look == '{')
7416 {
7417 getvcol(curwin, trypos, &col, NULL, NULL);
7418 amount = col;
7419 if (*start == '{')
7420 start_brace = BRACE_IN_COL0;
7421 else
7422 start_brace = BRACE_AT_START;
7423 }
7424 else
7425 {
7426 /*
7427 * that opening brace might have been on a continuation
7428 * line. if so, find the start of the line.
7429 */
7430 curwin->w_cursor.lnum = ourscope;
7431
7432 /*
7433 * position the cursor over the rightmost paren, so that
7434 * matching it will take us back to the start of the line.
7435 */
7436 lnum = ourscope;
7437 if (find_last_paren(start, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007438 && (trypos = find_match_paren(curbuf->b_ind_maxparen,
7439 curbuf->b_ind_maxcomment)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 lnum = trypos->lnum;
7441
7442 /*
7443 * It could have been something like
7444 * case 1: if (asdf &&
7445 * ldfd) {
7446 * }
7447 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007448 if (curbuf->b_ind_js || (curbuf->b_ind_keep_case_label
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007449 && cin_iscase(skipwhite(ml_get_curline()), FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 amount = get_indent();
7451 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007452 amount = skip_label(lnum, &l, curbuf->b_ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453
7454 start_brace = BRACE_AT_END;
7455 }
7456
7457 /*
7458 * if we're looking at a closing brace, that's where
7459 * we want to be. otherwise, add the amount of room
7460 * that an indent is supposed to be.
7461 */
7462 if (theline[0] == '}')
7463 {
7464 /*
7465 * they may want closing braces to line up with something
7466 * other than the open brace. indulge them, if so.
7467 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007468 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 }
7470 else
7471 {
7472 /*
7473 * If we're looking at an "else", try to find an "if"
7474 * to match it with.
7475 * If we're looking at a "while", try to find a "do"
7476 * to match it with.
7477 */
7478 lookfor = LOOKFOR_INITIAL;
7479 if (cin_iselse(theline))
7480 lookfor = LOOKFOR_IF;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007481 else if (cin_iswhileofdo(theline, cur_curpos.lnum,
7482 curbuf->b_ind_maxparen)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 lookfor = LOOKFOR_DO;
7484 if (lookfor != LOOKFOR_INITIAL)
7485 {
7486 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007487 if (find_match(lookfor, ourscope, curbuf->b_ind_maxparen,
7488 curbuf->b_ind_maxcomment) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 {
7490 amount = get_indent(); /* XXX */
7491 goto theend;
7492 }
7493 }
7494
7495 /*
7496 * We get here if we are not on an "while-of-do" or "else" (or
7497 * failed to find a matching "if").
7498 * Search backwards for something to line up with.
7499 * First set amount for when we don't find anything.
7500 */
7501
7502 /*
7503 * if the '{' is _really_ at the left margin, use the imaginary
7504 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007505 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 */
7507
7508 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7509 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007510 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007511 lookfor_cpp_namespace = TRUE;
7512 }
7513 else if (start_brace == BRACE_AT_START &&
7514 lookfor_cpp_namespace) /* '{' is at start */
7515 {
7516
7517 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 }
7519 else
7520 {
7521 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007522 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007523 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007524
7525 l = skipwhite(ml_get_curline());
7526 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007527 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 else
7530 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007531 /* Compensate for adding b_ind_open_extra later. */
7532 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 if (amount < 0)
7534 amount = 0;
7535 }
7536 }
7537
7538 lookfor_break = FALSE;
7539
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007540 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 {
7542 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007543 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 }
7545 else if (cin_isscopedecl(theline)) /* private:, ... */
7546 {
7547 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007548 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 }
7550 else
7551 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007552 if (curbuf->b_ind_case_break && cin_isbreak(theline))
7553 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 lookfor_break = TRUE;
7555
7556 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007557 /* b_ind_level from start of block */
7558 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 }
7560 scope_amount = amount;
7561 whilelevel = 0;
7562
7563 /*
7564 * Search backwards. If we find something we recognize, line up
7565 * with that.
7566 *
7567 * if we're looking at an open brace, indent
7568 * the usual amount relative to the conditional
7569 * that opens the block.
7570 */
7571 curwin->w_cursor = cur_curpos;
7572 for (;;)
7573 {
7574 curwin->w_cursor.lnum--;
7575 curwin->w_cursor.col = 0;
7576
7577 /*
7578 * If we went all the way back to the start of our scope, line
7579 * up with it.
7580 */
7581 if (curwin->w_cursor.lnum <= ourscope)
7582 {
7583 /* we reached end of scope:
7584 * if looking for a enum or structure initialization
7585 * go further back:
7586 * if it is an initializer (enum xxx or xxx =), then
7587 * don't add ind_continuation, otherwise it is a variable
7588 * declaration:
7589 * int x,
7590 * here; <-- add ind_continuation
7591 */
7592 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7593 {
7594 if (curwin->w_cursor.lnum == 0
7595 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007596 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007598 /* nothing found (abuse curbuf->b_ind_maxparen as
7599 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 * initialization) */
7601 if (cont_amount > 0)
7602 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007603 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 amount += ind_continuation;
7605 break;
7606 }
7607
7608 l = ml_get_curline();
7609
7610 /*
7611 * If we're in a comment now, skip to the start of the
7612 * comment.
7613 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007614 trypos = find_start_comment(curbuf->b_ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 if (trypos != NULL)
7616 {
7617 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007618 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 continue;
7620 }
7621
7622 /*
7623 * Skip preprocessor directives and blank lines.
7624 */
7625 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7626 continue;
7627
7628 if (cin_nocode(l))
7629 continue;
7630
7631 terminated = cin_isterminated(l, FALSE, TRUE);
7632
7633 /*
7634 * If we are at top level and the line looks like a
7635 * function declaration, we are done
7636 * (it's a variable declaration).
7637 */
7638 if (start_brace != BRACE_IN_COL0
Bram Moolenaarc367faa2011-12-14 20:21:35 +01007639 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum,
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007640 0, curbuf->b_ind_maxparen,
7641 curbuf->b_ind_maxcomment))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 {
7643 /* if the line is terminated with another ','
7644 * it is a continued variable initialization.
7645 * don't add extra indent.
7646 * TODO: does not work, if a function
7647 * declaration is split over multiple lines:
7648 * cin_isfuncdecl returns FALSE then.
7649 */
7650 if (terminated == ',')
7651 break;
7652
7653 /* if it es a enum declaration or an assignment,
7654 * we are done.
7655 */
7656 if (terminated != ';' && cin_isinit())
7657 break;
7658
7659 /* nothing useful found */
7660 if (terminated == 0 || terminated == '{')
7661 continue;
7662 }
7663
7664 if (terminated != ';')
7665 {
7666 /* Skip parens and braces. Position the cursor
7667 * over the rightmost paren, so that matching it
7668 * will take us back to the start of the line.
7669 */ /* XXX */
7670 trypos = NULL;
7671 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007672 trypos = find_match_paren(
7673 curbuf->b_ind_maxparen,
7674 curbuf->b_ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675
7676 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007677 trypos = find_start_brace(
7678 curbuf->b_ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679
7680 if (trypos != NULL)
7681 {
7682 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007683 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 continue;
7685 }
7686 }
7687
7688 /* it's a variable declaration, add indentation
7689 * like in
7690 * int a,
7691 * b;
7692 */
7693 if (cont_amount > 0)
7694 amount = cont_amount;
7695 else
7696 amount += ind_continuation;
7697 }
7698 else if (lookfor == LOOKFOR_UNTERM)
7699 {
7700 if (cont_amount > 0)
7701 amount = cont_amount;
7702 else
7703 amount += ind_continuation;
7704 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02007705 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007706 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02007707 if (lookfor != LOOKFOR_TERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 && lookfor != LOOKFOR_CPP_BASECLASS)
Bram Moolenaare79d1532011-10-04 18:03:47 +02007709 {
7710 amount = scope_amount;
7711 if (theline[0] == '{')
7712 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007713 amount += curbuf->b_ind_open_extra;
7714 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007715 }
7716 }
7717
7718 if (lookfor_cpp_namespace)
7719 {
7720 /*
7721 * Looking for C++ namespace, need to look further
7722 * back.
7723 */
7724 if (curwin->w_cursor.lnum == ourscope)
7725 continue;
7726
7727 if (curwin->w_cursor.lnum == 0
7728 || curwin->w_cursor.lnum
7729 < ourscope - FIND_NAMESPACE_LIM)
7730 break;
7731
7732 l = ml_get_curline();
7733
7734 /* If we're in a comment now, skip to the start of
7735 * the comment. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007736 trypos = find_start_comment(
7737 curbuf->b_ind_maxcomment);
Bram Moolenaare79d1532011-10-04 18:03:47 +02007738 if (trypos != NULL)
7739 {
7740 curwin->w_cursor.lnum = trypos->lnum + 1;
7741 curwin->w_cursor.col = 0;
7742 continue;
7743 }
7744
7745 /* Skip preprocessor directives and blank lines. */
7746 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7747 continue;
7748
7749 /* Finally the actual check for "namespace". */
7750 if (cin_is_cpp_namespace(l))
7751 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007752 amount += curbuf->b_ind_cpp_namespace
7753 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007754 break;
7755 }
7756
7757 if (cin_nocode(l))
7758 continue;
7759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 }
7761 break;
7762 }
7763
7764 /*
7765 * If we're in a comment now, skip to the start of the comment.
7766 */ /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007767 if ((trypos = find_start_comment(curbuf->b_ind_maxcomment))
7768 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 {
7770 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007771 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 continue;
7773 }
7774
7775 l = ml_get_curline();
7776
7777 /*
7778 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00007779 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007781 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 if (iscase || cin_isscopedecl(l))
7783 {
7784 /* we are only looking for cpp base class
7785 * declaration/initialization any longer */
7786 if (lookfor == LOOKFOR_CPP_BASECLASS)
7787 break;
7788
7789 /* When looking for a "do" we are not interested in
7790 * labels. */
7791 if (whilelevel > 0)
7792 continue;
7793
7794 /*
7795 * case xx:
7796 * c = 99 + <- this indent plus continuation
7797 *-> here;
7798 */
7799 if (lookfor == LOOKFOR_UNTERM
7800 || lookfor == LOOKFOR_ENUM_OR_INIT)
7801 {
7802 if (cont_amount > 0)
7803 amount = cont_amount;
7804 else
7805 amount += ind_continuation;
7806 break;
7807 }
7808
7809 /*
7810 * case xx: <- line up with this case
7811 * x = 333;
7812 * case yy:
7813 */
7814 if ( (iscase && lookfor == LOOKFOR_CASE)
7815 || (iscase && lookfor_break)
7816 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
7817 {
7818 /*
7819 * Check that this case label is not for another
7820 * switch()
7821 */ /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007822 if ((trypos = find_start_brace(
7823 curbuf->b_ind_maxcomment)) == NULL
7824 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 {
7826 amount = get_indent(); /* XXX */
7827 break;
7828 }
7829 continue;
7830 }
7831
7832 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
7833
7834 /*
7835 * case xx: if (cond) <- line up with this if
7836 * y = y + 1;
7837 * -> s = 99;
7838 *
7839 * case xx:
7840 * if (cond) <- line up with this line
7841 * y = y + 1;
7842 * -> s = 99;
7843 */
7844 if (lookfor == LOOKFOR_TERM)
7845 {
7846 if (n)
7847 amount = n;
7848
7849 if (!lookfor_break)
7850 break;
7851 }
7852
7853 /*
7854 * case xx: x = x + 1; <- line up with this x
7855 * -> y = y + 1;
7856 *
7857 * case xx: if (cond) <- line up with this if
7858 * -> y = y + 1;
7859 */
7860 if (n)
7861 {
7862 amount = n;
7863 l = after_label(ml_get_curline());
7864 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007865 {
7866 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007867 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007868 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007869 amount += curbuf->b_ind_level
7870 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 break;
7873 }
7874
7875 /*
7876 * Try to get the indent of a statement before the switch
7877 * label. If nothing is found, line up relative to the
7878 * switch label.
7879 * break; <- may line up with this line
7880 * case xx:
7881 * -> y = 1;
7882 */
7883 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007884 ? curbuf->b_ind_case_code
7885 : curbuf->b_ind_scopedecl_code);
7886 lookfor = curbuf->b_ind_case_break
7887 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 continue;
7889 }
7890
7891 /*
7892 * Looking for a switch() label or C++ scope declaration,
7893 * ignore other lines, skip {}-blocks.
7894 */
7895 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
7896 {
7897 if (find_last_paren(l, '{', '}') && (trypos =
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007898 find_start_brace(curbuf->b_ind_maxcomment)) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007899 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007901 curwin->w_cursor.col = 0;
7902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 continue;
7904 }
7905
7906 /*
7907 * Ignore jump labels with nothing after them.
7908 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007909 if (!curbuf->b_ind_js && cin_islabel(curbuf->b_ind_maxcomment))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 {
7911 l = after_label(ml_get_curline());
7912 if (l == NULL || cin_nocode(l))
7913 continue;
7914 }
7915
7916 /*
7917 * Ignore #defines, #if, etc.
7918 * Ignore comment and empty lines.
7919 * (need to get the line again, cin_islabel() may have
7920 * unlocked it)
7921 */
7922 l = ml_get_curline();
7923 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)
7924 || cin_nocode(l))
7925 continue;
7926
7927 /*
7928 * Are we at the start of a cpp base class declaration or
7929 * constructor initialization?
7930 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00007931 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007932 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00007933 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00007934 n = cin_is_cpp_baseclass(&col);
Bram Moolenaar18144c82006-04-12 21:52:12 +00007935 l = ml_get_curline();
7936 }
7937 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 {
7939 if (lookfor == LOOKFOR_UNTERM)
7940 {
7941 if (cont_amount > 0)
7942 amount = cont_amount;
7943 else
7944 amount += ind_continuation;
7945 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00007946 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00007948 /* Need to find start of the declaration. */
7949 lookfor = LOOKFOR_UNTERM;
7950 ind_continuation = 0;
7951 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 }
7953 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00007954 /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007955 amount = get_baseclass_amount(col,
7956 curbuf->b_ind_maxparen,
7957 curbuf->b_ind_maxcomment,
7958 curbuf->b_ind_cpp_baseclass);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 break;
7960 }
7961 else if (lookfor == LOOKFOR_CPP_BASECLASS)
7962 {
7963 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00007964 * declaration or initialization before the opening brace.
7965 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 if (cin_isterminated(l, TRUE, FALSE))
7967 break;
7968 else
7969 continue;
7970 }
7971
7972 /*
7973 * What happens next depends on the line being terminated.
7974 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00007975 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 * 123,
7977 * sizeof
7978 * here
7979 * Otherwise check whether it is a enumeration or structure
7980 * initialisation (not indented) or a variable declaration
7981 * (indented).
7982 */
7983 terminated = cin_isterminated(l, FALSE, TRUE);
7984
7985 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
7986 && terminated == ','))
7987 {
7988 /*
7989 * if we're in the middle of a paren thing,
7990 * go back to the line that starts it so
7991 * we can get the right prevailing indent
7992 * if ( foo &&
7993 * bar )
7994 */
7995 /*
7996 * position the cursor over the rightmost paren, so that
7997 * matching it will take us back to the start of the line.
7998 */
7999 (void)find_last_paren(l, '(', ')');
8000 trypos = find_match_paren(
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008001 corr_ind_maxparen(curbuf->b_ind_maxparen,
8002 &cur_curpos), curbuf->b_ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003
8004 /*
8005 * If we are looking for ',', we also look for matching
8006 * braces.
8007 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008008 if (trypos == NULL && terminated == ','
8009 && find_last_paren(l, '{', '}'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008010 trypos = find_start_brace(curbuf->b_ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011
8012 if (trypos != NULL)
8013 {
8014 /*
8015 * Check if we are on a case label now. This is
8016 * handled above.
8017 * case xx: if ( asdf &&
8018 * asdf)
8019 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008020 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008022 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 {
8024 ++curwin->w_cursor.lnum;
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 /*
8031 * Skip over continuation lines to find the one to get the
8032 * indent from
8033 * char *usethis = "bla\
8034 * bla",
8035 * here;
8036 */
8037 if (terminated == ',')
8038 {
8039 while (curwin->w_cursor.lnum > 1)
8040 {
8041 l = ml_get(curwin->w_cursor.lnum - 1);
8042 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8043 break;
8044 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008045 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 }
8047 }
8048
8049 /*
8050 * Get indent and pointer to text for current line,
8051 * ignoring any jump label. XXX
8052 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008053 if (!curbuf->b_ind_js)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008054 cur_amount = skip_label(curwin->w_cursor.lnum,
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008055 &l, curbuf->b_ind_maxcomment);
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008056 else
8057 cur_amount = get_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 /*
8059 * If this is just above the line we are indenting, and it
8060 * starts with a '{', line it up with this line.
8061 * while (not)
8062 * -> {
8063 * }
8064 */
8065 if (terminated != ',' && lookfor != LOOKFOR_TERM
8066 && theline[0] == '{')
8067 {
8068 amount = cur_amount;
8069 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008070 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 * doesn't start with a '{', which must have a match
8072 * in the same line (scope is the same). Probably:
8073 * { 1, 2 },
8074 * -> { 3, 4 }
8075 */
8076 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008077 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008079 if (curbuf->b_ind_cpp_baseclass)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 {
8081 /* have to look back, whether it is a cpp base
8082 * class declaration or initialization */
8083 lookfor = LOOKFOR_CPP_BASECLASS;
8084 continue;
8085 }
8086 break;
8087 }
8088
8089 /*
8090 * Check if we are after an "if", "while", etc.
8091 * Also allow " } else".
8092 */
8093 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8094 {
8095 /*
8096 * Found an unterminated line after an if (), line up
8097 * with the last one.
8098 * if (cond)
8099 * 100 +
8100 * -> here;
8101 */
8102 if (lookfor == LOOKFOR_UNTERM
8103 || lookfor == LOOKFOR_ENUM_OR_INIT)
8104 {
8105 if (cont_amount > 0)
8106 amount = cont_amount;
8107 else
8108 amount += ind_continuation;
8109 break;
8110 }
8111
8112 /*
8113 * If this is just above the line we are indenting, we
8114 * are finished.
8115 * while (not)
8116 * -> here;
8117 * Otherwise this indent can be used when the line
8118 * before this is terminated.
8119 * yyy;
8120 * if (stat)
8121 * while (not)
8122 * xxx;
8123 * -> here;
8124 */
8125 amount = cur_amount;
8126 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008127 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 if (lookfor != LOOKFOR_TERM)
8129 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008130 amount += curbuf->b_ind_level
8131 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 break;
8133 }
8134
8135 /*
8136 * Special trick: when expecting the while () after a
8137 * do, line up with the while()
8138 * do
8139 * x = 1;
8140 * -> here
8141 */
8142 l = skipwhite(ml_get_curline());
8143 if (cin_isdo(l))
8144 {
8145 if (whilelevel == 0)
8146 break;
8147 --whilelevel;
8148 }
8149
8150 /*
8151 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008152 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 * Need to use the scope of this "else". XXX
8154 * If whilelevel != 0 continue looking for a "do {".
8155 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008156 if (cin_iselse(l) && whilelevel == 0)
8157 {
8158 /* If we're looking at "} else", let's make sure we
8159 * find the opening brace of the enclosing scope,
8160 * not the one from "if () {". */
8161 if (*l == '}')
8162 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008163 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008164
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008165 if ((trypos = find_start_brace(
8166 curbuf->b_ind_maxcomment)) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 || find_match(LOOKFOR_IF, trypos->lnum,
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008168 curbuf->b_ind_maxparen,
8169 curbuf->b_ind_maxcomment) == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008170 break;
8171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 }
8173
8174 /*
8175 * If we're below an unterminated line that is not an
8176 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008177 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 * the line before this one.
8179 */
8180 else
8181 {
8182 /*
8183 * Found two unterminated lines on a row, line up with
8184 * the last one.
8185 * c = 99 +
8186 * 100 +
8187 * -> here;
8188 */
8189 if (lookfor == LOOKFOR_UNTERM)
8190 {
8191 /* When line ends in a comma add extra indent */
8192 if (terminated == ',')
8193 amount += ind_continuation;
8194 break;
8195 }
8196
8197 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8198 {
8199 /* Found two lines ending in ',', lineup with the
8200 * lowest one, but check for cpp base class
8201 * declaration/initialization, if it is an
8202 * opening brace or we are looking just for
8203 * enumerations/initializations. */
8204 if (terminated == ',')
8205 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008206 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 break;
8208
8209 lookfor = LOOKFOR_CPP_BASECLASS;
8210 continue;
8211 }
8212
8213 /* Ignore unterminated lines in between, but
8214 * reduce indent. */
8215 if (amount > cur_amount)
8216 amount = cur_amount;
8217 }
8218 else
8219 {
8220 /*
8221 * Found first unterminated line on a row, may
8222 * line up with this line, remember its indent
8223 * 100 +
8224 * -> here;
8225 */
8226 amount = cur_amount;
8227
8228 /*
8229 * If previous line ends in ',', check whether we
8230 * are in an initialization or enum
8231 * struct xxx =
8232 * {
8233 * sizeof a,
8234 * 124 };
8235 * or a normal possible continuation line.
8236 * but only, of no other statement has been found
8237 * yet.
8238 */
8239 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8240 {
8241 lookfor = LOOKFOR_ENUM_OR_INIT;
8242 cont_amount = cin_first_id_amount();
8243 }
8244 else
8245 {
8246 if (lookfor == LOOKFOR_INITIAL
8247 && *l != NUL
8248 && l[STRLEN(l) - 1] == '\\')
8249 /* XXX */
8250 cont_amount = cin_get_equal_amount(
8251 curwin->w_cursor.lnum);
8252 if (lookfor != LOOKFOR_TERM)
8253 lookfor = LOOKFOR_UNTERM;
8254 }
8255 }
8256 }
8257 }
8258
8259 /*
8260 * Check if we are after a while (cond);
8261 * If so: Ignore until the matching "do".
8262 */
8263 /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008264 else if (cin_iswhileofdo_end(terminated, curbuf->b_ind_maxparen,
8265 curbuf->b_ind_maxcomment))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 {
8267 /*
8268 * Found an unterminated line after a while ();, line up
8269 * with the last one.
8270 * while (cond);
8271 * 100 + <- line up with this one
8272 * -> here;
8273 */
8274 if (lookfor == LOOKFOR_UNTERM
8275 || lookfor == LOOKFOR_ENUM_OR_INIT)
8276 {
8277 if (cont_amount > 0)
8278 amount = cont_amount;
8279 else
8280 amount += ind_continuation;
8281 break;
8282 }
8283
8284 if (whilelevel == 0)
8285 {
8286 lookfor = LOOKFOR_TERM;
8287 amount = get_indent(); /* XXX */
8288 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008289 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 }
8291 ++whilelevel;
8292 }
8293
8294 /*
8295 * We are after a "normal" statement.
8296 * If we had another statement we can stop now and use the
8297 * indent of that other statement.
8298 * Otherwise the indent of the current statement may be used,
8299 * search backwards for the next "normal" statement.
8300 */
8301 else
8302 {
8303 /*
8304 * Skip single break line, if before a switch label. It
8305 * may be lined up with the case label.
8306 */
8307 if (lookfor == LOOKFOR_NOBREAK
8308 && cin_isbreak(skipwhite(ml_get_curline())))
8309 {
8310 lookfor = LOOKFOR_ANY;
8311 continue;
8312 }
8313
8314 /*
8315 * Handle "do {" line.
8316 */
8317 if (whilelevel > 0)
8318 {
8319 l = cin_skipcomment(ml_get_curline());
8320 if (cin_isdo(l))
8321 {
8322 amount = get_indent(); /* XXX */
8323 --whilelevel;
8324 continue;
8325 }
8326 }
8327
8328 /*
8329 * Found a terminated line above an unterminated line. Add
8330 * the amount for a continuation line.
8331 * x = 1;
8332 * y = foo +
8333 * -> here;
8334 * or
8335 * int x = 1;
8336 * int foo,
8337 * -> here;
8338 */
8339 if (lookfor == LOOKFOR_UNTERM
8340 || lookfor == LOOKFOR_ENUM_OR_INIT)
8341 {
8342 if (cont_amount > 0)
8343 amount = cont_amount;
8344 else
8345 amount += ind_continuation;
8346 break;
8347 }
8348
8349 /*
8350 * Found a terminated line above a terminated line or "if"
8351 * etc. line. Use the amount of the line below us.
8352 * x = 1; x = 1;
8353 * if (asdf) y = 2;
8354 * while (asdf) ->here;
8355 * here;
8356 * ->foo;
8357 */
8358 if (lookfor == LOOKFOR_TERM)
8359 {
8360 if (!lookfor_break && whilelevel == 0)
8361 break;
8362 }
8363
8364 /*
8365 * First line above the one we're indenting is terminated.
8366 * To know what needs to be done look further backward for
8367 * a terminated line.
8368 */
8369 else
8370 {
8371 /*
8372 * position the cursor over the rightmost paren, so
8373 * that matching it will take us back to the start of
8374 * the line. Helps for:
8375 * func(asdr,
8376 * asdfasdf);
8377 * here;
8378 */
8379term_again:
8380 l = ml_get_curline();
8381 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008382 && (trypos = find_match_paren(
8383 curbuf->b_ind_maxparen,
8384 curbuf->b_ind_maxcomment)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 {
8386 /*
8387 * Check if we are on a case label now. This is
8388 * handled above.
8389 * case xx: if ( asdf &&
8390 * asdf)
8391 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008392 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008394 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 {
8396 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008397 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 continue;
8399 }
8400 }
8401
8402 /* When aligning with the case statement, don't align
8403 * with a statement after it.
8404 * case 1: { <-- don't use this { position
8405 * stat;
8406 * }
8407 * case 2:
8408 * stat;
8409 * }
8410 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008411 iscase = (curbuf->b_ind_keep_case_label
8412 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413
8414 /*
8415 * Get indent and pointer to text for current line,
8416 * ignoring any jump label.
8417 */
8418 amount = skip_label(curwin->w_cursor.lnum,
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008419 &l, curbuf->b_ind_maxcomment);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420
8421 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008422 amount += curbuf->b_ind_open_extra;
8423 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008424 l = skipwhite(l);
8425 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008426 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8428
8429 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008430 * When a terminated line starts with "else" skip to
8431 * the matching "if":
8432 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008433 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008434 * Need to use the scope of this "else". XXX
8435 * If whilelevel != 0 continue looking for a "do {".
8436 */
8437 if (lookfor == LOOKFOR_TERM
8438 && *l != '}'
8439 && cin_iselse(l)
8440 && whilelevel == 0)
8441 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008442 if ((trypos = find_start_brace(
8443 curbuf->b_ind_maxcomment)) == NULL
Bram Moolenaar18144c82006-04-12 21:52:12 +00008444 || find_match(LOOKFOR_IF, trypos->lnum,
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008445 curbuf->b_ind_maxparen,
8446 curbuf->b_ind_maxcomment) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008447 break;
8448 continue;
8449 }
8450
8451 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 * If we're at the end of a block, skip to the start of
8453 * that block.
8454 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008455 l = ml_get_curline();
Bram Moolenaar50f42ca2011-07-15 14:12:30 +02008456 if (find_last_paren(l, '{', '}')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008457 && (trypos = find_start_brace(
8458 curbuf->b_ind_maxcomment)) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008460 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 /* if not "else {" check for terminated again */
8462 /* but skip block for "} else {" */
8463 l = cin_skipcomment(ml_get_curline());
8464 if (*l == '}' || !cin_iselse(l))
8465 goto term_again;
8466 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008467 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 }
8469 }
8470 }
8471 }
8472 }
8473 }
8474
8475 /* add extra indent for a comment */
8476 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008477 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008478
8479 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008480 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
8481 amount -= curbuf->b_ind_jump_label;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 }
8483
8484 /*
8485 * ok -- we're not inside any sort of structure at all!
8486 *
8487 * this means we're at the top level, and everything should
8488 * basically just match where the previous line is, except
8489 * for the lines immediately following a function declaration,
8490 * which are K&R-style parameters and need to be indented.
8491 */
8492 else
8493 {
8494 /*
8495 * if our line starts with an open brace, forget about any
8496 * prevailing indent and make sure it looks like the start
8497 * of a function
8498 */
8499
8500 if (theline[0] == '{')
8501 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008502 amount = curbuf->b_ind_first_open;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 }
8504
8505 /*
8506 * If the NEXT line is a function declaration, the current
8507 * line needs to be indented as a function type spec.
Bram Moolenaar1a89bbe2010-03-02 12:38:22 +01008508 * Don't do this if the current line looks like a comment or if the
8509 * current line is terminated, ie. ends in ';', or if the current line
8510 * contains { or }: "void f() {\n if (1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 */
8512 else if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8513 && !cin_nocode(theline)
Bram Moolenaar1a89bbe2010-03-02 12:38:22 +01008514 && vim_strchr(theline, '{') == NULL
8515 && vim_strchr(theline, '}') == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 && !cin_ends_in(theline, (char_u *)":", NULL)
8517 && !cin_ends_in(theline, (char_u *)",", NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01008518 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
8519 cur_curpos.lnum + 1,
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008520 curbuf->b_ind_maxparen,
8521 curbuf->b_ind_maxcomment)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 && !cin_isterminated(theline, FALSE, TRUE))
8523 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008524 amount = curbuf->b_ind_func_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 }
8526 else
8527 {
8528 amount = 0;
8529 curwin->w_cursor = cur_curpos;
8530
8531 /* search backwards until we find something we recognize */
8532
8533 while (curwin->w_cursor.lnum > 1)
8534 {
8535 curwin->w_cursor.lnum--;
8536 curwin->w_cursor.col = 0;
8537
8538 l = ml_get_curline();
8539
8540 /*
8541 * If we're in a comment now, skip to the start of the comment.
8542 */ /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008543 if ((trypos = find_start_comment(
8544 curbuf->b_ind_maxcomment)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 {
8546 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008547 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 continue;
8549 }
8550
8551 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008552 * Are we at the start of a cpp base class declaration or
8553 * constructor initialization?
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008555 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008556 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar18144c82006-04-12 21:52:12 +00008557 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00008558 n = cin_is_cpp_baseclass(&col);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008559 l = ml_get_curline();
8560 }
8561 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008563 /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008564 amount = get_baseclass_amount(col, curbuf->b_ind_maxparen,
8565 curbuf->b_ind_maxcomment,
8566 curbuf->b_ind_cpp_baseclass);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 break;
8568 }
8569
8570 /*
8571 * Skip preprocessor directives and blank lines.
8572 */
8573 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
8574 continue;
8575
8576 if (cin_nocode(l))
8577 continue;
8578
8579 /*
8580 * If the previous line ends in ',', use one level of
8581 * indentation:
8582 * int foo,
8583 * bar;
8584 * do this before checking for '}' in case of eg.
8585 * enum foobar
8586 * {
8587 * ...
8588 * } foo,
8589 * bar;
8590 */
8591 n = 0;
8592 if (cin_ends_in(l, (char_u *)",", NULL)
8593 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
8594 {
8595 /* take us back to opening paren */
8596 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008597 && (trypos = find_match_paren(
8598 curbuf->b_ind_maxparen,
8599 curbuf->b_ind_maxcomment)) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008600 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601
8602 /* For a line ending in ',' that is a continuation line go
8603 * back to the first line with a backslash:
8604 * char *foo = "bla\
8605 * bla",
8606 * here;
8607 */
8608 while (n == 0 && curwin->w_cursor.lnum > 1)
8609 {
8610 l = ml_get(curwin->w_cursor.lnum - 1);
8611 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8612 break;
8613 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008614 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 }
8616
8617 amount = get_indent(); /* XXX */
8618
8619 if (amount == 0)
8620 amount = cin_first_id_amount();
8621 if (amount == 0)
8622 amount = ind_continuation;
8623 break;
8624 }
8625
8626 /*
8627 * If the line looks like a function declaration, and we're
8628 * not in a comment, put it the left margin.
8629 */
Bram Moolenaarc367faa2011-12-14 20:21:35 +01008630 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0,
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008631 curbuf->b_ind_maxparen,
8632 curbuf->b_ind_maxcomment)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 break;
8634 l = ml_get_curline();
8635
8636 /*
8637 * Finding the closing '}' of a previous function. Put
8638 * current line at the left margin. For when 'cino' has "fs".
8639 */
8640 if (*skipwhite(l) == '}')
8641 break;
8642
8643 /* (matching {)
8644 * If the previous line ends on '};' (maybe followed by
8645 * comments) align at column 0. For example:
8646 * char *string_array[] = { "foo",
8647 * / * x * / "b};ar" }; / * foobar * /
8648 */
8649 if (cin_ends_in(l, (char_u *)"};", NULL))
8650 break;
8651
8652 /*
Bram Moolenaar3388bb42011-11-30 17:20:23 +01008653 * Find a line only has a semicolon that belongs to a previous
8654 * line ending in '}', e.g. before an #endif. Don't increase
8655 * indent then.
8656 */
8657 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
8658 {
8659 pos_T curpos_save = curwin->w_cursor;
8660
8661 while (curwin->w_cursor.lnum > 1)
8662 {
8663 look = ml_get(--curwin->w_cursor.lnum);
8664 if (!(cin_nocode(look) || cin_ispreproc_cont(
8665 &look, &curwin->w_cursor.lnum)))
8666 break;
8667 }
8668 if (curwin->w_cursor.lnum > 0
8669 && cin_ends_in(look, (char_u *)"}", NULL))
8670 break;
8671
8672 curwin->w_cursor = curpos_save;
8673 }
8674
8675 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 * If the PREVIOUS line is a function declaration, the current
8677 * line (and the ones that follow) needs to be indented as
8678 * parameters.
8679 */
Bram Moolenaarc367faa2011-12-14 20:21:35 +01008680 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0,
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008681 curbuf->b_ind_maxparen,
8682 curbuf->b_ind_maxcomment))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008684 amount = curbuf->b_ind_param;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 break;
8686 }
8687
8688 /*
8689 * If the previous line ends in ';' and the line before the
8690 * previous line ends in ',' or '\', ident to column zero:
8691 * int foo,
8692 * bar;
8693 * indent_to_0 here;
8694 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008695 if (cin_ends_in(l, (char_u *)";", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 {
8697 l = ml_get(curwin->w_cursor.lnum - 1);
8698 if (cin_ends_in(l, (char_u *)",", NULL)
8699 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
8700 break;
8701 l = ml_get_curline();
8702 }
8703
8704 /*
8705 * Doesn't look like anything interesting -- so just
8706 * use the indent of this line.
8707 *
8708 * Position the cursor over the rightmost paren, so that
8709 * matching it will take us back to the start of the line.
8710 */
8711 find_last_paren(l, '(', ')');
8712
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008713 if ((trypos = find_match_paren(curbuf->b_ind_maxparen,
8714 curbuf->b_ind_maxcomment)) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008715 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 amount = get_indent(); /* XXX */
8717 break;
8718 }
8719
8720 /* add extra indent for a comment */
8721 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008722 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723
8724 /* add extra indent if the previous line ended in a backslash:
8725 * "asdfasdf\
8726 * here";
8727 * char *foo = "asdf\
8728 * here";
8729 */
8730 if (cur_curpos.lnum > 1)
8731 {
8732 l = ml_get(cur_curpos.lnum - 1);
8733 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
8734 {
8735 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
8736 if (cur_amount > 0)
8737 amount = cur_amount;
8738 else if (cur_amount == 0)
8739 amount += ind_continuation;
8740 }
8741 }
8742 }
8743 }
8744
8745theend:
8746 /* put the cursor back where it belongs */
8747 curwin->w_cursor = cur_curpos;
8748
8749 vim_free(linecopy);
8750
8751 if (amount < 0)
8752 return 0;
8753 return amount;
8754}
8755
8756 static int
8757find_match(lookfor, ourscope, ind_maxparen, ind_maxcomment)
8758 int lookfor;
8759 linenr_T ourscope;
8760 int ind_maxparen;
8761 int ind_maxcomment;
8762{
8763 char_u *look;
8764 pos_T *theirscope;
8765 char_u *mightbeif;
8766 int elselevel;
8767 int whilelevel;
8768
8769 if (lookfor == LOOKFOR_IF)
8770 {
8771 elselevel = 1;
8772 whilelevel = 0;
8773 }
8774 else
8775 {
8776 elselevel = 0;
8777 whilelevel = 1;
8778 }
8779
8780 curwin->w_cursor.col = 0;
8781
8782 while (curwin->w_cursor.lnum > ourscope + 1)
8783 {
8784 curwin->w_cursor.lnum--;
8785 curwin->w_cursor.col = 0;
8786
8787 look = cin_skipcomment(ml_get_curline());
8788 if (cin_iselse(look)
8789 || cin_isif(look)
8790 || cin_isdo(look) /* XXX */
8791 || cin_iswhileofdo(look, curwin->w_cursor.lnum, ind_maxparen))
8792 {
8793 /*
8794 * if we've gone outside the braces entirely,
8795 * we must be out of scope...
8796 */
8797 theirscope = find_start_brace(ind_maxcomment); /* XXX */
8798 if (theirscope == NULL)
8799 break;
8800
8801 /*
8802 * and if the brace enclosing this is further
8803 * back than the one enclosing the else, we're
8804 * out of luck too.
8805 */
8806 if (theirscope->lnum < ourscope)
8807 break;
8808
8809 /*
8810 * and if they're enclosed in a *deeper* brace,
8811 * then we can ignore it because it's in a
8812 * different scope...
8813 */
8814 if (theirscope->lnum > ourscope)
8815 continue;
8816
8817 /*
8818 * if it was an "else" (that's not an "else if")
8819 * then we need to go back to another if, so
8820 * increment elselevel
8821 */
8822 look = cin_skipcomment(ml_get_curline());
8823 if (cin_iselse(look))
8824 {
8825 mightbeif = cin_skipcomment(look + 4);
8826 if (!cin_isif(mightbeif))
8827 ++elselevel;
8828 continue;
8829 }
8830
8831 /*
8832 * if it was a "while" then we need to go back to
8833 * another "do", so increment whilelevel. XXX
8834 */
8835 if (cin_iswhileofdo(look, curwin->w_cursor.lnum, ind_maxparen))
8836 {
8837 ++whilelevel;
8838 continue;
8839 }
8840
8841 /* If it's an "if" decrement elselevel */
8842 look = cin_skipcomment(ml_get_curline());
8843 if (cin_isif(look))
8844 {
8845 elselevel--;
8846 /*
8847 * When looking for an "if" ignore "while"s that
8848 * get in the way.
8849 */
8850 if (elselevel == 0 && lookfor == LOOKFOR_IF)
8851 whilelevel = 0;
8852 }
8853
8854 /* If it's a "do" decrement whilelevel */
8855 if (cin_isdo(look))
8856 whilelevel--;
8857
8858 /*
8859 * if we've used up all the elses, then
8860 * this must be the if that we want!
8861 * match the indent level of that if.
8862 */
8863 if (elselevel <= 0 && whilelevel <= 0)
8864 {
8865 return OK;
8866 }
8867 }
8868 }
8869 return FAIL;
8870}
8871
8872# if defined(FEAT_EVAL) || defined(PROTO)
8873/*
8874 * Get indent level from 'indentexpr'.
8875 */
8876 int
8877get_expr_indent()
8878{
8879 int indent;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008880 pos_T save_pos;
8881 colnr_T save_curswant;
8882 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008884 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
8885 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008887 /* Save and restore cursor position and curswant, in case it was changed
8888 * via :normal commands */
8889 save_pos = curwin->w_cursor;
8890 save_curswant = curwin->w_curswant;
8891 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008893 if (use_sandbox)
8894 ++sandbox;
8895 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896 indent = eval_to_number(curbuf->b_p_inde);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008897 if (use_sandbox)
8898 --sandbox;
8899 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900
8901 /* Restore the cursor position so that 'indentexpr' doesn't need to.
8902 * Pretend to be in Insert mode, allow cursor past end of line for "o"
8903 * command. */
8904 save_State = State;
8905 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008906 curwin->w_cursor = save_pos;
8907 curwin->w_curswant = save_curswant;
8908 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 check_cursor();
8910 State = save_State;
8911
8912 /* If there is an error, just keep the current indent. */
8913 if (indent < 0)
8914 indent = get_indent();
8915
8916 return indent;
8917}
8918# endif
8919
8920#endif /* FEAT_CINDENT */
8921
8922#if defined(FEAT_LISP) || defined(PROTO)
8923
8924static int lisp_match __ARGS((char_u *p));
8925
8926 static int
8927lisp_match(p)
8928 char_u *p;
8929{
8930 char_u buf[LSIZE];
8931 int len;
8932 char_u *word = p_lispwords;
8933
8934 while (*word != NUL)
8935 {
8936 (void)copy_option_part(&word, buf, LSIZE, ",");
8937 len = (int)STRLEN(buf);
8938 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
8939 return TRUE;
8940 }
8941 return FALSE;
8942}
8943
8944/*
8945 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
8946 * The incompatible newer method is quite a bit better at indenting
8947 * code in lisp-like languages than the traditional one; it's still
8948 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
8949 *
8950 * TODO:
8951 * Findmatch() should be adapted for lisp, also to make showmatch
8952 * work correctly: now (v5.3) it seems all C/C++ oriented:
8953 * - it does not recognize the #\( and #\) notations as character literals
8954 * - it doesn't know about comments starting with a semicolon
8955 * - it incorrectly interprets '(' as a character literal
8956 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008957 * Update from Sergey Khorev:
8958 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 */
8960 int
8961get_lisp_indent()
8962{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008963 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 int amount;
8965 char_u *that;
8966 colnr_T col;
8967 colnr_T firsttry;
8968 int parencount, quotecount;
8969 int vi_lisp;
8970
8971 /* Set vi_lisp to use the vi-compatible method */
8972 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
8973
8974 realpos = curwin->w_cursor;
8975 curwin->w_cursor.col = 0;
8976
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008977 if ((pos = findmatch(NULL, '(')) == NULL)
8978 pos = findmatch(NULL, '[');
8979 else
8980 {
8981 paren = *pos;
8982 pos = findmatch(NULL, '[');
8983 if (pos == NULL || ltp(pos, &paren))
8984 pos = &paren;
8985 }
8986 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 {
8988 /* Extra trick: Take the indent of the first previous non-white
8989 * line that is at the same () level. */
8990 amount = -1;
8991 parencount = 0;
8992
8993 while (--curwin->w_cursor.lnum >= pos->lnum)
8994 {
8995 if (linewhite(curwin->w_cursor.lnum))
8996 continue;
8997 for (that = ml_get_curline(); *that != NUL; ++that)
8998 {
8999 if (*that == ';')
9000 {
9001 while (*(that + 1) != NUL)
9002 ++that;
9003 continue;
9004 }
9005 if (*that == '\\')
9006 {
9007 if (*(that + 1) != NUL)
9008 ++that;
9009 continue;
9010 }
9011 if (*that == '"' && *(that + 1) != NUL)
9012 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00009013 while (*++that && *that != '"')
9014 {
9015 /* skipping escaped characters in the string */
9016 if (*that == '\\')
9017 {
9018 if (*++that == NUL)
9019 break;
9020 if (that[1] == NUL)
9021 {
9022 ++that;
9023 break;
9024 }
9025 }
9026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009028 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009030 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 --parencount;
9032 }
9033 if (parencount == 0)
9034 {
9035 amount = get_indent();
9036 break;
9037 }
9038 }
9039
9040 if (amount == -1)
9041 {
9042 curwin->w_cursor.lnum = pos->lnum;
9043 curwin->w_cursor.col = pos->col;
9044 col = pos->col;
9045
9046 that = ml_get_curline();
9047
9048 if (vi_lisp && get_indent() == 0)
9049 amount = 2;
9050 else
9051 {
9052 amount = 0;
9053 while (*that && col)
9054 {
9055 amount += lbr_chartabsize_adv(&that, (colnr_T)amount);
9056 col--;
9057 }
9058
9059 /*
9060 * Some keywords require "body" indenting rules (the
9061 * non-standard-lisp ones are Scheme special forms):
9062 *
9063 * (let ((a 1)) instead (let ((a 1))
9064 * (...)) of (...))
9065 */
9066
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009067 if (!vi_lisp && (*that == '(' || *that == '[')
9068 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 amount += 2;
9070 else
9071 {
9072 that++;
9073 amount++;
9074 firsttry = amount;
9075
9076 while (vim_iswhite(*that))
9077 {
9078 amount += lbr_chartabsize(that, (colnr_T)amount);
9079 ++that;
9080 }
9081
9082 if (*that && *that != ';') /* not a comment line */
9083 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009084 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009086 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 firsttry++;
9088
9089 parencount = 0;
9090 quotecount = 0;
9091
9092 if (vi_lisp
9093 || (*that != '"'
9094 && *that != '\''
9095 && *that != '#'
9096 && (*that < '0' || *that > '9')))
9097 {
9098 while (*that
9099 && (!vim_iswhite(*that)
9100 || quotecount
9101 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009102 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 && !quotecount
9104 && !parencount
9105 && vi_lisp)))
9106 {
9107 if (*that == '"')
9108 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009109 if ((*that == '(' || *that == '[')
9110 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009112 if ((*that == ')' || *that == ']')
9113 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 --parencount;
9115 if (*that == '\\' && *(that+1) != NUL)
9116 amount += lbr_chartabsize_adv(&that,
9117 (colnr_T)amount);
9118 amount += lbr_chartabsize_adv(&that,
9119 (colnr_T)amount);
9120 }
9121 }
9122 while (vim_iswhite(*that))
9123 {
9124 amount += lbr_chartabsize(that, (colnr_T)amount);
9125 that++;
9126 }
9127 if (!*that || *that == ';')
9128 amount = firsttry;
9129 }
9130 }
9131 }
9132 }
9133 }
9134 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009135 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136
9137 curwin->w_cursor = realpos;
9138
9139 return amount;
9140}
9141#endif /* FEAT_LISP */
9142
9143 void
9144prepare_to_exit()
9145{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009146#if defined(SIGHUP) && defined(SIG_IGN)
9147 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9148 * makes Vim exit and then handling SIGHUP causes various reentrance
9149 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009150 signal(SIGHUP, SIG_IGN);
9151#endif
9152
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153#ifdef FEAT_GUI
9154 if (gui.in_use)
9155 {
9156 gui.dying = TRUE;
9157 out_trash(); /* trash any pending output */
9158 }
9159 else
9160#endif
9161 {
9162 windgoto((int)Rows - 1, 0);
9163
9164 /*
9165 * Switch terminal mode back now, so messages end up on the "normal"
9166 * screen (if there are two screens).
9167 */
9168 settmode(TMODE_COOK);
9169#ifdef WIN3264
9170 if (can_end_termcap_mode(FALSE) == TRUE)
9171#endif
9172 stoptermcap();
9173 out_flush();
9174 }
9175}
9176
9177/*
9178 * Preserve files and exit.
9179 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009180 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9181 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 */
9183 void
9184preserve_exit()
9185{
9186 buf_T *buf;
9187
9188 prepare_to_exit();
9189
Bram Moolenaar4770d092006-01-12 23:22:24 +00009190 /* Setting this will prevent free() calls. That avoids calling free()
9191 * recursively when free() was invoked with a bad pointer. */
9192 really_exiting = TRUE;
9193
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 out_str(IObuff);
9195 screen_start(); /* don't know where cursor is now */
9196 out_flush();
9197
9198 ml_close_notmod(); /* close all not-modified buffers */
9199
9200 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9201 {
9202 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9203 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009204 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 screen_start(); /* don't know where cursor is now */
9206 out_flush();
9207 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9208 break;
9209 }
9210 }
9211
9212 ml_close_all(FALSE); /* close all memfiles, without deleting */
9213
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009214 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215
9216 getout(1);
9217}
9218
9219/*
9220 * return TRUE if "fname" exists.
9221 */
9222 int
9223vim_fexists(fname)
9224 char_u *fname;
9225{
9226 struct stat st;
9227
9228 if (mch_stat((char *)fname, &st))
9229 return FALSE;
9230 return TRUE;
9231}
9232
9233/*
9234 * Check for CTRL-C pressed, but only once in a while.
9235 * Should be used instead of ui_breakcheck() for functions that check for
9236 * each line in the file. Calling ui_breakcheck() each time takes too much
9237 * time, because it can be a system call.
9238 */
9239
9240#ifndef BREAKCHECK_SKIP
9241# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9242# define BREAKCHECK_SKIP 200
9243# else
9244# define BREAKCHECK_SKIP 32
9245# endif
9246#endif
9247
9248static int breakcheck_count = 0;
9249
9250 void
9251line_breakcheck()
9252{
9253 if (++breakcheck_count >= BREAKCHECK_SKIP)
9254 {
9255 breakcheck_count = 0;
9256 ui_breakcheck();
9257 }
9258}
9259
9260/*
9261 * Like line_breakcheck() but check 10 times less often.
9262 */
9263 void
9264fast_breakcheck()
9265{
9266 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9267 {
9268 breakcheck_count = 0;
9269 ui_breakcheck();
9270 }
9271}
9272
9273/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009274 * Invoke expand_wildcards() for one pattern.
9275 * Expand items like "%:h" before the expansion.
9276 * Returns OK or FAIL.
9277 */
9278 int
9279expand_wildcards_eval(pat, num_file, file, flags)
9280 char_u **pat; /* pointer to input pattern */
9281 int *num_file; /* resulting number of files */
9282 char_u ***file; /* array of resulting files */
9283 int flags; /* EW_DIR, etc. */
9284{
9285 int ret = FAIL;
9286 char_u *eval_pat = NULL;
9287 char_u *exp_pat = *pat;
9288 char_u *ignored_msg;
9289 int usedlen;
9290
9291 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9292 {
9293 ++emsg_off;
9294 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9295 NULL, &ignored_msg, NULL);
9296 --emsg_off;
9297 if (eval_pat != NULL)
9298 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9299 }
9300
9301 if (exp_pat != NULL)
9302 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9303
9304 if (eval_pat != NULL)
9305 {
9306 vim_free(exp_pat);
9307 vim_free(eval_pat);
9308 }
9309
9310 return ret;
9311}
9312
9313/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9315 * 'wildignore'.
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009316 * Returns OK or FAIL. When FAIL then "num_file" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 */
9318 int
9319expand_wildcards(num_pat, pat, num_file, file, flags)
9320 int num_pat; /* number of input patterns */
9321 char_u **pat; /* array of input patterns */
9322 int *num_file; /* resulting number of files */
9323 char_u ***file; /* array of resulting files */
9324 int flags; /* EW_DIR, etc. */
9325{
9326 int retval;
9327 int i, j;
9328 char_u *p;
9329 int non_suf_match; /* number without matching suffix */
9330
9331 retval = gen_expand_wildcards(num_pat, pat, num_file, file, flags);
9332
9333 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009334 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 return retval;
9336
9337#ifdef FEAT_WILDIGN
9338 /*
9339 * Remove names that match 'wildignore'.
9340 */
9341 if (*p_wig)
9342 {
9343 char_u *ffname;
9344
9345 /* check all files in (*file)[] */
9346 for (i = 0; i < *num_file; ++i)
9347 {
9348 ffname = FullName_save((*file)[i], FALSE);
9349 if (ffname == NULL) /* out of memory */
9350 break;
9351# ifdef VMS
9352 vms_remove_version(ffname);
9353# endif
9354 if (match_file_list(p_wig, (*file)[i], ffname))
9355 {
9356 /* remove this matching file from the list */
9357 vim_free((*file)[i]);
9358 for (j = i; j + 1 < *num_file; ++j)
9359 (*file)[j] = (*file)[j + 1];
9360 --*num_file;
9361 --i;
9362 }
9363 vim_free(ffname);
9364 }
9365 }
9366#endif
9367
9368 /*
9369 * Move the names where 'suffixes' match to the end.
9370 */
9371 if (*num_file > 1)
9372 {
9373 non_suf_match = 0;
9374 for (i = 0; i < *num_file; ++i)
9375 {
9376 if (!match_suffix((*file)[i]))
9377 {
9378 /*
9379 * Move the name without matching suffix to the front
9380 * of the list.
9381 */
9382 p = (*file)[i];
9383 for (j = i; j > non_suf_match; --j)
9384 (*file)[j] = (*file)[j - 1];
9385 (*file)[non_suf_match++] = p;
9386 }
9387 }
9388 }
9389
9390 return retval;
9391}
9392
9393/*
9394 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9395 */
9396 int
9397match_suffix(fname)
9398 char_u *fname;
9399{
9400 int fnamelen, setsuflen;
9401 char_u *setsuf;
9402#define MAXSUFLEN 30 /* maximum length of a file suffix */
9403 char_u suf_buf[MAXSUFLEN];
9404
9405 fnamelen = (int)STRLEN(fname);
9406 setsuflen = 0;
9407 for (setsuf = p_su; *setsuf; )
9408 {
9409 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009410 if (setsuflen == 0)
9411 {
9412 char_u *tail = gettail(fname);
9413
9414 /* empty entry: match name without a '.' */
9415 if (vim_strchr(tail, '.') == NULL)
9416 {
9417 setsuflen = 1;
9418 break;
9419 }
9420 }
9421 else
9422 {
9423 if (fnamelen >= setsuflen
9424 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9425 (size_t)setsuflen) == 0)
9426 break;
9427 setsuflen = 0;
9428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429 }
9430 return (setsuflen != 0);
9431}
9432
9433#if !defined(NO_EXPANDPATH) || defined(PROTO)
9434
9435# ifdef VIM_BACKTICK
9436static int vim_backtick __ARGS((char_u *p));
9437static int expand_backtick __ARGS((garray_T *gap, char_u *pat, int flags));
9438# endif
9439
9440# if defined(MSDOS) || defined(FEAT_GUI_W16) || defined(WIN3264)
9441/*
9442 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9443 * it's shared between these systems.
9444 */
9445# if defined(DJGPP) || defined(PROTO)
9446# define _cdecl /* DJGPP doesn't have this */
9447# else
9448# ifdef __BORLANDC__
9449# define _cdecl _RTLENTRYF
9450# endif
9451# endif
9452
9453/*
9454 * comparison function for qsort in dos_expandpath()
9455 */
9456 static int _cdecl
9457pstrcmp(const void *a, const void *b)
9458{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009459 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460}
9461
9462# ifndef WIN3264
9463 static void
9464namelowcpy(
9465 char_u *d,
9466 char_u *s)
9467{
9468# ifdef DJGPP
9469 if (USE_LONG_FNAME) /* don't lower case on Windows 95/NT systems */
9470 while (*s)
9471 *d++ = *s++;
9472 else
9473# endif
9474 while (*s)
9475 *d++ = TOLOWER_LOC(*s++);
9476 *d = NUL;
9477}
9478# endif
9479
9480/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009481 * Recursively expand one path component into all matching files and/or
9482 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 * Return the number of matches found.
9484 * "path" has backslashes before chars that are not to be expanded, starting
9485 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009486 * Return the number of matches found.
9487 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 */
9489 static int
9490dos_expandpath(
9491 garray_T *gap,
9492 char_u *path,
9493 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009494 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009495 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009497 char_u *buf;
9498 char_u *path_end;
9499 char_u *p, *s, *e;
9500 int start_len = gap->ga_len;
9501 char_u *pat;
9502 regmatch_T regmatch;
9503 int starts_with_dot;
9504 int matches;
9505 int len;
9506 int starstar = FALSE;
9507 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508#ifdef WIN3264
9509 WIN32_FIND_DATA fb;
9510 HANDLE hFind = (HANDLE)0;
9511# ifdef FEAT_MBYTE
9512 WIN32_FIND_DATAW wfb;
9513 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9514# endif
9515#else
9516 struct ffblk fb;
9517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009519 int ok;
9520
9521 /* Expanding "**" may take a long time, check for CTRL-C. */
9522 if (stardepth > 0)
9523 {
9524 ui_breakcheck();
9525 if (got_int)
9526 return 0;
9527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528
9529 /* make room for file name */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009530 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 if (buf == NULL)
9532 return 0;
9533
9534 /*
9535 * Find the first part in the path name that contains a wildcard or a ~1.
9536 * Copy it into buf, including the preceding characters.
9537 */
9538 p = buf;
9539 s = buf;
9540 e = NULL;
9541 path_end = path;
9542 while (*path_end != NUL)
9543 {
9544 /* May ignore a wildcard that has a backslash before it; it will
9545 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9546 if (path_end >= path + wildoff && rem_backslash(path_end))
9547 *p++ = *path_end++;
9548 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9549 {
9550 if (e != NULL)
9551 break;
9552 s = p + 1;
9553 }
9554 else if (path_end >= path + wildoff
9555 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9556 e = p;
9557#ifdef FEAT_MBYTE
9558 if (has_mbyte)
9559 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009560 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 STRNCPY(p, path_end, len);
9562 p += len;
9563 path_end += len;
9564 }
9565 else
9566#endif
9567 *p++ = *path_end++;
9568 }
9569 e = p;
9570 *e = NUL;
9571
9572 /* now we have one wildcard component between s and e */
9573 /* Remove backslashes between "wildoff" and the start of the wildcard
9574 * component. */
9575 for (p = buf + wildoff; p < s; ++p)
9576 if (rem_backslash(p))
9577 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009578 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 --e;
9580 --s;
9581 }
9582
Bram Moolenaar231334e2005-07-25 20:46:57 +00009583 /* Check for "**" between "s" and "e". */
9584 for (p = s; p < e; ++p)
9585 if (p[0] == '*' && p[1] == '*')
9586 starstar = TRUE;
9587
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 starts_with_dot = (*s == '.');
9589 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9590 if (pat == NULL)
9591 {
9592 vim_free(buf);
9593 return 0;
9594 }
9595
9596 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009597 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009598 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 regmatch.rm_ic = TRUE; /* Always ignore case */
9600 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009601 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009602 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 vim_free(pat);
9604
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009605 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 {
9607 vim_free(buf);
9608 return 0;
9609 }
9610
9611 /* remember the pattern or file name being looked for */
9612 matchname = vim_strsave(s);
9613
Bram Moolenaar231334e2005-07-25 20:46:57 +00009614 /* If "**" is by itself, this is the first time we encounter it and more
9615 * is following then find matches without any directory. */
9616 if (!didstar && stardepth < 100 && starstar && e - s == 2
9617 && *path_end == '/')
9618 {
9619 STRCPY(s, path_end + 1);
9620 ++stardepth;
9621 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9622 --stardepth;
9623 }
9624
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625 /* Scan all files in the directory with "dir/ *.*" */
9626 STRCPY(s, "*.*");
9627#ifdef WIN3264
9628# ifdef FEAT_MBYTE
9629 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
9630 {
9631 /* The active codepage differs from 'encoding'. Attempt using the
9632 * wide function. If it fails because it is not implemented fall back
9633 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009634 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635 if (wn != NULL)
9636 {
9637 hFind = FindFirstFileW(wn, &wfb);
9638 if (hFind == INVALID_HANDLE_VALUE
9639 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
9640 {
9641 vim_free(wn);
9642 wn = NULL;
9643 }
9644 }
9645 }
9646
9647 if (wn == NULL)
9648# endif
9649 hFind = FindFirstFile(buf, &fb);
9650 ok = (hFind != INVALID_HANDLE_VALUE);
9651#else
9652 /* If we are expanding wildcards we try both files and directories */
9653 ok = (findfirst((char *)buf, &fb,
9654 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
9655#endif
9656
9657 while (ok)
9658 {
9659#ifdef WIN3264
9660# ifdef FEAT_MBYTE
9661 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009662 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663 else
9664# endif
9665 p = (char_u *)fb.cFileName;
9666#else
9667 p = (char_u *)fb.ff_name;
9668#endif
9669 /* Ignore entries starting with a dot, unless when asked for. Accept
9670 * all entries found with "matchname". */
9671 if ((p[0] != '.' || starts_with_dot)
9672 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009673 || (regmatch.regprog != NULL
9674 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009675 || ((flags & EW_NOTWILD)
9676 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 {
9678#ifdef WIN3264
9679 STRCPY(s, p);
9680#else
9681 namelowcpy(s, p);
9682#endif
9683 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009684
9685 if (starstar && stardepth < 100)
9686 {
9687 /* For "**" in the pattern first go deeper in the tree to
9688 * find matches. */
9689 STRCPY(buf + len, "/**");
9690 STRCPY(buf + len + 3, path_end);
9691 ++stardepth;
9692 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
9693 --stardepth;
9694 }
9695
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 STRCPY(buf + len, path_end);
9697 if (mch_has_exp_wildcard(path_end))
9698 {
9699 /* need to expand another component of the path */
9700 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009701 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 }
9703 else
9704 {
9705 /* no more wildcards, check if there is a match */
9706 /* remove backslashes for the remaining components only */
9707 if (*path_end != 0)
9708 backslash_halve(buf + len + 1);
9709 if (mch_getperm(buf) >= 0) /* add existing file */
9710 addfile(gap, buf, flags);
9711 }
9712 }
9713
9714#ifdef WIN3264
9715# ifdef FEAT_MBYTE
9716 if (wn != NULL)
9717 {
9718 vim_free(p);
9719 ok = FindNextFileW(hFind, &wfb);
9720 }
9721 else
9722# endif
9723 ok = FindNextFile(hFind, &fb);
9724#else
9725 ok = (findnext(&fb) == 0);
9726#endif
9727
9728 /* If no more matches and no match was used, try expanding the name
9729 * itself. Finds the long name of a short filename. */
9730 if (!ok && matchname != NULL && gap->ga_len == start_len)
9731 {
9732 STRCPY(s, matchname);
9733#ifdef WIN3264
9734 FindClose(hFind);
9735# ifdef FEAT_MBYTE
9736 if (wn != NULL)
9737 {
9738 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009739 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 if (wn != NULL)
9741 hFind = FindFirstFileW(wn, &wfb);
9742 }
9743 if (wn == NULL)
9744# endif
9745 hFind = FindFirstFile(buf, &fb);
9746 ok = (hFind != INVALID_HANDLE_VALUE);
9747#else
9748 ok = (findfirst((char *)buf, &fb,
9749 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
9750#endif
9751 vim_free(matchname);
9752 matchname = NULL;
9753 }
9754 }
9755
9756#ifdef WIN3264
9757 FindClose(hFind);
9758# ifdef FEAT_MBYTE
9759 vim_free(wn);
9760# endif
9761#endif
9762 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +02009763 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 vim_free(matchname);
9765
9766 matches = gap->ga_len - start_len;
9767 if (matches > 0)
9768 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
9769 sizeof(char_u *), pstrcmp);
9770 return matches;
9771}
9772
9773 int
9774mch_expandpath(
9775 garray_T *gap,
9776 char_u *path,
9777 int flags) /* EW_* flags */
9778{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009779 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780}
9781# endif /* MSDOS || FEAT_GUI_W16 || WIN3264 */
9782
Bram Moolenaar231334e2005-07-25 20:46:57 +00009783#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
9784 || defined(PROTO)
9785/*
9786 * Unix style wildcard expansion code.
9787 * It's here because it's used both for Unix and Mac.
9788 */
9789static int pstrcmp __ARGS((const void *, const void *));
9790
9791 static int
9792pstrcmp(a, b)
9793 const void *a, *b;
9794{
9795 return (pathcmp(*(char **)a, *(char **)b, -1));
9796}
9797
9798/*
9799 * Recursively expand one path component into all matching files and/or
9800 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
9801 * "path" has backslashes before chars that are not to be expanded, starting
9802 * at "path + wildoff".
9803 * Return the number of matches found.
9804 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
9805 */
9806 int
9807unix_expandpath(gap, path, wildoff, flags, didstar)
9808 garray_T *gap;
9809 char_u *path;
9810 int wildoff;
9811 int flags; /* EW_* flags */
9812 int didstar; /* expanded "**" once already */
9813{
9814 char_u *buf;
9815 char_u *path_end;
9816 char_u *p, *s, *e;
9817 int start_len = gap->ga_len;
9818 char_u *pat;
9819 regmatch_T regmatch;
9820 int starts_with_dot;
9821 int matches;
9822 int len;
9823 int starstar = FALSE;
9824 static int stardepth = 0; /* depth for "**" expansion */
9825
9826 DIR *dirp;
9827 struct dirent *dp;
9828
9829 /* Expanding "**" may take a long time, check for CTRL-C. */
9830 if (stardepth > 0)
9831 {
9832 ui_breakcheck();
9833 if (got_int)
9834 return 0;
9835 }
9836
9837 /* make room for file name */
9838 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
9839 if (buf == NULL)
9840 return 0;
9841
9842 /*
9843 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +02009844 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +00009845 * Copy it into "buf", including the preceding characters.
9846 */
9847 p = buf;
9848 s = buf;
9849 e = NULL;
9850 path_end = path;
9851 while (*path_end != NUL)
9852 {
9853 /* May ignore a wildcard that has a backslash before it; it will
9854 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9855 if (path_end >= path + wildoff && rem_backslash(path_end))
9856 *p++ = *path_end++;
9857 else if (*path_end == '/')
9858 {
9859 if (e != NULL)
9860 break;
9861 s = p + 1;
9862 }
9863 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +02009864 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01009865 || (!p_fic && (flags & EW_ICASE)
9866 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +00009867 e = p;
9868#ifdef FEAT_MBYTE
9869 if (has_mbyte)
9870 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009871 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009872 STRNCPY(p, path_end, len);
9873 p += len;
9874 path_end += len;
9875 }
9876 else
9877#endif
9878 *p++ = *path_end++;
9879 }
9880 e = p;
9881 *e = NUL;
9882
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009883 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009884 /* Remove backslashes between "wildoff" and the start of the wildcard
9885 * component. */
9886 for (p = buf + wildoff; p < s; ++p)
9887 if (rem_backslash(p))
9888 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009889 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009890 --e;
9891 --s;
9892 }
9893
9894 /* Check for "**" between "s" and "e". */
9895 for (p = s; p < e; ++p)
9896 if (p[0] == '*' && p[1] == '*')
9897 starstar = TRUE;
9898
9899 /* convert the file pattern to a regexp pattern */
9900 starts_with_dot = (*s == '.');
9901 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9902 if (pat == NULL)
9903 {
9904 vim_free(buf);
9905 return 0;
9906 }
9907
9908 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +01009909 if (flags & EW_ICASE)
9910 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
9911 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01009912 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009913 if (flags & (EW_NOERROR | EW_NOTWILD))
9914 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009915 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009916 if (flags & (EW_NOERROR | EW_NOTWILD))
9917 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009918 vim_free(pat);
9919
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009920 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +00009921 {
9922 vim_free(buf);
9923 return 0;
9924 }
9925
9926 /* If "**" is by itself, this is the first time we encounter it and more
9927 * is following then find matches without any directory. */
9928 if (!didstar && stardepth < 100 && starstar && e - s == 2
9929 && *path_end == '/')
9930 {
9931 STRCPY(s, path_end + 1);
9932 ++stardepth;
9933 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9934 --stardepth;
9935 }
9936
9937 /* open the directory for scanning */
9938 *s = NUL;
9939 dirp = opendir(*buf == NUL ? "." : (char *)buf);
9940
9941 /* Find all matching entries */
9942 if (dirp != NULL)
9943 {
9944 for (;;)
9945 {
9946 dp = readdir(dirp);
9947 if (dp == NULL)
9948 break;
9949 if ((dp->d_name[0] != '.' || starts_with_dot)
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009950 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
9951 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009952 || ((flags & EW_NOTWILD)
9953 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +00009954 {
9955 STRCPY(s, dp->d_name);
9956 len = STRLEN(buf);
9957
9958 if (starstar && stardepth < 100)
9959 {
9960 /* For "**" in the pattern first go deeper in the tree to
9961 * find matches. */
9962 STRCPY(buf + len, "/**");
9963 STRCPY(buf + len + 3, path_end);
9964 ++stardepth;
9965 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
9966 --stardepth;
9967 }
9968
9969 STRCPY(buf + len, path_end);
9970 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
9971 {
9972 /* need to expand another component of the path */
9973 /* remove backslashes for the remaining components only */
9974 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
9975 }
9976 else
9977 {
9978 /* no more wildcards, check if there is a match */
9979 /* remove backslashes for the remaining components only */
9980 if (*path_end != NUL)
9981 backslash_halve(buf + len + 1);
9982 if (mch_getperm(buf) >= 0) /* add existing file */
9983 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +00009984#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +00009985 size_t precomp_len = STRLEN(buf)+1;
9986 char_u *precomp_buf =
9987 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +00009988
Bram Moolenaar231334e2005-07-25 20:46:57 +00009989 if (precomp_buf)
9990 {
9991 mch_memmove(buf, precomp_buf, precomp_len);
9992 vim_free(precomp_buf);
9993 }
9994#endif
9995 addfile(gap, buf, flags);
9996 }
9997 }
9998 }
9999 }
10000
10001 closedir(dirp);
10002 }
10003
10004 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +020010005 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +000010006
10007 matches = gap->ga_len - start_len;
10008 if (matches > 0)
10009 qsort(((char_u **)gap->ga_data) + start_len, matches,
10010 sizeof(char_u *), pstrcmp);
10011 return matches;
10012}
10013#endif
10014
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010015#if defined(FEAT_SEARCHPATH)
10016static int find_previous_pathsep __ARGS((char_u *path, char_u **psep));
10017static int is_unique __ARGS((char_u *maybe_unique, garray_T *gap, int i));
Bram Moolenaar162bd912010-07-28 22:29:10 +020010018static void expand_path_option __ARGS((char_u *curdir, garray_T *gap));
10019static char_u *get_path_cutoff __ARGS((char_u *fname, garray_T *gap));
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010020static void uniquefy_paths __ARGS((garray_T *gap, char_u *pattern));
10021static int expand_in_path __ARGS((garray_T *gap, char_u *pattern, int flags));
10022
10023/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010024 * Moves "*psep" back to the previous path separator in "path".
10025 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010026 */
10027 static int
10028find_previous_pathsep(path, psep)
10029 char_u *path;
10030 char_u **psep;
10031{
10032 /* skip the current separator */
10033 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010034 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010035
10036 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010037 while (*psep > path)
10038 {
10039 if (vim_ispathsep(**psep))
10040 return OK;
10041 mb_ptr_back(path, *psep);
10042 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010043
10044 return FAIL;
10045}
10046
10047/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010048 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10049 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010050 */
10051 static int
10052is_unique(maybe_unique, gap, i)
10053 char_u *maybe_unique;
10054 garray_T *gap;
10055 int i;
10056{
10057 int j;
10058 int candidate_len;
10059 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010060 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010061 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010062
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010063 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010064 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010065 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010066 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010067
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010068 candidate_len = (int)STRLEN(maybe_unique);
10069 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010070 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010071 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010072
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010073 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010074 if (fnamecmp(maybe_unique, rival) == 0
10075 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010076 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010077 }
10078
Bram Moolenaar162bd912010-07-28 22:29:10 +020010079 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010080}
10081
10082/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010083 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010084 * paths are expanded to their equivalent fullpath. This includes the "."
10085 * (relative to current buffer directory) and empty path (relative to current
10086 * directory) notations.
10087 *
10088 * TODO: handle upward search (;) and path limiter (**N) notations by
10089 * expanding each into their equivalent path(s).
10090 */
10091 static void
10092expand_path_option(curdir, gap)
10093 char_u *curdir;
10094 garray_T *gap;
10095{
10096 char_u *path_option = *curbuf->b_p_path == NUL
10097 ? p_path : curbuf->b_p_path;
10098 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010099 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010100 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010101
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010102 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010103 return;
10104
10105 while (*path_option != NUL)
10106 {
10107 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10108
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010109 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010110 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010111 /* Relative to current buffer:
10112 * "/path/file" + "." -> "/path/"
10113 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010114 if (curbuf->b_ffname == NULL)
10115 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010116 p = gettail(curbuf->b_ffname);
10117 len = (int)(p - curbuf->b_ffname);
10118 if (len + (int)STRLEN(buf) >= MAXPATHL)
10119 continue;
10120 if (buf[1] == NUL)
10121 buf[len] = NUL;
10122 else
10123 STRMOVE(buf + len, buf + 2);
10124 mch_memmove(buf, curbuf->b_ffname, len);
10125 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010126 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010127 else if (buf[0] == NUL)
10128 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010129 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010130 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010131 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010132 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010133 else if (!mch_isFullName(buf))
10134 {
10135 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010136 len = (int)STRLEN(curdir);
10137 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010138 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010139 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010140 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010141 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010142 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010143 }
10144
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010145 if (ga_grow(gap, 1) == FAIL)
10146 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010147
10148# if defined(MSWIN) || defined(MSDOS)
10149 /* Avoid the path ending in a backslash, it fails when a comma is
10150 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010151 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010152 if (buf[len - 1] == '\\')
10153 buf[len - 1] = '/';
10154# endif
10155
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010156 p = vim_strsave(buf);
10157 if (p == NULL)
10158 break;
10159 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010160 }
10161
10162 vim_free(buf);
10163}
10164
10165/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010166 * Returns a pointer to the file or directory name in "fname" that matches the
10167 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010168 *
10169 * path: /foo/bar/baz
10170 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010171 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010172 */
10173 static char_u *
10174get_path_cutoff(fname, gap)
10175 char_u *fname;
10176 garray_T *gap;
10177{
10178 int i;
10179 int maxlen = 0;
10180 char_u **path_part = (char_u **)gap->ga_data;
10181 char_u *cutoff = NULL;
10182
10183 for (i = 0; i < gap->ga_len; i++)
10184 {
10185 int j = 0;
10186
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010187 while ((fname[j] == path_part[i][j]
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +020010188# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010189 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10190#endif
10191 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010192 j++;
10193 if (j > maxlen)
10194 {
10195 maxlen = j;
10196 cutoff = &fname[j];
10197 }
10198 }
10199
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010200 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010201 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010202 while (vim_ispathsep(*cutoff))
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010203 mb_ptr_adv(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010204
10205 return cutoff;
10206}
10207
10208/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010209 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10210 * that they are unique with respect to each other while conserving the part
10211 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010212 */
10213 static void
10214uniquefy_paths(gap, pattern)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010215 garray_T *gap;
10216 char_u *pattern;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010217{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010218 int i;
10219 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010220 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010221 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010222 char_u *pat;
10223 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010224 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010225 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010226 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010227 char_u **in_curdir = NULL;
10228 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010229
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010230 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010231 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010232
10233 /*
10234 * We need to prepend a '*' at the beginning of file_pattern so that the
10235 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010236 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010237 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010238 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010239 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010240 if (file_pattern == NULL)
10241 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010242 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010243 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010244 STRCAT(file_pattern, pattern);
10245 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10246 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010247 if (pat == NULL)
10248 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010249
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010250 regmatch.rm_ic = TRUE; /* always ignore case */
10251 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10252 vim_free(pat);
10253 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010254 return;
10255
Bram Moolenaar162bd912010-07-28 22:29:10 +020010256 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010257 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010258 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010259 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010260
10261 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010262 if (in_curdir == NULL)
10263 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010264
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010265 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010266 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010267 char_u *path = fnames[i];
10268 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010269 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010270 char_u *pathsep_p;
10271 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010272
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010273 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010274 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010275 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010276 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010277 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010278
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010279 /* Shorten the filename while maintaining its uniqueness */
10280 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010281
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010282 /* we start at the end of the path */
10283 pathsep_p = path + len - 1;
10284
10285 while (find_previous_pathsep(path, &pathsep_p))
10286 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10287 && is_unique(pathsep_p + 1, gap, i)
10288 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10289 {
10290 sort_again = TRUE;
10291 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10292 break;
10293 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010294
10295 if (mch_isFullName(path))
10296 {
10297 /*
10298 * Last resort: shorten relative to curdir if possible.
10299 * 'possible' means:
10300 * 1. It is under the current directory.
10301 * 2. The result is actually shorter than the original.
10302 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010303 * Before curdir After
10304 * /foo/bar/file.txt /foo/bar ./file.txt
10305 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10306 * /file.txt / /file.txt
10307 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010308 */
10309 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010310 if (short_name != NULL && short_name > path + 1
10311#if defined(MSWIN) || defined(MSDOS)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010312 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010313 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010314 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010315 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010316 && !vim_ispathsep(*short_name)
10317#endif
10318 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010319 {
10320 STRCPY(path, ".");
10321 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010322 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010323 }
10324 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010325 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010326 }
10327
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010328 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010329 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010330 {
10331 char_u *rel_path;
10332 char_u *path = in_curdir[i];
10333
10334 if (path == NULL)
10335 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010336
10337 /* If the {filename} is not unique, change it to ./{filename}.
10338 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010339 short_name = shorten_fname(path, curdir);
10340 if (short_name == NULL)
10341 short_name = path;
10342 if (is_unique(short_name, gap, i))
10343 {
10344 STRCPY(fnames[i], short_name);
10345 continue;
10346 }
10347
10348 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10349 if (rel_path == NULL)
10350 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010351 STRCPY(rel_path, ".");
10352 add_pathsep(rel_path);
10353 STRCAT(rel_path, short_name);
10354
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010355 vim_free(fnames[i]);
10356 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010357 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010358 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010359 }
10360
Bram Moolenaar162bd912010-07-28 22:29:10 +020010361theend:
10362 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010363 if (in_curdir != NULL)
10364 {
10365 for (i = 0; i < gap->ga_len; i++)
10366 vim_free(in_curdir[i]);
10367 vim_free(in_curdir);
10368 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010369 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010370 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010371
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010372 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010373 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010374}
10375
10376/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010377 * Calls globpath() with 'path' values for the given pattern and stores the
10378 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010379 * Returns the total number of matches.
10380 */
10381 static int
10382expand_in_path(gap, pattern, flags)
10383 garray_T *gap;
10384 char_u *pattern;
10385 int flags; /* EW_* flags */
10386{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010387 char_u *curdir;
10388 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010389 char_u *files = NULL;
10390 char_u *s; /* start */
10391 char_u *e; /* end */
10392 char_u *paths = NULL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010393
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010394 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010395 return 0;
10396 mch_dirname(curdir, MAXPATHL);
10397
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010398 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010399 expand_path_option(curdir, &path_ga);
10400 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010401 if (path_ga.ga_len == 0)
10402 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010403
10404 paths = ga_concat_strings(&path_ga);
10405 ga_clear_strings(&path_ga);
10406 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010407 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010408
Bram Moolenaar94950a92010-12-02 16:01:29 +010010409 files = globpath(paths, pattern, (flags & EW_ICASE) ? WILD_ICASE : 0);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010410 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010411 if (files == NULL)
10412 return 0;
10413
10414 /* Copy each path in files into gap */
10415 s = e = files;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010416 while (*s != NUL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010417 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010418 while (*e != '\n' && *e != NUL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010419 e++;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010420 if (*e == NUL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010421 {
10422 addfile(gap, s, flags);
10423 break;
10424 }
10425 else
10426 {
10427 /* *e is '\n' */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010428 *e = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010429 addfile(gap, s, flags);
10430 e++;
10431 s = e;
10432 }
10433 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010434 vim_free(files);
10435
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010436 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010437}
10438#endif
10439
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010440#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10441/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010442 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10443 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010444 */
10445 void
10446remove_duplicates(gap)
10447 garray_T *gap;
10448{
10449 int i;
10450 int j;
10451 char_u **fnames = (char_u **)gap->ga_data;
10452
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010453 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010454 for (i = gap->ga_len - 1; i > 0; --i)
10455 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10456 {
10457 vim_free(fnames[i]);
10458 for (j = i + 1; j < gap->ga_len; ++j)
10459 fnames[j - 1] = fnames[j];
10460 --gap->ga_len;
10461 }
10462}
10463#endif
10464
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010465static int has_env_var __ARGS((char_u *p));
10466
10467/*
10468 * Return TRUE if "p" contains what looks like an environment variable.
10469 * Allowing for escaping.
10470 */
10471 static int
10472has_env_var(p)
10473 char_u *p;
10474{
10475 for ( ; *p; mb_ptr_adv(p))
10476 {
10477 if (*p == '\\' && p[1] != NUL)
10478 ++p;
10479 else if (vim_strchr((char_u *)
10480#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
10481 "$%"
10482#else
10483 "$"
10484#endif
10485 , *p) != NULL)
10486 return TRUE;
10487 }
10488 return FALSE;
10489}
10490
10491#ifdef SPECIAL_WILDCHAR
10492static int has_special_wildchar __ARGS((char_u *p));
10493
10494/*
10495 * Return TRUE if "p" contains a special wildcard character.
10496 * Allowing for escaping.
10497 */
10498 static int
10499has_special_wildchar(p)
10500 char_u *p;
10501{
10502 for ( ; *p; mb_ptr_adv(p))
10503 {
10504 if (*p == '\\' && p[1] != NUL)
10505 ++p;
10506 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10507 return TRUE;
10508 }
10509 return FALSE;
10510}
10511#endif
10512
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513/*
10514 * Generic wildcard expansion code.
10515 *
10516 * Characters in "pat" that should not be expanded must be preceded with a
10517 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10518 *
10519 * Return FAIL when no single file was found. In this case "num_file" is not
10520 * set, and "file" may contain an error message.
10521 * Return OK when some files found. "num_file" is set to the number of
10522 * matches, "file" to the array of matches. Call FreeWild() later.
10523 */
10524 int
10525gen_expand_wildcards(num_pat, pat, num_file, file, flags)
10526 int num_pat; /* number of input patterns */
10527 char_u **pat; /* array of input patterns */
10528 int *num_file; /* resulting number of files */
10529 char_u ***file; /* array of resulting files */
10530 int flags; /* EW_* flags */
10531{
10532 int i;
10533 garray_T ga;
10534 char_u *p;
10535 static int recursive = FALSE;
10536 int add_pat;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010537#if defined(FEAT_SEARCHPATH)
10538 int did_expand_in_path = FALSE;
10539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540
10541 /*
10542 * expand_env() is called to expand things like "~user". If this fails,
10543 * it calls ExpandOne(), which brings us back here. In this case, always
10544 * call the machine specific expansion function, if possible. Otherwise,
10545 * return FAIL.
10546 */
10547 if (recursive)
10548#ifdef SPECIAL_WILDCHAR
10549 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10550#else
10551 return FAIL;
10552#endif
10553
10554#ifdef SPECIAL_WILDCHAR
10555 /*
10556 * If there are any special wildcard characters which we cannot handle
10557 * here, call machine specific function for all the expansion. This
10558 * avoids starting the shell for each argument separately.
10559 * For `=expr` do use the internal function.
10560 */
10561 for (i = 0; i < num_pat; i++)
10562 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010563 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564# ifdef VIM_BACKTICK
10565 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10566# endif
10567 )
10568 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10569 }
10570#endif
10571
10572 recursive = TRUE;
10573
10574 /*
10575 * The matching file names are stored in a growarray. Init it empty.
10576 */
10577 ga_init2(&ga, (int)sizeof(char_u *), 30);
10578
10579 for (i = 0; i < num_pat; ++i)
10580 {
10581 add_pat = -1;
10582 p = pat[i];
10583
10584#ifdef VIM_BACKTICK
10585 if (vim_backtick(p))
10586 add_pat = expand_backtick(&ga, p, flags);
10587 else
10588#endif
10589 {
10590 /*
10591 * First expand environment variables, "~/" and "~user/".
10592 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010593 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010595 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596 if (p == NULL)
10597 p = pat[i];
10598#ifdef UNIX
10599 /*
10600 * On Unix, if expand_env() can't expand an environment
10601 * variable, use the shell to do that. Discard previously
10602 * found file names and start all over again.
10603 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010604 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605 {
10606 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010607 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608 i = mch_expand_wildcards(num_pat, pat, num_file, file,
10609 flags);
10610 recursive = FALSE;
10611 return i;
10612 }
10613#endif
10614 }
10615
10616 /*
10617 * If there are wildcards: Expand file names and add each match to
10618 * the list. If there is no match, and EW_NOTFOUND is given, add
10619 * the pattern.
10620 * If there are no wildcards: Add the file name if it exists or
10621 * when EW_NOTFOUND is given.
10622 */
10623 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010624 {
10625#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010626 if ((flags & EW_PATH)
10627 && !mch_isFullName(p)
10628 && !(p[0] == '.'
10629 && (vim_ispathsep(p[1])
10630 || (p[1] == '.' && vim_ispathsep(p[2]))))
10631 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010632 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010633 /* :find completion where 'path' is used.
10634 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010635 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010636 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010637 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010638 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010639 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010640 else
10641#endif
10642 add_pat = mch_expandpath(&ga, p, flags);
10643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644 }
10645
10646 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
10647 {
10648 char_u *t = backslash_halve_save(p);
10649
10650#if defined(MACOS_CLASSIC)
10651 slash_to_colon(t);
10652#endif
10653 /* When EW_NOTFOUND is used, always add files and dirs. Makes
10654 * "vim c:/" work. */
10655 if (flags & EW_NOTFOUND)
10656 addfile(&ga, t, flags | EW_DIR | EW_FILE);
10657 else if (mch_getperm(t) >= 0)
10658 addfile(&ga, t, flags);
10659 vim_free(t);
10660 }
10661
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010662#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010663 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010664 uniquefy_paths(&ga, p);
10665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 if (p != pat[i])
10667 vim_free(p);
10668 }
10669
10670 *num_file = ga.ga_len;
10671 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
10672
10673 recursive = FALSE;
10674
10675 return (ga.ga_data != NULL) ? OK : FAIL;
10676}
10677
10678# ifdef VIM_BACKTICK
10679
10680/*
10681 * Return TRUE if we can expand this backtick thing here.
10682 */
10683 static int
10684vim_backtick(p)
10685 char_u *p;
10686{
10687 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
10688}
10689
10690/*
10691 * Expand an item in `backticks` by executing it as a command.
10692 * Currently only works when pat[] starts and ends with a `.
10693 * Returns number of file names found.
10694 */
10695 static int
10696expand_backtick(gap, pat, flags)
10697 garray_T *gap;
10698 char_u *pat;
10699 int flags; /* EW_* flags */
10700{
10701 char_u *p;
10702 char_u *cmd;
10703 char_u *buffer;
10704 int cnt = 0;
10705 int i;
10706
10707 /* Create the command: lop off the backticks. */
10708 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
10709 if (cmd == NULL)
10710 return 0;
10711
10712#ifdef FEAT_EVAL
10713 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000010714 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715 else
10716#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010717 buffer = get_cmd_output(cmd, NULL,
10718 (flags & EW_SILENT) ? SHELL_SILENT : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719 vim_free(cmd);
10720 if (buffer == NULL)
10721 return 0;
10722
10723 cmd = buffer;
10724 while (*cmd != NUL)
10725 {
10726 cmd = skipwhite(cmd); /* skip over white space */
10727 p = cmd;
10728 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
10729 ++p;
10730 /* add an entry if it is not empty */
10731 if (p > cmd)
10732 {
10733 i = *p;
10734 *p = NUL;
10735 addfile(gap, cmd, flags);
10736 *p = i;
10737 ++cnt;
10738 }
10739 cmd = p;
10740 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
10741 ++cmd;
10742 }
10743
10744 vim_free(buffer);
10745 return cnt;
10746}
10747# endif /* VIM_BACKTICK */
10748
10749/*
10750 * Add a file to a file list. Accepted flags:
10751 * EW_DIR add directories
10752 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010753 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754 * EW_NOTFOUND add even when it doesn't exist
10755 * EW_ADDSLASH add slash after directory name
10756 */
10757 void
10758addfile(gap, f, flags)
10759 garray_T *gap;
10760 char_u *f; /* filename */
10761 int flags;
10762{
10763 char_u *p;
10764 int isdir;
10765
10766 /* if the file/dir doesn't exist, may not add it */
10767 if (!(flags & EW_NOTFOUND) && mch_getperm(f) < 0)
10768 return;
10769
10770#ifdef FNAME_ILLEGAL
10771 /* if the file/dir contains illegal characters, don't add it */
10772 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
10773 return;
10774#endif
10775
10776 isdir = mch_isdir(f);
10777 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
10778 return;
10779
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010780 /* If the file isn't executable, may not add it. Do accept directories. */
10781 if (!isdir && (flags & EW_EXEC) && !mch_can_exe(f))
10782 return;
10783
Bram Moolenaar071d4272004-06-13 20:20:40 +000010784 /* Make room for another item in the file list. */
10785 if (ga_grow(gap, 1) == FAIL)
10786 return;
10787
10788 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
10789 if (p == NULL)
10790 return;
10791
10792 STRCPY(p, f);
10793#ifdef BACKSLASH_IN_FILENAME
10794 slash_adjust(p);
10795#endif
10796 /*
10797 * Append a slash or backslash after directory names if none is present.
10798 */
10799#ifndef DONT_ADD_PATHSEP_TO_DIR
10800 if (isdir && (flags & EW_ADDSLASH))
10801 add_pathsep(p);
10802#endif
10803 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804}
10805#endif /* !NO_EXPANDPATH */
10806
10807#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
10808
10809#ifndef SEEK_SET
10810# define SEEK_SET 0
10811#endif
10812#ifndef SEEK_END
10813# define SEEK_END 2
10814#endif
10815
10816/*
10817 * Get the stdout of an external command.
10818 * Returns an allocated string, or NULL for error.
10819 */
10820 char_u *
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010821get_cmd_output(cmd, infile, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822 char_u *cmd;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010823 char_u *infile; /* optional input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824 int flags; /* can be SHELL_SILENT */
10825{
10826 char_u *tempname;
10827 char_u *command;
10828 char_u *buffer = NULL;
10829 int len;
10830 int i = 0;
10831 FILE *fd;
10832
10833 if (check_restricted() || check_secure())
10834 return NULL;
10835
10836 /* get a name for the temp file */
10837 if ((tempname = vim_tempname('o')) == NULL)
10838 {
10839 EMSG(_(e_notmp));
10840 return NULL;
10841 }
10842
10843 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010844 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 if (command == NULL)
10846 goto done;
10847
10848 /*
10849 * Call the shell to execute the command (errors are ignored).
10850 * Don't check timestamps here.
10851 */
10852 ++no_check_timestamps;
10853 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
10854 --no_check_timestamps;
10855
10856 vim_free(command);
10857
10858 /*
10859 * read the names from the file into memory
10860 */
10861# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000010862 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863 fd = mch_fopen((char *)tempname, "r");
10864# else
10865 fd = mch_fopen((char *)tempname, READBIN);
10866# endif
10867
10868 if (fd == NULL)
10869 {
10870 EMSG2(_(e_notopen), tempname);
10871 goto done;
10872 }
10873
10874 fseek(fd, 0L, SEEK_END);
10875 len = ftell(fd); /* get size of temp file */
10876 fseek(fd, 0L, SEEK_SET);
10877
10878 buffer = alloc(len + 1);
10879 if (buffer != NULL)
10880 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
10881 fclose(fd);
10882 mch_remove(tempname);
10883 if (buffer == NULL)
10884 goto done;
10885#ifdef VMS
10886 len = i; /* VMS doesn't give us what we asked for... */
10887#endif
10888 if (i != len)
10889 {
10890 EMSG2(_(e_notread), tempname);
10891 vim_free(buffer);
10892 buffer = NULL;
10893 }
10894 else
Bram Moolenaarfb332a22013-08-03 14:10:50 +020010895 {
10896 /* Change NUL into SOH, otherwise the string is truncated. */
10897 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020010898 if (buffer[i] == NUL)
10899 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020010900
Bram Moolenaar162bd912010-07-28 22:29:10 +020010901 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020010902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903
10904done:
10905 vim_free(tempname);
10906 return buffer;
10907}
10908#endif
10909
10910/*
10911 * Free the list of files returned by expand_wildcards() or other expansion
10912 * functions.
10913 */
10914 void
10915FreeWild(count, files)
10916 int count;
10917 char_u **files;
10918{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000010919 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920 return;
10921#if defined(__EMX__) && defined(__ALWAYS_HAS_TRAILING_NULL_POINTER) /* XXX */
10922 /*
10923 * Is this still OK for when other functions than expand_wildcards() have
10924 * been used???
10925 */
10926 _fnexplodefree((char **)files);
10927#else
10928 while (count--)
10929 vim_free(files[count]);
10930 vim_free(files);
10931#endif
10932}
10933
10934/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020010935 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936 * Don't do this when still processing a command or a mapping.
10937 * Don't do this when inside a ":normal" command.
10938 */
10939 int
10940goto_im()
10941{
10942 return (p_im && stuff_empty() && typebuf_typed());
10943}