blob: 95d18a571fb4eb80d8ac4df9d326431b3425fdc7 [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 Moolenaar75a8d742014-05-07 15:10:21 +02001408 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 Moolenaarfd859c92014-05-13 12:44:24 +02003128 /* relative numbering may require updating more */
3129 if (wp->w_p_rnu)
3130 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 }
3132 }
3133
3134 /* Call update_screen() later, which checks out what needs to be redrawn,
3135 * since it notices b_mod_set and then uses b_mod_*. */
3136 if (must_redraw < VALID)
3137 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003138
3139#ifdef FEAT_AUTOCMD
3140 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003141 if (lnum <= curwin->w_cursor.lnum
3142 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003143 last_cursormoved.lnum = 0;
3144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145}
3146
3147/*
3148 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3149 */
3150 void
3151unchanged(buf, ff)
3152 buf_T *buf;
3153 int ff; /* also reset 'fileformat' */
3154{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003155 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 {
3157 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003158 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 if (ff)
3160 save_file_ff(buf);
3161#ifdef FEAT_WINDOWS
3162 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003163 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164#endif
3165#ifdef FEAT_TITLE
3166 need_maketitle = TRUE; /* set window title later */
3167#endif
3168 }
3169 ++buf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170#ifdef FEAT_NETBEANS_INTG
3171 netbeans_unmodified(buf);
3172#endif
3173}
3174
3175#if defined(FEAT_WINDOWS) || defined(PROTO)
3176/*
3177 * check_status: called when the status bars for the buffer 'buf'
3178 * need to be updated
3179 */
3180 void
3181check_status(buf)
3182 buf_T *buf;
3183{
3184 win_T *wp;
3185
3186 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3187 if (wp->w_buffer == buf && wp->w_status_height)
3188 {
3189 wp->w_redr_status = TRUE;
3190 if (must_redraw < VALID)
3191 must_redraw = VALID;
3192 }
3193}
3194#endif
3195
3196/*
3197 * If the file is readonly, give a warning message with the first change.
3198 * Don't do this for autocommands.
3199 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003200 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003202 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 */
3204 void
3205change_warning(col)
3206 int col; /* column for message; non-zero when in insert
3207 mode and 'showmode' is on */
3208{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003209 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3210
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 if (curbuf->b_did_warn == FALSE
3212 && curbufIsChanged() == 0
3213#ifdef FEAT_AUTOCMD
3214 && !autocmd_busy
3215#endif
3216 && curbuf->b_p_ro)
3217 {
3218#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003219 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003221 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 if (!curbuf->b_p_ro)
3223 return;
3224#endif
3225 /*
3226 * Do what msg() does, but with a column offset if the warning should
3227 * be after the mode message.
3228 */
3229 msg_start();
3230 if (msg_row == Rows - 1)
3231 msg_col = col;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003232 msg_source(hl_attr(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003233 MSG_PUTS_ATTR(_(w_readonly), hl_attr(HLF_W) | MSG_HIST);
3234#ifdef FEAT_EVAL
3235 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 msg_clr_eos();
3238 (void)msg_end();
3239 if (msg_silent == 0 && !silent_mode)
3240 {
3241 out_flush();
3242 ui_delay(1000L, TRUE); /* give the user time to think about it */
3243 }
3244 curbuf->b_did_warn = TRUE;
3245 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3246 if (msg_row < Rows - 1)
3247 showmode();
3248 }
3249}
3250
3251/*
3252 * Ask for a reply from the user, a 'y' or a 'n'.
3253 * No other characters are accepted, the message is repeated until a valid
3254 * reply is entered or CTRL-C is hit.
3255 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3256 * from any buffers but directly from the user.
3257 *
3258 * return the 'y' or 'n'
3259 */
3260 int
3261ask_yesno(str, direct)
3262 char_u *str;
3263 int direct;
3264{
3265 int r = ' ';
3266 int save_State = State;
3267
3268 if (exiting) /* put terminal in raw mode for this question */
3269 settmode(TMODE_RAW);
3270 ++no_wait_return;
3271#ifdef USE_ON_FLY_SCROLL
3272 dont_scroll = TRUE; /* disallow scrolling here */
3273#endif
3274 State = CONFIRM; /* mouse behaves like with :confirm */
3275#ifdef FEAT_MOUSE
3276 setmouse(); /* disables mouse for xterm */
3277#endif
3278 ++no_mapping;
3279 ++allow_keys; /* no mapping here, but recognize keys */
3280
3281 while (r != 'y' && r != 'n')
3282 {
3283 /* same highlighting as for wait_return */
3284 smsg_attr(hl_attr(HLF_R), (char_u *)"%s (y/n)?", str);
3285 if (direct)
3286 r = get_keystroke();
3287 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003288 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 if (r == Ctrl_C || r == ESC)
3290 r = 'n';
3291 msg_putchar(r); /* show what you typed */
3292 out_flush();
3293 }
3294 --no_wait_return;
3295 State = save_State;
3296#ifdef FEAT_MOUSE
3297 setmouse();
3298#endif
3299 --no_mapping;
3300 --allow_keys;
3301
3302 return r;
3303}
3304
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003305#if defined(FEAT_MOUSE) || defined(PROTO)
3306/*
3307 * Return TRUE if "c" is a mouse key.
3308 */
3309 int
3310is_mouse_key(c)
3311 int c;
3312{
3313 return c == K_LEFTMOUSE
3314 || c == K_LEFTMOUSE_NM
3315 || c == K_LEFTDRAG
3316 || c == K_LEFTRELEASE
3317 || c == K_LEFTRELEASE_NM
3318 || c == K_MIDDLEMOUSE
3319 || c == K_MIDDLEDRAG
3320 || c == K_MIDDLERELEASE
3321 || c == K_RIGHTMOUSE
3322 || c == K_RIGHTDRAG
3323 || c == K_RIGHTRELEASE
3324 || c == K_MOUSEDOWN
3325 || c == K_MOUSEUP
3326 || c == K_MOUSELEFT
3327 || c == K_MOUSERIGHT
3328 || c == K_X1MOUSE
3329 || c == K_X1DRAG
3330 || c == K_X1RELEASE
3331 || c == K_X2MOUSE
3332 || c == K_X2DRAG
3333 || c == K_X2RELEASE;
3334}
3335#endif
3336
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337/*
3338 * Get a key stroke directly from the user.
3339 * Ignores mouse clicks and scrollbar events, except a click for the left
3340 * button (used at the more prompt).
3341 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3342 * Disadvantage: typeahead is ignored.
3343 * Translates the interrupt character for unix to ESC.
3344 */
3345 int
3346get_keystroke()
3347{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003348 char_u *buf = NULL;
3349 int buflen = 150;
3350 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 int len = 0;
3352 int n;
3353 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003354 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355
3356 mapped_ctrl_c = FALSE; /* mappings are not used here */
3357 for (;;)
3358 {
3359 cursor_on();
3360 out_flush();
3361
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003362 /* Leave some room for check_termcode() to insert a key code into (max
3363 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3364 * bytes. */
3365 maxlen = (buflen - 6 - len) / 3;
3366 if (buf == NULL)
3367 buf = alloc(buflen);
3368 else if (maxlen < 10)
3369 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003370 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003371 * escape sequence. */
3372 buflen += 100;
3373 buf = vim_realloc(buf, buflen);
3374 maxlen = (buflen - 6 - len) / 3;
3375 }
3376 if (buf == NULL)
3377 {
3378 do_outofmem_msg((long_u)buflen);
3379 return ESC; /* panic! */
3380 }
3381
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003383 * terminal code to complete. */
3384 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 if (n > 0)
3386 {
3387 /* Replace zero and CSI by a special key code. */
3388 n = fix_input_buffer(buf + len, n, FALSE);
3389 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003390 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003392 else if (len > 0)
3393 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394
Bram Moolenaar4395a712006-09-05 18:57:57 +00003395 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003396 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003397 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003399
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003400 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003401 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003402 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003403 {
3404 /* Redrawing was postponed, do it now. */
3405 update_screen(0);
3406 setcursor(); /* put cursor back where it belongs */
3407 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003408 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003409 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003410 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003412 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 continue;
3414
3415 /* Handle modifier and/or special key code. */
3416 n = buf[0];
3417 if (n == K_SPECIAL)
3418 {
3419 n = TO_SPECIAL(buf[1], buf[2]);
3420 if (buf[1] == KS_MODIFIER
3421 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003422#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003423 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003424#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003425#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 || n == K_VER_SCROLLBAR
3427 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428#endif
3429 )
3430 {
3431 if (buf[1] == KS_MODIFIER)
3432 mod_mask = buf[2];
3433 len -= 3;
3434 if (len > 0)
3435 mch_memmove(buf, buf + 3, (size_t)len);
3436 continue;
3437 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003438 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 }
3440#ifdef FEAT_MBYTE
3441 if (has_mbyte)
3442 {
3443 if (MB_BYTE2LEN(n) > len)
3444 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003445 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 n = (*mb_ptr2char)(buf);
3447 }
3448#endif
3449#ifdef UNIX
3450 if (n == intr_char)
3451 n = ESC;
3452#endif
3453 break;
3454 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003455 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456
3457 mapped_ctrl_c = save_mapped_ctrl_c;
3458 return n;
3459}
3460
3461/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003462 * Get a number from the user.
3463 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 */
3465 int
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003466get_number(colon, mouse_used)
3467 int colon; /* allow colon to abort */
3468 int *mouse_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469{
3470 int n = 0;
3471 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003472 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003474 if (mouse_used != NULL)
3475 *mouse_used = FALSE;
3476
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 /* When not printing messages, the user won't know what to type, return a
3478 * zero (as if CR was hit). */
3479 if (msg_silent != 0)
3480 return 0;
3481
3482#ifdef USE_ON_FLY_SCROLL
3483 dont_scroll = TRUE; /* disallow scrolling here */
3484#endif
3485 ++no_mapping;
3486 ++allow_keys; /* no mapping here, but recognize keys */
3487 for (;;)
3488 {
3489 windgoto(msg_row, msg_col);
3490 c = safe_vgetc();
3491 if (VIM_ISDIGIT(c))
3492 {
3493 n = n * 10 + c - '0';
3494 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003495 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 }
3497 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3498 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003499 if (typed > 0)
3500 {
3501 MSG_PUTS("\b \b");
3502 --typed;
3503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003506#ifdef FEAT_MOUSE
3507 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3508 {
3509 *mouse_used = TRUE;
3510 n = mouse_row + 1;
3511 break;
3512 }
3513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 else if (n == 0 && c == ':' && colon)
3515 {
3516 stuffcharReadbuff(':');
3517 if (!exmode_active)
3518 cmdline_row = msg_row;
3519 skip_redraw = TRUE; /* skip redraw once */
3520 do_redraw = FALSE;
3521 break;
3522 }
3523 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3524 break;
3525 }
3526 --no_mapping;
3527 --allow_keys;
3528 return n;
3529}
3530
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003531/*
3532 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003533 * When "mouse_used" is not NULL allow using the mouse and in that case return
3534 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003535 */
3536 int
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003537prompt_for_number(mouse_used)
3538 int *mouse_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003539{
3540 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003541 int save_cmdline_row;
3542 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003543
3544 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003545 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003546 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003547 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003548 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003549
Bram Moolenaar203335e2006-09-03 14:35:42 +00003550 /* Set the state such that text can be selected/copied/pasted and we still
3551 * get mouse events. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003552 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003553 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003554 save_State = State;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003555 State = CMDLINE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003556
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003557 i = get_number(TRUE, mouse_used);
3558 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003559 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003560 /* don't call wait_return() now */
3561 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003562 cmdline_row = msg_row - 1;
3563 need_wait_return = FALSE;
3564 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003565 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003566 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003567 else
3568 cmdline_row = save_cmdline_row;
3569 State = save_State;
3570
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003571 return i;
3572}
3573
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 void
3575msgmore(n)
3576 long n;
3577{
3578 long pn;
3579
3580 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3582 return;
3583
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003584 /* We don't want to overwrite another important message, but do overwrite
3585 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3586 * then "put" reports the last action. */
3587 if (keep_msg != NULL && !keep_msg_more)
3588 return;
3589
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 if (n > 0)
3591 pn = n;
3592 else
3593 pn = -n;
3594
3595 if (pn > p_report)
3596 {
3597 if (pn == 1)
3598 {
3599 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003600 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3601 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003603 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3604 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 }
3606 else
3607 {
3608 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003609 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3610 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003612 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3613 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 }
3615 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003616 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 if (msg(msg_buf))
3618 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003619 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003620 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 }
3622 }
3623}
3624
3625/*
3626 * flush map and typeahead buffers and give a warning for an error
3627 */
3628 void
3629beep_flush()
3630{
3631 if (emsg_silent == 0)
3632 {
3633 flush_buffers(FALSE);
3634 vim_beep();
3635 }
3636}
3637
3638/*
3639 * give a warning for an error
3640 */
3641 void
3642vim_beep()
3643{
3644 if (emsg_silent == 0)
3645 {
3646 if (p_vb
3647#ifdef FEAT_GUI
3648 /* While the GUI is starting up the termcap is set for the GUI
3649 * but the output still goes to a terminal. */
3650 && !(gui.in_use && gui.starting)
3651#endif
3652 )
3653 {
3654 out_str(T_VB);
3655 }
3656 else
3657 {
3658#ifdef MSDOS
3659 /*
3660 * The number of beeps outputted is reduced to avoid having to wait
3661 * for all the beeps to finish. This is only a problem on systems
3662 * where the beeps don't overlap.
3663 */
3664 if (beep_count == 0 || beep_count == 10)
3665 {
3666 out_char(BELL);
3667 beep_count = 1;
3668 }
3669 else
3670 ++beep_count;
3671#else
3672 out_char(BELL);
3673#endif
3674 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003675
3676 /* When 'verbose' is set and we are sourcing a script or executing a
3677 * function give the user a hint where the beep comes from. */
3678 if (vim_strchr(p_debug, 'e') != NULL)
3679 {
3680 msg_source(hl_attr(HLF_W));
3681 msg_attr((char_u *)_("Beep!"), hl_attr(HLF_W));
3682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 }
3684}
3685
3686/*
3687 * To get the "real" home directory:
3688 * - get value of $HOME
3689 * For Unix:
3690 * - go to that directory
3691 * - do mch_dirname() to get the real name of that directory.
3692 * This also works with mounts and links.
3693 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3694 */
3695static char_u *homedir = NULL;
3696
3697 void
3698init_homedir()
3699{
3700 char_u *var;
3701
Bram Moolenaar05159a02005-02-26 23:04:13 +00003702 /* In case we are called a second time (when 'encoding' changes). */
3703 vim_free(homedir);
3704 homedir = NULL;
3705
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706#ifdef VMS
3707 var = mch_getenv((char_u *)"SYS$LOGIN");
3708#else
3709 var = mch_getenv((char_u *)"HOME");
3710#endif
3711
3712 if (var != NULL && *var == NUL) /* empty is same as not set */
3713 var = NULL;
3714
3715#ifdef WIN3264
3716 /*
3717 * Weird but true: $HOME may contain an indirect reference to another
3718 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3719 * when $HOME is being set.
3720 */
3721 if (var != NULL && *var == '%')
3722 {
3723 char_u *p;
3724 char_u *exp;
3725
3726 p = vim_strchr(var + 1, '%');
3727 if (p != NULL)
3728 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003729 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 exp = mch_getenv(NameBuff);
3731 if (exp != NULL && *exp != NUL
3732 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3733 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003734 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 var = NameBuff;
3736 /* Also set $HOME, it's needed for _viminfo. */
3737 vim_setenv((char_u *)"HOME", NameBuff);
3738 }
3739 }
3740 }
3741
3742 /*
3743 * Typically, $HOME is not defined on Windows, unless the user has
3744 * specifically defined it for Vim's sake. However, on Windows NT
3745 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3746 * each user. Try constructing $HOME from these.
3747 */
3748 if (var == NULL)
3749 {
3750 char_u *homedrive, *homepath;
3751
3752 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3753 homepath = mch_getenv((char_u *)"HOMEPATH");
Bram Moolenaar6f977012010-01-06 17:53:38 +01003754 if (homepath == NULL || *homepath == NUL)
3755 homepath = "\\";
3756 if (homedrive != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3758 {
3759 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3760 if (NameBuff[0] != NUL)
3761 {
3762 var = NameBuff;
3763 /* Also set $HOME, it's needed for _viminfo. */
3764 vim_setenv((char_u *)"HOME", NameBuff);
3765 }
3766 }
3767 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003768
3769# if defined(FEAT_MBYTE)
3770 if (enc_utf8 && var != NULL)
3771 {
3772 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003773 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003774
3775 /* Convert from active codepage to UTF-8. Other conversions are
3776 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003777 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003778 if (pp != NULL)
3779 {
3780 homedir = pp;
3781 return;
3782 }
3783 }
3784# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785#endif
3786
3787#if defined(OS2) || defined(MSDOS) || defined(MSWIN)
3788 /*
3789 * Default home dir is C:/
3790 * Best assumption we can make in such a situation.
3791 */
3792 if (var == NULL)
3793 var = "C:/";
3794#endif
3795 if (var != NULL)
3796 {
3797#ifdef UNIX
3798 /*
3799 * Change to the directory and get the actual path. This resolves
3800 * links. Don't do it when we can't return.
3801 */
3802 if (mch_dirname(NameBuff, MAXPATHL) == OK
3803 && mch_chdir((char *)NameBuff) == 0)
3804 {
3805 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3806 var = IObuff;
3807 if (mch_chdir((char *)NameBuff) != 0)
3808 EMSG(_(e_prev_dir));
3809 }
3810#endif
3811 homedir = vim_strsave(var);
3812 }
3813}
3814
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003815#if defined(EXITFREE) || defined(PROTO)
3816 void
3817free_homedir()
3818{
3819 vim_free(homedir);
3820}
Bram Moolenaar24305862012-08-15 14:05:05 +02003821
3822# ifdef FEAT_CMDL_COMPL
3823 void
3824free_users()
3825{
3826 ga_clear_strings(&ga_users);
3827}
3828# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003829#endif
3830
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003832 * Call expand_env() and store the result in an allocated string.
3833 * This is not very memory efficient, this expects the result to be freed
3834 * again soon.
3835 */
3836 char_u *
3837expand_env_save(src)
3838 char_u *src;
3839{
3840 return expand_env_save_opt(src, FALSE);
3841}
3842
3843/*
3844 * Idem, but when "one" is TRUE handle the string as one file name, only
3845 * expand "~" at the start.
3846 */
3847 char_u *
3848expand_env_save_opt(src, one)
3849 char_u *src;
3850 int one;
3851{
3852 char_u *p;
3853
3854 p = alloc(MAXPATHL);
3855 if (p != NULL)
3856 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3857 return p;
3858}
3859
3860/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 * Expand environment variable with path name.
3862 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003863 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 * If anything fails no expansion is done and dst equals src.
3865 */
3866 void
3867expand_env(src, dst, dstlen)
3868 char_u *src; /* input string e.g. "$HOME/vim.hlp" */
3869 char_u *dst; /* where to put the result */
3870 int dstlen; /* maximum length of the result */
3871{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003872 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873}
3874
3875 void
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003876expand_env_esc(srcp, dst, dstlen, esc, one, startstr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003877 char_u *srcp; /* input string e.g. "$HOME/vim.hlp" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 char_u *dst; /* where to put the result */
3879 int dstlen; /* maximum length of the result */
3880 int esc; /* escape spaces in expanded variables */
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003881 int one; /* "srcp" is one file name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003882 char_u *startstr; /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003884 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 char_u *tail;
3886 int c;
3887 char_u *var;
3888 int copy_char;
3889 int mustfree; /* var was allocated, need to free it later */
3890 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003891 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003893 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003894 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003895
3896 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 --dstlen; /* leave one char space for "\," */
3898 while (*src && dstlen > 0)
3899 {
3900 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003901 if ((*src == '$'
3902#ifdef VMS
3903 && at_start
3904#endif
3905 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3907 || *src == '%'
3908#endif
3909 || (*src == '~' && at_start))
3910 {
3911 mustfree = FALSE;
3912
3913 /*
3914 * The variable name is copied into dst temporarily, because it may
3915 * be a string in read-only memory and a NUL needs to be appended.
3916 */
3917 if (*src != '~') /* environment var */
3918 {
3919 tail = src + 1;
3920 var = dst;
3921 c = dstlen - 1;
3922
3923#ifdef UNIX
3924 /* Unix has ${var-name} type environment vars */
3925 if (*tail == '{' && !vim_isIDc('{'))
3926 {
3927 tail++; /* ignore '{' */
3928 while (c-- > 0 && *tail && *tail != '}')
3929 *var++ = *tail++;
3930 }
3931 else
3932#endif
3933 {
3934 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
3935#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3936 || (*src == '%' && *tail != '%')
3937#endif
3938 ))
3939 {
3940#ifdef OS2 /* env vars only in uppercase */
3941 *var++ = TOUPPER_LOC(*tail);
3942 tail++; /* toupper() may be a macro! */
3943#else
3944 *var++ = *tail++;
3945#endif
3946 }
3947 }
3948
3949#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX)
3950# ifdef UNIX
3951 if (src[1] == '{' && *tail != '}')
3952# else
3953 if (*src == '%' && *tail != '%')
3954# endif
3955 var = NULL;
3956 else
3957 {
3958# ifdef UNIX
3959 if (src[1] == '{')
3960# else
3961 if (*src == '%')
3962#endif
3963 ++tail;
3964#endif
3965 *var = NUL;
3966 var = vim_getenv(dst, &mustfree);
3967#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX)
3968 }
3969#endif
3970 }
3971 /* home directory */
3972 else if ( src[1] == NUL
3973 || vim_ispathsep(src[1])
3974 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
3975 {
3976 var = homedir;
3977 tail = src + 1;
3978 }
3979 else /* user directory */
3980 {
3981#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
3982 /*
3983 * Copy ~user to dst[], so we can put a NUL after it.
3984 */
3985 tail = src;
3986 var = dst;
3987 c = dstlen - 1;
3988 while ( c-- > 0
3989 && *tail
3990 && vim_isfilec(*tail)
3991 && !vim_ispathsep(*tail))
3992 *var++ = *tail++;
3993 *var = NUL;
3994# ifdef UNIX
3995 /*
3996 * If the system supports getpwnam(), use it.
3997 * Otherwise, or if getpwnam() fails, the shell is used to
3998 * expand ~user. This is slower and may fail if the shell
3999 * does not support ~user (old versions of /bin/sh).
4000 */
4001# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
4002 {
4003 struct passwd *pw;
4004
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004005 /* Note: memory allocated by getpwnam() is never freed.
4006 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 pw = getpwnam((char *)dst + 1);
4008 if (pw != NULL)
4009 var = (char_u *)pw->pw_dir;
4010 else
4011 var = NULL;
4012 }
4013 if (var == NULL)
4014# endif
4015 {
4016 expand_T xpc;
4017
4018 ExpandInit(&xpc);
4019 xpc.xp_context = EXPAND_FILES;
4020 var = ExpandOne(&xpc, dst, NULL,
4021 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 mustfree = TRUE;
4023 }
4024
4025# else /* !UNIX, thus VMS */
4026 /*
4027 * USER_HOME is a comma-separated list of
4028 * directories to search for the user account in.
4029 */
4030 {
4031 char_u test[MAXPATHL], paths[MAXPATHL];
4032 char_u *path, *next_path, *ptr;
4033 struct stat st;
4034
4035 STRCPY(paths, USER_HOME);
4036 next_path = paths;
4037 while (*next_path)
4038 {
4039 for (path = next_path; *next_path && *next_path != ',';
4040 next_path++);
4041 if (*next_path)
4042 *next_path++ = NUL;
4043 STRCPY(test, path);
4044 STRCAT(test, "/");
4045 STRCAT(test, dst + 1);
4046 if (mch_stat(test, &st) == 0)
4047 {
4048 var = alloc(STRLEN(test) + 1);
4049 STRCPY(var, test);
4050 mustfree = TRUE;
4051 break;
4052 }
4053 }
4054 }
4055# endif /* UNIX */
4056#else
4057 /* cannot expand user's home directory, so don't try */
4058 var = NULL;
4059 tail = (char_u *)""; /* for gcc */
4060#endif /* UNIX || VMS */
4061 }
4062
4063#ifdef BACKSLASH_IN_FILENAME
4064 /* If 'shellslash' is set change backslashes to forward slashes.
4065 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4066 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4067 {
4068 char_u *p = vim_strsave(var);
4069
4070 if (p != NULL)
4071 {
4072 if (mustfree)
4073 vim_free(var);
4074 var = p;
4075 mustfree = TRUE;
4076 forward_slash(var);
4077 }
4078 }
4079#endif
4080
4081 /* If "var" contains white space, escape it with a backslash.
4082 * Required for ":e ~/tt" when $HOME includes a space. */
4083 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4084 {
4085 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4086
4087 if (p != NULL)
4088 {
4089 if (mustfree)
4090 vim_free(var);
4091 var = p;
4092 mustfree = TRUE;
4093 }
4094 }
4095
4096 if (var != NULL && *var != NUL
4097 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4098 {
4099 STRCPY(dst, var);
4100 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004101 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 /* if var[] ends in a path separator and tail[] starts
4103 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004104 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4106 && dst[-1] != ':'
4107#endif
4108 && vim_ispathsep(*tail))
4109 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004110 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 src = tail;
4112 copy_char = FALSE;
4113 }
4114 if (mustfree)
4115 vim_free(var);
4116 }
4117
4118 if (copy_char) /* copy at least one char */
4119 {
4120 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004121 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004122 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4123 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 */
4125 at_start = FALSE;
4126 if (src[0] == '\\' && src[1] != NUL)
4127 {
4128 *dst++ = *src++;
4129 --dstlen;
4130 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004131 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 at_start = TRUE;
4133 *dst++ = *src++;
4134 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004135
4136 if (startstr != NULL && src - startstr_len >= srcp
4137 && STRNCMP(src - startstr_len, startstr, startstr_len) == 0)
4138 at_start = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 }
4140 }
4141 *dst = NUL;
4142}
4143
4144/*
4145 * Vim's version of getenv().
4146 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004147 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004148 * "mustfree" is set to TRUE when returned is allocated, it must be
4149 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 */
4151 char_u *
4152vim_getenv(name, mustfree)
4153 char_u *name;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004154 int *mustfree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155{
4156 char_u *p;
4157 char_u *pend;
4158 int vimruntime;
4159
4160#if defined(OS2) || defined(MSDOS) || defined(MSWIN)
4161 /* use "C:/" when $HOME is not set */
4162 if (STRCMP(name, "HOME") == 0)
4163 return homedir;
4164#endif
4165
4166 p = mch_getenv(name);
4167 if (p != NULL && *p == NUL) /* empty is the same as not set */
4168 p = NULL;
4169
4170 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004171 {
4172#if defined(FEAT_MBYTE) && defined(WIN3264)
4173 if (enc_utf8)
4174 {
4175 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004176 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004177
4178 /* Convert from active codepage to UTF-8. Other conversions are
4179 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004180 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004181 if (pp != NULL)
4182 {
4183 p = pp;
4184 *mustfree = TRUE;
4185 }
4186 }
4187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190
4191 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4192 if (!vimruntime && STRCMP(name, "VIM") != 0)
4193 return NULL;
4194
4195 /*
4196 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4197 * Don't do this when default_vimruntime_dir is non-empty.
4198 */
4199 if (vimruntime
4200#ifdef HAVE_PATHDEF
4201 && *default_vimruntime_dir == NUL
4202#endif
4203 )
4204 {
4205 p = mch_getenv((char_u *)"VIM");
4206 if (p != NULL && *p == NUL) /* empty is the same as not set */
4207 p = NULL;
4208 if (p != NULL)
4209 {
4210 p = vim_version_dir(p);
4211 if (p != NULL)
4212 *mustfree = TRUE;
4213 else
4214 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004215
4216#if defined(FEAT_MBYTE) && defined(WIN3264)
4217 if (enc_utf8)
4218 {
4219 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004220 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004221
4222 /* Convert from active codepage to UTF-8. Other conversions
4223 * are not done, because they would fail for non-ASCII
4224 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004225 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004226 if (pp != NULL)
4227 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004228 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004229 vim_free(p);
4230 p = pp;
4231 *mustfree = TRUE;
4232 }
4233 }
4234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 }
4236 }
4237
4238 /*
4239 * When expanding $VIM or $VIMRUNTIME fails, try using:
4240 * - the directory name from 'helpfile' (unless it contains '$')
4241 * - the executable name from argv[0]
4242 */
4243 if (p == NULL)
4244 {
4245 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4246 p = p_hf;
4247#ifdef USE_EXE_NAME
4248 /*
4249 * Use the name of the executable, obtained from argv[0].
4250 */
4251 else
4252 p = exe_name;
4253#endif
4254 if (p != NULL)
4255 {
4256 /* remove the file name */
4257 pend = gettail(p);
4258
4259 /* remove "doc/" from 'helpfile', if present */
4260 if (p == p_hf)
4261 pend = remove_tail(p, pend, (char_u *)"doc");
4262
4263#ifdef USE_EXE_NAME
4264# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004265 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 if (p == exe_name)
4267 {
4268 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004269 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004271 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4272 if (pend1 != pend)
4273 {
4274 pnew = alloc((unsigned)(pend1 - p) + 15);
4275 if (pnew != NULL)
4276 {
4277 STRNCPY(pnew, p, (pend1 - p));
4278 STRCPY(pnew + (pend1 - p), "Resources/vim");
4279 p = pnew;
4280 pend = p + STRLEN(p);
4281 }
4282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 }
4284# endif
4285 /* remove "src/" from exe_name, if present */
4286 if (p == exe_name)
4287 pend = remove_tail(p, pend, (char_u *)"src");
4288#endif
4289
4290 /* for $VIM, remove "runtime/" or "vim54/", if present */
4291 if (!vimruntime)
4292 {
4293 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4294 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4295 }
4296
4297 /* remove trailing path separator */
4298#ifndef MACOS_CLASSIC
4299 /* With MacOS path (with colons) the final colon is required */
Bram Moolenaare21877a2008-02-13 09:58:14 +00004300 /* to avoid confusion between absolute and relative path */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004301 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 --pend;
4303#endif
4304
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004305#ifdef MACOS_X
4306 if (p == exe_name || p == p_hf)
4307#endif
4308 /* check that the result is a directory name */
4309 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310
4311 if (p != NULL && !mch_isdir(p))
4312 {
4313 vim_free(p);
4314 p = NULL;
4315 }
4316 else
4317 {
4318#ifdef USE_EXE_NAME
4319 /* may add "/vim54" or "/runtime" if it exists */
4320 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4321 {
4322 vim_free(p);
4323 p = pend;
4324 }
4325#endif
4326 *mustfree = TRUE;
4327 }
4328 }
4329 }
4330
4331#ifdef HAVE_PATHDEF
4332 /* When there is a pathdef.c file we can use default_vim_dir and
4333 * default_vimruntime_dir */
4334 if (p == NULL)
4335 {
4336 /* Only use default_vimruntime_dir when it is not empty */
4337 if (vimruntime && *default_vimruntime_dir != NUL)
4338 {
4339 p = default_vimruntime_dir;
4340 *mustfree = FALSE;
4341 }
4342 else if (*default_vim_dir != NUL)
4343 {
4344 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4345 *mustfree = TRUE;
4346 else
4347 {
4348 p = default_vim_dir;
4349 *mustfree = FALSE;
4350 }
4351 }
4352 }
4353#endif
4354
4355 /*
4356 * Set the environment variable, so that the new value can be found fast
4357 * next time, and others can also use it (e.g. Perl).
4358 */
4359 if (p != NULL)
4360 {
4361 if (vimruntime)
4362 {
4363 vim_setenv((char_u *)"VIMRUNTIME", p);
4364 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
4366 else
4367 {
4368 vim_setenv((char_u *)"VIM", p);
4369 didset_vim = TRUE;
4370 }
4371 }
4372 return p;
4373}
4374
4375/*
4376 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4377 * Return NULL if not, return its name in allocated memory otherwise.
4378 */
4379 static char_u *
4380vim_version_dir(vimdir)
4381 char_u *vimdir;
4382{
4383 char_u *p;
4384
4385 if (vimdir == NULL || *vimdir == NUL)
4386 return NULL;
4387 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4388 if (p != NULL && mch_isdir(p))
4389 return p;
4390 vim_free(p);
4391 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4392 if (p != NULL && mch_isdir(p))
4393 return p;
4394 vim_free(p);
4395 return NULL;
4396}
4397
4398/*
4399 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4400 * the length of "name/". Otherwise return "pend".
4401 */
4402 static char_u *
4403remove_tail(p, pend, name)
4404 char_u *p;
4405 char_u *pend;
4406 char_u *name;
4407{
4408 int len = (int)STRLEN(name) + 1;
4409 char_u *newend = pend - len;
4410
4411 if (newend >= p
4412 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004413 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 return newend;
4415 return pend;
4416}
4417
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 * Our portable version of setenv.
4420 */
4421 void
4422vim_setenv(name, val)
4423 char_u *name;
4424 char_u *val;
4425{
4426#ifdef HAVE_SETENV
4427 mch_setenv((char *)name, (char *)val, 1);
4428#else
4429 char_u *envbuf;
4430
4431 /*
4432 * Putenv does not copy the string, it has to remain
4433 * valid. The allocated memory will never be freed.
4434 */
4435 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4436 if (envbuf != NULL)
4437 {
4438 sprintf((char *)envbuf, "%s=%s", name, val);
4439 putenv((char *)envbuf);
4440 }
4441#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004442#ifdef FEAT_GETTEXT
4443 /*
4444 * When setting $VIMRUNTIME adjust the directory to find message
4445 * translations to $VIMRUNTIME/lang.
4446 */
4447 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4448 {
4449 char_u *buf = concat_str(val, (char_u *)"/lang");
4450
4451 if (buf != NULL)
4452 {
4453 bindtextdomain(VIMPACKAGE, (char *)buf);
4454 vim_free(buf);
4455 }
4456 }
4457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458}
4459
4460#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4461/*
4462 * Function given to ExpandGeneric() to obtain an environment variable name.
4463 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 char_u *
4465get_env_name(xp, idx)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004466 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 int idx;
4468{
4469# if defined(AMIGA) || defined(__MRC__) || defined(__SC__)
4470 /*
4471 * No environ[] on the Amiga and on the Mac (using MPW).
4472 */
4473 return NULL;
4474# else
4475# ifndef __WIN32__
4476 /* Borland C++ 5.2 has this in a header file. */
4477 extern char **environ;
4478# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004479# define ENVNAMELEN 100
4480 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 char_u *str;
4482 int n;
4483
4484 str = (char_u *)environ[idx];
4485 if (str == NULL)
4486 return NULL;
4487
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004488 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 {
4490 if (str[n] == '=' || str[n] == NUL)
4491 break;
4492 name[n] = str[n];
4493 }
4494 name[n] = NUL;
4495 return name;
4496# endif
4497}
Bram Moolenaar24305862012-08-15 14:05:05 +02004498
4499/*
4500 * Find all user names for user completion.
4501 * Done only once and then cached.
4502 */
4503 static void
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004504init_users()
4505{
Bram Moolenaar24305862012-08-15 14:05:05 +02004506 static int lazy_init_done = FALSE;
4507
4508 if (lazy_init_done)
4509 return;
4510
4511 lazy_init_done = TRUE;
4512 ga_init2(&ga_users, sizeof(char_u *), 20);
4513
4514# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4515 {
4516 char_u* user;
4517 struct passwd* pw;
4518
4519 setpwent();
4520 while ((pw = getpwent()) != NULL)
4521 /* pw->pw_name shouldn't be NULL but just in case... */
4522 if (pw->pw_name != NULL)
4523 {
4524 if (ga_grow(&ga_users, 1) == FAIL)
4525 break;
4526 user = vim_strsave((char_u*)pw->pw_name);
4527 if (user == NULL)
4528 break;
4529 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4530 }
4531 endpwent();
4532 }
4533# endif
4534}
4535
4536/*
4537 * Function given to ExpandGeneric() to obtain an user names.
4538 */
4539 char_u*
4540get_users(xp, idx)
4541 expand_T *xp UNUSED;
4542 int idx;
4543{
4544 init_users();
4545 if (idx < ga_users.ga_len)
4546 return ((char_u **)ga_users.ga_data)[idx];
4547 return NULL;
4548}
4549
4550/*
4551 * Check whether name matches a user name. Return:
4552 * 0 if name does not match any user name.
4553 * 1 if name partially matches the beginning of a user name.
4554 * 2 is name fully matches a user name.
4555 */
4556int match_user(name)
4557 char_u* name;
4558{
4559 int i;
4560 int n = (int)STRLEN(name);
4561 int result = 0;
4562
4563 init_users();
4564 for (i = 0; i < ga_users.ga_len; i++)
4565 {
4566 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4567 return 2; /* full match */
4568 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4569 result = 1; /* partial match */
4570 }
4571 return result;
4572}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573#endif
4574
4575/*
4576 * Replace home directory by "~" in each space or comma separated file name in
4577 * 'src'.
4578 * If anything fails (except when out of space) dst equals src.
4579 */
4580 void
4581home_replace(buf, src, dst, dstlen, one)
4582 buf_T *buf; /* when not NULL, check for help files */
4583 char_u *src; /* input file name */
4584 char_u *dst; /* where to put the result */
4585 int dstlen; /* maximum length of the result */
4586 int one; /* if TRUE, only replace one file name, include
4587 spaces and commas in the file name. */
4588{
4589 size_t dirlen = 0, envlen = 0;
4590 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004591 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 char_u *p;
4593
4594 if (src == NULL)
4595 {
4596 *dst = NUL;
4597 return;
4598 }
4599
4600 /*
4601 * If the file is a help file, remove the path completely.
4602 */
4603 if (buf != NULL && buf->b_help)
4604 {
4605 STRCPY(dst, gettail(src));
4606 return;
4607 }
4608
4609 /*
4610 * We check both the value of the $HOME environment variable and the
4611 * "real" home directory.
4612 */
4613 if (homedir != NULL)
4614 dirlen = STRLEN(homedir);
4615
4616#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004617 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004619 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4620#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004621 /* Empty is the same as not set. */
4622 if (homedir_env != NULL && *homedir_env == NUL)
4623 homedir_env = NULL;
4624
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004625#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004626 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004627 {
4628 int usedlen = 0;
4629 int flen;
4630 char_u *fbuf = NULL;
4631
4632 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004633 (void)modify_fname((char_u *)":p", &usedlen,
4634 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004635 flen = (int)STRLEN(homedir_env);
4636 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4637 /* Remove the trailing / that is added to a directory. */
4638 homedir_env[flen - 1] = NUL;
4639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640#endif
4641
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 if (homedir_env != NULL)
4643 envlen = STRLEN(homedir_env);
4644
4645 if (!one)
4646 src = skipwhite(src);
4647 while (*src && dstlen > 0)
4648 {
4649 /*
4650 * Here we are at the beginning of a file name.
4651 * First, check to see if the beginning of the file name matches
4652 * $HOME or the "real" home directory. Check that there is a '/'
4653 * after the match (so that if e.g. the file is "/home/pieter/bla",
4654 * and the home directory is "/home/piet", the file does not end up
4655 * as "~er/bla" (which would seem to indicate the file "bla" in user
4656 * er's home directory)).
4657 */
4658 p = homedir;
4659 len = dirlen;
4660 for (;;)
4661 {
4662 if ( len
4663 && fnamencmp(src, p, len) == 0
4664 && (vim_ispathsep(src[len])
4665 || (!one && (src[len] == ',' || src[len] == ' '))
4666 || src[len] == NUL))
4667 {
4668 src += len;
4669 if (--dstlen > 0)
4670 *dst++ = '~';
4671
4672 /*
4673 * If it's just the home directory, add "/".
4674 */
4675 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4676 *dst++ = '/';
4677 break;
4678 }
4679 if (p == homedir_env)
4680 break;
4681 p = homedir_env;
4682 len = envlen;
4683 }
4684
4685 /* if (!one) skip to separator: space or comma */
4686 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4687 *dst++ = *src++;
4688 /* skip separator */
4689 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4690 *dst++ = *src++;
4691 }
4692 /* if (dstlen == 0) out of space, what to do??? */
4693
4694 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004695
4696 if (homedir_env != homedir_env_orig)
4697 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698}
4699
4700/*
4701 * Like home_replace, store the replaced string in allocated memory.
4702 * When something fails, NULL is returned.
4703 */
4704 char_u *
4705home_replace_save(buf, src)
4706 buf_T *buf; /* when not NULL, check for help files */
4707 char_u *src; /* input file name */
4708{
4709 char_u *dst;
4710 unsigned len;
4711
4712 len = 3; /* space for "~/" and trailing NUL */
4713 if (src != NULL) /* just in case */
4714 len += (unsigned)STRLEN(src);
4715 dst = alloc(len);
4716 if (dst != NULL)
4717 home_replace(buf, src, dst, len, TRUE);
4718 return dst;
4719}
4720
4721/*
4722 * Compare two file names and return:
4723 * FPC_SAME if they both exist and are the same file.
4724 * FPC_SAMEX if they both don't exist and have the same file name.
4725 * FPC_DIFF if they both exist and are different files.
4726 * FPC_NOTX if they both don't exist.
4727 * FPC_DIFFX if one of them doesn't exist.
4728 * For the first name environment variables are expanded
4729 */
4730 int
4731fullpathcmp(s1, s2, checkname)
4732 char_u *s1, *s2;
4733 int checkname; /* when both don't exist, check file names */
4734{
4735#ifdef UNIX
4736 char_u exp1[MAXPATHL];
4737 char_u full1[MAXPATHL];
4738 char_u full2[MAXPATHL];
4739 struct stat st1, st2;
4740 int r1, r2;
4741
4742 expand_env(s1, exp1, MAXPATHL);
4743 r1 = mch_stat((char *)exp1, &st1);
4744 r2 = mch_stat((char *)s2, &st2);
4745 if (r1 != 0 && r2 != 0)
4746 {
4747 /* if mch_stat() doesn't work, may compare the names */
4748 if (checkname)
4749 {
4750 if (fnamecmp(exp1, s2) == 0)
4751 return FPC_SAMEX;
4752 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4753 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4754 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4755 return FPC_SAMEX;
4756 }
4757 return FPC_NOTX;
4758 }
4759 if (r1 != 0 || r2 != 0)
4760 return FPC_DIFFX;
4761 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4762 return FPC_SAME;
4763 return FPC_DIFF;
4764#else
4765 char_u *exp1; /* expanded s1 */
4766 char_u *full1; /* full path of s1 */
4767 char_u *full2; /* full path of s2 */
4768 int retval = FPC_DIFF;
4769 int r1, r2;
4770
4771 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4772 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4773 {
4774 full1 = exp1 + MAXPATHL;
4775 full2 = full1 + MAXPATHL;
4776
4777 expand_env(s1, exp1, MAXPATHL);
4778 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4779 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4780
4781 /* If vim_FullName() fails, the file probably doesn't exist. */
4782 if (r1 != OK && r2 != OK)
4783 {
4784 if (checkname && fnamecmp(exp1, s2) == 0)
4785 retval = FPC_SAMEX;
4786 else
4787 retval = FPC_NOTX;
4788 }
4789 else if (r1 != OK || r2 != OK)
4790 retval = FPC_DIFFX;
4791 else if (fnamecmp(full1, full2))
4792 retval = FPC_DIFF;
4793 else
4794 retval = FPC_SAME;
4795 vim_free(exp1);
4796 }
4797 return retval;
4798#endif
4799}
4800
4801/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004802 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004803 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004804 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 */
4806 char_u *
4807gettail(fname)
4808 char_u *fname;
4809{
4810 char_u *p1, *p2;
4811
4812 if (fname == NULL)
4813 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01004814 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01004816 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 p1 = p2 + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004818 mb_ptr_adv(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 }
4820 return p1;
4821}
4822
Bram Moolenaar31710262010-08-13 13:36:15 +02004823#if defined(FEAT_SEARCHPATH)
4824static char_u *gettail_dir __ARGS((char_u *fname));
4825
4826/*
4827 * Return the end of the directory name, on the first path
4828 * separator:
4829 * "/path/file", "/path/dir/", "/path//dir", "/file"
4830 * ^ ^ ^ ^
4831 */
4832 static char_u *
4833gettail_dir(fname)
4834 char_u *fname;
4835{
4836 char_u *dir_end = fname;
4837 char_u *next_dir_end = fname;
4838 int look_for_sep = TRUE;
4839 char_u *p;
4840
4841 for (p = fname; *p != NUL; )
4842 {
4843 if (vim_ispathsep(*p))
4844 {
4845 if (look_for_sep)
4846 {
4847 next_dir_end = p;
4848 look_for_sep = FALSE;
4849 }
4850 }
4851 else
4852 {
4853 if (!look_for_sep)
4854 dir_end = next_dir_end;
4855 look_for_sep = TRUE;
4856 }
4857 mb_ptr_adv(p);
4858 }
4859 return dir_end;
4860}
4861#endif
4862
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004864 * Get pointer to tail of "fname", including path separators. Putting a NUL
4865 * here leaves the directory name. Takes care of "c:/" and "//".
4866 * Always returns a valid pointer.
4867 */
4868 char_u *
4869gettail_sep(fname)
4870 char_u *fname;
4871{
4872 char_u *p;
4873 char_u *t;
4874
4875 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4876 t = gettail(fname);
4877 while (t > p && after_pathsep(fname, t))
4878 --t;
4879#ifdef VMS
4880 /* path separator is part of the path */
4881 ++t;
4882#endif
4883 return t;
4884}
4885
4886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 * get the next path component (just after the next path separator).
4888 */
4889 char_u *
4890getnextcomp(fname)
4891 char_u *fname;
4892{
4893 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004894 mb_ptr_adv(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 if (*fname)
4896 ++fname;
4897 return fname;
4898}
4899
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900/*
4901 * Get a pointer to one character past the head of a path name.
4902 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4903 * If there is no head, path is returned.
4904 */
4905 char_u *
4906get_past_head(path)
4907 char_u *path;
4908{
4909 char_u *retval;
4910
4911#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4912 /* may skip "c:" */
4913 if (isalpha(path[0]) && path[1] == ':')
4914 retval = path + 2;
4915 else
4916 retval = path;
4917#else
4918# if defined(AMIGA)
4919 /* may skip "label:" */
4920 retval = vim_strchr(path, ':');
4921 if (retval == NULL)
4922 retval = path;
4923# else /* Unix */
4924 retval = path;
4925# endif
4926#endif
4927
4928 while (vim_ispathsep(*retval))
4929 ++retval;
4930
4931 return retval;
4932}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933
4934/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01004935 * Return TRUE if 'c' is a path separator.
4936 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 */
4938 int
4939vim_ispathsep(c)
4940 int c;
4941{
Bram Moolenaare60acc12011-05-10 16:41:25 +02004942#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02004944#else
4945# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004947# else
4948# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
4950 return (c == ':' || c == '[' || c == ']' || c == '/'
4951 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02004952# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004954# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02004956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957}
4958
Bram Moolenaar69c35002013-11-04 02:54:12 +01004959/*
4960 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
4961 */
4962 int
4963vim_ispathsep_nocolon(c)
4964 int c;
4965{
4966 return vim_ispathsep(c)
4967#ifdef BACKSLASH_IN_FILENAME
4968 && c != ':'
4969#endif
4970 ;
4971}
4972
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973#if defined(FEAT_SEARCHPATH) || defined(PROTO)
4974/*
4975 * return TRUE if 'c' is a path list separator.
4976 */
4977 int
4978vim_ispathlistsep(c)
4979 int c;
4980{
4981#ifdef UNIX
4982 return (c == ':');
4983#else
Bram Moolenaar25394022007-05-10 19:06:20 +00004984 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985#endif
4986}
4987#endif
4988
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004989#if defined(FEAT_GUI_TABLINE) || defined(FEAT_WINDOWS) \
4990 || defined(FEAT_EVAL) || defined(PROTO)
4991/*
4992 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
4993 * It's done in-place.
4994 */
4995 void
4996shorten_dir(str)
4997 char_u *str;
4998{
4999 char_u *tail, *s, *d;
5000 int skip = FALSE;
5001
5002 tail = gettail(str);
5003 d = str;
5004 for (s = str; ; ++s)
5005 {
5006 if (s >= tail) /* copy the whole tail */
5007 {
5008 *d++ = *s;
5009 if (*s == NUL)
5010 break;
5011 }
5012 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5013 {
5014 *d++ = *s;
5015 skip = FALSE;
5016 }
5017 else if (!skip)
5018 {
5019 *d++ = *s; /* copy next char */
5020 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5021 skip = TRUE;
5022# ifdef FEAT_MBYTE
5023 if (has_mbyte)
5024 {
5025 int l = mb_ptr2len(s);
5026
5027 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005028 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005029 }
5030# endif
5031 }
5032 }
5033}
5034#endif
5035
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005036/*
5037 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5038 * Also returns TRUE if there is no directory name.
5039 * "fname" must be writable!.
5040 */
5041 int
5042dir_of_file_exists(fname)
5043 char_u *fname;
5044{
5045 char_u *p;
5046 int c;
5047 int retval;
5048
5049 p = gettail_sep(fname);
5050 if (p == fname)
5051 return TRUE;
5052 c = *p;
5053 *p = NUL;
5054 retval = mch_isdir(fname);
5055 *p = c;
5056 return retval;
5057}
5058
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005060 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5061 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 */
5063 int
5064vim_fnamecmp(x, y)
5065 char_u *x, *y;
5066{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005067#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005069#else
5070 if (p_fic)
5071 return MB_STRICMP(x, y);
5072 return STRCMP(x, y);
5073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074}
5075
5076 int
5077vim_fnamencmp(x, y, len)
5078 char_u *x, *y;
5079 size_t len;
5080{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005081#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005082 char_u *px = x;
5083 char_u *py = y;
5084 int cx = NUL;
5085 int cy = NUL;
5086
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005087 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005089 cx = PTR2CHAR(px);
5090 cy = PTR2CHAR(py);
5091 if (cx == NUL || cy == NUL
5092 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5093 && !(cx == '/' && cy == '\\')
5094 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005096 len -= MB_PTR2LEN(px);
5097 px += MB_PTR2LEN(px);
5098 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 }
5100 if (len == 0)
5101 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005102 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005103#else
5104 if (p_fic)
5105 return MB_STRNICMP(x, y, len);
5106 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005108}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109
5110/*
5111 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005112 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 */
5114 char_u *
5115concat_fnames(fname1, fname2, sep)
5116 char_u *fname1;
5117 char_u *fname2;
5118 int sep;
5119{
5120 char_u *dest;
5121
5122 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5123 if (dest != NULL)
5124 {
5125 STRCPY(dest, fname1);
5126 if (sep)
5127 add_pathsep(dest);
5128 STRCAT(dest, fname2);
5129 }
5130 return dest;
5131}
5132
Bram Moolenaard6754642005-01-17 22:18:45 +00005133/*
5134 * Concatenate two strings and return the result in allocated memory.
5135 * Returns NULL when out of memory.
5136 */
5137 char_u *
5138concat_str(str1, str2)
5139 char_u *str1;
5140 char_u *str2;
5141{
5142 char_u *dest;
5143 size_t l = STRLEN(str1);
5144
5145 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5146 if (dest != NULL)
5147 {
5148 STRCPY(dest, str1);
5149 STRCPY(dest + l, str2);
5150 }
5151 return dest;
5152}
Bram Moolenaard6754642005-01-17 22:18:45 +00005153
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154/*
5155 * Add a path separator to a file name, unless it already ends in a path
5156 * separator.
5157 */
5158 void
5159add_pathsep(p)
5160 char_u *p;
5161{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005162 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 STRCAT(p, PATHSEPSTR);
5164}
5165
5166/*
5167 * FullName_save - Make an allocated copy of a full file name.
5168 * Returns NULL when out of memory.
5169 */
5170 char_u *
5171FullName_save(fname, force)
5172 char_u *fname;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005173 int force; /* force expansion, even when it already looks
5174 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175{
5176 char_u *buf;
5177 char_u *new_fname = NULL;
5178
5179 if (fname == NULL)
5180 return NULL;
5181
5182 buf = alloc((unsigned)MAXPATHL);
5183 if (buf != NULL)
5184 {
5185 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5186 new_fname = vim_strsave(buf);
5187 else
5188 new_fname = vim_strsave(fname);
5189 vim_free(buf);
5190 }
5191 return new_fname;
5192}
5193
5194#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5195
5196static char_u *skip_string __ARGS((char_u *p));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005197static pos_T *ind_find_start_comment __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198
5199/*
5200 * Find the start of a comment, not knowing if we are in a comment right now.
5201 * Search starts at w_cursor.lnum and goes backwards.
5202 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005203 static pos_T *
5204ind_find_start_comment() /* XXX */
5205{
5206 return find_start_comment(curbuf->b_ind_maxcomment);
5207}
5208
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 pos_T *
5210find_start_comment(ind_maxcomment) /* XXX */
5211 int ind_maxcomment;
5212{
5213 pos_T *pos;
5214 char_u *line;
5215 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005216 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005218 for (;;)
5219 {
5220 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5221 if (pos == NULL)
5222 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005224 /*
5225 * Check if the comment start we found is inside a string.
5226 * If it is then restrict the search to below this line and try again.
5227 */
5228 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005229 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005230 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005231 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005232 break;
5233 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5234 if (cur_maxcomment <= 0)
5235 {
5236 pos = NULL;
5237 break;
5238 }
5239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 return pos;
5241}
5242
5243/*
5244 * Skip to the end of a "string" and a 'c' character.
5245 * If there is no string or character, return argument unmodified.
5246 */
5247 static char_u *
5248skip_string(p)
5249 char_u *p;
5250{
5251 int i;
5252
5253 /*
5254 * We loop, because strings may be concatenated: "date""time".
5255 */
5256 for ( ; ; ++p)
5257 {
5258 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5259 {
5260 if (!p[1]) /* ' at end of line */
5261 break;
5262 i = 2;
5263 if (p[1] == '\\') /* '\n' or '\000' */
5264 {
5265 ++i;
5266 while (vim_isdigit(p[i - 1])) /* '\000' */
5267 ++i;
5268 }
5269 if (p[i] == '\'') /* check for trailing ' */
5270 {
5271 p += i;
5272 continue;
5273 }
5274 }
5275 else if (p[0] == '"') /* start of string */
5276 {
5277 for (++p; p[0]; ++p)
5278 {
5279 if (p[0] == '\\' && p[1] != NUL)
5280 ++p;
5281 else if (p[0] == '"') /* end of string */
5282 break;
5283 }
5284 if (p[0] == '"')
5285 continue;
5286 }
5287 break; /* no string found */
5288 }
5289 if (!*p)
5290 --p; /* backup from NUL */
5291 return p;
5292}
5293#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5294
5295#if defined(FEAT_CINDENT) || defined(PROTO)
5296
5297/*
5298 * Do C or expression indenting on the current line.
5299 */
5300 void
5301do_c_expr_indent()
5302{
5303# ifdef FEAT_EVAL
5304 if (*curbuf->b_p_inde != NUL)
5305 fixthisline(get_expr_indent);
5306 else
5307# endif
5308 fixthisline(get_c_indent);
5309}
5310
5311/*
5312 * Functions for C-indenting.
5313 * Most of this originally comes from Eric Fischer.
5314 */
5315/*
5316 * Below "XXX" means that this function may unlock the current line.
5317 */
5318
5319static char_u *cin_skipcomment __ARGS((char_u *));
5320static int cin_nocode __ARGS((char_u *));
5321static pos_T *find_line_comment __ARGS((void));
5322static int cin_islabel_skip __ARGS((char_u **));
5323static int cin_isdefault __ARGS((char_u *));
5324static char_u *after_label __ARGS((char_u *l));
5325static int get_indent_nolabel __ARGS((linenr_T lnum));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005326static int skip_label __ARGS((linenr_T, char_u **pp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327static int cin_first_id_amount __ARGS((void));
5328static int cin_get_equal_amount __ARGS((linenr_T lnum));
5329static int cin_ispreproc __ARGS((char_u *));
5330static int cin_ispreproc_cont __ARGS((char_u **pp, linenr_T *lnump));
5331static int cin_iscomment __ARGS((char_u *));
5332static int cin_islinecomment __ARGS((char_u *));
5333static int cin_isterminated __ARGS((char_u *, int, int));
5334static int cin_isinit __ARGS((void));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005335static int cin_isfuncdecl __ARGS((char_u **, linenr_T, linenr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336static int cin_isif __ARGS((char_u *));
5337static int cin_iselse __ARGS((char_u *));
5338static int cin_isdo __ARGS((char_u *));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005339static int cin_iswhileofdo __ARGS((char_u *, linenr_T));
Bram Moolenaarb345d492012-04-09 20:42:26 +02005340static int cin_is_if_for_while_before_offset __ARGS((char_u *line, int *poffset));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005341static int cin_iswhileofdo_end __ARGS((int terminated));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342static int cin_isbreak __ARGS((char_u *));
Bram Moolenaare7c56862007-08-04 10:14:52 +00005343static int cin_is_cpp_baseclass __ARGS((colnr_T *col));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005344static int get_baseclass_amount __ARGS((int col));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345static int cin_ends_in __ARGS((char_u *, char_u *, char_u *));
Bram Moolenaar75342212013-03-07 13:13:52 +01005346static int cin_starts_with __ARGS((char_u *s, char *word));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347static int cin_skip2pos __ARGS((pos_T *trypos));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005348static pos_T *find_start_brace __ARGS((void));
5349static pos_T *find_match_paren __ARGS((int));
5350static int corr_ind_maxparen __ARGS((pos_T *startpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351static int find_last_paren __ARGS((char_u *l, int start, int end));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005352static int find_match __ARGS((int lookfor, linenr_T ourscope));
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005353static int cin_is_cpp_namespace __ARGS((char_u *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354
5355/*
5356 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005357 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 */
5359 static char_u *
5360cin_skipcomment(s)
5361 char_u *s;
5362{
5363 while (*s)
5364 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005365 char_u *prev_s = s;
5366
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005368
5369 /* Perl/shell # comment comment continues until eol. Require a space
5370 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005371 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005372 {
5373 s += STRLEN(s);
5374 break;
5375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 if (*s != '/')
5377 break;
5378 ++s;
5379 if (*s == '/') /* slash-slash comment continues till eol */
5380 {
5381 s += STRLEN(s);
5382 break;
5383 }
5384 if (*s != '*')
5385 break;
5386 for (++s; *s; ++s) /* skip slash-star comment */
5387 if (s[0] == '*' && s[1] == '/')
5388 {
5389 s += 2;
5390 break;
5391 }
5392 }
5393 return s;
5394}
5395
5396/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005397 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 * not considered code.
5399 */
5400 static int
5401cin_nocode(s)
5402 char_u *s;
5403{
5404 return *cin_skipcomment(s) == NUL;
5405}
5406
5407/*
5408 * Check previous lines for a "//" line comment, skipping over blank lines.
5409 */
5410 static pos_T *
5411find_line_comment() /* XXX */
5412{
5413 static pos_T pos;
5414 char_u *line;
5415 char_u *p;
5416
5417 pos = curwin->w_cursor;
5418 while (--pos.lnum > 0)
5419 {
5420 line = ml_get(pos.lnum);
5421 p = skipwhite(line);
5422 if (cin_islinecomment(p))
5423 {
5424 pos.col = (int)(p - line);
5425 return &pos;
5426 }
5427 if (*p != NUL)
5428 break;
5429 }
5430 return NULL;
5431}
5432
5433/*
5434 * Check if string matches "label:"; move to character after ':' if true.
5435 */
5436 static int
5437cin_islabel_skip(s)
5438 char_u **s;
5439{
5440 if (!vim_isIDc(**s)) /* need at least one ID character */
5441 return FALSE;
5442
5443 while (vim_isIDc(**s))
5444 (*s)++;
5445
5446 *s = cin_skipcomment(*s);
5447
5448 /* "::" is not a label, it's C++ */
5449 return (**s == ':' && *++*s != ':');
5450}
5451
5452/*
5453 * Recognize a label: "label:".
5454 * Note: curwin->w_cursor must be where we are looking for the label.
5455 */
5456 int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005457cin_islabel() /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458{
5459 char_u *s;
5460
5461 s = cin_skipcomment(ml_get_curline());
5462
5463 /*
5464 * Exclude "default" from labels, since it should be indented
5465 * like a switch label. Same for C++ scope declarations.
5466 */
5467 if (cin_isdefault(s))
5468 return FALSE;
5469 if (cin_isscopedecl(s))
5470 return FALSE;
5471
5472 if (cin_islabel_skip(&s))
5473 {
5474 /*
5475 * Only accept a label if the previous line is terminated or is a case
5476 * label.
5477 */
5478 pos_T cursor_save;
5479 pos_T *trypos;
5480 char_u *line;
5481
5482 cursor_save = curwin->w_cursor;
5483 while (curwin->w_cursor.lnum > 1)
5484 {
5485 --curwin->w_cursor.lnum;
5486
5487 /*
5488 * If we're in a comment now, skip to the start of the comment.
5489 */
5490 curwin->w_cursor.col = 0;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005491 if ((trypos = ind_find_start_comment()) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 curwin->w_cursor = *trypos;
5493
5494 line = ml_get_curline();
5495 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5496 continue;
5497 if (*(line = cin_skipcomment(line)) == NUL)
5498 continue;
5499
5500 curwin->w_cursor = cursor_save;
5501 if (cin_isterminated(line, TRUE, FALSE)
5502 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005503 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 || (cin_islabel_skip(&line) && cin_nocode(line)))
5505 return TRUE;
5506 return FALSE;
5507 }
5508 curwin->w_cursor = cursor_save;
5509 return TRUE; /* label at start of file??? */
5510 }
5511 return FALSE;
5512}
5513
5514/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005515 * Recognize structure initialization and enumerations:
5516 * "[typedef] [static|public|protected|private] enum"
5517 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 */
5519 static int
5520cin_isinit(void)
5521{
5522 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005523 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524
5525 s = cin_skipcomment(ml_get_curline());
5526
Bram Moolenaar75342212013-03-07 13:13:52 +01005527 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 s = cin_skipcomment(s + 7);
5529
Bram Moolenaar75342212013-03-07 13:13:52 +01005530 for (;;)
5531 {
5532 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005533
Bram Moolenaar75342212013-03-07 13:13:52 +01005534 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5535 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005536 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005537 if (cin_starts_with(s, skip[i]))
5538 {
5539 s = cin_skipcomment(s + l);
5540 l = 0;
5541 break;
5542 }
5543 }
5544 if (l != 0)
5545 break;
5546 }
5547
5548 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 return TRUE;
5550
5551 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5552 return TRUE;
5553
5554 return FALSE;
5555}
5556
5557/*
5558 * Recognize a switch label: "case .*:" or "default:".
5559 */
5560 int
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005561cin_iscase(s, strict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 char_u *s;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005563 int strict; /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564{
5565 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005566 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 {
5568 for (s += 4; *s; ++s)
5569 {
5570 s = cin_skipcomment(s);
5571 if (*s == ':')
5572 {
5573 if (s[1] == ':') /* skip over "::" for C++ */
5574 ++s;
5575 else
5576 return TRUE;
5577 }
5578 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005579 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5581 return FALSE; /* stop at comment */
5582 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005583 {
5584 /* JS etc. */
5585 if (strict)
5586 return FALSE; /* stop at string */
5587 else
5588 return TRUE;
5589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 }
5591 return FALSE;
5592 }
5593
5594 if (cin_isdefault(s))
5595 return TRUE;
5596 return FALSE;
5597}
5598
5599/*
5600 * Recognize a "default" switch label.
5601 */
5602 static int
5603cin_isdefault(s)
5604 char_u *s;
5605{
5606 return (STRNCMP(s, "default", 7) == 0
5607 && *(s = cin_skipcomment(s + 7)) == ':'
5608 && s[1] != ':');
5609}
5610
5611/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005612 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 */
5614 int
5615cin_isscopedecl(s)
5616 char_u *s;
5617{
5618 int i;
5619
5620 s = cin_skipcomment(s);
5621 if (STRNCMP(s, "public", 6) == 0)
5622 i = 6;
5623 else if (STRNCMP(s, "protected", 9) == 0)
5624 i = 9;
5625 else if (STRNCMP(s, "private", 7) == 0)
5626 i = 7;
5627 else
5628 return FALSE;
5629 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5630}
5631
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005632/* Maximum number of lines to search back for a "namespace" line. */
5633#define FIND_NAMESPACE_LIM 20
5634
5635/*
5636 * Recognize a "namespace" scope declaration.
5637 */
5638 static int
5639cin_is_cpp_namespace(s)
5640 char_u *s;
5641{
5642 char_u *p;
5643 int has_name = FALSE;
5644
5645 s = cin_skipcomment(s);
5646 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5647 {
5648 p = cin_skipcomment(skipwhite(s + 9));
5649 while (*p != NUL)
5650 {
5651 if (vim_iswhite(*p))
5652 {
5653 has_name = TRUE; /* found end of a name */
5654 p = cin_skipcomment(skipwhite(p));
5655 }
5656 else if (*p == '{')
5657 {
5658 break;
5659 }
5660 else if (vim_iswordc(*p))
5661 {
5662 if (has_name)
5663 return FALSE; /* word character after skipping past name */
5664 ++p;
5665 }
5666 else
5667 {
5668 return FALSE;
5669 }
5670 }
5671 return TRUE;
5672 }
5673 return FALSE;
5674}
5675
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676/*
5677 * Return a pointer to the first non-empty non-comment character after a ':'.
5678 * Return NULL if not found.
5679 * case 234: a = b;
5680 * ^
5681 */
5682 static char_u *
5683after_label(l)
5684 char_u *l;
5685{
5686 for ( ; *l; ++l)
5687 {
5688 if (*l == ':')
5689 {
5690 if (l[1] == ':') /* skip over "::" for C++ */
5691 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005692 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 break;
5694 }
5695 else if (*l == '\'' && l[1] && l[2] == '\'')
5696 l += 2; /* skip over 'x' */
5697 }
5698 if (*l == NUL)
5699 return NULL;
5700 l = cin_skipcomment(l + 1);
5701 if (*l == NUL)
5702 return NULL;
5703 return l;
5704}
5705
5706/*
5707 * Get indent of line "lnum", skipping a label.
5708 * Return 0 if there is nothing after the label.
5709 */
5710 static int
5711get_indent_nolabel(lnum) /* XXX */
5712 linenr_T lnum;
5713{
5714 char_u *l;
5715 pos_T fp;
5716 colnr_T col;
5717 char_u *p;
5718
5719 l = ml_get(lnum);
5720 p = after_label(l);
5721 if (p == NULL)
5722 return 0;
5723
5724 fp.col = (colnr_T)(p - l);
5725 fp.lnum = lnum;
5726 getvcol(curwin, &fp, &col, NULL, NULL);
5727 return (int)col;
5728}
5729
5730/*
5731 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005732 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 * label: if (asdf && asdfasdf)
5734 * ^
5735 */
5736 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005737skip_label(lnum, pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 linenr_T lnum;
5739 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740{
5741 char_u *l;
5742 int amount;
5743 pos_T cursor_save;
5744
5745 cursor_save = curwin->w_cursor;
5746 curwin->w_cursor.lnum = lnum;
5747 l = ml_get_curline();
5748 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005749 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 {
5751 amount = get_indent_nolabel(lnum);
5752 l = after_label(ml_get_curline());
5753 if (l == NULL) /* just in case */
5754 l = ml_get_curline();
5755 }
5756 else
5757 {
5758 amount = get_indent();
5759 l = ml_get_curline();
5760 }
5761 *pp = l;
5762
5763 curwin->w_cursor = cursor_save;
5764 return amount;
5765}
5766
5767/*
5768 * Return the indent of the first variable name after a type in a declaration.
5769 * int a, indent of "a"
5770 * static struct foo b, indent of "b"
5771 * enum bla c, indent of "c"
5772 * Returns zero when it doesn't look like a declaration.
5773 */
5774 static int
5775cin_first_id_amount()
5776{
5777 char_u *line, *p, *s;
5778 int len;
5779 pos_T fp;
5780 colnr_T col;
5781
5782 line = ml_get_curline();
5783 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005784 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 if (len == 6 && STRNCMP(p, "static", 6) == 0)
5786 {
5787 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005788 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 }
5790 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
5791 p = skipwhite(p + 6);
5792 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
5793 p = skipwhite(p + 4);
5794 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
5795 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
5796 {
5797 s = skipwhite(p + len);
5798 if ((STRNCMP(s, "int", 3) == 0 && vim_iswhite(s[3]))
5799 || (STRNCMP(s, "long", 4) == 0 && vim_iswhite(s[4]))
5800 || (STRNCMP(s, "short", 5) == 0 && vim_iswhite(s[5]))
5801 || (STRNCMP(s, "char", 4) == 0 && vim_iswhite(s[4])))
5802 p = s;
5803 }
5804 for (len = 0; vim_isIDc(p[len]); ++len)
5805 ;
5806 if (len == 0 || !vim_iswhite(p[len]) || cin_nocode(p))
5807 return 0;
5808
5809 p = skipwhite(p + len);
5810 fp.lnum = curwin->w_cursor.lnum;
5811 fp.col = (colnr_T)(p - line);
5812 getvcol(curwin, &fp, &col, NULL, NULL);
5813 return (int)col;
5814}
5815
5816/*
5817 * Return the indent of the first non-blank after an equal sign.
5818 * char *foo = "here";
5819 * Return zero if no (useful) equal sign found.
5820 * Return -1 if the line above "lnum" ends in a backslash.
5821 * foo = "asdf\
5822 * asdf\
5823 * here";
5824 */
5825 static int
5826cin_get_equal_amount(lnum)
5827 linenr_T lnum;
5828{
5829 char_u *line;
5830 char_u *s;
5831 colnr_T col;
5832 pos_T fp;
5833
5834 if (lnum > 1)
5835 {
5836 line = ml_get(lnum - 1);
5837 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
5838 return -1;
5839 }
5840
5841 line = s = ml_get(lnum);
5842 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
5843 {
5844 if (cin_iscomment(s)) /* ignore comments */
5845 s = cin_skipcomment(s);
5846 else
5847 ++s;
5848 }
5849 if (*s != '=')
5850 return 0;
5851
5852 s = skipwhite(s + 1);
5853 if (cin_nocode(s))
5854 return 0;
5855
5856 if (*s == '"') /* nice alignment for continued strings */
5857 ++s;
5858
5859 fp.lnum = lnum;
5860 fp.col = (colnr_T)(s - line);
5861 getvcol(curwin, &fp, &col, NULL, NULL);
5862 return (int)col;
5863}
5864
5865/*
5866 * Recognize a preprocessor statement: Any line that starts with '#'.
5867 */
5868 static int
5869cin_ispreproc(s)
5870 char_u *s;
5871{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02005872 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 return TRUE;
5874 return FALSE;
5875}
5876
5877/*
5878 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
5879 * continuation line of a preprocessor statement. Decrease "*lnump" to the
5880 * start and return the line in "*pp".
5881 */
5882 static int
5883cin_ispreproc_cont(pp, lnump)
5884 char_u **pp;
5885 linenr_T *lnump;
5886{
5887 char_u *line = *pp;
5888 linenr_T lnum = *lnump;
5889 int retval = FALSE;
5890
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00005891 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 {
5893 if (cin_ispreproc(line))
5894 {
5895 retval = TRUE;
5896 *lnump = lnum;
5897 break;
5898 }
5899 if (lnum == 1)
5900 break;
5901 line = ml_get(--lnum);
5902 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
5903 break;
5904 }
5905
5906 if (lnum != *lnump)
5907 *pp = ml_get(*lnump);
5908 return retval;
5909}
5910
5911/*
5912 * Recognize the start of a C or C++ comment.
5913 */
5914 static int
5915cin_iscomment(p)
5916 char_u *p;
5917{
5918 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
5919}
5920
5921/*
5922 * Recognize the start of a "//" comment.
5923 */
5924 static int
5925cin_islinecomment(p)
5926 char_u *p;
5927{
5928 return (p[0] == '/' && p[1] == '/');
5929}
5930
5931/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005932 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
5933 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02005935 * If a line begins with an "else", only consider it terminated if no unmatched
5936 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 * Return the character terminating the line (ending char's have precedence if
5938 * both apply in order to determine initializations).
5939 */
5940 static int
5941cin_isterminated(s, incl_open, incl_comma)
5942 char_u *s;
5943 int incl_open; /* include '{' at the end as terminator */
5944 int incl_comma; /* recognize a trailing comma */
5945{
Bram Moolenaar496f9512011-05-19 16:35:09 +02005946 char_u found_start = 0;
5947 unsigned n_open = 0;
5948 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949
5950 s = cin_skipcomment(s);
5951
5952 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
5953 found_start = *s;
5954
Bram Moolenaar496f9512011-05-19 16:35:09 +02005955 if (!found_start)
5956 is_else = cin_iselse(s);
5957
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 while (*s)
5959 {
5960 /* skip over comments, "" strings and 'c'haracters */
5961 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005962 if (*s == '}' && n_open > 0)
5963 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02005964 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005965 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 && cin_nocode(s + 1))
5967 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005968 else if (*s == '{')
5969 {
5970 if (incl_open && cin_nocode(s + 1))
5971 return *s;
5972 else
5973 ++n_open;
5974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975
5976 if (*s)
5977 s++;
5978 }
5979 return found_start;
5980}
5981
5982/*
5983 * Recognize the basic picture of a function declaration -- it needs to
5984 * have an open paren somewhere and a close paren at the end of the line and
5985 * no semicolons anywhere.
5986 * When a line ends in a comma we continue looking in the next line.
5987 * "sp" points to a string with the line. When looking at other lines it must
5988 * be restored to the line. When it's NULL fetch lines here.
5989 * "lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005990 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 */
5992 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005993cin_isfuncdecl(sp, first_lnum, min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 char_u **sp;
5995 linenr_T first_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005996 linenr_T min_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997{
5998 char_u *s;
5999 linenr_T lnum = first_lnum;
6000 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006001 pos_T *trypos;
6002 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003
6004 if (sp == NULL)
6005 s = ml_get(lnum);
6006 else
6007 s = *sp;
6008
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006009 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006010 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006011 {
6012 lnum = trypos->lnum;
6013 if (lnum < min_lnum)
6014 return FALSE;
6015
6016 s = ml_get(lnum);
6017 }
6018
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006019 /* Ignore line starting with #. */
6020 if (cin_ispreproc(s))
6021 return FALSE;
6022
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6024 {
6025 if (cin_iscomment(s)) /* ignore comments */
6026 s = cin_skipcomment(s);
6027 else
6028 ++s;
6029 }
6030 if (*s != '(')
6031 return FALSE; /* ';', ' or " before any () or no '(' */
6032
6033 while (*s && *s != ';' && *s != '\'' && *s != '"')
6034 {
6035 if (*s == ')' && cin_nocode(s + 1))
6036 {
6037 /* ')' at the end: may have found a match
6038 * Check for he previous line not to end in a backslash:
6039 * #if defined(x) && \
6040 * defined(y)
6041 */
6042 lnum = first_lnum - 1;
6043 s = ml_get(lnum);
6044 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6045 retval = TRUE;
6046 goto done;
6047 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006048 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006050 int comma = (*s == ',');
6051
6052 /* ',' at the end: continue looking in the next line.
6053 * At the end: check for ',' in the next line, for this style:
6054 * func(arg1
6055 * , arg2) */
6056 for (;;)
6057 {
6058 if (lnum >= curbuf->b_ml.ml_line_count)
6059 break;
6060 s = ml_get(++lnum);
6061 if (!cin_ispreproc(s))
6062 break;
6063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 if (lnum >= curbuf->b_ml.ml_line_count)
6065 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006066 /* Require a comma at end of the line or a comma or ')' at the
6067 * start of next line. */
6068 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006069 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006070 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006071 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 }
6073 else if (cin_iscomment(s)) /* ignore comments */
6074 s = cin_skipcomment(s);
6075 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006076 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006078 just_started = FALSE;
6079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 }
6081
6082done:
6083 if (lnum != first_lnum && sp != NULL)
6084 *sp = ml_get(first_lnum);
6085
6086 return retval;
6087}
6088
6089 static int
6090cin_isif(p)
6091 char_u *p;
6092{
6093 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
6094}
6095
6096 static int
6097cin_iselse(p)
6098 char_u *p;
6099{
6100 if (*p == '}') /* accept "} else" */
6101 p = cin_skipcomment(p + 1);
6102 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6103}
6104
6105 static int
6106cin_isdo(p)
6107 char_u *p;
6108{
6109 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6110}
6111
6112/*
6113 * Check if this is a "while" that should have a matching "do".
6114 * We only accept a "while (condition) ;", with only white space between the
6115 * ')' and ';'. The condition may be spread over several lines.
6116 */
6117 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006118cin_iswhileofdo(p, lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 char_u *p;
6120 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121{
6122 pos_T cursor_save;
6123 pos_T *trypos;
6124 int retval = FALSE;
6125
6126 p = cin_skipcomment(p);
6127 if (*p == '}') /* accept "} while (cond);" */
6128 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006129 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 {
6131 cursor_save = curwin->w_cursor;
6132 curwin->w_cursor.lnum = lnum;
6133 curwin->w_cursor.col = 0;
6134 p = ml_get_curline();
6135 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6136 {
6137 ++p;
6138 ++curwin->w_cursor.col;
6139 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006140 if ((trypos = findmatchlimit(NULL, 0, 0,
6141 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6143 retval = TRUE;
6144 curwin->w_cursor = cursor_save;
6145 }
6146 return retval;
6147}
6148
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006149/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006150 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006151 * Return 0 if there is none.
6152 * Otherwise return !0 and update "*poffset" to point to the place where the
6153 * string was found.
6154 */
6155 static int
Bram Moolenaarb345d492012-04-09 20:42:26 +02006156cin_is_if_for_while_before_offset(line, poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006157 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006158 int *poffset;
6159{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006160 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006161
6162 if (offset-- < 2)
6163 return 0;
6164 while (offset > 2 && vim_iswhite(line[offset]))
6165 --offset;
6166
6167 offset -= 1;
6168 if (!STRNCMP(line + offset, "if", 2))
6169 goto probablyFound;
6170
6171 if (offset >= 1)
6172 {
6173 offset -= 1;
6174 if (!STRNCMP(line + offset, "for", 3))
6175 goto probablyFound;
6176
6177 if (offset >= 2)
6178 {
6179 offset -= 2;
6180 if (!STRNCMP(line + offset, "while", 5))
6181 goto probablyFound;
6182 }
6183 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006184 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006185
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006186probablyFound:
6187 if (!offset || !vim_isIDc(line[offset - 1]))
6188 {
6189 *poffset = offset;
6190 return 1;
6191 }
6192 return 0;
6193}
6194
6195/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006196 * Return TRUE if we are at the end of a do-while.
6197 * do
6198 * nothing;
6199 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006200 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006201 * Adjust the cursor to the line with "while".
6202 */
6203 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006204cin_iswhileofdo_end(terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006205 int terminated;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006206{
6207 char_u *line;
6208 char_u *p;
6209 char_u *s;
6210 pos_T *trypos;
6211 int i;
6212
6213 if (terminated != ';') /* there must be a ';' at the end */
6214 return FALSE;
6215
6216 p = line = ml_get_curline();
6217 while (*p != NUL)
6218 {
6219 p = cin_skipcomment(p);
6220 if (*p == ')')
6221 {
6222 s = skipwhite(p + 1);
6223 if (*s == ';' && cin_nocode(s + 1))
6224 {
6225 /* Found ");" at end of the line, now check there is "while"
6226 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006227 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006228 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006229 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006230 if (trypos != NULL)
6231 {
6232 s = cin_skipcomment(ml_get(trypos->lnum));
6233 if (*s == '}') /* accept "} while (cond);" */
6234 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006235 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006236 {
6237 curwin->w_cursor.lnum = trypos->lnum;
6238 return TRUE;
6239 }
6240 }
6241
6242 /* Searching may have made "line" invalid, get it again. */
6243 line = ml_get_curline();
6244 p = line + i;
6245 }
6246 }
6247 if (*p != NUL)
6248 ++p;
6249 }
6250 return FALSE;
6251}
6252
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 static int
6254cin_isbreak(p)
6255 char_u *p;
6256{
6257 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6258}
6259
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006260/*
6261 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 * constructor-initialization. eg:
6263 *
6264 * class MyClass :
6265 * baseClass <-- here
6266 * class MyClass : public baseClass,
6267 * anotherBaseClass <-- here (should probably lineup ??)
6268 * MyClass::MyClass(...) :
6269 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006270 *
6271 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 */
6273 static int
Bram Moolenaare7c56862007-08-04 10:14:52 +00006274cin_is_cpp_baseclass(col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006275 colnr_T *col; /* return: column to align with */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276{
6277 char_u *s;
6278 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006279 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006280 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281
6282 *col = 0;
6283
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006284 s = skipwhite(line);
6285 if (*s == '#') /* skip #define FOO x ? (x) : x */
6286 return FALSE;
6287 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 if (*s == NUL)
6289 return FALSE;
6290
6291 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6292
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006293 /* Search for a line starting with '#', empty, ending in ';' or containing
6294 * '{' or '}' and start below it. This handles the following situations:
6295 * a = cond ?
6296 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006297 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006298 * func::foo()
6299 * : something
6300 * {}
6301 * Foo::Foo (int one, int two)
6302 * : something(4),
6303 * somethingelse(3)
6304 * {}
6305 */
6306 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006308 line = ml_get(lnum - 1);
6309 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006310 if (*s == '#' || *s == NUL)
6311 break;
6312 while (*s != NUL)
6313 {
6314 s = cin_skipcomment(s);
6315 if (*s == '{' || *s == '}'
6316 || (*s == ';' && cin_nocode(s + 1)))
6317 break;
6318 if (*s != NUL)
6319 ++s;
6320 }
6321 if (*s != NUL)
6322 break;
6323 --lnum;
6324 }
6325
Bram Moolenaare7c56862007-08-04 10:14:52 +00006326 line = ml_get(lnum);
6327 s = cin_skipcomment(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006328 for (;;)
6329 {
6330 if (*s == NUL)
6331 {
6332 if (lnum == curwin->w_cursor.lnum)
6333 break;
6334 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006335 line = ml_get(++lnum);
6336 s = cin_skipcomment(line);
6337 if (*s == NUL)
6338 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006339 }
6340
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006341 if (s[0] == '"')
6342 s = skip_string(s) + 1;
6343 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 {
6345 if (s[1] == ':')
6346 {
6347 /* skip double colon. It can't be a constructor
6348 * initialization any more */
6349 lookfor_ctor_init = FALSE;
6350 s = cin_skipcomment(s + 2);
6351 }
6352 else if (lookfor_ctor_init || class_or_struct)
6353 {
6354 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006355 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 cpp_base_class = TRUE;
6357 lookfor_ctor_init = class_or_struct = FALSE;
6358 *col = 0;
6359 s = cin_skipcomment(s + 1);
6360 }
6361 else
6362 s = cin_skipcomment(s + 1);
6363 }
6364 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6365 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6366 {
6367 class_or_struct = TRUE;
6368 lookfor_ctor_init = FALSE;
6369
6370 if (*s == 'c')
6371 s = cin_skipcomment(s + 5);
6372 else
6373 s = cin_skipcomment(s + 6);
6374 }
6375 else
6376 {
6377 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6378 {
6379 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6380 }
6381 else if (s[0] == ')')
6382 {
6383 /* Constructor-initialization is assumed if we come across
6384 * something like "):" */
6385 class_or_struct = FALSE;
6386 lookfor_ctor_init = TRUE;
6387 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006388 else if (s[0] == '?')
6389 {
6390 /* Avoid seeing '() :' after '?' as constructor init. */
6391 return FALSE;
6392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 else if (!vim_isIDc(s[0]))
6394 {
6395 /* if it is not an identifier, we are wrong */
6396 class_or_struct = FALSE;
6397 lookfor_ctor_init = FALSE;
6398 }
6399 else if (*col == 0)
6400 {
6401 /* it can't be a constructor-initialization any more */
6402 lookfor_ctor_init = FALSE;
6403
6404 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006405 if (cpp_base_class)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 *col = (colnr_T)(s - line);
6407 }
6408
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006409 /* When the line ends in a comma don't align with it. */
6410 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
6411 *col = 0;
6412
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 s = cin_skipcomment(s + 1);
6414 }
6415 }
6416
6417 return cpp_base_class;
6418}
6419
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006420 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006421get_baseclass_amount(col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006422 int col;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006423{
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(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006432 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006433 amount = get_indent_lnum(trypos->lnum); /* XXX */
6434 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006435 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006436 }
6437 else
6438 {
6439 curwin->w_cursor.col = col;
6440 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6441 amount = (int)vcol;
6442 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006443 if (amount < curbuf->b_ind_cpp_baseclass)
6444 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006445 return amount;
6446}
6447
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448/*
6449 * Return TRUE if string "s" ends with the string "find", possibly followed by
6450 * white space and comments. Skip strings and comments.
6451 * Ignore "ignore" after "find" if it's not NULL.
6452 */
6453 static int
6454cin_ends_in(s, find, ignore)
6455 char_u *s;
6456 char_u *find;
6457 char_u *ignore;
6458{
6459 char_u *p = s;
6460 char_u *r;
6461 int len = (int)STRLEN(find);
6462
6463 while (*p != NUL)
6464 {
6465 p = cin_skipcomment(p);
6466 if (STRNCMP(p, find, len) == 0)
6467 {
6468 r = skipwhite(p + len);
6469 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6470 r = skipwhite(r + STRLEN(ignore));
6471 if (cin_nocode(r))
6472 return TRUE;
6473 }
6474 if (*p != NUL)
6475 ++p;
6476 }
6477 return FALSE;
6478}
6479
6480/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006481 * Return TRUE when "s" starts with "word" and then a non-ID character.
6482 */
6483 static int
6484cin_starts_with(s, word)
6485 char_u *s;
6486 char *word;
6487{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006488 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006489
6490 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6491}
6492
6493/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 * Skip strings, chars and comments until at or past "trypos".
6495 * Return the column found.
6496 */
6497 static int
6498cin_skip2pos(trypos)
6499 pos_T *trypos;
6500{
6501 char_u *line;
6502 char_u *p;
6503
6504 p = line = ml_get(trypos->lnum);
6505 while (*p && (colnr_T)(p - line) < trypos->col)
6506 {
6507 if (cin_iscomment(p))
6508 p = cin_skipcomment(p);
6509 else
6510 {
6511 p = skip_string(p);
6512 ++p;
6513 }
6514 }
6515 return (int)(p - line);
6516}
6517
6518/*
6519 * Find the '{' at the start of the block we are in.
6520 * Return NULL if no match found.
6521 * Ignore a '{' that is in a comment, makes indenting the next three lines
6522 * work. */
6523/* foo() */
6524/* { */
6525/* } */
6526
6527 static pos_T *
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006528find_start_brace() /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529{
6530 pos_T cursor_save;
6531 pos_T *trypos;
6532 pos_T *pos;
6533 static pos_T pos_copy;
6534
6535 cursor_save = curwin->w_cursor;
6536 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6537 {
6538 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6539 trypos = &pos_copy;
6540 curwin->w_cursor = *trypos;
6541 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006542 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006544 && (pos = ind_find_start_comment()) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 break;
6546 if (pos != NULL)
6547 curwin->w_cursor.lnum = pos->lnum;
6548 }
6549 curwin->w_cursor = cursor_save;
6550 return trypos;
6551}
6552
6553/*
6554 * Find the matching '(', failing if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006555 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 */
6557 static pos_T *
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006558find_match_paren(ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 int ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560{
6561 pos_T cursor_save;
6562 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006563 static pos_T pos_copy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564
6565 cursor_save = curwin->w_cursor;
6566 if ((trypos = findmatchlimit(NULL, '(', 0, ind_maxparen)) != NULL)
6567 {
6568 /* check if the ( is in a // comment */
6569 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
6570 trypos = NULL;
6571 else
6572 {
6573 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6574 trypos = &pos_copy;
6575 curwin->w_cursor = *trypos;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006576 if (ind_find_start_comment() != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 trypos = NULL;
6578 }
6579 }
6580 curwin->w_cursor = cursor_save;
6581 return trypos;
6582}
6583
6584/*
6585 * Return ind_maxparen corrected for the difference in line number between the
6586 * cursor position and "startpos". This makes sure that searching for a
6587 * matching paren above the cursor line doesn't find a match because of
6588 * looking a few lines further.
6589 */
6590 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006591corr_ind_maxparen(startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 pos_T *startpos;
6593{
6594 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6595
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006596 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
6597 return curbuf->b_ind_maxparen - (int)n;
6598 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599}
6600
6601/*
6602 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006603 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 */
6605 static int
6606find_last_paren(l, start, end)
6607 char_u *l;
6608 int start, end;
6609{
6610 int i;
6611 int retval = FALSE;
6612 int open_count = 0;
6613
6614 curwin->w_cursor.col = 0; /* default is start of line */
6615
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006616 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 {
6618 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6619 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6620 if (l[i] == start)
6621 ++open_count;
6622 else if (l[i] == end)
6623 {
6624 if (open_count > 0)
6625 --open_count;
6626 else
6627 {
6628 curwin->w_cursor.col = i;
6629 retval = TRUE;
6630 }
6631 }
6632 }
6633 return retval;
6634}
6635
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006636/*
6637 * Parse 'cinoptions' and set the values in "curbuf".
6638 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
6639 */
6640 void
6641parse_cino(buf)
6642 buf_T *buf;
6643{
6644 char_u *p;
6645 char_u *l;
6646 char_u *digits;
6647 int n;
6648 int divider;
6649 int fraction = 0;
6650 int sw = (int)get_sw_value(buf);
6651
6652 /*
6653 * Set the default values.
6654 */
6655 /* Spaces from a block's opening brace the prevailing indent for that
6656 * block should be. */
6657 buf->b_ind_level = sw;
6658
6659 /* Spaces from the edge of the line an open brace that's at the end of a
6660 * line is imagined to be. */
6661 buf->b_ind_open_imag = 0;
6662
6663 /* Spaces from the prevailing indent for a line that is not preceded by
6664 * an opening brace. */
6665 buf->b_ind_no_brace = 0;
6666
6667 /* Column where the first { of a function should be located }. */
6668 buf->b_ind_first_open = 0;
6669
6670 /* Spaces from the prevailing indent a leftmost open brace should be
6671 * located. */
6672 buf->b_ind_open_extra = 0;
6673
6674 /* Spaces from the matching open brace (real location for one at the left
6675 * edge; imaginary location from one that ends a line) the matching close
6676 * brace should be located. */
6677 buf->b_ind_close_extra = 0;
6678
6679 /* Spaces from the edge of the line an open brace sitting in the leftmost
6680 * column is imagined to be. */
6681 buf->b_ind_open_left_imag = 0;
6682
6683 /* Spaces jump labels should be shifted to the left if N is non-negative,
6684 * otherwise the jump label will be put to column 1. */
6685 buf->b_ind_jump_label = -1;
6686
6687 /* Spaces from the switch() indent a "case xx" label should be located. */
6688 buf->b_ind_case = sw;
6689
6690 /* Spaces from the "case xx:" code after a switch() should be located. */
6691 buf->b_ind_case_code = sw;
6692
6693 /* Lineup break at end of case in switch() with case label. */
6694 buf->b_ind_case_break = 0;
6695
6696 /* Spaces from the class declaration indent a scope declaration label
6697 * should be located. */
6698 buf->b_ind_scopedecl = sw;
6699
6700 /* Spaces from the scope declaration label code should be located. */
6701 buf->b_ind_scopedecl_code = sw;
6702
6703 /* Amount K&R-style parameters should be indented. */
6704 buf->b_ind_param = sw;
6705
6706 /* Amount a function type spec should be indented. */
6707 buf->b_ind_func_type = sw;
6708
6709 /* Amount a cpp base class declaration or constructor initialization
6710 * should be indented. */
6711 buf->b_ind_cpp_baseclass = sw;
6712
6713 /* additional spaces beyond the prevailing indent a continuation line
6714 * should be located. */
6715 buf->b_ind_continuation = sw;
6716
6717 /* Spaces from the indent of the line with an unclosed parentheses. */
6718 buf->b_ind_unclosed = sw * 2;
6719
6720 /* Spaces from the indent of the line with an unclosed parentheses, which
6721 * itself is also unclosed. */
6722 buf->b_ind_unclosed2 = sw;
6723
6724 /* Suppress ignoring spaces from the indent of a line starting with an
6725 * unclosed parentheses. */
6726 buf->b_ind_unclosed_noignore = 0;
6727
6728 /* If the opening paren is the last nonwhite character on the line, and
6729 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
6730 * context (for very long lines). */
6731 buf->b_ind_unclosed_wrapped = 0;
6732
6733 /* Suppress ignoring white space when lining up with the character after
6734 * an unclosed parentheses. */
6735 buf->b_ind_unclosed_whiteok = 0;
6736
6737 /* Indent a closing parentheses under the line start of the matching
6738 * opening parentheses. */
6739 buf->b_ind_matching_paren = 0;
6740
6741 /* Indent a closing parentheses under the previous line. */
6742 buf->b_ind_paren_prev = 0;
6743
6744 /* Extra indent for comments. */
6745 buf->b_ind_comment = 0;
6746
6747 /* Spaces from the comment opener when there is nothing after it. */
6748 buf->b_ind_in_comment = 3;
6749
6750 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
6751 * after the comment opener. */
6752 buf->b_ind_in_comment2 = 0;
6753
6754 /* Max lines to search for an open paren. */
6755 buf->b_ind_maxparen = 20;
6756
6757 /* Max lines to search for an open comment. */
6758 buf->b_ind_maxcomment = 70;
6759
6760 /* Handle braces for java code. */
6761 buf->b_ind_java = 0;
6762
6763 /* Not to confuse JS object properties with labels. */
6764 buf->b_ind_js = 0;
6765
6766 /* Handle blocked cases correctly. */
6767 buf->b_ind_keep_case_label = 0;
6768
6769 /* Handle C++ namespace. */
6770 buf->b_ind_cpp_namespace = 0;
6771
6772 /* Handle continuation lines containing conditions of if(), for() and
6773 * while(). */
6774 buf->b_ind_if_for_while = 0;
6775
6776 for (p = buf->b_p_cino; *p; )
6777 {
6778 l = p++;
6779 if (*p == '-')
6780 ++p;
6781 digits = p; /* remember where the digits start */
6782 n = getdigits(&p);
6783 divider = 0;
6784 if (*p == '.') /* ".5s" means a fraction */
6785 {
6786 fraction = atol((char *)++p);
6787 while (VIM_ISDIGIT(*p))
6788 {
6789 ++p;
6790 if (divider)
6791 divider *= 10;
6792 else
6793 divider = 10;
6794 }
6795 }
6796 if (*p == 's') /* "2s" means two times 'shiftwidth' */
6797 {
6798 if (p == digits)
6799 n = sw; /* just "s" is one 'shiftwidth' */
6800 else
6801 {
6802 n *= sw;
6803 if (divider)
6804 n += (sw * fraction + divider / 2) / divider;
6805 }
6806 ++p;
6807 }
6808 if (l[1] == '-')
6809 n = -n;
6810
6811 /* When adding an entry here, also update the default 'cinoptions' in
6812 * doc/indent.txt, and add explanation for it! */
6813 switch (*l)
6814 {
6815 case '>': buf->b_ind_level = n; break;
6816 case 'e': buf->b_ind_open_imag = n; break;
6817 case 'n': buf->b_ind_no_brace = n; break;
6818 case 'f': buf->b_ind_first_open = n; break;
6819 case '{': buf->b_ind_open_extra = n; break;
6820 case '}': buf->b_ind_close_extra = n; break;
6821 case '^': buf->b_ind_open_left_imag = n; break;
6822 case 'L': buf->b_ind_jump_label = n; break;
6823 case ':': buf->b_ind_case = n; break;
6824 case '=': buf->b_ind_case_code = n; break;
6825 case 'b': buf->b_ind_case_break = n; break;
6826 case 'p': buf->b_ind_param = n; break;
6827 case 't': buf->b_ind_func_type = n; break;
6828 case '/': buf->b_ind_comment = n; break;
6829 case 'c': buf->b_ind_in_comment = n; break;
6830 case 'C': buf->b_ind_in_comment2 = n; break;
6831 case 'i': buf->b_ind_cpp_baseclass = n; break;
6832 case '+': buf->b_ind_continuation = n; break;
6833 case '(': buf->b_ind_unclosed = n; break;
6834 case 'u': buf->b_ind_unclosed2 = n; break;
6835 case 'U': buf->b_ind_unclosed_noignore = n; break;
6836 case 'W': buf->b_ind_unclosed_wrapped = n; break;
6837 case 'w': buf->b_ind_unclosed_whiteok = n; break;
6838 case 'm': buf->b_ind_matching_paren = n; break;
6839 case 'M': buf->b_ind_paren_prev = n; break;
6840 case ')': buf->b_ind_maxparen = n; break;
6841 case '*': buf->b_ind_maxcomment = n; break;
6842 case 'g': buf->b_ind_scopedecl = n; break;
6843 case 'h': buf->b_ind_scopedecl_code = n; break;
6844 case 'j': buf->b_ind_java = n; break;
6845 case 'J': buf->b_ind_js = n; break;
6846 case 'l': buf->b_ind_keep_case_label = n; break;
6847 case '#': buf->b_ind_hash_comment = n; break;
6848 case 'N': buf->b_ind_cpp_namespace = n; break;
6849 case 'k': buf->b_ind_if_for_while = n; break;
6850 }
6851 if (*p == ',')
6852 ++p;
6853 }
6854}
6855
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 int
6857get_c_indent()
6858{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 pos_T cur_curpos;
6860 int amount;
6861 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00006862 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 colnr_T col;
6864 char_u *theline;
6865 char_u *linecopy;
6866 pos_T *trypos;
6867 pos_T *tryposBrace = NULL;
6868 pos_T our_paren_pos;
6869 char_u *start;
6870 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00006871#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872#define BRACE_AT_START 2 /* '{' is at start of line */
6873#define BRACE_AT_END 3 /* '{' is at end of line */
6874 linenr_T ourscope;
6875 char_u *l;
6876 char_u *look;
6877 char_u terminated;
6878 int lookfor;
6879#define LOOKFOR_INITIAL 0
6880#define LOOKFOR_IF 1
6881#define LOOKFOR_DO 2
6882#define LOOKFOR_CASE 3
6883#define LOOKFOR_ANY 4
6884#define LOOKFOR_TERM 5
6885#define LOOKFOR_UNTERM 6
6886#define LOOKFOR_SCOPEDECL 7
6887#define LOOKFOR_NOBREAK 8
6888#define LOOKFOR_CPP_BASECLASS 9
6889#define LOOKFOR_ENUM_OR_INIT 10
6890
6891 int whilelevel;
6892 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 int n;
6894 int iscase;
6895 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006896 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006898 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02006899 int added_to_amount = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006901 /* make a copy, value is changed below */
6902 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903
6904 /* remember where the cursor was when we started */
6905 cur_curpos = curwin->w_cursor;
6906
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006907 /* if we are at line 1 0 is fine, right? */
6908 if (cur_curpos.lnum == 1)
6909 return 0;
6910
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 /* Get a copy of the current contents of the line.
6912 * This is required, because only the most recent line obtained with
6913 * ml_get is valid! */
6914 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
6915 if (linecopy == NULL)
6916 return 0;
6917
6918 /*
6919 * In insert mode and the cursor is on a ')' truncate the line at the
6920 * cursor position. We don't want to line up with the matching '(' when
6921 * inserting new stuff.
6922 * For unknown reasons the cursor might be past the end of the line, thus
6923 * check for that.
6924 */
6925 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006926 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 && linecopy[curwin->w_cursor.col] == ')')
6928 linecopy[curwin->w_cursor.col] = NUL;
6929
6930 theline = skipwhite(linecopy);
6931
6932 /* move the cursor to the start of the line */
6933
6934 curwin->w_cursor.col = 0;
6935
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006936 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006937
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 /*
6939 * #defines and so on always go at the left when included in 'cinkeys'.
6940 */
6941 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006942 amount = curbuf->b_ind_hash_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943
6944 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006945 * Is it a non-case label? Then that goes at the left margin too unless:
6946 * - JS flag is set.
6947 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006949 else if (original_line_islabel && !curbuf->b_ind_js
6950 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 {
6952 amount = 0;
6953 }
6954
6955 /*
6956 * If we're inside a "//" comment and there is a "//" comment in a
6957 * previous line, lineup with that one.
6958 */
6959 else if (cin_islinecomment(theline)
6960 && (trypos = find_line_comment()) != NULL) /* XXX */
6961 {
6962 /* find how indented the line beginning the comment is */
6963 getvcol(curwin, trypos, &col, NULL, NULL);
6964 amount = col;
6965 }
6966
6967 /*
6968 * If we're inside a comment and not looking at the start of the
6969 * comment, try using the 'comments' option.
6970 */
6971 else if (!cin_iscomment(theline)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006972 && (trypos = ind_find_start_comment()) != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006973 /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 {
6975 int lead_start_len = 2;
6976 int lead_middle_len = 1;
6977 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
6978 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
6979 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6980 char_u *p;
6981 int start_align = 0;
6982 int start_off = 0;
6983 int done = FALSE;
6984
6985 /* find how indented the line beginning the comment is */
6986 getvcol(curwin, trypos, &col, NULL, NULL);
6987 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02006988 *lead_start = NUL;
6989 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990
6991 p = curbuf->b_p_com;
6992 while (*p != NUL)
6993 {
6994 int align = 0;
6995 int off = 0;
6996 int what = 0;
6997
6998 while (*p != NUL && *p != ':')
6999 {
7000 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
7001 what = *p++;
7002 else if (*p == COM_LEFT || *p == COM_RIGHT)
7003 align = *p++;
7004 else if (VIM_ISDIGIT(*p) || *p == '-')
7005 off = getdigits(&p);
7006 else
7007 ++p;
7008 }
7009
7010 if (*p == ':')
7011 ++p;
7012 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7013 if (what == COM_START)
7014 {
7015 STRCPY(lead_start, lead_end);
7016 lead_start_len = (int)STRLEN(lead_start);
7017 start_off = off;
7018 start_align = align;
7019 }
7020 else if (what == COM_MIDDLE)
7021 {
7022 STRCPY(lead_middle, lead_end);
7023 lead_middle_len = (int)STRLEN(lead_middle);
7024 }
7025 else if (what == COM_END)
7026 {
7027 /* If our line starts with the middle comment string, line it
7028 * up with the comment opener per the 'comments' option. */
7029 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7030 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7031 {
7032 done = TRUE;
7033 if (curwin->w_cursor.lnum > 1)
7034 {
7035 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007036 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 * the middle comment string matches in the previous
7038 * line, use the indent of that line. XXX */
7039 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7040 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7041 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7042 else if (STRNCMP(look, lead_middle,
7043 lead_middle_len) == 0)
7044 {
7045 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7046 break;
7047 }
7048 /* If the start comment string doesn't match with the
7049 * start of the comment, skip this entry. XXX */
7050 else if (STRNCMP(ml_get(trypos->lnum) + trypos->col,
7051 lead_start, lead_start_len) != 0)
7052 continue;
7053 }
7054 if (start_off != 0)
7055 amount += start_off;
7056 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007057 amount += vim_strsize(lead_start)
7058 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 break;
7060 }
7061
7062 /* If our line starts with the end comment string, line it up
7063 * with the middle comment */
7064 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7065 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7066 {
7067 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7068 /* XXX */
7069 if (off != 0)
7070 amount += off;
7071 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007072 amount += vim_strsize(lead_start)
7073 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 done = TRUE;
7075 break;
7076 }
7077 }
7078 }
7079
7080 /* If our line starts with an asterisk, line up with the
7081 * asterisk in the comment opener; otherwise, line up
7082 * with the first character of the comment text.
7083 */
7084 if (done)
7085 ;
7086 else if (theline[0] == '*')
7087 amount += 1;
7088 else
7089 {
7090 /*
7091 * If we are more than one line away from the comment opener, take
7092 * the indent of the previous non-empty line. If 'cino' has "CO"
7093 * and we are just below the comment opener and there are any
7094 * white characters after it line up with the text after it;
7095 * otherwise, add the amount specified by "c" in 'cino'
7096 */
7097 amount = -1;
7098 for (lnum = cur_curpos.lnum - 1; lnum > trypos->lnum; --lnum)
7099 {
7100 if (linewhite(lnum)) /* skip blank lines */
7101 continue;
7102 amount = get_indent_lnum(lnum); /* XXX */
7103 break;
7104 }
7105 if (amount == -1) /* use the comment opener */
7106 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007107 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 {
7109 start = ml_get(trypos->lnum);
7110 look = start + trypos->col + 2; /* skip / and * */
7111 if (*look != NUL) /* if something after it */
7112 trypos->col = (colnr_T)(skipwhite(look) - start);
7113 }
7114 getvcol(curwin, trypos, &col, NULL, NULL);
7115 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007116 if (curbuf->b_ind_in_comment2 || *look == NUL)
7117 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 }
7119 }
7120 }
7121
7122 /*
7123 * Are we inside parentheses or braces?
7124 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007125 else if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007126 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007127 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 || trypos != NULL)
7129 {
7130 if (trypos != NULL && tryposBrace != NULL)
7131 {
7132 /* Both an unmatched '(' and '{' is found. Use the one which is
7133 * closer to the current cursor position, set the other to NULL. */
7134 if (trypos->lnum != tryposBrace->lnum
7135 ? trypos->lnum < tryposBrace->lnum
7136 : trypos->col < tryposBrace->col)
7137 trypos = NULL;
7138 else
7139 tryposBrace = NULL;
7140 }
7141
7142 if (trypos != NULL)
7143 {
7144 /*
7145 * If the matching paren is more than one line away, use the indent of
7146 * a previous non-empty line that matches the same paren.
7147 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007148 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007150 /* Line up with the start of the matching paren line. */
7151 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7152 }
7153 else
7154 {
7155 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007156 our_paren_pos = *trypos;
7157 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007159 l = skipwhite(ml_get(lnum));
7160 if (cin_nocode(l)) /* skip comment lines */
7161 continue;
7162 if (cin_ispreproc_cont(&l, &lnum))
7163 continue; /* ignore #define, #if, etc. */
7164 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007166 /* Skip a comment. XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007167 if ((trypos = ind_find_start_comment()) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007168 {
7169 lnum = trypos->lnum + 1;
7170 continue;
7171 }
7172
7173 /* XXX */
7174 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007175 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007176 && trypos->lnum == our_paren_pos.lnum
7177 && trypos->col == our_paren_pos.col)
7178 {
7179 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007181 if (theline[0] == ')')
7182 {
7183 if (our_paren_pos.lnum != lnum
7184 && cur_amount > amount)
7185 cur_amount = amount;
7186 amount = -1;
7187 }
7188 break;
7189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 }
7191 }
7192
7193 /*
7194 * Line up with line where the matching paren is. XXX
7195 * If the line starts with a '(' or the indent for unclosed
7196 * parentheses is zero, line up with the unclosed parentheses.
7197 */
7198 if (amount == -1)
7199 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007200 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007201 int is_if_for_while = 0;
7202
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007203 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007204 {
7205 /* Look for the outermost opening parenthesis on this line
7206 * and check whether it belongs to an "if", "for" or "while". */
7207
7208 pos_T cursor_save = curwin->w_cursor;
7209 pos_T outermost;
7210 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007211
7212 trypos = &our_paren_pos;
7213 do {
7214 outermost = *trypos;
7215 curwin->w_cursor.lnum = outermost.lnum;
7216 curwin->w_cursor.col = outermost.col;
7217
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007218 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007219 } while (trypos && trypos->lnum == outermost.lnum);
7220
7221 curwin->w_cursor = cursor_save;
7222
7223 line = ml_get(outermost.lnum);
7224
7225 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007226 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007227 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007228
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007229 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007230 look = skipwhite(look);
7231 if (*look == '(')
7232 {
7233 linenr_T save_lnum = curwin->w_cursor.lnum;
7234 char_u *line;
7235 int look_col;
7236
7237 /* Ignore a '(' in front of the line that has a match before
7238 * our matching '('. */
7239 curwin->w_cursor.lnum = our_paren_pos.lnum;
7240 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007241 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007242 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007243 if ((trypos = findmatchlimit(NULL, ')', 0,
7244 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007245 != NULL
7246 && trypos->lnum == our_paren_pos.lnum
7247 && trypos->col < our_paren_pos.col)
7248 ignore_paren_col = trypos->col + 1;
7249
7250 curwin->w_cursor.lnum = save_lnum;
7251 look = ml_get(our_paren_pos.lnum) + look_col;
7252 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007253 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7254 && is_if_for_while == 0)
7255 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007256 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 {
7258 /*
7259 * If we're looking at a close paren, line up right there;
7260 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007261 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 * the last nonwhite character of the line, use either the
7263 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007264 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 * lines).
7266 */
7267 if (theline[0] != ')')
7268 {
7269 cur_amount = MAXCOL;
7270 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007271 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 && cin_ends_in(l, (char_u *)"(", NULL))
7273 {
7274 /* look for opening unmatched paren, indent one level
7275 * for each additional level */
7276 n = 1;
7277 for (col = 0; col < our_paren_pos.col; ++col)
7278 {
7279 switch (l[col])
7280 {
7281 case '(':
7282 case '{': ++n;
7283 break;
7284
7285 case ')':
7286 case '}': if (n > 1)
7287 --n;
7288 break;
7289 }
7290 }
7291
7292 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007293 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007295 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 our_paren_pos.col++;
7297 else
7298 {
7299 col = our_paren_pos.col + 1;
7300 while (vim_iswhite(l[col]))
7301 col++;
7302 if (l[col] != NUL) /* In case of trailing space */
7303 our_paren_pos.col = col;
7304 else
7305 our_paren_pos.col++;
7306 }
7307 }
7308
7309 /*
7310 * Find how indented the paren is, or the character after it
7311 * if we did the above "if".
7312 */
7313 if (our_paren_pos.col > 0)
7314 {
7315 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7316 if (cur_amount > (int)col)
7317 cur_amount = col;
7318 }
7319 }
7320
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007321 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 {
7323 /* Line up with the start of the matching paren line. */
7324 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007325 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7326 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007327 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 {
7329 if (cur_amount != MAXCOL)
7330 amount = cur_amount;
7331 }
7332 else
7333 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007334 /* Add b_ind_unclosed2 for each '(' before our matching one,
7335 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007337 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 {
7339 --our_paren_pos.col;
7340 switch (*ml_get_pos(&our_paren_pos))
7341 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007342 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 col = our_paren_pos.col;
7344 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007345 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 col = MAXCOL;
7347 break;
7348 }
7349 }
7350
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007351 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 * braces */
7353 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007354 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 else
7356 {
7357 curwin->w_cursor.lnum = our_paren_pos.lnum;
7358 curwin->w_cursor.col = col;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007359 if (find_match_paren(curbuf->b_ind_maxparen) != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007360 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007362 {
7363 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007364 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007365 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007366 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 }
7369 /*
7370 * For a line starting with ')' use the minimum of the two
7371 * positions, to avoid giving it more indent than the previous
7372 * lines:
7373 * func_long_name( if (x
7374 * arg && yy
7375 * ) ^ not here ) ^ not here
7376 */
7377 if (cur_amount < amount)
7378 amount = cur_amount;
7379 }
7380 }
7381
7382 /* add extra indent for a comment */
7383 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007384 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 }
7386
7387 /*
7388 * Are we at least inside braces, then?
7389 */
7390 else
7391 {
7392 trypos = tryposBrace;
7393
7394 ourscope = trypos->lnum;
7395 start = ml_get(ourscope);
7396
7397 /*
7398 * Now figure out how indented the line is in general.
7399 * If the brace was at the start of the line, we use that;
7400 * otherwise, check out the indentation of the line as
7401 * a whole and then add the "imaginary indent" to that.
7402 */
7403 look = skipwhite(start);
7404 if (*look == '{')
7405 {
7406 getvcol(curwin, trypos, &col, NULL, NULL);
7407 amount = col;
7408 if (*start == '{')
7409 start_brace = BRACE_IN_COL0;
7410 else
7411 start_brace = BRACE_AT_START;
7412 }
7413 else
7414 {
7415 /*
7416 * that opening brace might have been on a continuation
7417 * line. if so, find the start of the line.
7418 */
7419 curwin->w_cursor.lnum = ourscope;
7420
7421 /*
7422 * position the cursor over the rightmost paren, so that
7423 * matching it will take us back to the start of the line.
7424 */
7425 lnum = ourscope;
7426 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007427 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7428 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 lnum = trypos->lnum;
7430
7431 /*
7432 * It could have been something like
7433 * case 1: if (asdf &&
7434 * ldfd) {
7435 * }
7436 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007437 if (curbuf->b_ind_js || (curbuf->b_ind_keep_case_label
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007438 && cin_iscase(skipwhite(ml_get_curline()), FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 amount = get_indent();
7440 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007441 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442
7443 start_brace = BRACE_AT_END;
7444 }
7445
7446 /*
7447 * if we're looking at a closing brace, that's where
7448 * we want to be. otherwise, add the amount of room
7449 * that an indent is supposed to be.
7450 */
7451 if (theline[0] == '}')
7452 {
7453 /*
7454 * they may want closing braces to line up with something
7455 * other than the open brace. indulge them, if so.
7456 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007457 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 }
7459 else
7460 {
7461 /*
7462 * If we're looking at an "else", try to find an "if"
7463 * to match it with.
7464 * If we're looking at a "while", try to find a "do"
7465 * to match it with.
7466 */
7467 lookfor = LOOKFOR_INITIAL;
7468 if (cin_iselse(theline))
7469 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007470 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 lookfor = LOOKFOR_DO;
7472 if (lookfor != LOOKFOR_INITIAL)
7473 {
7474 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007475 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 {
7477 amount = get_indent(); /* XXX */
7478 goto theend;
7479 }
7480 }
7481
7482 /*
7483 * We get here if we are not on an "while-of-do" or "else" (or
7484 * failed to find a matching "if").
7485 * Search backwards for something to line up with.
7486 * First set amount for when we don't find anything.
7487 */
7488
7489 /*
7490 * if the '{' is _really_ at the left margin, use the imaginary
7491 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007492 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 */
7494
7495 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7496 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007497 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007498 lookfor_cpp_namespace = TRUE;
7499 }
7500 else if (start_brace == BRACE_AT_START &&
7501 lookfor_cpp_namespace) /* '{' is at start */
7502 {
7503
7504 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 }
7506 else
7507 {
7508 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007509 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007510 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007511
7512 l = skipwhite(ml_get_curline());
7513 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007514 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 else
7517 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007518 /* Compensate for adding b_ind_open_extra later. */
7519 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 if (amount < 0)
7521 amount = 0;
7522 }
7523 }
7524
7525 lookfor_break = FALSE;
7526
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007527 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 {
7529 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007530 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 }
7532 else if (cin_isscopedecl(theline)) /* private:, ... */
7533 {
7534 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007535 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 }
7537 else
7538 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007539 if (curbuf->b_ind_case_break && cin_isbreak(theline))
7540 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 lookfor_break = TRUE;
7542
7543 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007544 /* b_ind_level from start of block */
7545 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 }
7547 scope_amount = amount;
7548 whilelevel = 0;
7549
7550 /*
7551 * Search backwards. If we find something we recognize, line up
7552 * with that.
7553 *
7554 * if we're looking at an open brace, indent
7555 * the usual amount relative to the conditional
7556 * that opens the block.
7557 */
7558 curwin->w_cursor = cur_curpos;
7559 for (;;)
7560 {
7561 curwin->w_cursor.lnum--;
7562 curwin->w_cursor.col = 0;
7563
7564 /*
7565 * If we went all the way back to the start of our scope, line
7566 * up with it.
7567 */
7568 if (curwin->w_cursor.lnum <= ourscope)
7569 {
7570 /* we reached end of scope:
7571 * if looking for a enum or structure initialization
7572 * go further back:
7573 * if it is an initializer (enum xxx or xxx =), then
7574 * don't add ind_continuation, otherwise it is a variable
7575 * declaration:
7576 * int x,
7577 * here; <-- add ind_continuation
7578 */
7579 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7580 {
7581 if (curwin->w_cursor.lnum == 0
7582 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007583 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007585 /* nothing found (abuse curbuf->b_ind_maxparen as
7586 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 * initialization) */
7588 if (cont_amount > 0)
7589 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007590 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 amount += ind_continuation;
7592 break;
7593 }
7594
7595 l = ml_get_curline();
7596
7597 /*
7598 * If we're in a comment now, skip to the start of the
7599 * comment.
7600 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007601 trypos = ind_find_start_comment();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 if (trypos != NULL)
7603 {
7604 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007605 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 continue;
7607 }
7608
7609 /*
7610 * Skip preprocessor directives and blank lines.
7611 */
7612 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7613 continue;
7614
7615 if (cin_nocode(l))
7616 continue;
7617
7618 terminated = cin_isterminated(l, FALSE, TRUE);
7619
7620 /*
7621 * If we are at top level and the line looks like a
7622 * function declaration, we are done
7623 * (it's a variable declaration).
7624 */
7625 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007626 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 {
7628 /* if the line is terminated with another ','
7629 * it is a continued variable initialization.
7630 * don't add extra indent.
7631 * TODO: does not work, if a function
7632 * declaration is split over multiple lines:
7633 * cin_isfuncdecl returns FALSE then.
7634 */
7635 if (terminated == ',')
7636 break;
7637
7638 /* if it es a enum declaration or an assignment,
7639 * we are done.
7640 */
7641 if (terminated != ';' && cin_isinit())
7642 break;
7643
7644 /* nothing useful found */
7645 if (terminated == 0 || terminated == '{')
7646 continue;
7647 }
7648
7649 if (terminated != ';')
7650 {
7651 /* Skip parens and braces. Position the cursor
7652 * over the rightmost paren, so that matching it
7653 * will take us back to the start of the line.
7654 */ /* XXX */
7655 trypos = NULL;
7656 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007657 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007658 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659
7660 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007661 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662
7663 if (trypos != NULL)
7664 {
7665 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007666 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 continue;
7668 }
7669 }
7670
7671 /* it's a variable declaration, add indentation
7672 * like in
7673 * int a,
7674 * b;
7675 */
7676 if (cont_amount > 0)
7677 amount = cont_amount;
7678 else
7679 amount += ind_continuation;
7680 }
7681 else if (lookfor == LOOKFOR_UNTERM)
7682 {
7683 if (cont_amount > 0)
7684 amount = cont_amount;
7685 else
7686 amount += ind_continuation;
7687 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02007688 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007689 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02007690 if (lookfor != LOOKFOR_TERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 && lookfor != LOOKFOR_CPP_BASECLASS)
Bram Moolenaare79d1532011-10-04 18:03:47 +02007692 {
7693 amount = scope_amount;
7694 if (theline[0] == '{')
7695 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007696 amount += curbuf->b_ind_open_extra;
7697 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007698 }
7699 }
7700
7701 if (lookfor_cpp_namespace)
7702 {
7703 /*
7704 * Looking for C++ namespace, need to look further
7705 * back.
7706 */
7707 if (curwin->w_cursor.lnum == ourscope)
7708 continue;
7709
7710 if (curwin->w_cursor.lnum == 0
7711 || curwin->w_cursor.lnum
7712 < ourscope - FIND_NAMESPACE_LIM)
7713 break;
7714
7715 l = ml_get_curline();
7716
7717 /* If we're in a comment now, skip to the start of
7718 * the comment. */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007719 trypos = ind_find_start_comment();
Bram Moolenaare79d1532011-10-04 18:03:47 +02007720 if (trypos != NULL)
7721 {
7722 curwin->w_cursor.lnum = trypos->lnum + 1;
7723 curwin->w_cursor.col = 0;
7724 continue;
7725 }
7726
7727 /* Skip preprocessor directives and blank lines. */
7728 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7729 continue;
7730
7731 /* Finally the actual check for "namespace". */
7732 if (cin_is_cpp_namespace(l))
7733 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007734 amount += curbuf->b_ind_cpp_namespace
7735 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007736 break;
7737 }
7738
7739 if (cin_nocode(l))
7740 continue;
7741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 }
7743 break;
7744 }
7745
7746 /*
7747 * If we're in a comment now, skip to the start of the comment.
7748 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007749 if ((trypos = ind_find_start_comment()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 {
7751 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007752 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 continue;
7754 }
7755
7756 l = ml_get_curline();
7757
7758 /*
7759 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00007760 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007762 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 if (iscase || cin_isscopedecl(l))
7764 {
7765 /* we are only looking for cpp base class
7766 * declaration/initialization any longer */
7767 if (lookfor == LOOKFOR_CPP_BASECLASS)
7768 break;
7769
7770 /* When looking for a "do" we are not interested in
7771 * labels. */
7772 if (whilelevel > 0)
7773 continue;
7774
7775 /*
7776 * case xx:
7777 * c = 99 + <- this indent plus continuation
7778 *-> here;
7779 */
7780 if (lookfor == LOOKFOR_UNTERM
7781 || lookfor == LOOKFOR_ENUM_OR_INIT)
7782 {
7783 if (cont_amount > 0)
7784 amount = cont_amount;
7785 else
7786 amount += ind_continuation;
7787 break;
7788 }
7789
7790 /*
7791 * case xx: <- line up with this case
7792 * x = 333;
7793 * case yy:
7794 */
7795 if ( (iscase && lookfor == LOOKFOR_CASE)
7796 || (iscase && lookfor_break)
7797 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
7798 {
7799 /*
7800 * Check that this case label is not for another
7801 * switch()
7802 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007803 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007804 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 {
7806 amount = get_indent(); /* XXX */
7807 break;
7808 }
7809 continue;
7810 }
7811
7812 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
7813
7814 /*
7815 * case xx: if (cond) <- line up with this if
7816 * y = y + 1;
7817 * -> s = 99;
7818 *
7819 * case xx:
7820 * if (cond) <- line up with this line
7821 * y = y + 1;
7822 * -> s = 99;
7823 */
7824 if (lookfor == LOOKFOR_TERM)
7825 {
7826 if (n)
7827 amount = n;
7828
7829 if (!lookfor_break)
7830 break;
7831 }
7832
7833 /*
7834 * case xx: x = x + 1; <- line up with this x
7835 * -> y = y + 1;
7836 *
7837 * case xx: if (cond) <- line up with this if
7838 * -> y = y + 1;
7839 */
7840 if (n)
7841 {
7842 amount = n;
7843 l = after_label(ml_get_curline());
7844 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007845 {
7846 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007847 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007848 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007849 amount += curbuf->b_ind_level
7850 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 break;
7853 }
7854
7855 /*
7856 * Try to get the indent of a statement before the switch
7857 * label. If nothing is found, line up relative to the
7858 * switch label.
7859 * break; <- may line up with this line
7860 * case xx:
7861 * -> y = 1;
7862 */
7863 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007864 ? curbuf->b_ind_case_code
7865 : curbuf->b_ind_scopedecl_code);
7866 lookfor = curbuf->b_ind_case_break
7867 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 continue;
7869 }
7870
7871 /*
7872 * Looking for a switch() label or C++ scope declaration,
7873 * ignore other lines, skip {}-blocks.
7874 */
7875 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
7876 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007877 if (find_last_paren(l, '{', '}')
7878 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007879 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007881 curwin->w_cursor.col = 0;
7882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 continue;
7884 }
7885
7886 /*
7887 * Ignore jump labels with nothing after them.
7888 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007889 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 {
7891 l = after_label(ml_get_curline());
7892 if (l == NULL || cin_nocode(l))
7893 continue;
7894 }
7895
7896 /*
7897 * Ignore #defines, #if, etc.
7898 * Ignore comment and empty lines.
7899 * (need to get the line again, cin_islabel() may have
7900 * unlocked it)
7901 */
7902 l = ml_get_curline();
7903 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)
7904 || cin_nocode(l))
7905 continue;
7906
7907 /*
7908 * Are we at the start of a cpp base class declaration or
7909 * constructor initialization?
7910 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00007911 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007912 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00007913 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00007914 n = cin_is_cpp_baseclass(&col);
Bram Moolenaar18144c82006-04-12 21:52:12 +00007915 l = ml_get_curline();
7916 }
7917 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 {
7919 if (lookfor == LOOKFOR_UNTERM)
7920 {
7921 if (cont_amount > 0)
7922 amount = cont_amount;
7923 else
7924 amount += ind_continuation;
7925 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00007926 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00007928 /* Need to find start of the declaration. */
7929 lookfor = LOOKFOR_UNTERM;
7930 ind_continuation = 0;
7931 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 }
7933 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00007934 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007935 amount = get_baseclass_amount(col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 break;
7937 }
7938 else if (lookfor == LOOKFOR_CPP_BASECLASS)
7939 {
7940 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00007941 * declaration or initialization before the opening brace.
7942 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 if (cin_isterminated(l, TRUE, FALSE))
7944 break;
7945 else
7946 continue;
7947 }
7948
7949 /*
7950 * What happens next depends on the line being terminated.
7951 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00007952 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 * 123,
7954 * sizeof
7955 * here
7956 * Otherwise check whether it is a enumeration or structure
7957 * initialisation (not indented) or a variable declaration
7958 * (indented).
7959 */
7960 terminated = cin_isterminated(l, FALSE, TRUE);
7961
7962 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
7963 && terminated == ','))
7964 {
7965 /*
7966 * if we're in the middle of a paren thing,
7967 * go back to the line that starts it so
7968 * we can get the right prevailing indent
7969 * if ( foo &&
7970 * bar )
7971 */
7972 /*
7973 * position the cursor over the rightmost paren, so that
7974 * matching it will take us back to the start of the line.
7975 */
7976 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007977 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978
7979 /*
7980 * If we are looking for ',', we also look for matching
7981 * braces.
7982 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00007983 if (trypos == NULL && terminated == ','
7984 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007985 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986
7987 if (trypos != NULL)
7988 {
7989 /*
7990 * Check if we are on a case label now. This is
7991 * handled above.
7992 * case xx: if ( asdf &&
7993 * asdf)
7994 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007995 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007997 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 {
7999 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008000 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 continue;
8002 }
8003 }
8004
8005 /*
8006 * Skip over continuation lines to find the one to get the
8007 * indent from
8008 * char *usethis = "bla\
8009 * bla",
8010 * here;
8011 */
8012 if (terminated == ',')
8013 {
8014 while (curwin->w_cursor.lnum > 1)
8015 {
8016 l = ml_get(curwin->w_cursor.lnum - 1);
8017 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8018 break;
8019 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008020 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 }
8022 }
8023
8024 /*
8025 * Get indent and pointer to text for current line,
8026 * ignoring any jump label. XXX
8027 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008028 if (!curbuf->b_ind_js)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008029 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008030 else
8031 cur_amount = get_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 /*
8033 * If this is just above the line we are indenting, and it
8034 * starts with a '{', line it up with this line.
8035 * while (not)
8036 * -> {
8037 * }
8038 */
8039 if (terminated != ',' && lookfor != LOOKFOR_TERM
8040 && theline[0] == '{')
8041 {
8042 amount = cur_amount;
8043 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008044 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 * doesn't start with a '{', which must have a match
8046 * in the same line (scope is the same). Probably:
8047 * { 1, 2 },
8048 * -> { 3, 4 }
8049 */
8050 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008051 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008053 if (curbuf->b_ind_cpp_baseclass)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 {
8055 /* have to look back, whether it is a cpp base
8056 * class declaration or initialization */
8057 lookfor = LOOKFOR_CPP_BASECLASS;
8058 continue;
8059 }
8060 break;
8061 }
8062
8063 /*
8064 * Check if we are after an "if", "while", etc.
8065 * Also allow " } else".
8066 */
8067 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8068 {
8069 /*
8070 * Found an unterminated line after an if (), line up
8071 * with the last one.
8072 * if (cond)
8073 * 100 +
8074 * -> here;
8075 */
8076 if (lookfor == LOOKFOR_UNTERM
8077 || lookfor == LOOKFOR_ENUM_OR_INIT)
8078 {
8079 if (cont_amount > 0)
8080 amount = cont_amount;
8081 else
8082 amount += ind_continuation;
8083 break;
8084 }
8085
8086 /*
8087 * If this is just above the line we are indenting, we
8088 * are finished.
8089 * while (not)
8090 * -> here;
8091 * Otherwise this indent can be used when the line
8092 * before this is terminated.
8093 * yyy;
8094 * if (stat)
8095 * while (not)
8096 * xxx;
8097 * -> here;
8098 */
8099 amount = cur_amount;
8100 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008101 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 if (lookfor != LOOKFOR_TERM)
8103 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008104 amount += curbuf->b_ind_level
8105 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 break;
8107 }
8108
8109 /*
8110 * Special trick: when expecting the while () after a
8111 * do, line up with the while()
8112 * do
8113 * x = 1;
8114 * -> here
8115 */
8116 l = skipwhite(ml_get_curline());
8117 if (cin_isdo(l))
8118 {
8119 if (whilelevel == 0)
8120 break;
8121 --whilelevel;
8122 }
8123
8124 /*
8125 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008126 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 * Need to use the scope of this "else". XXX
8128 * If whilelevel != 0 continue looking for a "do {".
8129 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008130 if (cin_iselse(l) && whilelevel == 0)
8131 {
8132 /* If we're looking at "} else", let's make sure we
8133 * find the opening brace of the enclosing scope,
8134 * not the one from "if () {". */
8135 if (*l == '}')
8136 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008137 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008138
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008139 if ((trypos = find_start_brace()) == NULL
8140 || find_match(LOOKFOR_IF, trypos->lnum)
8141 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008142 break;
8143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 }
8145
8146 /*
8147 * If we're below an unterminated line that is not an
8148 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008149 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 * the line before this one.
8151 */
8152 else
8153 {
8154 /*
8155 * Found two unterminated lines on a row, line up with
8156 * the last one.
8157 * c = 99 +
8158 * 100 +
8159 * -> here;
8160 */
8161 if (lookfor == LOOKFOR_UNTERM)
8162 {
8163 /* When line ends in a comma add extra indent */
8164 if (terminated == ',')
8165 amount += ind_continuation;
8166 break;
8167 }
8168
8169 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8170 {
8171 /* Found two lines ending in ',', lineup with the
8172 * lowest one, but check for cpp base class
8173 * declaration/initialization, if it is an
8174 * opening brace or we are looking just for
8175 * enumerations/initializations. */
8176 if (terminated == ',')
8177 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008178 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 break;
8180
8181 lookfor = LOOKFOR_CPP_BASECLASS;
8182 continue;
8183 }
8184
8185 /* Ignore unterminated lines in between, but
8186 * reduce indent. */
8187 if (amount > cur_amount)
8188 amount = cur_amount;
8189 }
8190 else
8191 {
8192 /*
8193 * Found first unterminated line on a row, may
8194 * line up with this line, remember its indent
8195 * 100 +
8196 * -> here;
8197 */
8198 amount = cur_amount;
8199
8200 /*
8201 * If previous line ends in ',', check whether we
8202 * are in an initialization or enum
8203 * struct xxx =
8204 * {
8205 * sizeof a,
8206 * 124 };
8207 * or a normal possible continuation line.
8208 * but only, of no other statement has been found
8209 * yet.
8210 */
8211 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8212 {
8213 lookfor = LOOKFOR_ENUM_OR_INIT;
8214 cont_amount = cin_first_id_amount();
8215 }
8216 else
8217 {
8218 if (lookfor == LOOKFOR_INITIAL
8219 && *l != NUL
8220 && l[STRLEN(l) - 1] == '\\')
8221 /* XXX */
8222 cont_amount = cin_get_equal_amount(
8223 curwin->w_cursor.lnum);
8224 if (lookfor != LOOKFOR_TERM)
8225 lookfor = LOOKFOR_UNTERM;
8226 }
8227 }
8228 }
8229 }
8230
8231 /*
8232 * Check if we are after a while (cond);
8233 * If so: Ignore until the matching "do".
8234 */
8235 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008236 else if (cin_iswhileofdo_end(terminated))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 {
8238 /*
8239 * Found an unterminated line after a while ();, line up
8240 * with the last one.
8241 * while (cond);
8242 * 100 + <- line up with this one
8243 * -> here;
8244 */
8245 if (lookfor == LOOKFOR_UNTERM
8246 || lookfor == LOOKFOR_ENUM_OR_INIT)
8247 {
8248 if (cont_amount > 0)
8249 amount = cont_amount;
8250 else
8251 amount += ind_continuation;
8252 break;
8253 }
8254
8255 if (whilelevel == 0)
8256 {
8257 lookfor = LOOKFOR_TERM;
8258 amount = get_indent(); /* XXX */
8259 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008260 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 }
8262 ++whilelevel;
8263 }
8264
8265 /*
8266 * We are after a "normal" statement.
8267 * If we had another statement we can stop now and use the
8268 * indent of that other statement.
8269 * Otherwise the indent of the current statement may be used,
8270 * search backwards for the next "normal" statement.
8271 */
8272 else
8273 {
8274 /*
8275 * Skip single break line, if before a switch label. It
8276 * may be lined up with the case label.
8277 */
8278 if (lookfor == LOOKFOR_NOBREAK
8279 && cin_isbreak(skipwhite(ml_get_curline())))
8280 {
8281 lookfor = LOOKFOR_ANY;
8282 continue;
8283 }
8284
8285 /*
8286 * Handle "do {" line.
8287 */
8288 if (whilelevel > 0)
8289 {
8290 l = cin_skipcomment(ml_get_curline());
8291 if (cin_isdo(l))
8292 {
8293 amount = get_indent(); /* XXX */
8294 --whilelevel;
8295 continue;
8296 }
8297 }
8298
8299 /*
8300 * Found a terminated line above an unterminated line. Add
8301 * the amount for a continuation line.
8302 * x = 1;
8303 * y = foo +
8304 * -> here;
8305 * or
8306 * int x = 1;
8307 * int foo,
8308 * -> here;
8309 */
8310 if (lookfor == LOOKFOR_UNTERM
8311 || lookfor == LOOKFOR_ENUM_OR_INIT)
8312 {
8313 if (cont_amount > 0)
8314 amount = cont_amount;
8315 else
8316 amount += ind_continuation;
8317 break;
8318 }
8319
8320 /*
8321 * Found a terminated line above a terminated line or "if"
8322 * etc. line. Use the amount of the line below us.
8323 * x = 1; x = 1;
8324 * if (asdf) y = 2;
8325 * while (asdf) ->here;
8326 * here;
8327 * ->foo;
8328 */
8329 if (lookfor == LOOKFOR_TERM)
8330 {
8331 if (!lookfor_break && whilelevel == 0)
8332 break;
8333 }
8334
8335 /*
8336 * First line above the one we're indenting is terminated.
8337 * To know what needs to be done look further backward for
8338 * a terminated line.
8339 */
8340 else
8341 {
8342 /*
8343 * position the cursor over the rightmost paren, so
8344 * that matching it will take us back to the start of
8345 * the line. Helps for:
8346 * func(asdr,
8347 * asdfasdf);
8348 * here;
8349 */
8350term_again:
8351 l = ml_get_curline();
8352 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008353 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008354 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 {
8356 /*
8357 * Check if we are on a case label now. This is
8358 * handled above.
8359 * case xx: if ( asdf &&
8360 * asdf)
8361 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008362 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008364 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 {
8366 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008367 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 continue;
8369 }
8370 }
8371
8372 /* When aligning with the case statement, don't align
8373 * with a statement after it.
8374 * case 1: { <-- don't use this { position
8375 * stat;
8376 * }
8377 * case 2:
8378 * stat;
8379 * }
8380 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008381 iscase = (curbuf->b_ind_keep_case_label
8382 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383
8384 /*
8385 * Get indent and pointer to text for current line,
8386 * ignoring any jump label.
8387 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008388 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389
8390 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008391 amount += curbuf->b_ind_open_extra;
8392 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008393 l = skipwhite(l);
8394 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008395 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8397
8398 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008399 * When a terminated line starts with "else" skip to
8400 * the matching "if":
8401 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008402 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008403 * Need to use the scope of this "else". XXX
8404 * If whilelevel != 0 continue looking for a "do {".
8405 */
8406 if (lookfor == LOOKFOR_TERM
8407 && *l != '}'
8408 && cin_iselse(l)
8409 && whilelevel == 0)
8410 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008411 if ((trypos = find_start_brace()) == NULL
8412 || find_match(LOOKFOR_IF, trypos->lnum)
8413 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008414 break;
8415 continue;
8416 }
8417
8418 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 * If we're at the end of a block, skip to the start of
8420 * that block.
8421 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008422 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008423 if (find_last_paren(l, '{', '}') /* XXX */
8424 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008426 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 /* if not "else {" check for terminated again */
8428 /* but skip block for "} else {" */
8429 l = cin_skipcomment(ml_get_curline());
8430 if (*l == '}' || !cin_iselse(l))
8431 goto term_again;
8432 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008433 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 }
8435 }
8436 }
8437 }
8438 }
8439 }
8440
8441 /* add extra indent for a comment */
8442 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008443 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008444
8445 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008446 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
8447 amount -= curbuf->b_ind_jump_label;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 }
8449
8450 /*
8451 * ok -- we're not inside any sort of structure at all!
8452 *
8453 * this means we're at the top level, and everything should
8454 * basically just match where the previous line is, except
8455 * for the lines immediately following a function declaration,
8456 * which are K&R-style parameters and need to be indented.
8457 */
8458 else
8459 {
8460 /*
8461 * if our line starts with an open brace, forget about any
8462 * prevailing indent and make sure it looks like the start
8463 * of a function
8464 */
8465
8466 if (theline[0] == '{')
8467 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008468 amount = curbuf->b_ind_first_open;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 }
8470
8471 /*
8472 * If the NEXT line is a function declaration, the current
8473 * line needs to be indented as a function type spec.
Bram Moolenaar1a89bbe2010-03-02 12:38:22 +01008474 * Don't do this if the current line looks like a comment or if the
8475 * current line is terminated, ie. ends in ';', or if the current line
8476 * contains { or }: "void f() {\n if (1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 */
8478 else if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8479 && !cin_nocode(theline)
Bram Moolenaar1a89bbe2010-03-02 12:38:22 +01008480 && vim_strchr(theline, '{') == NULL
8481 && vim_strchr(theline, '}') == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 && !cin_ends_in(theline, (char_u *)":", NULL)
8483 && !cin_ends_in(theline, (char_u *)",", NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01008484 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008485 cur_curpos.lnum + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 && !cin_isterminated(theline, FALSE, TRUE))
8487 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008488 amount = curbuf->b_ind_func_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 }
8490 else
8491 {
8492 amount = 0;
8493 curwin->w_cursor = cur_curpos;
8494
8495 /* search backwards until we find something we recognize */
8496
8497 while (curwin->w_cursor.lnum > 1)
8498 {
8499 curwin->w_cursor.lnum--;
8500 curwin->w_cursor.col = 0;
8501
8502 l = ml_get_curline();
8503
8504 /*
8505 * If we're in a comment now, skip to the start of the comment.
8506 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008507 if ((trypos = ind_find_start_comment()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 {
8509 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008510 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 continue;
8512 }
8513
8514 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008515 * Are we at the start of a cpp base class declaration or
8516 * constructor initialization?
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008518 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008519 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar18144c82006-04-12 21:52:12 +00008520 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00008521 n = cin_is_cpp_baseclass(&col);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008522 l = ml_get_curline();
8523 }
8524 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008526 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008527 amount = get_baseclass_amount(col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 break;
8529 }
8530
8531 /*
8532 * Skip preprocessor directives and blank lines.
8533 */
8534 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
8535 continue;
8536
8537 if (cin_nocode(l))
8538 continue;
8539
8540 /*
8541 * If the previous line ends in ',', use one level of
8542 * indentation:
8543 * int foo,
8544 * bar;
8545 * do this before checking for '}' in case of eg.
8546 * enum foobar
8547 * {
8548 * ...
8549 * } foo,
8550 * bar;
8551 */
8552 n = 0;
8553 if (cin_ends_in(l, (char_u *)",", NULL)
8554 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
8555 {
8556 /* take us back to opening paren */
8557 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008558 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008559 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008560 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561
8562 /* For a line ending in ',' that is a continuation line go
8563 * back to the first line with a backslash:
8564 * char *foo = "bla\
8565 * bla",
8566 * here;
8567 */
8568 while (n == 0 && curwin->w_cursor.lnum > 1)
8569 {
8570 l = ml_get(curwin->w_cursor.lnum - 1);
8571 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8572 break;
8573 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008574 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 }
8576
8577 amount = get_indent(); /* XXX */
8578
8579 if (amount == 0)
8580 amount = cin_first_id_amount();
8581 if (amount == 0)
8582 amount = ind_continuation;
8583 break;
8584 }
8585
8586 /*
8587 * If the line looks like a function declaration, and we're
8588 * not in a comment, put it the left margin.
8589 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008590 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 break;
8592 l = ml_get_curline();
8593
8594 /*
8595 * Finding the closing '}' of a previous function. Put
8596 * current line at the left margin. For when 'cino' has "fs".
8597 */
8598 if (*skipwhite(l) == '}')
8599 break;
8600
8601 /* (matching {)
8602 * If the previous line ends on '};' (maybe followed by
8603 * comments) align at column 0. For example:
8604 * char *string_array[] = { "foo",
8605 * / * x * / "b};ar" }; / * foobar * /
8606 */
8607 if (cin_ends_in(l, (char_u *)"};", NULL))
8608 break;
8609
8610 /*
Bram Moolenaar3388bb42011-11-30 17:20:23 +01008611 * Find a line only has a semicolon that belongs to a previous
8612 * line ending in '}', e.g. before an #endif. Don't increase
8613 * indent then.
8614 */
8615 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
8616 {
8617 pos_T curpos_save = curwin->w_cursor;
8618
8619 while (curwin->w_cursor.lnum > 1)
8620 {
8621 look = ml_get(--curwin->w_cursor.lnum);
8622 if (!(cin_nocode(look) || cin_ispreproc_cont(
8623 &look, &curwin->w_cursor.lnum)))
8624 break;
8625 }
8626 if (curwin->w_cursor.lnum > 0
8627 && cin_ends_in(look, (char_u *)"}", NULL))
8628 break;
8629
8630 curwin->w_cursor = curpos_save;
8631 }
8632
8633 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 * If the PREVIOUS line is a function declaration, the current
8635 * line (and the ones that follow) needs to be indented as
8636 * parameters.
8637 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008638 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008640 amount = curbuf->b_ind_param;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 break;
8642 }
8643
8644 /*
8645 * If the previous line ends in ';' and the line before the
8646 * previous line ends in ',' or '\', ident to column zero:
8647 * int foo,
8648 * bar;
8649 * indent_to_0 here;
8650 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008651 if (cin_ends_in(l, (char_u *)";", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 {
8653 l = ml_get(curwin->w_cursor.lnum - 1);
8654 if (cin_ends_in(l, (char_u *)",", NULL)
8655 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
8656 break;
8657 l = ml_get_curline();
8658 }
8659
8660 /*
8661 * Doesn't look like anything interesting -- so just
8662 * use the indent of this line.
8663 *
8664 * Position the cursor over the rightmost paren, so that
8665 * matching it will take us back to the start of the line.
8666 */
8667 find_last_paren(l, '(', ')');
8668
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008669 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008670 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 amount = get_indent(); /* XXX */
8672 break;
8673 }
8674
8675 /* add extra indent for a comment */
8676 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008677 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678
8679 /* add extra indent if the previous line ended in a backslash:
8680 * "asdfasdf\
8681 * here";
8682 * char *foo = "asdf\
8683 * here";
8684 */
8685 if (cur_curpos.lnum > 1)
8686 {
8687 l = ml_get(cur_curpos.lnum - 1);
8688 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
8689 {
8690 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
8691 if (cur_amount > 0)
8692 amount = cur_amount;
8693 else if (cur_amount == 0)
8694 amount += ind_continuation;
8695 }
8696 }
8697 }
8698 }
8699
8700theend:
8701 /* put the cursor back where it belongs */
8702 curwin->w_cursor = cur_curpos;
8703
8704 vim_free(linecopy);
8705
8706 if (amount < 0)
8707 return 0;
8708 return amount;
8709}
8710
8711 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008712find_match(lookfor, ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 int lookfor;
8714 linenr_T ourscope;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715{
8716 char_u *look;
8717 pos_T *theirscope;
8718 char_u *mightbeif;
8719 int elselevel;
8720 int whilelevel;
8721
8722 if (lookfor == LOOKFOR_IF)
8723 {
8724 elselevel = 1;
8725 whilelevel = 0;
8726 }
8727 else
8728 {
8729 elselevel = 0;
8730 whilelevel = 1;
8731 }
8732
8733 curwin->w_cursor.col = 0;
8734
8735 while (curwin->w_cursor.lnum > ourscope + 1)
8736 {
8737 curwin->w_cursor.lnum--;
8738 curwin->w_cursor.col = 0;
8739
8740 look = cin_skipcomment(ml_get_curline());
8741 if (cin_iselse(look)
8742 || cin_isif(look)
8743 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008744 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 {
8746 /*
8747 * if we've gone outside the braces entirely,
8748 * we must be out of scope...
8749 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008750 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 if (theirscope == NULL)
8752 break;
8753
8754 /*
8755 * and if the brace enclosing this is further
8756 * back than the one enclosing the else, we're
8757 * out of luck too.
8758 */
8759 if (theirscope->lnum < ourscope)
8760 break;
8761
8762 /*
8763 * and if they're enclosed in a *deeper* brace,
8764 * then we can ignore it because it's in a
8765 * different scope...
8766 */
8767 if (theirscope->lnum > ourscope)
8768 continue;
8769
8770 /*
8771 * if it was an "else" (that's not an "else if")
8772 * then we need to go back to another if, so
8773 * increment elselevel
8774 */
8775 look = cin_skipcomment(ml_get_curline());
8776 if (cin_iselse(look))
8777 {
8778 mightbeif = cin_skipcomment(look + 4);
8779 if (!cin_isif(mightbeif))
8780 ++elselevel;
8781 continue;
8782 }
8783
8784 /*
8785 * if it was a "while" then we need to go back to
8786 * another "do", so increment whilelevel. XXX
8787 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008788 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 {
8790 ++whilelevel;
8791 continue;
8792 }
8793
8794 /* If it's an "if" decrement elselevel */
8795 look = cin_skipcomment(ml_get_curline());
8796 if (cin_isif(look))
8797 {
8798 elselevel--;
8799 /*
8800 * When looking for an "if" ignore "while"s that
8801 * get in the way.
8802 */
8803 if (elselevel == 0 && lookfor == LOOKFOR_IF)
8804 whilelevel = 0;
8805 }
8806
8807 /* If it's a "do" decrement whilelevel */
8808 if (cin_isdo(look))
8809 whilelevel--;
8810
8811 /*
8812 * if we've used up all the elses, then
8813 * this must be the if that we want!
8814 * match the indent level of that if.
8815 */
8816 if (elselevel <= 0 && whilelevel <= 0)
8817 {
8818 return OK;
8819 }
8820 }
8821 }
8822 return FAIL;
8823}
8824
8825# if defined(FEAT_EVAL) || defined(PROTO)
8826/*
8827 * Get indent level from 'indentexpr'.
8828 */
8829 int
8830get_expr_indent()
8831{
8832 int indent;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008833 pos_T save_pos;
8834 colnr_T save_curswant;
8835 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008837 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
8838 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008840 /* Save and restore cursor position and curswant, in case it was changed
8841 * via :normal commands */
8842 save_pos = curwin->w_cursor;
8843 save_curswant = curwin->w_curswant;
8844 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008846 if (use_sandbox)
8847 ++sandbox;
8848 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 indent = eval_to_number(curbuf->b_p_inde);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008850 if (use_sandbox)
8851 --sandbox;
8852 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853
8854 /* Restore the cursor position so that 'indentexpr' doesn't need to.
8855 * Pretend to be in Insert mode, allow cursor past end of line for "o"
8856 * command. */
8857 save_State = State;
8858 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008859 curwin->w_cursor = save_pos;
8860 curwin->w_curswant = save_curswant;
8861 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 check_cursor();
8863 State = save_State;
8864
8865 /* If there is an error, just keep the current indent. */
8866 if (indent < 0)
8867 indent = get_indent();
8868
8869 return indent;
8870}
8871# endif
8872
8873#endif /* FEAT_CINDENT */
8874
8875#if defined(FEAT_LISP) || defined(PROTO)
8876
8877static int lisp_match __ARGS((char_u *p));
8878
8879 static int
8880lisp_match(p)
8881 char_u *p;
8882{
8883 char_u buf[LSIZE];
8884 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01008885 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886
8887 while (*word != NUL)
8888 {
8889 (void)copy_option_part(&word, buf, LSIZE, ",");
8890 len = (int)STRLEN(buf);
8891 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
8892 return TRUE;
8893 }
8894 return FALSE;
8895}
8896
8897/*
8898 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
8899 * The incompatible newer method is quite a bit better at indenting
8900 * code in lisp-like languages than the traditional one; it's still
8901 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
8902 *
8903 * TODO:
8904 * Findmatch() should be adapted for lisp, also to make showmatch
8905 * work correctly: now (v5.3) it seems all C/C++ oriented:
8906 * - it does not recognize the #\( and #\) notations as character literals
8907 * - it doesn't know about comments starting with a semicolon
8908 * - it incorrectly interprets '(' as a character literal
8909 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008910 * Update from Sergey Khorev:
8911 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 */
8913 int
8914get_lisp_indent()
8915{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008916 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 int amount;
8918 char_u *that;
8919 colnr_T col;
8920 colnr_T firsttry;
8921 int parencount, quotecount;
8922 int vi_lisp;
8923
8924 /* Set vi_lisp to use the vi-compatible method */
8925 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
8926
8927 realpos = curwin->w_cursor;
8928 curwin->w_cursor.col = 0;
8929
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008930 if ((pos = findmatch(NULL, '(')) == NULL)
8931 pos = findmatch(NULL, '[');
8932 else
8933 {
8934 paren = *pos;
8935 pos = findmatch(NULL, '[');
8936 if (pos == NULL || ltp(pos, &paren))
8937 pos = &paren;
8938 }
8939 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 {
8941 /* Extra trick: Take the indent of the first previous non-white
8942 * line that is at the same () level. */
8943 amount = -1;
8944 parencount = 0;
8945
8946 while (--curwin->w_cursor.lnum >= pos->lnum)
8947 {
8948 if (linewhite(curwin->w_cursor.lnum))
8949 continue;
8950 for (that = ml_get_curline(); *that != NUL; ++that)
8951 {
8952 if (*that == ';')
8953 {
8954 while (*(that + 1) != NUL)
8955 ++that;
8956 continue;
8957 }
8958 if (*that == '\\')
8959 {
8960 if (*(that + 1) != NUL)
8961 ++that;
8962 continue;
8963 }
8964 if (*that == '"' && *(that + 1) != NUL)
8965 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00008966 while (*++that && *that != '"')
8967 {
8968 /* skipping escaped characters in the string */
8969 if (*that == '\\')
8970 {
8971 if (*++that == NUL)
8972 break;
8973 if (that[1] == NUL)
8974 {
8975 ++that;
8976 break;
8977 }
8978 }
8979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008981 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008983 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 --parencount;
8985 }
8986 if (parencount == 0)
8987 {
8988 amount = get_indent();
8989 break;
8990 }
8991 }
8992
8993 if (amount == -1)
8994 {
8995 curwin->w_cursor.lnum = pos->lnum;
8996 curwin->w_cursor.col = pos->col;
8997 col = pos->col;
8998
8999 that = ml_get_curline();
9000
9001 if (vi_lisp && get_indent() == 0)
9002 amount = 2;
9003 else
9004 {
9005 amount = 0;
9006 while (*that && col)
9007 {
9008 amount += lbr_chartabsize_adv(&that, (colnr_T)amount);
9009 col--;
9010 }
9011
9012 /*
9013 * Some keywords require "body" indenting rules (the
9014 * non-standard-lisp ones are Scheme special forms):
9015 *
9016 * (let ((a 1)) instead (let ((a 1))
9017 * (...)) of (...))
9018 */
9019
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009020 if (!vi_lisp && (*that == '(' || *that == '[')
9021 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 amount += 2;
9023 else
9024 {
9025 that++;
9026 amount++;
9027 firsttry = amount;
9028
9029 while (vim_iswhite(*that))
9030 {
9031 amount += lbr_chartabsize(that, (colnr_T)amount);
9032 ++that;
9033 }
9034
9035 if (*that && *that != ';') /* not a comment line */
9036 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009037 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009039 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 firsttry++;
9041
9042 parencount = 0;
9043 quotecount = 0;
9044
9045 if (vi_lisp
9046 || (*that != '"'
9047 && *that != '\''
9048 && *that != '#'
9049 && (*that < '0' || *that > '9')))
9050 {
9051 while (*that
9052 && (!vim_iswhite(*that)
9053 || quotecount
9054 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009055 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 && !quotecount
9057 && !parencount
9058 && vi_lisp)))
9059 {
9060 if (*that == '"')
9061 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009062 if ((*that == '(' || *that == '[')
9063 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009065 if ((*that == ')' || *that == ']')
9066 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 --parencount;
9068 if (*that == '\\' && *(that+1) != NUL)
9069 amount += lbr_chartabsize_adv(&that,
9070 (colnr_T)amount);
9071 amount += lbr_chartabsize_adv(&that,
9072 (colnr_T)amount);
9073 }
9074 }
9075 while (vim_iswhite(*that))
9076 {
9077 amount += lbr_chartabsize(that, (colnr_T)amount);
9078 that++;
9079 }
9080 if (!*that || *that == ';')
9081 amount = firsttry;
9082 }
9083 }
9084 }
9085 }
9086 }
9087 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009088 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089
9090 curwin->w_cursor = realpos;
9091
9092 return amount;
9093}
9094#endif /* FEAT_LISP */
9095
9096 void
9097prepare_to_exit()
9098{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009099#if defined(SIGHUP) && defined(SIG_IGN)
9100 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9101 * makes Vim exit and then handling SIGHUP causes various reentrance
9102 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009103 signal(SIGHUP, SIG_IGN);
9104#endif
9105
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106#ifdef FEAT_GUI
9107 if (gui.in_use)
9108 {
9109 gui.dying = TRUE;
9110 out_trash(); /* trash any pending output */
9111 }
9112 else
9113#endif
9114 {
9115 windgoto((int)Rows - 1, 0);
9116
9117 /*
9118 * Switch terminal mode back now, so messages end up on the "normal"
9119 * screen (if there are two screens).
9120 */
9121 settmode(TMODE_COOK);
9122#ifdef WIN3264
9123 if (can_end_termcap_mode(FALSE) == TRUE)
9124#endif
9125 stoptermcap();
9126 out_flush();
9127 }
9128}
9129
9130/*
9131 * Preserve files and exit.
9132 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009133 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9134 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 */
9136 void
9137preserve_exit()
9138{
9139 buf_T *buf;
9140
9141 prepare_to_exit();
9142
Bram Moolenaar4770d092006-01-12 23:22:24 +00009143 /* Setting this will prevent free() calls. That avoids calling free()
9144 * recursively when free() was invoked with a bad pointer. */
9145 really_exiting = TRUE;
9146
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 out_str(IObuff);
9148 screen_start(); /* don't know where cursor is now */
9149 out_flush();
9150
9151 ml_close_notmod(); /* close all not-modified buffers */
9152
9153 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9154 {
9155 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9156 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009157 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 screen_start(); /* don't know where cursor is now */
9159 out_flush();
9160 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9161 break;
9162 }
9163 }
9164
9165 ml_close_all(FALSE); /* close all memfiles, without deleting */
9166
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009167 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168
9169 getout(1);
9170}
9171
9172/*
9173 * return TRUE if "fname" exists.
9174 */
9175 int
9176vim_fexists(fname)
9177 char_u *fname;
9178{
9179 struct stat st;
9180
9181 if (mch_stat((char *)fname, &st))
9182 return FALSE;
9183 return TRUE;
9184}
9185
9186/*
9187 * Check for CTRL-C pressed, but only once in a while.
9188 * Should be used instead of ui_breakcheck() for functions that check for
9189 * each line in the file. Calling ui_breakcheck() each time takes too much
9190 * time, because it can be a system call.
9191 */
9192
9193#ifndef BREAKCHECK_SKIP
9194# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9195# define BREAKCHECK_SKIP 200
9196# else
9197# define BREAKCHECK_SKIP 32
9198# endif
9199#endif
9200
9201static int breakcheck_count = 0;
9202
9203 void
9204line_breakcheck()
9205{
9206 if (++breakcheck_count >= BREAKCHECK_SKIP)
9207 {
9208 breakcheck_count = 0;
9209 ui_breakcheck();
9210 }
9211}
9212
9213/*
9214 * Like line_breakcheck() but check 10 times less often.
9215 */
9216 void
9217fast_breakcheck()
9218{
9219 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9220 {
9221 breakcheck_count = 0;
9222 ui_breakcheck();
9223 }
9224}
9225
9226/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009227 * Invoke expand_wildcards() for one pattern.
9228 * Expand items like "%:h" before the expansion.
9229 * Returns OK or FAIL.
9230 */
9231 int
9232expand_wildcards_eval(pat, num_file, file, flags)
9233 char_u **pat; /* pointer to input pattern */
9234 int *num_file; /* resulting number of files */
9235 char_u ***file; /* array of resulting files */
9236 int flags; /* EW_DIR, etc. */
9237{
9238 int ret = FAIL;
9239 char_u *eval_pat = NULL;
9240 char_u *exp_pat = *pat;
9241 char_u *ignored_msg;
9242 int usedlen;
9243
9244 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9245 {
9246 ++emsg_off;
9247 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9248 NULL, &ignored_msg, NULL);
9249 --emsg_off;
9250 if (eval_pat != NULL)
9251 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9252 }
9253
9254 if (exp_pat != NULL)
9255 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9256
9257 if (eval_pat != NULL)
9258 {
9259 vim_free(exp_pat);
9260 vim_free(eval_pat);
9261 }
9262
9263 return ret;
9264}
9265
9266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9268 * 'wildignore'.
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009269 * Returns OK or FAIL. When FAIL then "num_file" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 */
9271 int
9272expand_wildcards(num_pat, pat, num_file, file, flags)
9273 int num_pat; /* number of input patterns */
9274 char_u **pat; /* array of input patterns */
9275 int *num_file; /* resulting number of files */
9276 char_u ***file; /* array of resulting files */
9277 int flags; /* EW_DIR, etc. */
9278{
9279 int retval;
9280 int i, j;
9281 char_u *p;
9282 int non_suf_match; /* number without matching suffix */
9283
9284 retval = gen_expand_wildcards(num_pat, pat, num_file, file, flags);
9285
9286 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009287 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288 return retval;
9289
9290#ifdef FEAT_WILDIGN
9291 /*
9292 * Remove names that match 'wildignore'.
9293 */
9294 if (*p_wig)
9295 {
9296 char_u *ffname;
9297
9298 /* check all files in (*file)[] */
9299 for (i = 0; i < *num_file; ++i)
9300 {
9301 ffname = FullName_save((*file)[i], FALSE);
9302 if (ffname == NULL) /* out of memory */
9303 break;
9304# ifdef VMS
9305 vms_remove_version(ffname);
9306# endif
9307 if (match_file_list(p_wig, (*file)[i], ffname))
9308 {
9309 /* remove this matching file from the list */
9310 vim_free((*file)[i]);
9311 for (j = i; j + 1 < *num_file; ++j)
9312 (*file)[j] = (*file)[j + 1];
9313 --*num_file;
9314 --i;
9315 }
9316 vim_free(ffname);
9317 }
9318 }
9319#endif
9320
9321 /*
9322 * Move the names where 'suffixes' match to the end.
9323 */
9324 if (*num_file > 1)
9325 {
9326 non_suf_match = 0;
9327 for (i = 0; i < *num_file; ++i)
9328 {
9329 if (!match_suffix((*file)[i]))
9330 {
9331 /*
9332 * Move the name without matching suffix to the front
9333 * of the list.
9334 */
9335 p = (*file)[i];
9336 for (j = i; j > non_suf_match; --j)
9337 (*file)[j] = (*file)[j - 1];
9338 (*file)[non_suf_match++] = p;
9339 }
9340 }
9341 }
9342
9343 return retval;
9344}
9345
9346/*
9347 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9348 */
9349 int
9350match_suffix(fname)
9351 char_u *fname;
9352{
9353 int fnamelen, setsuflen;
9354 char_u *setsuf;
9355#define MAXSUFLEN 30 /* maximum length of a file suffix */
9356 char_u suf_buf[MAXSUFLEN];
9357
9358 fnamelen = (int)STRLEN(fname);
9359 setsuflen = 0;
9360 for (setsuf = p_su; *setsuf; )
9361 {
9362 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009363 if (setsuflen == 0)
9364 {
9365 char_u *tail = gettail(fname);
9366
9367 /* empty entry: match name without a '.' */
9368 if (vim_strchr(tail, '.') == NULL)
9369 {
9370 setsuflen = 1;
9371 break;
9372 }
9373 }
9374 else
9375 {
9376 if (fnamelen >= setsuflen
9377 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9378 (size_t)setsuflen) == 0)
9379 break;
9380 setsuflen = 0;
9381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 }
9383 return (setsuflen != 0);
9384}
9385
9386#if !defined(NO_EXPANDPATH) || defined(PROTO)
9387
9388# ifdef VIM_BACKTICK
9389static int vim_backtick __ARGS((char_u *p));
9390static int expand_backtick __ARGS((garray_T *gap, char_u *pat, int flags));
9391# endif
9392
9393# if defined(MSDOS) || defined(FEAT_GUI_W16) || defined(WIN3264)
9394/*
9395 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9396 * it's shared between these systems.
9397 */
9398# if defined(DJGPP) || defined(PROTO)
9399# define _cdecl /* DJGPP doesn't have this */
9400# else
9401# ifdef __BORLANDC__
9402# define _cdecl _RTLENTRYF
9403# endif
9404# endif
9405
9406/*
9407 * comparison function for qsort in dos_expandpath()
9408 */
9409 static int _cdecl
9410pstrcmp(const void *a, const void *b)
9411{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009412 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413}
9414
9415# ifndef WIN3264
9416 static void
9417namelowcpy(
9418 char_u *d,
9419 char_u *s)
9420{
9421# ifdef DJGPP
9422 if (USE_LONG_FNAME) /* don't lower case on Windows 95/NT systems */
9423 while (*s)
9424 *d++ = *s++;
9425 else
9426# endif
9427 while (*s)
9428 *d++ = TOLOWER_LOC(*s++);
9429 *d = NUL;
9430}
9431# endif
9432
9433/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009434 * Recursively expand one path component into all matching files and/or
9435 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 * Return the number of matches found.
9437 * "path" has backslashes before chars that are not to be expanded, starting
9438 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009439 * Return the number of matches found.
9440 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 */
9442 static int
9443dos_expandpath(
9444 garray_T *gap,
9445 char_u *path,
9446 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009447 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009448 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009450 char_u *buf;
9451 char_u *path_end;
9452 char_u *p, *s, *e;
9453 int start_len = gap->ga_len;
9454 char_u *pat;
9455 regmatch_T regmatch;
9456 int starts_with_dot;
9457 int matches;
9458 int len;
9459 int starstar = FALSE;
9460 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461#ifdef WIN3264
9462 WIN32_FIND_DATA fb;
9463 HANDLE hFind = (HANDLE)0;
9464# ifdef FEAT_MBYTE
9465 WIN32_FIND_DATAW wfb;
9466 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9467# endif
9468#else
9469 struct ffblk fb;
9470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009472 int ok;
9473
9474 /* Expanding "**" may take a long time, check for CTRL-C. */
9475 if (stardepth > 0)
9476 {
9477 ui_breakcheck();
9478 if (got_int)
9479 return 0;
9480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481
9482 /* make room for file name */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009483 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 if (buf == NULL)
9485 return 0;
9486
9487 /*
9488 * Find the first part in the path name that contains a wildcard or a ~1.
9489 * Copy it into buf, including the preceding characters.
9490 */
9491 p = buf;
9492 s = buf;
9493 e = NULL;
9494 path_end = path;
9495 while (*path_end != NUL)
9496 {
9497 /* May ignore a wildcard that has a backslash before it; it will
9498 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9499 if (path_end >= path + wildoff && rem_backslash(path_end))
9500 *p++ = *path_end++;
9501 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9502 {
9503 if (e != NULL)
9504 break;
9505 s = p + 1;
9506 }
9507 else if (path_end >= path + wildoff
9508 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9509 e = p;
9510#ifdef FEAT_MBYTE
9511 if (has_mbyte)
9512 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009513 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514 STRNCPY(p, path_end, len);
9515 p += len;
9516 path_end += len;
9517 }
9518 else
9519#endif
9520 *p++ = *path_end++;
9521 }
9522 e = p;
9523 *e = NUL;
9524
9525 /* now we have one wildcard component between s and e */
9526 /* Remove backslashes between "wildoff" and the start of the wildcard
9527 * component. */
9528 for (p = buf + wildoff; p < s; ++p)
9529 if (rem_backslash(p))
9530 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009531 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 --e;
9533 --s;
9534 }
9535
Bram Moolenaar231334e2005-07-25 20:46:57 +00009536 /* Check for "**" between "s" and "e". */
9537 for (p = s; p < e; ++p)
9538 if (p[0] == '*' && p[1] == '*')
9539 starstar = TRUE;
9540
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 starts_with_dot = (*s == '.');
9542 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9543 if (pat == NULL)
9544 {
9545 vim_free(buf);
9546 return 0;
9547 }
9548
9549 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009550 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009551 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552 regmatch.rm_ic = TRUE; /* Always ignore case */
9553 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009554 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009555 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 vim_free(pat);
9557
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009558 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 {
9560 vim_free(buf);
9561 return 0;
9562 }
9563
9564 /* remember the pattern or file name being looked for */
9565 matchname = vim_strsave(s);
9566
Bram Moolenaar231334e2005-07-25 20:46:57 +00009567 /* If "**" is by itself, this is the first time we encounter it and more
9568 * is following then find matches without any directory. */
9569 if (!didstar && stardepth < 100 && starstar && e - s == 2
9570 && *path_end == '/')
9571 {
9572 STRCPY(s, path_end + 1);
9573 ++stardepth;
9574 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9575 --stardepth;
9576 }
9577
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 /* Scan all files in the directory with "dir/ *.*" */
9579 STRCPY(s, "*.*");
9580#ifdef WIN3264
9581# ifdef FEAT_MBYTE
9582 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
9583 {
9584 /* The active codepage differs from 'encoding'. Attempt using the
9585 * wide function. If it fails because it is not implemented fall back
9586 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009587 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 if (wn != NULL)
9589 {
9590 hFind = FindFirstFileW(wn, &wfb);
9591 if (hFind == INVALID_HANDLE_VALUE
9592 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
9593 {
9594 vim_free(wn);
9595 wn = NULL;
9596 }
9597 }
9598 }
9599
9600 if (wn == NULL)
9601# endif
9602 hFind = FindFirstFile(buf, &fb);
9603 ok = (hFind != INVALID_HANDLE_VALUE);
9604#else
9605 /* If we are expanding wildcards we try both files and directories */
9606 ok = (findfirst((char *)buf, &fb,
9607 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
9608#endif
9609
9610 while (ok)
9611 {
9612#ifdef WIN3264
9613# ifdef FEAT_MBYTE
9614 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009615 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 else
9617# endif
9618 p = (char_u *)fb.cFileName;
9619#else
9620 p = (char_u *)fb.ff_name;
9621#endif
9622 /* Ignore entries starting with a dot, unless when asked for. Accept
9623 * all entries found with "matchname". */
9624 if ((p[0] != '.' || starts_with_dot)
9625 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009626 || (regmatch.regprog != NULL
9627 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009628 || ((flags & EW_NOTWILD)
9629 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 {
9631#ifdef WIN3264
9632 STRCPY(s, p);
9633#else
9634 namelowcpy(s, p);
9635#endif
9636 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009637
9638 if (starstar && stardepth < 100)
9639 {
9640 /* For "**" in the pattern first go deeper in the tree to
9641 * find matches. */
9642 STRCPY(buf + len, "/**");
9643 STRCPY(buf + len + 3, path_end);
9644 ++stardepth;
9645 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
9646 --stardepth;
9647 }
9648
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649 STRCPY(buf + len, path_end);
9650 if (mch_has_exp_wildcard(path_end))
9651 {
9652 /* need to expand another component of the path */
9653 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009654 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 }
9656 else
9657 {
9658 /* no more wildcards, check if there is a match */
9659 /* remove backslashes for the remaining components only */
9660 if (*path_end != 0)
9661 backslash_halve(buf + len + 1);
9662 if (mch_getperm(buf) >= 0) /* add existing file */
9663 addfile(gap, buf, flags);
9664 }
9665 }
9666
9667#ifdef WIN3264
9668# ifdef FEAT_MBYTE
9669 if (wn != NULL)
9670 {
9671 vim_free(p);
9672 ok = FindNextFileW(hFind, &wfb);
9673 }
9674 else
9675# endif
9676 ok = FindNextFile(hFind, &fb);
9677#else
9678 ok = (findnext(&fb) == 0);
9679#endif
9680
9681 /* If no more matches and no match was used, try expanding the name
9682 * itself. Finds the long name of a short filename. */
9683 if (!ok && matchname != NULL && gap->ga_len == start_len)
9684 {
9685 STRCPY(s, matchname);
9686#ifdef WIN3264
9687 FindClose(hFind);
9688# ifdef FEAT_MBYTE
9689 if (wn != NULL)
9690 {
9691 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009692 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 if (wn != NULL)
9694 hFind = FindFirstFileW(wn, &wfb);
9695 }
9696 if (wn == NULL)
9697# endif
9698 hFind = FindFirstFile(buf, &fb);
9699 ok = (hFind != INVALID_HANDLE_VALUE);
9700#else
9701 ok = (findfirst((char *)buf, &fb,
9702 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
9703#endif
9704 vim_free(matchname);
9705 matchname = NULL;
9706 }
9707 }
9708
9709#ifdef WIN3264
9710 FindClose(hFind);
9711# ifdef FEAT_MBYTE
9712 vim_free(wn);
9713# endif
9714#endif
9715 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +02009716 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 vim_free(matchname);
9718
9719 matches = gap->ga_len - start_len;
9720 if (matches > 0)
9721 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
9722 sizeof(char_u *), pstrcmp);
9723 return matches;
9724}
9725
9726 int
9727mch_expandpath(
9728 garray_T *gap,
9729 char_u *path,
9730 int flags) /* EW_* flags */
9731{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009732 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733}
9734# endif /* MSDOS || FEAT_GUI_W16 || WIN3264 */
9735
Bram Moolenaar231334e2005-07-25 20:46:57 +00009736#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
9737 || defined(PROTO)
9738/*
9739 * Unix style wildcard expansion code.
9740 * It's here because it's used both for Unix and Mac.
9741 */
9742static int pstrcmp __ARGS((const void *, const void *));
9743
9744 static int
9745pstrcmp(a, b)
9746 const void *a, *b;
9747{
9748 return (pathcmp(*(char **)a, *(char **)b, -1));
9749}
9750
9751/*
9752 * Recursively expand one path component into all matching files and/or
9753 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
9754 * "path" has backslashes before chars that are not to be expanded, starting
9755 * at "path + wildoff".
9756 * Return the number of matches found.
9757 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
9758 */
9759 int
9760unix_expandpath(gap, path, wildoff, flags, didstar)
9761 garray_T *gap;
9762 char_u *path;
9763 int wildoff;
9764 int flags; /* EW_* flags */
9765 int didstar; /* expanded "**" once already */
9766{
9767 char_u *buf;
9768 char_u *path_end;
9769 char_u *p, *s, *e;
9770 int start_len = gap->ga_len;
9771 char_u *pat;
9772 regmatch_T regmatch;
9773 int starts_with_dot;
9774 int matches;
9775 int len;
9776 int starstar = FALSE;
9777 static int stardepth = 0; /* depth for "**" expansion */
9778
9779 DIR *dirp;
9780 struct dirent *dp;
9781
9782 /* Expanding "**" may take a long time, check for CTRL-C. */
9783 if (stardepth > 0)
9784 {
9785 ui_breakcheck();
9786 if (got_int)
9787 return 0;
9788 }
9789
9790 /* make room for file name */
9791 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
9792 if (buf == NULL)
9793 return 0;
9794
9795 /*
9796 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +02009797 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +00009798 * Copy it into "buf", including the preceding characters.
9799 */
9800 p = buf;
9801 s = buf;
9802 e = NULL;
9803 path_end = path;
9804 while (*path_end != NUL)
9805 {
9806 /* May ignore a wildcard that has a backslash before it; it will
9807 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9808 if (path_end >= path + wildoff && rem_backslash(path_end))
9809 *p++ = *path_end++;
9810 else if (*path_end == '/')
9811 {
9812 if (e != NULL)
9813 break;
9814 s = p + 1;
9815 }
9816 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +02009817 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01009818 || (!p_fic && (flags & EW_ICASE)
9819 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +00009820 e = p;
9821#ifdef FEAT_MBYTE
9822 if (has_mbyte)
9823 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009824 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009825 STRNCPY(p, path_end, len);
9826 p += len;
9827 path_end += len;
9828 }
9829 else
9830#endif
9831 *p++ = *path_end++;
9832 }
9833 e = p;
9834 *e = NUL;
9835
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009836 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009837 /* Remove backslashes between "wildoff" and the start of the wildcard
9838 * component. */
9839 for (p = buf + wildoff; p < s; ++p)
9840 if (rem_backslash(p))
9841 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009842 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009843 --e;
9844 --s;
9845 }
9846
9847 /* Check for "**" between "s" and "e". */
9848 for (p = s; p < e; ++p)
9849 if (p[0] == '*' && p[1] == '*')
9850 starstar = TRUE;
9851
9852 /* convert the file pattern to a regexp pattern */
9853 starts_with_dot = (*s == '.');
9854 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9855 if (pat == NULL)
9856 {
9857 vim_free(buf);
9858 return 0;
9859 }
9860
9861 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +01009862 if (flags & EW_ICASE)
9863 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
9864 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01009865 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009866 if (flags & (EW_NOERROR | EW_NOTWILD))
9867 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009868 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009869 if (flags & (EW_NOERROR | EW_NOTWILD))
9870 --emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009871 vim_free(pat);
9872
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009873 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +00009874 {
9875 vim_free(buf);
9876 return 0;
9877 }
9878
9879 /* If "**" is by itself, this is the first time we encounter it and more
9880 * is following then find matches without any directory. */
9881 if (!didstar && stardepth < 100 && starstar && e - s == 2
9882 && *path_end == '/')
9883 {
9884 STRCPY(s, path_end + 1);
9885 ++stardepth;
9886 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9887 --stardepth;
9888 }
9889
9890 /* open the directory for scanning */
9891 *s = NUL;
9892 dirp = opendir(*buf == NUL ? "." : (char *)buf);
9893
9894 /* Find all matching entries */
9895 if (dirp != NULL)
9896 {
9897 for (;;)
9898 {
9899 dp = readdir(dirp);
9900 if (dp == NULL)
9901 break;
9902 if ((dp->d_name[0] != '.' || starts_with_dot)
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009903 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
9904 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009905 || ((flags & EW_NOTWILD)
9906 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +00009907 {
9908 STRCPY(s, dp->d_name);
9909 len = STRLEN(buf);
9910
9911 if (starstar && stardepth < 100)
9912 {
9913 /* For "**" in the pattern first go deeper in the tree to
9914 * find matches. */
9915 STRCPY(buf + len, "/**");
9916 STRCPY(buf + len + 3, path_end);
9917 ++stardepth;
9918 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
9919 --stardepth;
9920 }
9921
9922 STRCPY(buf + len, path_end);
9923 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
9924 {
9925 /* need to expand another component of the path */
9926 /* remove backslashes for the remaining components only */
9927 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
9928 }
9929 else
9930 {
9931 /* no more wildcards, check if there is a match */
9932 /* remove backslashes for the remaining components only */
9933 if (*path_end != NUL)
9934 backslash_halve(buf + len + 1);
9935 if (mch_getperm(buf) >= 0) /* add existing file */
9936 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +00009937#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +00009938 size_t precomp_len = STRLEN(buf)+1;
9939 char_u *precomp_buf =
9940 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +00009941
Bram Moolenaar231334e2005-07-25 20:46:57 +00009942 if (precomp_buf)
9943 {
9944 mch_memmove(buf, precomp_buf, precomp_len);
9945 vim_free(precomp_buf);
9946 }
9947#endif
9948 addfile(gap, buf, flags);
9949 }
9950 }
9951 }
9952 }
9953
9954 closedir(dirp);
9955 }
9956
9957 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +02009958 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009959
9960 matches = gap->ga_len - start_len;
9961 if (matches > 0)
9962 qsort(((char_u **)gap->ga_data) + start_len, matches,
9963 sizeof(char_u *), pstrcmp);
9964 return matches;
9965}
9966#endif
9967
Bram Moolenaarcc448b32010-07-14 16:52:17 +02009968#if defined(FEAT_SEARCHPATH)
9969static int find_previous_pathsep __ARGS((char_u *path, char_u **psep));
9970static int is_unique __ARGS((char_u *maybe_unique, garray_T *gap, int i));
Bram Moolenaar162bd912010-07-28 22:29:10 +02009971static void expand_path_option __ARGS((char_u *curdir, garray_T *gap));
9972static char_u *get_path_cutoff __ARGS((char_u *fname, garray_T *gap));
Bram Moolenaarcc448b32010-07-14 16:52:17 +02009973static void uniquefy_paths __ARGS((garray_T *gap, char_u *pattern));
9974static int expand_in_path __ARGS((garray_T *gap, char_u *pattern, int flags));
9975
9976/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +02009977 * Moves "*psep" back to the previous path separator in "path".
9978 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +02009979 */
9980 static int
9981find_previous_pathsep(path, psep)
9982 char_u *path;
9983 char_u **psep;
9984{
9985 /* skip the current separator */
9986 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +02009987 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02009988
9989 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +02009990 while (*psep > path)
9991 {
9992 if (vim_ispathsep(**psep))
9993 return OK;
9994 mb_ptr_back(path, *psep);
9995 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02009996
9997 return FAIL;
9998}
9999
10000/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010001 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
10002 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010003 */
10004 static int
10005is_unique(maybe_unique, gap, i)
10006 char_u *maybe_unique;
10007 garray_T *gap;
10008 int i;
10009{
10010 int j;
10011 int candidate_len;
10012 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010013 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010014 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010015
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010016 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010017 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010018 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010019 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010020
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010021 candidate_len = (int)STRLEN(maybe_unique);
10022 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010023 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010024 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010025
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010026 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010027 if (fnamecmp(maybe_unique, rival) == 0
10028 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010029 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010030 }
10031
Bram Moolenaar162bd912010-07-28 22:29:10 +020010032 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010033}
10034
10035/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010036 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010037 * paths are expanded to their equivalent fullpath. This includes the "."
10038 * (relative to current buffer directory) and empty path (relative to current
10039 * directory) notations.
10040 *
10041 * TODO: handle upward search (;) and path limiter (**N) notations by
10042 * expanding each into their equivalent path(s).
10043 */
10044 static void
10045expand_path_option(curdir, gap)
10046 char_u *curdir;
10047 garray_T *gap;
10048{
10049 char_u *path_option = *curbuf->b_p_path == NUL
10050 ? p_path : curbuf->b_p_path;
10051 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010052 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010053 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010054
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010055 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010056 return;
10057
10058 while (*path_option != NUL)
10059 {
10060 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10061
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010062 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010063 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010064 /* Relative to current buffer:
10065 * "/path/file" + "." -> "/path/"
10066 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010067 if (curbuf->b_ffname == NULL)
10068 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010069 p = gettail(curbuf->b_ffname);
10070 len = (int)(p - curbuf->b_ffname);
10071 if (len + (int)STRLEN(buf) >= MAXPATHL)
10072 continue;
10073 if (buf[1] == NUL)
10074 buf[len] = NUL;
10075 else
10076 STRMOVE(buf + len, buf + 2);
10077 mch_memmove(buf, curbuf->b_ffname, len);
10078 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010079 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010080 else if (buf[0] == NUL)
10081 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010082 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010083 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010084 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010085 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010086 else if (!mch_isFullName(buf))
10087 {
10088 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010089 len = (int)STRLEN(curdir);
10090 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010091 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010092 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010093 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010094 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010095 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010096 }
10097
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010098 if (ga_grow(gap, 1) == FAIL)
10099 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010100
10101# if defined(MSWIN) || defined(MSDOS)
10102 /* Avoid the path ending in a backslash, it fails when a comma is
10103 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010104 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010105 if (buf[len - 1] == '\\')
10106 buf[len - 1] = '/';
10107# endif
10108
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010109 p = vim_strsave(buf);
10110 if (p == NULL)
10111 break;
10112 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010113 }
10114
10115 vim_free(buf);
10116}
10117
10118/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010119 * Returns a pointer to the file or directory name in "fname" that matches the
10120 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010121 *
10122 * path: /foo/bar/baz
10123 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010124 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010125 */
10126 static char_u *
10127get_path_cutoff(fname, gap)
10128 char_u *fname;
10129 garray_T *gap;
10130{
10131 int i;
10132 int maxlen = 0;
10133 char_u **path_part = (char_u **)gap->ga_data;
10134 char_u *cutoff = NULL;
10135
10136 for (i = 0; i < gap->ga_len; i++)
10137 {
10138 int j = 0;
10139
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010140 while ((fname[j] == path_part[i][j]
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +020010141# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010142 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10143#endif
10144 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010145 j++;
10146 if (j > maxlen)
10147 {
10148 maxlen = j;
10149 cutoff = &fname[j];
10150 }
10151 }
10152
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010153 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010154 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010155 while (vim_ispathsep(*cutoff))
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010156 mb_ptr_adv(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010157
10158 return cutoff;
10159}
10160
10161/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010162 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10163 * that they are unique with respect to each other while conserving the part
10164 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010165 */
10166 static void
10167uniquefy_paths(gap, pattern)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010168 garray_T *gap;
10169 char_u *pattern;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010170{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010171 int i;
10172 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010173 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010174 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010175 char_u *pat;
10176 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010177 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010178 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010179 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010180 char_u **in_curdir = NULL;
10181 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010182
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010183 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010184 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010185
10186 /*
10187 * We need to prepend a '*' at the beginning of file_pattern so that the
10188 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010189 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010190 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010191 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010192 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010193 if (file_pattern == NULL)
10194 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010195 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010196 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010197 STRCAT(file_pattern, pattern);
10198 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10199 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010200 if (pat == NULL)
10201 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010202
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010203 regmatch.rm_ic = TRUE; /* always ignore case */
10204 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10205 vim_free(pat);
10206 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010207 return;
10208
Bram Moolenaar162bd912010-07-28 22:29:10 +020010209 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010210 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010211 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010212 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010213
10214 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010215 if (in_curdir == NULL)
10216 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010217
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010218 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010219 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010220 char_u *path = fnames[i];
10221 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010222 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010223 char_u *pathsep_p;
10224 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010225
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010226 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010227 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010228 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010229 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010230 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010231
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010232 /* Shorten the filename while maintaining its uniqueness */
10233 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010234
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010235 /* we start at the end of the path */
10236 pathsep_p = path + len - 1;
10237
10238 while (find_previous_pathsep(path, &pathsep_p))
10239 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10240 && is_unique(pathsep_p + 1, gap, i)
10241 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10242 {
10243 sort_again = TRUE;
10244 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10245 break;
10246 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010247
10248 if (mch_isFullName(path))
10249 {
10250 /*
10251 * Last resort: shorten relative to curdir if possible.
10252 * 'possible' means:
10253 * 1. It is under the current directory.
10254 * 2. The result is actually shorter than the original.
10255 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010256 * Before curdir After
10257 * /foo/bar/file.txt /foo/bar ./file.txt
10258 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10259 * /file.txt / /file.txt
10260 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010261 */
10262 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010263 if (short_name != NULL && short_name > path + 1
10264#if defined(MSWIN) || defined(MSDOS)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010265 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010266 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010267 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010268 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010269 && !vim_ispathsep(*short_name)
10270#endif
10271 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010272 {
10273 STRCPY(path, ".");
10274 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010275 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010276 }
10277 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010278 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010279 }
10280
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010281 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010282 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010283 {
10284 char_u *rel_path;
10285 char_u *path = in_curdir[i];
10286
10287 if (path == NULL)
10288 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010289
10290 /* If the {filename} is not unique, change it to ./{filename}.
10291 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010292 short_name = shorten_fname(path, curdir);
10293 if (short_name == NULL)
10294 short_name = path;
10295 if (is_unique(short_name, gap, i))
10296 {
10297 STRCPY(fnames[i], short_name);
10298 continue;
10299 }
10300
10301 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10302 if (rel_path == NULL)
10303 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010304 STRCPY(rel_path, ".");
10305 add_pathsep(rel_path);
10306 STRCAT(rel_path, short_name);
10307
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010308 vim_free(fnames[i]);
10309 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010310 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010311 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010312 }
10313
Bram Moolenaar162bd912010-07-28 22:29:10 +020010314theend:
10315 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010316 if (in_curdir != NULL)
10317 {
10318 for (i = 0; i < gap->ga_len; i++)
10319 vim_free(in_curdir[i]);
10320 vim_free(in_curdir);
10321 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010322 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010323 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010324
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010325 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010326 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010327}
10328
10329/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010330 * Calls globpath() with 'path' values for the given pattern and stores the
10331 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010332 * Returns the total number of matches.
10333 */
10334 static int
10335expand_in_path(gap, pattern, flags)
10336 garray_T *gap;
10337 char_u *pattern;
10338 int flags; /* EW_* flags */
10339{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010340 char_u *curdir;
10341 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010342 char_u *paths = NULL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010343
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010344 if ((curdir = alloc((unsigned)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010345 return 0;
10346 mch_dirname(curdir, MAXPATHL);
10347
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010348 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010349 expand_path_option(curdir, &path_ga);
10350 vim_free(curdir);
Bram Moolenaar006d2b02010-08-04 12:39:44 +020010351 if (path_ga.ga_len == 0)
10352 return 0;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010353
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010354 paths = ga_concat_strings(&path_ga, ",");
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010355 ga_clear_strings(&path_ga);
10356 if (paths == NULL)
Bram Moolenaar7f0f6212010-08-03 22:21:00 +020010357 return 0;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010358
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020010359 globpath(paths, pattern, gap, (flags & EW_ICASE) ? WILD_ICASE : 0);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010360 vim_free(paths);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010361
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010362 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010363}
10364#endif
10365
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010366#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10367/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010368 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10369 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010370 */
10371 void
10372remove_duplicates(gap)
10373 garray_T *gap;
10374{
10375 int i;
10376 int j;
10377 char_u **fnames = (char_u **)gap->ga_data;
10378
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010379 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010380 for (i = gap->ga_len - 1; i > 0; --i)
10381 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10382 {
10383 vim_free(fnames[i]);
10384 for (j = i + 1; j < gap->ga_len; ++j)
10385 fnames[j - 1] = fnames[j];
10386 --gap->ga_len;
10387 }
10388}
10389#endif
10390
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010391static int has_env_var __ARGS((char_u *p));
10392
10393/*
10394 * Return TRUE if "p" contains what looks like an environment variable.
10395 * Allowing for escaping.
10396 */
10397 static int
10398has_env_var(p)
10399 char_u *p;
10400{
10401 for ( ; *p; mb_ptr_adv(p))
10402 {
10403 if (*p == '\\' && p[1] != NUL)
10404 ++p;
10405 else if (vim_strchr((char_u *)
10406#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
10407 "$%"
10408#else
10409 "$"
10410#endif
10411 , *p) != NULL)
10412 return TRUE;
10413 }
10414 return FALSE;
10415}
10416
10417#ifdef SPECIAL_WILDCHAR
10418static int has_special_wildchar __ARGS((char_u *p));
10419
10420/*
10421 * Return TRUE if "p" contains a special wildcard character.
10422 * Allowing for escaping.
10423 */
10424 static int
10425has_special_wildchar(p)
10426 char_u *p;
10427{
10428 for ( ; *p; mb_ptr_adv(p))
10429 {
10430 if (*p == '\\' && p[1] != NUL)
10431 ++p;
10432 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10433 return TRUE;
10434 }
10435 return FALSE;
10436}
10437#endif
10438
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439/*
10440 * Generic wildcard expansion code.
10441 *
10442 * Characters in "pat" that should not be expanded must be preceded with a
10443 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10444 *
10445 * Return FAIL when no single file was found. In this case "num_file" is not
10446 * set, and "file" may contain an error message.
10447 * Return OK when some files found. "num_file" is set to the number of
10448 * matches, "file" to the array of matches. Call FreeWild() later.
10449 */
10450 int
10451gen_expand_wildcards(num_pat, pat, num_file, file, flags)
10452 int num_pat; /* number of input patterns */
10453 char_u **pat; /* array of input patterns */
10454 int *num_file; /* resulting number of files */
10455 char_u ***file; /* array of resulting files */
10456 int flags; /* EW_* flags */
10457{
10458 int i;
10459 garray_T ga;
10460 char_u *p;
10461 static int recursive = FALSE;
10462 int add_pat;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010463#if defined(FEAT_SEARCHPATH)
10464 int did_expand_in_path = FALSE;
10465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466
10467 /*
10468 * expand_env() is called to expand things like "~user". If this fails,
10469 * it calls ExpandOne(), which brings us back here. In this case, always
10470 * call the machine specific expansion function, if possible. Otherwise,
10471 * return FAIL.
10472 */
10473 if (recursive)
10474#ifdef SPECIAL_WILDCHAR
10475 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10476#else
10477 return FAIL;
10478#endif
10479
10480#ifdef SPECIAL_WILDCHAR
10481 /*
10482 * If there are any special wildcard characters which we cannot handle
10483 * here, call machine specific function for all the expansion. This
10484 * avoids starting the shell for each argument separately.
10485 * For `=expr` do use the internal function.
10486 */
10487 for (i = 0; i < num_pat; i++)
10488 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010489 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490# ifdef VIM_BACKTICK
10491 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10492# endif
10493 )
10494 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10495 }
10496#endif
10497
10498 recursive = TRUE;
10499
10500 /*
10501 * The matching file names are stored in a growarray. Init it empty.
10502 */
10503 ga_init2(&ga, (int)sizeof(char_u *), 30);
10504
10505 for (i = 0; i < num_pat; ++i)
10506 {
10507 add_pat = -1;
10508 p = pat[i];
10509
10510#ifdef VIM_BACKTICK
10511 if (vim_backtick(p))
10512 add_pat = expand_backtick(&ga, p, flags);
10513 else
10514#endif
10515 {
10516 /*
10517 * First expand environment variables, "~/" and "~user/".
10518 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010519 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010521 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522 if (p == NULL)
10523 p = pat[i];
10524#ifdef UNIX
10525 /*
10526 * On Unix, if expand_env() can't expand an environment
10527 * variable, use the shell to do that. Discard previously
10528 * found file names and start all over again.
10529 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010530 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 {
10532 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010533 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534 i = mch_expand_wildcards(num_pat, pat, num_file, file,
10535 flags);
10536 recursive = FALSE;
10537 return i;
10538 }
10539#endif
10540 }
10541
10542 /*
10543 * If there are wildcards: Expand file names and add each match to
10544 * the list. If there is no match, and EW_NOTFOUND is given, add
10545 * the pattern.
10546 * If there are no wildcards: Add the file name if it exists or
10547 * when EW_NOTFOUND is given.
10548 */
10549 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010550 {
10551#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010552 if ((flags & EW_PATH)
10553 && !mch_isFullName(p)
10554 && !(p[0] == '.'
10555 && (vim_ispathsep(p[1])
10556 || (p[1] == '.' && vim_ispathsep(p[2]))))
10557 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010558 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010559 /* :find completion where 'path' is used.
10560 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010561 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010562 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010563 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010564 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010565 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010566 else
10567#endif
10568 add_pat = mch_expandpath(&ga, p, flags);
10569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 }
10571
10572 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
10573 {
10574 char_u *t = backslash_halve_save(p);
10575
10576#if defined(MACOS_CLASSIC)
10577 slash_to_colon(t);
10578#endif
10579 /* When EW_NOTFOUND is used, always add files and dirs. Makes
10580 * "vim c:/" work. */
10581 if (flags & EW_NOTFOUND)
10582 addfile(&ga, t, flags | EW_DIR | EW_FILE);
10583 else if (mch_getperm(t) >= 0)
10584 addfile(&ga, t, flags);
10585 vim_free(t);
10586 }
10587
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010588#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010589 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010590 uniquefy_paths(&ga, p);
10591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592 if (p != pat[i])
10593 vim_free(p);
10594 }
10595
10596 *num_file = ga.ga_len;
10597 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
10598
10599 recursive = FALSE;
10600
10601 return (ga.ga_data != NULL) ? OK : FAIL;
10602}
10603
10604# ifdef VIM_BACKTICK
10605
10606/*
10607 * Return TRUE if we can expand this backtick thing here.
10608 */
10609 static int
10610vim_backtick(p)
10611 char_u *p;
10612{
10613 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
10614}
10615
10616/*
10617 * Expand an item in `backticks` by executing it as a command.
10618 * Currently only works when pat[] starts and ends with a `.
10619 * Returns number of file names found.
10620 */
10621 static int
10622expand_backtick(gap, pat, flags)
10623 garray_T *gap;
10624 char_u *pat;
10625 int flags; /* EW_* flags */
10626{
10627 char_u *p;
10628 char_u *cmd;
10629 char_u *buffer;
10630 int cnt = 0;
10631 int i;
10632
10633 /* Create the command: lop off the backticks. */
10634 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
10635 if (cmd == NULL)
10636 return 0;
10637
10638#ifdef FEAT_EVAL
10639 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000010640 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641 else
10642#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010643 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010644 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645 vim_free(cmd);
10646 if (buffer == NULL)
10647 return 0;
10648
10649 cmd = buffer;
10650 while (*cmd != NUL)
10651 {
10652 cmd = skipwhite(cmd); /* skip over white space */
10653 p = cmd;
10654 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
10655 ++p;
10656 /* add an entry if it is not empty */
10657 if (p > cmd)
10658 {
10659 i = *p;
10660 *p = NUL;
10661 addfile(gap, cmd, flags);
10662 *p = i;
10663 ++cnt;
10664 }
10665 cmd = p;
10666 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
10667 ++cmd;
10668 }
10669
10670 vim_free(buffer);
10671 return cnt;
10672}
10673# endif /* VIM_BACKTICK */
10674
10675/*
10676 * Add a file to a file list. Accepted flags:
10677 * EW_DIR add directories
10678 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010679 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680 * EW_NOTFOUND add even when it doesn't exist
10681 * EW_ADDSLASH add slash after directory name
10682 */
10683 void
10684addfile(gap, f, flags)
10685 garray_T *gap;
10686 char_u *f; /* filename */
10687 int flags;
10688{
10689 char_u *p;
10690 int isdir;
10691
10692 /* if the file/dir doesn't exist, may not add it */
10693 if (!(flags & EW_NOTFOUND) && mch_getperm(f) < 0)
10694 return;
10695
10696#ifdef FNAME_ILLEGAL
10697 /* if the file/dir contains illegal characters, don't add it */
10698 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
10699 return;
10700#endif
10701
10702 isdir = mch_isdir(f);
10703 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
10704 return;
10705
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010706 /* If the file isn't executable, may not add it. Do accept directories. */
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010707 if (!isdir && (flags & EW_EXEC) && !mch_can_exe(f, NULL))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010708 return;
10709
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710 /* Make room for another item in the file list. */
10711 if (ga_grow(gap, 1) == FAIL)
10712 return;
10713
10714 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
10715 if (p == NULL)
10716 return;
10717
10718 STRCPY(p, f);
10719#ifdef BACKSLASH_IN_FILENAME
10720 slash_adjust(p);
10721#endif
10722 /*
10723 * Append a slash or backslash after directory names if none is present.
10724 */
10725#ifndef DONT_ADD_PATHSEP_TO_DIR
10726 if (isdir && (flags & EW_ADDSLASH))
10727 add_pathsep(p);
10728#endif
10729 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730}
10731#endif /* !NO_EXPANDPATH */
10732
10733#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
10734
10735#ifndef SEEK_SET
10736# define SEEK_SET 0
10737#endif
10738#ifndef SEEK_END
10739# define SEEK_END 2
10740#endif
10741
10742/*
10743 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010744 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
10745 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746 * Returns an allocated string, or NULL for error.
10747 */
10748 char_u *
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010749get_cmd_output(cmd, infile, flags, ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750 char_u *cmd;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010751 char_u *infile; /* optional input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752 int flags; /* can be SHELL_SILENT */
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010753 int *ret_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754{
10755 char_u *tempname;
10756 char_u *command;
10757 char_u *buffer = NULL;
10758 int len;
10759 int i = 0;
10760 FILE *fd;
10761
10762 if (check_restricted() || check_secure())
10763 return NULL;
10764
10765 /* get a name for the temp file */
10766 if ((tempname = vim_tempname('o')) == NULL)
10767 {
10768 EMSG(_(e_notmp));
10769 return NULL;
10770 }
10771
10772 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010773 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774 if (command == NULL)
10775 goto done;
10776
10777 /*
10778 * Call the shell to execute the command (errors are ignored).
10779 * Don't check timestamps here.
10780 */
10781 ++no_check_timestamps;
10782 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
10783 --no_check_timestamps;
10784
10785 vim_free(command);
10786
10787 /*
10788 * read the names from the file into memory
10789 */
10790# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000010791 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792 fd = mch_fopen((char *)tempname, "r");
10793# else
10794 fd = mch_fopen((char *)tempname, READBIN);
10795# endif
10796
10797 if (fd == NULL)
10798 {
10799 EMSG2(_(e_notopen), tempname);
10800 goto done;
10801 }
10802
10803 fseek(fd, 0L, SEEK_END);
10804 len = ftell(fd); /* get size of temp file */
10805 fseek(fd, 0L, SEEK_SET);
10806
10807 buffer = alloc(len + 1);
10808 if (buffer != NULL)
10809 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
10810 fclose(fd);
10811 mch_remove(tempname);
10812 if (buffer == NULL)
10813 goto done;
10814#ifdef VMS
10815 len = i; /* VMS doesn't give us what we asked for... */
10816#endif
10817 if (i != len)
10818 {
10819 EMSG2(_(e_notread), tempname);
10820 vim_free(buffer);
10821 buffer = NULL;
10822 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010823 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020010824 {
10825 /* Change NUL into SOH, otherwise the string is truncated. */
10826 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020010827 if (buffer[i] == NUL)
10828 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020010829
Bram Moolenaar162bd912010-07-28 22:29:10 +020010830 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020010831 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010832 else
10833 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834
10835done:
10836 vim_free(tempname);
10837 return buffer;
10838}
10839#endif
10840
10841/*
10842 * Free the list of files returned by expand_wildcards() or other expansion
10843 * functions.
10844 */
10845 void
10846FreeWild(count, files)
10847 int count;
10848 char_u **files;
10849{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000010850 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 return;
10852#if defined(__EMX__) && defined(__ALWAYS_HAS_TRAILING_NULL_POINTER) /* XXX */
10853 /*
10854 * Is this still OK for when other functions than expand_wildcards() have
10855 * been used???
10856 */
10857 _fnexplodefree((char **)files);
10858#else
10859 while (count--)
10860 vim_free(files[count]);
10861 vim_free(files);
10862#endif
10863}
10864
10865/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020010866 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 * Don't do this when still processing a command or a mapping.
10868 * Don't do this when inside a ":normal" command.
10869 */
10870 int
10871goto_im()
10872{
10873 return (p_im && stuff_empty() && typebuf_typed());
10874}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020010875
10876/*
Bram Moolenaar050fe7e2014-05-22 14:00:16 +020010877 * Returns the isolated name of the shell in allocated memory:
Bram Moolenaar75a8d742014-05-07 15:10:21 +020010878 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
10879 * - Remove any argument. E.g., "csh -f" -> "csh".
10880 * But don't allow a space in the path, so that this works:
10881 * "/usr/bin/csh --rcfile ~/.cshrc"
10882 * But don't do that for Windows, it's common to have a space in the path.
10883 */
10884 char_u *
10885get_isolated_shell_name()
10886{
10887 char_u *p;
10888
10889#ifdef WIN3264
10890 p = gettail(p_sh);
10891 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
10892#else
10893 p = skiptowhite(p_sh);
10894 if (*p == NUL)
10895 {
10896 /* No white space, use the tail. */
10897 p = vim_strsave(gettail(p_sh));
10898 }
10899 else
10900 {
10901 char_u *p1, *p2;
10902
10903 /* Find the last path separator before the space. */
10904 p1 = p_sh;
10905 for (p2 = p_sh; p2 < p; mb_ptr_adv(p2))
10906 if (vim_ispathsep(*p2))
10907 p1 = p2 + 1;
10908 p = vim_strnsave(p1, (int)(p - p1));
10909 }
10910#endif
10911 return p;
10912}