blob: 477aba4bd3b2e0be93b2cbce353799ff085d179e [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 Moolenaar071d4272004-06-13 20:20:40 +00003128 }
3129 }
3130
3131 /* Call update_screen() later, which checks out what needs to be redrawn,
3132 * since it notices b_mod_set and then uses b_mod_*. */
3133 if (must_redraw < VALID)
3134 must_redraw = VALID;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003135
3136#ifdef FEAT_AUTOCMD
3137 /* when the cursor line is changed always trigger CursorMoved */
Bram Moolenaare163f1c2006-10-17 09:12:21 +00003138 if (lnum <= curwin->w_cursor.lnum
3139 && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003140 last_cursormoved.lnum = 0;
3141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142}
3143
3144/*
3145 * unchanged() is called when the changed flag must be reset for buffer 'buf'
3146 */
3147 void
3148unchanged(buf, ff)
3149 buf_T *buf;
3150 int ff; /* also reset 'fileformat' */
3151{
Bram Moolenaar164c60f2011-01-22 00:11:50 +01003152 if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 {
3154 buf->b_changed = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003155 ml_setflags(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 if (ff)
3157 save_file_ff(buf);
3158#ifdef FEAT_WINDOWS
3159 check_status(buf);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003160 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161#endif
3162#ifdef FEAT_TITLE
3163 need_maketitle = TRUE; /* set window title later */
3164#endif
3165 }
3166 ++buf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167#ifdef FEAT_NETBEANS_INTG
3168 netbeans_unmodified(buf);
3169#endif
3170}
3171
3172#if defined(FEAT_WINDOWS) || defined(PROTO)
3173/*
3174 * check_status: called when the status bars for the buffer 'buf'
3175 * need to be updated
3176 */
3177 void
3178check_status(buf)
3179 buf_T *buf;
3180{
3181 win_T *wp;
3182
3183 for (wp = firstwin; wp != NULL; wp = wp->w_next)
3184 if (wp->w_buffer == buf && wp->w_status_height)
3185 {
3186 wp->w_redr_status = TRUE;
3187 if (must_redraw < VALID)
3188 must_redraw = VALID;
3189 }
3190}
3191#endif
3192
3193/*
3194 * If the file is readonly, give a warning message with the first change.
3195 * Don't do this for autocommands.
3196 * Don't use emsg(), because it flushes the macro buffer.
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003197 * If we have undone all changes b_changed will be FALSE, but "b_did_warn"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 * will be TRUE.
Bram Moolenaarb0b50882010-07-07 18:26:28 +02003199 * Careful: may trigger autocommands that reload the buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 */
3201 void
3202change_warning(col)
3203 int col; /* column for message; non-zero when in insert
3204 mode and 'showmode' is on */
3205{
Bram Moolenaar496c5262009-03-18 14:42:00 +00003206 static char *w_readonly = N_("W10: Warning: Changing a readonly file");
3207
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 if (curbuf->b_did_warn == FALSE
3209 && curbufIsChanged() == 0
3210#ifdef FEAT_AUTOCMD
3211 && !autocmd_busy
3212#endif
3213 && curbuf->b_p_ro)
3214 {
3215#ifdef FEAT_AUTOCMD
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003216 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003218 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 if (!curbuf->b_p_ro)
3220 return;
3221#endif
3222 /*
3223 * Do what msg() does, but with a column offset if the warning should
3224 * be after the mode message.
3225 */
3226 msg_start();
3227 if (msg_row == Rows - 1)
3228 msg_col = col;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003229 msg_source(hl_attr(HLF_W));
Bram Moolenaar496c5262009-03-18 14:42:00 +00003230 MSG_PUTS_ATTR(_(w_readonly), hl_attr(HLF_W) | MSG_HIST);
3231#ifdef FEAT_EVAL
3232 set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1);
3233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 msg_clr_eos();
3235 (void)msg_end();
3236 if (msg_silent == 0 && !silent_mode)
3237 {
3238 out_flush();
3239 ui_delay(1000L, TRUE); /* give the user time to think about it */
3240 }
3241 curbuf->b_did_warn = TRUE;
3242 redraw_cmdline = FALSE; /* don't redraw and erase the message */
3243 if (msg_row < Rows - 1)
3244 showmode();
3245 }
3246}
3247
3248/*
3249 * Ask for a reply from the user, a 'y' or a 'n'.
3250 * No other characters are accepted, the message is repeated until a valid
3251 * reply is entered or CTRL-C is hit.
3252 * If direct is TRUE, don't use vgetc() but ui_inchar(), don't get characters
3253 * from any buffers but directly from the user.
3254 *
3255 * return the 'y' or 'n'
3256 */
3257 int
3258ask_yesno(str, direct)
3259 char_u *str;
3260 int direct;
3261{
3262 int r = ' ';
3263 int save_State = State;
3264
3265 if (exiting) /* put terminal in raw mode for this question */
3266 settmode(TMODE_RAW);
3267 ++no_wait_return;
3268#ifdef USE_ON_FLY_SCROLL
3269 dont_scroll = TRUE; /* disallow scrolling here */
3270#endif
3271 State = CONFIRM; /* mouse behaves like with :confirm */
3272#ifdef FEAT_MOUSE
3273 setmouse(); /* disables mouse for xterm */
3274#endif
3275 ++no_mapping;
3276 ++allow_keys; /* no mapping here, but recognize keys */
3277
3278 while (r != 'y' && r != 'n')
3279 {
3280 /* same highlighting as for wait_return */
3281 smsg_attr(hl_attr(HLF_R), (char_u *)"%s (y/n)?", str);
3282 if (direct)
3283 r = get_keystroke();
3284 else
Bram Moolenaar913626c2008-01-03 11:43:42 +00003285 r = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 if (r == Ctrl_C || r == ESC)
3287 r = 'n';
3288 msg_putchar(r); /* show what you typed */
3289 out_flush();
3290 }
3291 --no_wait_return;
3292 State = save_State;
3293#ifdef FEAT_MOUSE
3294 setmouse();
3295#endif
3296 --no_mapping;
3297 --allow_keys;
3298
3299 return r;
3300}
3301
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003302#if defined(FEAT_MOUSE) || defined(PROTO)
3303/*
3304 * Return TRUE if "c" is a mouse key.
3305 */
3306 int
3307is_mouse_key(c)
3308 int c;
3309{
3310 return c == K_LEFTMOUSE
3311 || c == K_LEFTMOUSE_NM
3312 || c == K_LEFTDRAG
3313 || c == K_LEFTRELEASE
3314 || c == K_LEFTRELEASE_NM
3315 || c == K_MIDDLEMOUSE
3316 || c == K_MIDDLEDRAG
3317 || c == K_MIDDLERELEASE
3318 || c == K_RIGHTMOUSE
3319 || c == K_RIGHTDRAG
3320 || c == K_RIGHTRELEASE
3321 || c == K_MOUSEDOWN
3322 || c == K_MOUSEUP
3323 || c == K_MOUSELEFT
3324 || c == K_MOUSERIGHT
3325 || c == K_X1MOUSE
3326 || c == K_X1DRAG
3327 || c == K_X1RELEASE
3328 || c == K_X2MOUSE
3329 || c == K_X2DRAG
3330 || c == K_X2RELEASE;
3331}
3332#endif
3333
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334/*
3335 * Get a key stroke directly from the user.
3336 * Ignores mouse clicks and scrollbar events, except a click for the left
3337 * button (used at the more prompt).
3338 * Doesn't use vgetc(), because it syncs undo and eats mapped characters.
3339 * Disadvantage: typeahead is ignored.
3340 * Translates the interrupt character for unix to ESC.
3341 */
3342 int
3343get_keystroke()
3344{
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003345 char_u *buf = NULL;
3346 int buflen = 150;
3347 int maxlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 int len = 0;
3349 int n;
3350 int save_mapped_ctrl_c = mapped_ctrl_c;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003351 int waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352
3353 mapped_ctrl_c = FALSE; /* mappings are not used here */
3354 for (;;)
3355 {
3356 cursor_on();
3357 out_flush();
3358
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003359 /* Leave some room for check_termcode() to insert a key code into (max
3360 * 5 chars plus NUL). And fix_input_buffer() can triple the number of
3361 * bytes. */
3362 maxlen = (buflen - 6 - len) / 3;
3363 if (buf == NULL)
3364 buf = alloc(buflen);
3365 else if (maxlen < 10)
3366 {
Bram Moolenaardc7e85e2012-06-20 12:40:08 +02003367 /* Need some more space. This might happen when receiving a long
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003368 * escape sequence. */
3369 buflen += 100;
3370 buf = vim_realloc(buf, buflen);
3371 maxlen = (buflen - 6 - len) / 3;
3372 }
3373 if (buf == NULL)
3374 {
3375 do_outofmem_msg((long_u)buflen);
3376 return ESC; /* panic! */
3377 }
3378
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 /* First time: blocking wait. Second time: wait up to 100ms for a
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003380 * terminal code to complete. */
3381 n = ui_inchar(buf + len, maxlen, len == 0 ? -1L : 100L, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 if (n > 0)
3383 {
3384 /* Replace zero and CSI by a special key code. */
3385 n = fix_input_buffer(buf + len, n, FALSE);
3386 len += n;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003387 waited = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 }
Bram Moolenaar4395a712006-09-05 18:57:57 +00003389 else if (len > 0)
3390 ++waited; /* keep track of the waiting time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391
Bram Moolenaar4395a712006-09-05 18:57:57 +00003392 /* Incomplete termcode and not timed out yet: get more characters */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003393 if ((n = check_termcode(1, buf, buflen, &len)) < 0
Bram Moolenaar4395a712006-09-05 18:57:57 +00003394 && (!p_ttimeout || waited * 100L < (p_ttm < 0 ? p_tm : p_ttm)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 continue;
Bram Moolenaar4395a712006-09-05 18:57:57 +00003396
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003397 if (n == KEYLEN_REMOVED) /* key code removed */
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003398 {
Bram Moolenaarfd30cd42011-03-22 13:07:26 +01003399 if (must_redraw != 0 && !need_wait_return && (State & CMDLINE) == 0)
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003400 {
3401 /* Redrawing was postponed, do it now. */
3402 update_screen(0);
3403 setcursor(); /* put cursor back where it belongs */
3404 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003405 continue;
Bram Moolenaar6eb634e2011-03-03 15:04:08 +01003406 }
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003407 if (n > 0) /* found a termcode: adjust length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 len = n;
Bram Moolenaar946ffd42010-12-30 12:30:31 +01003409 if (len == 0) /* nothing typed yet */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 continue;
3411
3412 /* Handle modifier and/or special key code. */
3413 n = buf[0];
3414 if (n == K_SPECIAL)
3415 {
3416 n = TO_SPECIAL(buf[1], buf[2]);
3417 if (buf[1] == KS_MODIFIER
3418 || n == K_IGNORE
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003419#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003420 || (is_mouse_key(n) && n != K_LEFTMOUSE)
Bram Moolenaara5be25e2013-03-16 21:35:33 +01003421#endif
Bram Moolenaar2526ef22013-03-16 14:20:51 +01003422#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 || n == K_VER_SCROLLBAR
3424 || n == K_HOR_SCROLLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425#endif
3426 )
3427 {
3428 if (buf[1] == KS_MODIFIER)
3429 mod_mask = buf[2];
3430 len -= 3;
3431 if (len > 0)
3432 mch_memmove(buf, buf + 3, (size_t)len);
3433 continue;
3434 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00003435 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 }
3437#ifdef FEAT_MBYTE
3438 if (has_mbyte)
3439 {
3440 if (MB_BYTE2LEN(n) > len)
3441 continue; /* more bytes to get */
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003442 buf[len >= buflen ? buflen - 1 : len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 n = (*mb_ptr2char)(buf);
3444 }
3445#endif
3446#ifdef UNIX
3447 if (n == intr_char)
3448 n = ESC;
3449#endif
3450 break;
3451 }
Bram Moolenaara8c8a682012-02-05 22:05:48 +01003452 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453
3454 mapped_ctrl_c = save_mapped_ctrl_c;
3455 return n;
3456}
3457
3458/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003459 * Get a number from the user.
3460 * When "mouse_used" is not NULL allow using the mouse.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 */
3462 int
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003463get_number(colon, mouse_used)
3464 int colon; /* allow colon to abort */
3465 int *mouse_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466{
3467 int n = 0;
3468 int c;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003469 int typed = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003471 if (mouse_used != NULL)
3472 *mouse_used = FALSE;
3473
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 /* When not printing messages, the user won't know what to type, return a
3475 * zero (as if CR was hit). */
3476 if (msg_silent != 0)
3477 return 0;
3478
3479#ifdef USE_ON_FLY_SCROLL
3480 dont_scroll = TRUE; /* disallow scrolling here */
3481#endif
3482 ++no_mapping;
3483 ++allow_keys; /* no mapping here, but recognize keys */
3484 for (;;)
3485 {
3486 windgoto(msg_row, msg_col);
3487 c = safe_vgetc();
3488 if (VIM_ISDIGIT(c))
3489 {
3490 n = n * 10 + c - '0';
3491 msg_putchar(c);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003492 ++typed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 }
3494 else if (c == K_DEL || c == K_KDEL || c == K_BS || c == Ctrl_H)
3495 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00003496 if (typed > 0)
3497 {
3498 MSG_PUTS("\b \b");
3499 --typed;
3500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 n /= 10;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 }
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003503#ifdef FEAT_MOUSE
3504 else if (mouse_used != NULL && c == K_LEFTMOUSE)
3505 {
3506 *mouse_used = TRUE;
3507 n = mouse_row + 1;
3508 break;
3509 }
3510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 else if (n == 0 && c == ':' && colon)
3512 {
3513 stuffcharReadbuff(':');
3514 if (!exmode_active)
3515 cmdline_row = msg_row;
3516 skip_redraw = TRUE; /* skip redraw once */
3517 do_redraw = FALSE;
3518 break;
3519 }
3520 else if (c == CAR || c == NL || c == Ctrl_C || c == ESC)
3521 break;
3522 }
3523 --no_mapping;
3524 --allow_keys;
3525 return n;
3526}
3527
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003528/*
3529 * Ask the user to enter a number.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003530 * When "mouse_used" is not NULL allow using the mouse and in that case return
3531 * the line number.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003532 */
3533 int
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003534prompt_for_number(mouse_used)
3535 int *mouse_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003536{
3537 int i;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003538 int save_cmdline_row;
3539 int save_State;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003540
3541 /* When using ":silent" assume that <CR> was entered. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003542 if (mouse_used != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00003543 MSG_PUTS(_("Type number and <Enter> or click with mouse (empty cancels): "));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003544 else
Bram Moolenaard812df62008-11-09 12:46:09 +00003545 MSG_PUTS(_("Type number and <Enter> (empty cancels): "));
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003546
Bram Moolenaar203335e2006-09-03 14:35:42 +00003547 /* Set the state such that text can be selected/copied/pasted and we still
3548 * get mouse events. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003549 save_cmdline_row = cmdline_row;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003550 cmdline_row = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003551 save_State = State;
Bram Moolenaar203335e2006-09-03 14:35:42 +00003552 State = CMDLINE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003553
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003554 i = get_number(TRUE, mouse_used);
3555 if (KeyTyped)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003556 {
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003557 /* don't call wait_return() now */
3558 /* msg_putchar('\n'); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003559 cmdline_row = msg_row - 1;
3560 need_wait_return = FALSE;
3561 msg_didany = FALSE;
Bram Moolenaarb2450162009-07-22 09:04:20 +00003562 msg_didout = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003563 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003564 else
3565 cmdline_row = save_cmdline_row;
3566 State = save_State;
3567
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003568 return i;
3569}
3570
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 void
3572msgmore(n)
3573 long n;
3574{
3575 long pn;
3576
3577 if (global_busy /* no messages now, wait until global is finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 || !messaging()) /* 'lazyredraw' set, don't do messages now */
3579 return;
3580
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003581 /* We don't want to overwrite another important message, but do overwrite
3582 * a previous "more lines" or "fewer lines" message, so that "5dd" and
3583 * then "put" reports the last action. */
3584 if (keep_msg != NULL && !keep_msg_more)
3585 return;
3586
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 if (n > 0)
3588 pn = n;
3589 else
3590 pn = -n;
3591
3592 if (pn > p_report)
3593 {
3594 if (pn == 1)
3595 {
3596 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003597 vim_strncpy(msg_buf, (char_u *)_("1 more line"),
3598 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003600 vim_strncpy(msg_buf, (char_u *)_("1 line less"),
3601 MSG_BUF_LEN - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 }
3603 else
3604 {
3605 if (n > 0)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003606 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3607 _("%ld more lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 else
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003609 vim_snprintf((char *)msg_buf, MSG_BUF_LEN,
3610 _("%ld fewer lines"), pn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 }
3612 if (got_int)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003613 vim_strcat(msg_buf, (char_u *)_(" (Interrupted)"), MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 if (msg(msg_buf))
3615 {
Bram Moolenaar238a5642006-02-21 22:12:05 +00003616 set_keep_msg(msg_buf, 0);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00003617 keep_msg_more = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 }
3619 }
3620}
3621
3622/*
3623 * flush map and typeahead buffers and give a warning for an error
3624 */
3625 void
3626beep_flush()
3627{
3628 if (emsg_silent == 0)
3629 {
3630 flush_buffers(FALSE);
3631 vim_beep();
3632 }
3633}
3634
3635/*
3636 * give a warning for an error
3637 */
3638 void
3639vim_beep()
3640{
3641 if (emsg_silent == 0)
3642 {
3643 if (p_vb
3644#ifdef FEAT_GUI
3645 /* While the GUI is starting up the termcap is set for the GUI
3646 * but the output still goes to a terminal. */
3647 && !(gui.in_use && gui.starting)
3648#endif
3649 )
3650 {
3651 out_str(T_VB);
3652 }
3653 else
3654 {
3655#ifdef MSDOS
3656 /*
3657 * The number of beeps outputted is reduced to avoid having to wait
3658 * for all the beeps to finish. This is only a problem on systems
3659 * where the beeps don't overlap.
3660 */
3661 if (beep_count == 0 || beep_count == 10)
3662 {
3663 out_char(BELL);
3664 beep_count = 1;
3665 }
3666 else
3667 ++beep_count;
3668#else
3669 out_char(BELL);
3670#endif
3671 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00003672
3673 /* When 'verbose' is set and we are sourcing a script or executing a
3674 * function give the user a hint where the beep comes from. */
3675 if (vim_strchr(p_debug, 'e') != NULL)
3676 {
3677 msg_source(hl_attr(HLF_W));
3678 msg_attr((char_u *)_("Beep!"), hl_attr(HLF_W));
3679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 }
3681}
3682
3683/*
3684 * To get the "real" home directory:
3685 * - get value of $HOME
3686 * For Unix:
3687 * - go to that directory
3688 * - do mch_dirname() to get the real name of that directory.
3689 * This also works with mounts and links.
3690 * Don't do this for MS-DOS, it will change the "current dir" for a drive.
3691 */
3692static char_u *homedir = NULL;
3693
3694 void
3695init_homedir()
3696{
3697 char_u *var;
3698
Bram Moolenaar05159a02005-02-26 23:04:13 +00003699 /* In case we are called a second time (when 'encoding' changes). */
3700 vim_free(homedir);
3701 homedir = NULL;
3702
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703#ifdef VMS
3704 var = mch_getenv((char_u *)"SYS$LOGIN");
3705#else
3706 var = mch_getenv((char_u *)"HOME");
3707#endif
3708
3709 if (var != NULL && *var == NUL) /* empty is same as not set */
3710 var = NULL;
3711
3712#ifdef WIN3264
3713 /*
3714 * Weird but true: $HOME may contain an indirect reference to another
3715 * variable, esp. "%USERPROFILE%". Happens when $USERPROFILE isn't set
3716 * when $HOME is being set.
3717 */
3718 if (var != NULL && *var == '%')
3719 {
3720 char_u *p;
3721 char_u *exp;
3722
3723 p = vim_strchr(var + 1, '%');
3724 if (p != NULL)
3725 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003726 vim_strncpy(NameBuff, var + 1, p - (var + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 exp = mch_getenv(NameBuff);
3728 if (exp != NULL && *exp != NUL
3729 && STRLEN(exp) + STRLEN(p) < MAXPATHL)
3730 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00003731 vim_snprintf((char *)NameBuff, MAXPATHL, "%s%s", exp, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 var = NameBuff;
3733 /* Also set $HOME, it's needed for _viminfo. */
3734 vim_setenv((char_u *)"HOME", NameBuff);
3735 }
3736 }
3737 }
3738
3739 /*
3740 * Typically, $HOME is not defined on Windows, unless the user has
3741 * specifically defined it for Vim's sake. However, on Windows NT
3742 * platforms, $HOMEDRIVE and $HOMEPATH are automatically defined for
3743 * each user. Try constructing $HOME from these.
3744 */
3745 if (var == NULL)
3746 {
3747 char_u *homedrive, *homepath;
3748
3749 homedrive = mch_getenv((char_u *)"HOMEDRIVE");
3750 homepath = mch_getenv((char_u *)"HOMEPATH");
Bram Moolenaar6f977012010-01-06 17:53:38 +01003751 if (homepath == NULL || *homepath == NUL)
3752 homepath = "\\";
3753 if (homedrive != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 && STRLEN(homedrive) + STRLEN(homepath) < MAXPATHL)
3755 {
3756 sprintf((char *)NameBuff, "%s%s", homedrive, homepath);
3757 if (NameBuff[0] != NUL)
3758 {
3759 var = NameBuff;
3760 /* Also set $HOME, it's needed for _viminfo. */
3761 vim_setenv((char_u *)"HOME", NameBuff);
3762 }
3763 }
3764 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003765
3766# if defined(FEAT_MBYTE)
3767 if (enc_utf8 && var != NULL)
3768 {
3769 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02003770 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003771
3772 /* Convert from active codepage to UTF-8. Other conversions are
3773 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003774 acp_to_enc(var, (int)STRLEN(var), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003775 if (pp != NULL)
3776 {
3777 homedir = pp;
3778 return;
3779 }
3780 }
3781# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782#endif
3783
3784#if defined(OS2) || defined(MSDOS) || defined(MSWIN)
3785 /*
3786 * Default home dir is C:/
3787 * Best assumption we can make in such a situation.
3788 */
3789 if (var == NULL)
3790 var = "C:/";
3791#endif
3792 if (var != NULL)
3793 {
3794#ifdef UNIX
3795 /*
3796 * Change to the directory and get the actual path. This resolves
3797 * links. Don't do it when we can't return.
3798 */
3799 if (mch_dirname(NameBuff, MAXPATHL) == OK
3800 && mch_chdir((char *)NameBuff) == 0)
3801 {
3802 if (!mch_chdir((char *)var) && mch_dirname(IObuff, IOSIZE) == OK)
3803 var = IObuff;
3804 if (mch_chdir((char *)NameBuff) != 0)
3805 EMSG(_(e_prev_dir));
3806 }
3807#endif
3808 homedir = vim_strsave(var);
3809 }
3810}
3811
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003812#if defined(EXITFREE) || defined(PROTO)
3813 void
3814free_homedir()
3815{
3816 vim_free(homedir);
3817}
Bram Moolenaar24305862012-08-15 14:05:05 +02003818
3819# ifdef FEAT_CMDL_COMPL
3820 void
3821free_users()
3822{
3823 ga_clear_strings(&ga_users);
3824}
3825# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003826#endif
3827
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828/*
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003829 * Call expand_env() and store the result in an allocated string.
3830 * This is not very memory efficient, this expects the result to be freed
3831 * again soon.
3832 */
3833 char_u *
3834expand_env_save(src)
3835 char_u *src;
3836{
3837 return expand_env_save_opt(src, FALSE);
3838}
3839
3840/*
3841 * Idem, but when "one" is TRUE handle the string as one file name, only
3842 * expand "~" at the start.
3843 */
3844 char_u *
3845expand_env_save_opt(src, one)
3846 char_u *src;
3847 int one;
3848{
3849 char_u *p;
3850
3851 p = alloc(MAXPATHL);
3852 if (p != NULL)
3853 expand_env_esc(src, p, MAXPATHL, FALSE, one, NULL);
3854 return p;
3855}
3856
3857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 * Expand environment variable with path name.
3859 * "~/" is also expanded, using $HOME. For Unix "~user/" is expanded.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003860 * Skips over "\ ", "\~" and "\$" (not for Win32 though).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 * If anything fails no expansion is done and dst equals src.
3862 */
3863 void
3864expand_env(src, dst, dstlen)
3865 char_u *src; /* input string e.g. "$HOME/vim.hlp" */
3866 char_u *dst; /* where to put the result */
3867 int dstlen; /* maximum length of the result */
3868{
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003869 expand_env_esc(src, dst, dstlen, FALSE, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870}
3871
3872 void
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003873expand_env_esc(srcp, dst, dstlen, esc, one, startstr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003874 char_u *srcp; /* input string e.g. "$HOME/vim.hlp" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 char_u *dst; /* where to put the result */
3876 int dstlen; /* maximum length of the result */
3877 int esc; /* escape spaces in expanded variables */
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00003878 int one; /* "srcp" is one file name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003879 char_u *startstr; /* start again after this (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880{
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003881 char_u *src;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 char_u *tail;
3883 int c;
3884 char_u *var;
3885 int copy_char;
3886 int mustfree; /* var was allocated, need to free it later */
3887 int at_start = TRUE; /* at start of a name */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003888 int startstr_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003890 if (startstr != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003891 startstr_len = (int)STRLEN(startstr);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00003892
3893 src = skipwhite(srcp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 --dstlen; /* leave one char space for "\," */
3895 while (*src && dstlen > 0)
3896 {
3897 copy_char = TRUE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003898 if ((*src == '$'
3899#ifdef VMS
3900 && at_start
3901#endif
3902 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3904 || *src == '%'
3905#endif
3906 || (*src == '~' && at_start))
3907 {
3908 mustfree = FALSE;
3909
3910 /*
3911 * The variable name is copied into dst temporarily, because it may
3912 * be a string in read-only memory and a NUL needs to be appended.
3913 */
3914 if (*src != '~') /* environment var */
3915 {
3916 tail = src + 1;
3917 var = dst;
3918 c = dstlen - 1;
3919
3920#ifdef UNIX
3921 /* Unix has ${var-name} type environment vars */
3922 if (*tail == '{' && !vim_isIDc('{'))
3923 {
3924 tail++; /* ignore '{' */
3925 while (c-- > 0 && *tail && *tail != '}')
3926 *var++ = *tail++;
3927 }
3928 else
3929#endif
3930 {
3931 while (c-- > 0 && *tail != NUL && ((vim_isIDc(*tail))
3932#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3933 || (*src == '%' && *tail != '%')
3934#endif
3935 ))
3936 {
3937#ifdef OS2 /* env vars only in uppercase */
3938 *var++ = TOUPPER_LOC(*tail);
3939 tail++; /* toupper() may be a macro! */
3940#else
3941 *var++ = *tail++;
3942#endif
3943 }
3944 }
3945
3946#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX)
3947# ifdef UNIX
3948 if (src[1] == '{' && *tail != '}')
3949# else
3950 if (*src == '%' && *tail != '%')
3951# endif
3952 var = NULL;
3953 else
3954 {
3955# ifdef UNIX
3956 if (src[1] == '{')
3957# else
3958 if (*src == '%')
3959#endif
3960 ++tail;
3961#endif
3962 *var = NUL;
3963 var = vim_getenv(dst, &mustfree);
3964#if defined(MSDOS) || defined(MSWIN) || defined(OS2) || defined(UNIX)
3965 }
3966#endif
3967 }
3968 /* home directory */
3969 else if ( src[1] == NUL
3970 || vim_ispathsep(src[1])
3971 || vim_strchr((char_u *)" ,\t\n", src[1]) != NULL)
3972 {
3973 var = homedir;
3974 tail = src + 1;
3975 }
3976 else /* user directory */
3977 {
3978#if defined(UNIX) || (defined(VMS) && defined(USER_HOME))
3979 /*
3980 * Copy ~user to dst[], so we can put a NUL after it.
3981 */
3982 tail = src;
3983 var = dst;
3984 c = dstlen - 1;
3985 while ( c-- > 0
3986 && *tail
3987 && vim_isfilec(*tail)
3988 && !vim_ispathsep(*tail))
3989 *var++ = *tail++;
3990 *var = NUL;
3991# ifdef UNIX
3992 /*
3993 * If the system supports getpwnam(), use it.
3994 * Otherwise, or if getpwnam() fails, the shell is used to
3995 * expand ~user. This is slower and may fail if the shell
3996 * does not support ~user (old versions of /bin/sh).
3997 */
3998# if defined(HAVE_GETPWNAM) && defined(HAVE_PWD_H)
3999 {
4000 struct passwd *pw;
4001
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004002 /* Note: memory allocated by getpwnam() is never freed.
4003 * Calling endpwent() apparently doesn't help. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 pw = getpwnam((char *)dst + 1);
4005 if (pw != NULL)
4006 var = (char_u *)pw->pw_dir;
4007 else
4008 var = NULL;
4009 }
4010 if (var == NULL)
4011# endif
4012 {
4013 expand_T xpc;
4014
4015 ExpandInit(&xpc);
4016 xpc.xp_context = EXPAND_FILES;
4017 var = ExpandOne(&xpc, dst, NULL,
4018 WILD_ADD_SLASH|WILD_SILENT, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 mustfree = TRUE;
4020 }
4021
4022# else /* !UNIX, thus VMS */
4023 /*
4024 * USER_HOME is a comma-separated list of
4025 * directories to search for the user account in.
4026 */
4027 {
4028 char_u test[MAXPATHL], paths[MAXPATHL];
4029 char_u *path, *next_path, *ptr;
4030 struct stat st;
4031
4032 STRCPY(paths, USER_HOME);
4033 next_path = paths;
4034 while (*next_path)
4035 {
4036 for (path = next_path; *next_path && *next_path != ',';
4037 next_path++);
4038 if (*next_path)
4039 *next_path++ = NUL;
4040 STRCPY(test, path);
4041 STRCAT(test, "/");
4042 STRCAT(test, dst + 1);
4043 if (mch_stat(test, &st) == 0)
4044 {
4045 var = alloc(STRLEN(test) + 1);
4046 STRCPY(var, test);
4047 mustfree = TRUE;
4048 break;
4049 }
4050 }
4051 }
4052# endif /* UNIX */
4053#else
4054 /* cannot expand user's home directory, so don't try */
4055 var = NULL;
4056 tail = (char_u *)""; /* for gcc */
4057#endif /* UNIX || VMS */
4058 }
4059
4060#ifdef BACKSLASH_IN_FILENAME
4061 /* If 'shellslash' is set change backslashes to forward slashes.
4062 * Can't use slash_adjust(), p_ssl may be set temporarily. */
4063 if (p_ssl && var != NULL && vim_strchr(var, '\\') != NULL)
4064 {
4065 char_u *p = vim_strsave(var);
4066
4067 if (p != NULL)
4068 {
4069 if (mustfree)
4070 vim_free(var);
4071 var = p;
4072 mustfree = TRUE;
4073 forward_slash(var);
4074 }
4075 }
4076#endif
4077
4078 /* If "var" contains white space, escape it with a backslash.
4079 * Required for ":e ~/tt" when $HOME includes a space. */
4080 if (esc && var != NULL && vim_strpbrk(var, (char_u *)" \t") != NULL)
4081 {
4082 char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
4083
4084 if (p != NULL)
4085 {
4086 if (mustfree)
4087 vim_free(var);
4088 var = p;
4089 mustfree = TRUE;
4090 }
4091 }
4092
4093 if (var != NULL && *var != NUL
4094 && (STRLEN(var) + STRLEN(tail) + 1 < (unsigned)dstlen))
4095 {
4096 STRCPY(dst, var);
4097 dstlen -= (int)STRLEN(var);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004098 c = (int)STRLEN(var);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 /* if var[] ends in a path separator and tail[] starts
4100 * with it, skip a character */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004101 if (*var != NUL && after_pathsep(dst, dst + c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA)
4103 && dst[-1] != ':'
4104#endif
4105 && vim_ispathsep(*tail))
4106 ++tail;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004107 dst += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 src = tail;
4109 copy_char = FALSE;
4110 }
4111 if (mustfree)
4112 vim_free(var);
4113 }
4114
4115 if (copy_char) /* copy at least one char */
4116 {
4117 /*
Bram Moolenaar25394022007-05-10 19:06:20 +00004118 * Recognize the start of a new name, for '~'.
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004119 * Don't do this when "one" is TRUE, to avoid expanding "~" in
4120 * ":edit foo ~ foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 */
4122 at_start = FALSE;
4123 if (src[0] == '\\' && src[1] != NUL)
4124 {
4125 *dst++ = *src++;
4126 --dstlen;
4127 }
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004128 else if ((src[0] == ' ' || src[0] == ',') && !one)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 at_start = TRUE;
4130 *dst++ = *src++;
4131 --dstlen;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00004132
4133 if (startstr != NULL && src - startstr_len >= srcp
4134 && STRNCMP(src - startstr_len, startstr, startstr_len) == 0)
4135 at_start = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 }
4137 }
4138 *dst = NUL;
4139}
4140
4141/*
4142 * Vim's version of getenv().
4143 * Special handling of $HOME, $VIM and $VIMRUNTIME.
Bram Moolenaar2f6b0b82005-03-08 22:43:10 +00004144 * Also does ACP to 'enc' conversion for Win32.
Bram Moolenaarb453a532011-04-28 17:48:44 +02004145 * "mustfree" is set to TRUE when returned is allocated, it must be
4146 * initialized to FALSE by the caller.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 */
4148 char_u *
4149vim_getenv(name, mustfree)
4150 char_u *name;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004151 int *mustfree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152{
4153 char_u *p;
4154 char_u *pend;
4155 int vimruntime;
4156
4157#if defined(OS2) || defined(MSDOS) || defined(MSWIN)
4158 /* use "C:/" when $HOME is not set */
4159 if (STRCMP(name, "HOME") == 0)
4160 return homedir;
4161#endif
4162
4163 p = mch_getenv(name);
4164 if (p != NULL && *p == NUL) /* empty is the same as not set */
4165 p = NULL;
4166
4167 if (p != NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004168 {
4169#if defined(FEAT_MBYTE) && defined(WIN3264)
4170 if (enc_utf8)
4171 {
4172 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004173 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004174
4175 /* Convert from active codepage to UTF-8. Other conversions are
4176 * not done, because they would fail for non-ASCII characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004177 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004178 if (pp != NULL)
4179 {
4180 p = pp;
4181 *mustfree = TRUE;
4182 }
4183 }
4184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 return p;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187
4188 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4189 if (!vimruntime && STRCMP(name, "VIM") != 0)
4190 return NULL;
4191
4192 /*
4193 * When expanding $VIMRUNTIME fails, try using $VIM/vim<version> or $VIM.
4194 * Don't do this when default_vimruntime_dir is non-empty.
4195 */
4196 if (vimruntime
4197#ifdef HAVE_PATHDEF
4198 && *default_vimruntime_dir == NUL
4199#endif
4200 )
4201 {
4202 p = mch_getenv((char_u *)"VIM");
4203 if (p != NULL && *p == NUL) /* empty is the same as not set */
4204 p = NULL;
4205 if (p != NULL)
4206 {
4207 p = vim_version_dir(p);
4208 if (p != NULL)
4209 *mustfree = TRUE;
4210 else
4211 p = mch_getenv((char_u *)"VIM");
Bram Moolenaar05159a02005-02-26 23:04:13 +00004212
4213#if defined(FEAT_MBYTE) && defined(WIN3264)
4214 if (enc_utf8)
4215 {
4216 int len;
Bram Moolenaarb453a532011-04-28 17:48:44 +02004217 char_u *pp = NULL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004218
4219 /* Convert from active codepage to UTF-8. Other conversions
4220 * are not done, because they would fail for non-ASCII
4221 * characters. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004222 acp_to_enc(p, (int)STRLEN(p), &pp, &len);
Bram Moolenaar05159a02005-02-26 23:04:13 +00004223 if (pp != NULL)
4224 {
Bram Moolenaarb453a532011-04-28 17:48:44 +02004225 if (*mustfree)
Bram Moolenaar05159a02005-02-26 23:04:13 +00004226 vim_free(p);
4227 p = pp;
4228 *mustfree = TRUE;
4229 }
4230 }
4231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 }
4233 }
4234
4235 /*
4236 * When expanding $VIM or $VIMRUNTIME fails, try using:
4237 * - the directory name from 'helpfile' (unless it contains '$')
4238 * - the executable name from argv[0]
4239 */
4240 if (p == NULL)
4241 {
4242 if (p_hf != NULL && vim_strchr(p_hf, '$') == NULL)
4243 p = p_hf;
4244#ifdef USE_EXE_NAME
4245 /*
4246 * Use the name of the executable, obtained from argv[0].
4247 */
4248 else
4249 p = exe_name;
4250#endif
4251 if (p != NULL)
4252 {
4253 /* remove the file name */
4254 pend = gettail(p);
4255
4256 /* remove "doc/" from 'helpfile', if present */
4257 if (p == p_hf)
4258 pend = remove_tail(p, pend, (char_u *)"doc");
4259
4260#ifdef USE_EXE_NAME
4261# ifdef MACOS_X
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004262 /* remove "MacOS" from exe_name and add "Resources/vim" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 if (p == exe_name)
4264 {
4265 char_u *pend1;
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004266 char_u *pnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004268 pend1 = remove_tail(p, pend, (char_u *)"MacOS");
4269 if (pend1 != pend)
4270 {
4271 pnew = alloc((unsigned)(pend1 - p) + 15);
4272 if (pnew != NULL)
4273 {
4274 STRNCPY(pnew, p, (pend1 - p));
4275 STRCPY(pnew + (pend1 - p), "Resources/vim");
4276 p = pnew;
4277 pend = p + STRLEN(p);
4278 }
4279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 }
4281# endif
4282 /* remove "src/" from exe_name, if present */
4283 if (p == exe_name)
4284 pend = remove_tail(p, pend, (char_u *)"src");
4285#endif
4286
4287 /* for $VIM, remove "runtime/" or "vim54/", if present */
4288 if (!vimruntime)
4289 {
4290 pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
4291 pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
4292 }
4293
4294 /* remove trailing path separator */
4295#ifndef MACOS_CLASSIC
4296 /* With MacOS path (with colons) the final colon is required */
Bram Moolenaare21877a2008-02-13 09:58:14 +00004297 /* to avoid confusion between absolute and relative path */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004298 if (pend > p && after_pathsep(p, pend))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 --pend;
4300#endif
4301
Bram Moolenaar95e9b492006-03-15 23:04:43 +00004302#ifdef MACOS_X
4303 if (p == exe_name || p == p_hf)
4304#endif
4305 /* check that the result is a directory name */
4306 p = vim_strnsave(p, (int)(pend - p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307
4308 if (p != NULL && !mch_isdir(p))
4309 {
4310 vim_free(p);
4311 p = NULL;
4312 }
4313 else
4314 {
4315#ifdef USE_EXE_NAME
4316 /* may add "/vim54" or "/runtime" if it exists */
4317 if (vimruntime && (pend = vim_version_dir(p)) != NULL)
4318 {
4319 vim_free(p);
4320 p = pend;
4321 }
4322#endif
4323 *mustfree = TRUE;
4324 }
4325 }
4326 }
4327
4328#ifdef HAVE_PATHDEF
4329 /* When there is a pathdef.c file we can use default_vim_dir and
4330 * default_vimruntime_dir */
4331 if (p == NULL)
4332 {
4333 /* Only use default_vimruntime_dir when it is not empty */
4334 if (vimruntime && *default_vimruntime_dir != NUL)
4335 {
4336 p = default_vimruntime_dir;
4337 *mustfree = FALSE;
4338 }
4339 else if (*default_vim_dir != NUL)
4340 {
4341 if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL)
4342 *mustfree = TRUE;
4343 else
4344 {
4345 p = default_vim_dir;
4346 *mustfree = FALSE;
4347 }
4348 }
4349 }
4350#endif
4351
4352 /*
4353 * Set the environment variable, so that the new value can be found fast
4354 * next time, and others can also use it (e.g. Perl).
4355 */
4356 if (p != NULL)
4357 {
4358 if (vimruntime)
4359 {
4360 vim_setenv((char_u *)"VIMRUNTIME", p);
4361 didset_vimruntime = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 }
4363 else
4364 {
4365 vim_setenv((char_u *)"VIM", p);
4366 didset_vim = TRUE;
4367 }
4368 }
4369 return p;
4370}
4371
4372/*
4373 * Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
4374 * Return NULL if not, return its name in allocated memory otherwise.
4375 */
4376 static char_u *
4377vim_version_dir(vimdir)
4378 char_u *vimdir;
4379{
4380 char_u *p;
4381
4382 if (vimdir == NULL || *vimdir == NUL)
4383 return NULL;
4384 p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, TRUE);
4385 if (p != NULL && mch_isdir(p))
4386 return p;
4387 vim_free(p);
4388 p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, TRUE);
4389 if (p != NULL && mch_isdir(p))
4390 return p;
4391 vim_free(p);
4392 return NULL;
4393}
4394
4395/*
4396 * If the string between "p" and "pend" ends in "name/", return "pend" minus
4397 * the length of "name/". Otherwise return "pend".
4398 */
4399 static char_u *
4400remove_tail(p, pend, name)
4401 char_u *p;
4402 char_u *pend;
4403 char_u *name;
4404{
4405 int len = (int)STRLEN(name) + 1;
4406 char_u *newend = pend - len;
4407
4408 if (newend >= p
4409 && fnamencmp(newend, name, len - 1) == 0
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004410 && (newend == p || after_pathsep(p, newend)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 return newend;
4412 return pend;
4413}
4414
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 * Our portable version of setenv.
4417 */
4418 void
4419vim_setenv(name, val)
4420 char_u *name;
4421 char_u *val;
4422{
4423#ifdef HAVE_SETENV
4424 mch_setenv((char *)name, (char *)val, 1);
4425#else
4426 char_u *envbuf;
4427
4428 /*
4429 * Putenv does not copy the string, it has to remain
4430 * valid. The allocated memory will never be freed.
4431 */
4432 envbuf = alloc((unsigned)(STRLEN(name) + STRLEN(val) + 2));
4433 if (envbuf != NULL)
4434 {
4435 sprintf((char *)envbuf, "%s=%s", name, val);
4436 putenv((char *)envbuf);
4437 }
4438#endif
Bram Moolenaar011a34d2012-02-29 13:49:09 +01004439#ifdef FEAT_GETTEXT
4440 /*
4441 * When setting $VIMRUNTIME adjust the directory to find message
4442 * translations to $VIMRUNTIME/lang.
4443 */
4444 if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0)
4445 {
4446 char_u *buf = concat_str(val, (char_u *)"/lang");
4447
4448 if (buf != NULL)
4449 {
4450 bindtextdomain(VIMPACKAGE, (char *)buf);
4451 vim_free(buf);
4452 }
4453 }
4454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455}
4456
4457#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4458/*
4459 * Function given to ExpandGeneric() to obtain an environment variable name.
4460 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 char_u *
4462get_env_name(xp, idx)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004463 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 int idx;
4465{
4466# if defined(AMIGA) || defined(__MRC__) || defined(__SC__)
4467 /*
4468 * No environ[] on the Amiga and on the Mac (using MPW).
4469 */
4470 return NULL;
4471# else
4472# ifndef __WIN32__
4473 /* Borland C++ 5.2 has this in a header file. */
4474 extern char **environ;
4475# endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004476# define ENVNAMELEN 100
4477 static char_u name[ENVNAMELEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 char_u *str;
4479 int n;
4480
4481 str = (char_u *)environ[idx];
4482 if (str == NULL)
4483 return NULL;
4484
Bram Moolenaar21cf8232004-07-16 20:18:37 +00004485 for (n = 0; n < ENVNAMELEN - 1; ++n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 {
4487 if (str[n] == '=' || str[n] == NUL)
4488 break;
4489 name[n] = str[n];
4490 }
4491 name[n] = NUL;
4492 return name;
4493# endif
4494}
Bram Moolenaar24305862012-08-15 14:05:05 +02004495
4496/*
4497 * Find all user names for user completion.
4498 * Done only once and then cached.
4499 */
4500 static void
Bram Moolenaar01b626c2013-06-16 22:49:14 +02004501init_users()
4502{
Bram Moolenaar24305862012-08-15 14:05:05 +02004503 static int lazy_init_done = FALSE;
4504
4505 if (lazy_init_done)
4506 return;
4507
4508 lazy_init_done = TRUE;
4509 ga_init2(&ga_users, sizeof(char_u *), 20);
4510
4511# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
4512 {
4513 char_u* user;
4514 struct passwd* pw;
4515
4516 setpwent();
4517 while ((pw = getpwent()) != NULL)
4518 /* pw->pw_name shouldn't be NULL but just in case... */
4519 if (pw->pw_name != NULL)
4520 {
4521 if (ga_grow(&ga_users, 1) == FAIL)
4522 break;
4523 user = vim_strsave((char_u*)pw->pw_name);
4524 if (user == NULL)
4525 break;
4526 ((char_u **)(ga_users.ga_data))[ga_users.ga_len++] = user;
4527 }
4528 endpwent();
4529 }
4530# endif
4531}
4532
4533/*
4534 * Function given to ExpandGeneric() to obtain an user names.
4535 */
4536 char_u*
4537get_users(xp, idx)
4538 expand_T *xp UNUSED;
4539 int idx;
4540{
4541 init_users();
4542 if (idx < ga_users.ga_len)
4543 return ((char_u **)ga_users.ga_data)[idx];
4544 return NULL;
4545}
4546
4547/*
4548 * Check whether name matches a user name. Return:
4549 * 0 if name does not match any user name.
4550 * 1 if name partially matches the beginning of a user name.
4551 * 2 is name fully matches a user name.
4552 */
4553int match_user(name)
4554 char_u* name;
4555{
4556 int i;
4557 int n = (int)STRLEN(name);
4558 int result = 0;
4559
4560 init_users();
4561 for (i = 0; i < ga_users.ga_len; i++)
4562 {
4563 if (STRCMP(((char_u **)ga_users.ga_data)[i], name) == 0)
4564 return 2; /* full match */
4565 if (STRNCMP(((char_u **)ga_users.ga_data)[i], name, n) == 0)
4566 result = 1; /* partial match */
4567 }
4568 return result;
4569}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570#endif
4571
4572/*
4573 * Replace home directory by "~" in each space or comma separated file name in
4574 * 'src'.
4575 * If anything fails (except when out of space) dst equals src.
4576 */
4577 void
4578home_replace(buf, src, dst, dstlen, one)
4579 buf_T *buf; /* when not NULL, check for help files */
4580 char_u *src; /* input file name */
4581 char_u *dst; /* where to put the result */
4582 int dstlen; /* maximum length of the result */
4583 int one; /* if TRUE, only replace one file name, include
4584 spaces and commas in the file name. */
4585{
4586 size_t dirlen = 0, envlen = 0;
4587 size_t len;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004588 char_u *homedir_env, *homedir_env_orig;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 char_u *p;
4590
4591 if (src == NULL)
4592 {
4593 *dst = NUL;
4594 return;
4595 }
4596
4597 /*
4598 * If the file is a help file, remove the path completely.
4599 */
4600 if (buf != NULL && buf->b_help)
4601 {
4602 STRCPY(dst, gettail(src));
4603 return;
4604 }
4605
4606 /*
4607 * We check both the value of the $HOME environment variable and the
4608 * "real" home directory.
4609 */
4610 if (homedir != NULL)
4611 dirlen = STRLEN(homedir);
4612
4613#ifdef VMS
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004614 homedir_env_orig = homedir_env = mch_getenv((char_u *)"SYS$LOGIN");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615#else
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004616 homedir_env_orig = homedir_env = mch_getenv((char_u *)"HOME");
4617#endif
Bram Moolenaarbef47902012-07-06 16:49:40 +02004618 /* Empty is the same as not set. */
4619 if (homedir_env != NULL && *homedir_env == NUL)
4620 homedir_env = NULL;
4621
Bram Moolenaare60c2e52013-06-05 19:35:38 +02004622#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL)
Bram Moolenaarbef47902012-07-06 16:49:40 +02004623 if (homedir_env != NULL && vim_strchr(homedir_env, '~') != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004624 {
4625 int usedlen = 0;
4626 int flen;
4627 char_u *fbuf = NULL;
4628
4629 flen = (int)STRLEN(homedir_env);
Bram Moolenaard12f8112012-06-20 17:56:09 +02004630 (void)modify_fname((char_u *)":p", &usedlen,
4631 &homedir_env, &fbuf, &flen);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004632 flen = (int)STRLEN(homedir_env);
4633 if (flen > 0 && vim_ispathsep(homedir_env[flen - 1]))
4634 /* Remove the trailing / that is added to a directory. */
4635 homedir_env[flen - 1] = NUL;
4636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637#endif
4638
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 if (homedir_env != NULL)
4640 envlen = STRLEN(homedir_env);
4641
4642 if (!one)
4643 src = skipwhite(src);
4644 while (*src && dstlen > 0)
4645 {
4646 /*
4647 * Here we are at the beginning of a file name.
4648 * First, check to see if the beginning of the file name matches
4649 * $HOME or the "real" home directory. Check that there is a '/'
4650 * after the match (so that if e.g. the file is "/home/pieter/bla",
4651 * and the home directory is "/home/piet", the file does not end up
4652 * as "~er/bla" (which would seem to indicate the file "bla" in user
4653 * er's home directory)).
4654 */
4655 p = homedir;
4656 len = dirlen;
4657 for (;;)
4658 {
4659 if ( len
4660 && fnamencmp(src, p, len) == 0
4661 && (vim_ispathsep(src[len])
4662 || (!one && (src[len] == ',' || src[len] == ' '))
4663 || src[len] == NUL))
4664 {
4665 src += len;
4666 if (--dstlen > 0)
4667 *dst++ = '~';
4668
4669 /*
4670 * If it's just the home directory, add "/".
4671 */
4672 if (!vim_ispathsep(src[0]) && --dstlen > 0)
4673 *dst++ = '/';
4674 break;
4675 }
4676 if (p == homedir_env)
4677 break;
4678 p = homedir_env;
4679 len = envlen;
4680 }
4681
4682 /* if (!one) skip to separator: space or comma */
4683 while (*src && (one || (*src != ',' && *src != ' ')) && --dstlen > 0)
4684 *dst++ = *src++;
4685 /* skip separator */
4686 while ((*src == ' ' || *src == ',') && --dstlen > 0)
4687 *dst++ = *src++;
4688 }
4689 /* if (dstlen == 0) out of space, what to do??? */
4690
4691 *dst = NUL;
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02004692
4693 if (homedir_env != homedir_env_orig)
4694 vim_free(homedir_env);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695}
4696
4697/*
4698 * Like home_replace, store the replaced string in allocated memory.
4699 * When something fails, NULL is returned.
4700 */
4701 char_u *
4702home_replace_save(buf, src)
4703 buf_T *buf; /* when not NULL, check for help files */
4704 char_u *src; /* input file name */
4705{
4706 char_u *dst;
4707 unsigned len;
4708
4709 len = 3; /* space for "~/" and trailing NUL */
4710 if (src != NULL) /* just in case */
4711 len += (unsigned)STRLEN(src);
4712 dst = alloc(len);
4713 if (dst != NULL)
4714 home_replace(buf, src, dst, len, TRUE);
4715 return dst;
4716}
4717
4718/*
4719 * Compare two file names and return:
4720 * FPC_SAME if they both exist and are the same file.
4721 * FPC_SAMEX if they both don't exist and have the same file name.
4722 * FPC_DIFF if they both exist and are different files.
4723 * FPC_NOTX if they both don't exist.
4724 * FPC_DIFFX if one of them doesn't exist.
4725 * For the first name environment variables are expanded
4726 */
4727 int
4728fullpathcmp(s1, s2, checkname)
4729 char_u *s1, *s2;
4730 int checkname; /* when both don't exist, check file names */
4731{
4732#ifdef UNIX
4733 char_u exp1[MAXPATHL];
4734 char_u full1[MAXPATHL];
4735 char_u full2[MAXPATHL];
4736 struct stat st1, st2;
4737 int r1, r2;
4738
4739 expand_env(s1, exp1, MAXPATHL);
4740 r1 = mch_stat((char *)exp1, &st1);
4741 r2 = mch_stat((char *)s2, &st2);
4742 if (r1 != 0 && r2 != 0)
4743 {
4744 /* if mch_stat() doesn't work, may compare the names */
4745 if (checkname)
4746 {
4747 if (fnamecmp(exp1, s2) == 0)
4748 return FPC_SAMEX;
4749 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4750 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4751 if (r1 == OK && r2 == OK && fnamecmp(full1, full2) == 0)
4752 return FPC_SAMEX;
4753 }
4754 return FPC_NOTX;
4755 }
4756 if (r1 != 0 || r2 != 0)
4757 return FPC_DIFFX;
4758 if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino)
4759 return FPC_SAME;
4760 return FPC_DIFF;
4761#else
4762 char_u *exp1; /* expanded s1 */
4763 char_u *full1; /* full path of s1 */
4764 char_u *full2; /* full path of s2 */
4765 int retval = FPC_DIFF;
4766 int r1, r2;
4767
4768 /* allocate one buffer to store three paths (alloc()/free() is slow!) */
4769 if ((exp1 = alloc(MAXPATHL * 3)) != NULL)
4770 {
4771 full1 = exp1 + MAXPATHL;
4772 full2 = full1 + MAXPATHL;
4773
4774 expand_env(s1, exp1, MAXPATHL);
4775 r1 = vim_FullName(exp1, full1, MAXPATHL, FALSE);
4776 r2 = vim_FullName(s2, full2, MAXPATHL, FALSE);
4777
4778 /* If vim_FullName() fails, the file probably doesn't exist. */
4779 if (r1 != OK && r2 != OK)
4780 {
4781 if (checkname && fnamecmp(exp1, s2) == 0)
4782 retval = FPC_SAMEX;
4783 else
4784 retval = FPC_NOTX;
4785 }
4786 else if (r1 != OK || r2 != OK)
4787 retval = FPC_DIFFX;
4788 else if (fnamecmp(full1, full2))
4789 retval = FPC_DIFF;
4790 else
4791 retval = FPC_SAME;
4792 vim_free(exp1);
4793 }
4794 return retval;
4795#endif
4796}
4797
4798/*
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004799 * Get the tail of a path: the file name.
Bram Moolenaar31710262010-08-13 13:36:15 +02004800 * When the path ends in a path separator the tail is the NUL after it.
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004801 * Fail safe: never returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 */
4803 char_u *
4804gettail(fname)
4805 char_u *fname;
4806{
4807 char_u *p1, *p2;
4808
4809 if (fname == NULL)
4810 return (char_u *)"";
Bram Moolenaar69c35002013-11-04 02:54:12 +01004811 for (p1 = p2 = get_past_head(fname); *p2; ) /* find last part of path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 {
Bram Moolenaar69c35002013-11-04 02:54:12 +01004813 if (vim_ispathsep_nocolon(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 p1 = p2 + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004815 mb_ptr_adv(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 }
4817 return p1;
4818}
4819
Bram Moolenaar31710262010-08-13 13:36:15 +02004820#if defined(FEAT_SEARCHPATH)
4821static char_u *gettail_dir __ARGS((char_u *fname));
4822
4823/*
4824 * Return the end of the directory name, on the first path
4825 * separator:
4826 * "/path/file", "/path/dir/", "/path//dir", "/file"
4827 * ^ ^ ^ ^
4828 */
4829 static char_u *
4830gettail_dir(fname)
4831 char_u *fname;
4832{
4833 char_u *dir_end = fname;
4834 char_u *next_dir_end = fname;
4835 int look_for_sep = TRUE;
4836 char_u *p;
4837
4838 for (p = fname; *p != NUL; )
4839 {
4840 if (vim_ispathsep(*p))
4841 {
4842 if (look_for_sep)
4843 {
4844 next_dir_end = p;
4845 look_for_sep = FALSE;
4846 }
4847 }
4848 else
4849 {
4850 if (!look_for_sep)
4851 dir_end = next_dir_end;
4852 look_for_sep = TRUE;
4853 }
4854 mb_ptr_adv(p);
4855 }
4856 return dir_end;
4857}
4858#endif
4859
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004861 * Get pointer to tail of "fname", including path separators. Putting a NUL
4862 * here leaves the directory name. Takes care of "c:/" and "//".
4863 * Always returns a valid pointer.
4864 */
4865 char_u *
4866gettail_sep(fname)
4867 char_u *fname;
4868{
4869 char_u *p;
4870 char_u *t;
4871
4872 p = get_past_head(fname); /* don't remove the '/' from "c:/file" */
4873 t = gettail(fname);
4874 while (t > p && after_pathsep(fname, t))
4875 --t;
4876#ifdef VMS
4877 /* path separator is part of the path */
4878 ++t;
4879#endif
4880 return t;
4881}
4882
4883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 * get the next path component (just after the next path separator).
4885 */
4886 char_u *
4887getnextcomp(fname)
4888 char_u *fname;
4889{
4890 while (*fname && !vim_ispathsep(*fname))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004891 mb_ptr_adv(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 if (*fname)
4893 ++fname;
4894 return fname;
4895}
4896
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897/*
4898 * Get a pointer to one character past the head of a path name.
4899 * Unix: after "/"; DOS: after "c:\"; Amiga: after "disk:/"; Mac: no head.
4900 * If there is no head, path is returned.
4901 */
4902 char_u *
4903get_past_head(path)
4904 char_u *path;
4905{
4906 char_u *retval;
4907
4908#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4909 /* may skip "c:" */
4910 if (isalpha(path[0]) && path[1] == ':')
4911 retval = path + 2;
4912 else
4913 retval = path;
4914#else
4915# if defined(AMIGA)
4916 /* may skip "label:" */
4917 retval = vim_strchr(path, ':');
4918 if (retval == NULL)
4919 retval = path;
4920# else /* Unix */
4921 retval = path;
4922# endif
4923#endif
4924
4925 while (vim_ispathsep(*retval))
4926 ++retval;
4927
4928 return retval;
4929}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930
4931/*
Bram Moolenaar69c35002013-11-04 02:54:12 +01004932 * Return TRUE if 'c' is a path separator.
4933 * Note that for MS-Windows this includes the colon.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 */
4935 int
4936vim_ispathsep(c)
4937 int c;
4938{
Bram Moolenaare60acc12011-05-10 16:41:25 +02004939#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 return (c == '/'); /* UNIX has ':' inside file names */
Bram Moolenaare60acc12011-05-10 16:41:25 +02004941#else
4942# ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 return (c == ':' || c == '/' || c == '\\');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004944# else
4945# ifdef VMS
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 /* server"user passwd"::device:[full.path.name]fname.extension;version" */
4947 return (c == ':' || c == '[' || c == ']' || c == '/'
4948 || c == '<' || c == '>' || c == '"' );
Bram Moolenaare60acc12011-05-10 16:41:25 +02004949# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 return (c == ':' || c == '/');
Bram Moolenaare60acc12011-05-10 16:41:25 +02004951# endif /* VMS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02004953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954}
4955
Bram Moolenaar69c35002013-11-04 02:54:12 +01004956/*
4957 * Like vim_ispathsep(c), but exclude the colon for MS-Windows.
4958 */
4959 int
4960vim_ispathsep_nocolon(c)
4961 int c;
4962{
4963 return vim_ispathsep(c)
4964#ifdef BACKSLASH_IN_FILENAME
4965 && c != ':'
4966#endif
4967 ;
4968}
4969
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970#if defined(FEAT_SEARCHPATH) || defined(PROTO)
4971/*
4972 * return TRUE if 'c' is a path list separator.
4973 */
4974 int
4975vim_ispathlistsep(c)
4976 int c;
4977{
4978#ifdef UNIX
4979 return (c == ':');
4980#else
Bram Moolenaar25394022007-05-10 19:06:20 +00004981 return (c == ';'); /* might not be right for every system... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982#endif
4983}
4984#endif
4985
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004986#if defined(FEAT_GUI_TABLINE) || defined(FEAT_WINDOWS) \
4987 || defined(FEAT_EVAL) || defined(PROTO)
4988/*
4989 * Shorten the path of a file from "~/foo/../.bar/fname" to "~/f/../.b/fname"
4990 * It's done in-place.
4991 */
4992 void
4993shorten_dir(str)
4994 char_u *str;
4995{
4996 char_u *tail, *s, *d;
4997 int skip = FALSE;
4998
4999 tail = gettail(str);
5000 d = str;
5001 for (s = str; ; ++s)
5002 {
5003 if (s >= tail) /* copy the whole tail */
5004 {
5005 *d++ = *s;
5006 if (*s == NUL)
5007 break;
5008 }
5009 else if (vim_ispathsep(*s)) /* copy '/' and next char */
5010 {
5011 *d++ = *s;
5012 skip = FALSE;
5013 }
5014 else if (!skip)
5015 {
5016 *d++ = *s; /* copy next char */
5017 if (*s != '~' && *s != '.') /* and leading "~" and "." */
5018 skip = TRUE;
5019# ifdef FEAT_MBYTE
5020 if (has_mbyte)
5021 {
5022 int l = mb_ptr2len(s);
5023
5024 while (--l > 0)
Bram Moolenaarb6baca52006-08-15 20:24:14 +00005025 *d++ = *++s;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005026 }
5027# endif
5028 }
5029 }
5030}
5031#endif
5032
Bram Moolenaar900b4d72005-12-12 22:05:50 +00005033/*
5034 * Return TRUE if the directory of "fname" exists, FALSE otherwise.
5035 * Also returns TRUE if there is no directory name.
5036 * "fname" must be writable!.
5037 */
5038 int
5039dir_of_file_exists(fname)
5040 char_u *fname;
5041{
5042 char_u *p;
5043 int c;
5044 int retval;
5045
5046 p = gettail_sep(fname);
5047 if (p == fname)
5048 return TRUE;
5049 c = *p;
5050 *p = NUL;
5051 retval = mch_isdir(fname);
5052 *p = c;
5053 return retval;
5054}
5055
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056/*
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005057 * Versions of fnamecmp() and fnamencmp() that handle '/' and '\' equally
5058 * and deal with 'fileignorecase'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 */
5060 int
5061vim_fnamecmp(x, y)
5062 char_u *x, *y;
5063{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005064#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 return vim_fnamencmp(x, y, MAXPATHL);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005066#else
5067 if (p_fic)
5068 return MB_STRICMP(x, y);
5069 return STRCMP(x, y);
5070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071}
5072
5073 int
5074vim_fnamencmp(x, y, len)
5075 char_u *x, *y;
5076 size_t len;
5077{
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005078#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005079 char_u *px = x;
5080 char_u *py = y;
5081 int cx = NUL;
5082 int cy = NUL;
5083
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005084 while (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 {
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005086 cx = PTR2CHAR(px);
5087 cy = PTR2CHAR(py);
5088 if (cx == NUL || cy == NUL
5089 || ((p_fic ? MB_TOLOWER(cx) != MB_TOLOWER(cy) : cx != cy)
5090 && !(cx == '/' && cy == '\\')
5091 && !(cx == '\\' && cy == '/')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 break;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005093 len -= MB_PTR2LEN(px);
5094 px += MB_PTR2LEN(px);
5095 py += MB_PTR2LEN(py);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 }
5097 if (len == 0)
5098 return 0;
Bram Moolenaard0e2d942013-03-19 18:31:49 +01005099 return (cx - cy);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005100#else
5101 if (p_fic)
5102 return MB_STRNICMP(x, y, len);
5103 return STRNCMP(x, y, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104#endif
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01005105}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106
5107/*
5108 * Concatenate file names fname1 and fname2 into allocated memory.
Bram Moolenaar25394022007-05-10 19:06:20 +00005109 * Only add a '/' or '\\' when 'sep' is TRUE and it is necessary.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 */
5111 char_u *
5112concat_fnames(fname1, fname2, sep)
5113 char_u *fname1;
5114 char_u *fname2;
5115 int sep;
5116{
5117 char_u *dest;
5118
5119 dest = alloc((unsigned)(STRLEN(fname1) + STRLEN(fname2) + 3));
5120 if (dest != NULL)
5121 {
5122 STRCPY(dest, fname1);
5123 if (sep)
5124 add_pathsep(dest);
5125 STRCAT(dest, fname2);
5126 }
5127 return dest;
5128}
5129
Bram Moolenaard6754642005-01-17 22:18:45 +00005130/*
5131 * Concatenate two strings and return the result in allocated memory.
5132 * Returns NULL when out of memory.
5133 */
5134 char_u *
5135concat_str(str1, str2)
5136 char_u *str1;
5137 char_u *str2;
5138{
5139 char_u *dest;
5140 size_t l = STRLEN(str1);
5141
5142 dest = alloc((unsigned)(l + STRLEN(str2) + 1L));
5143 if (dest != NULL)
5144 {
5145 STRCPY(dest, str1);
5146 STRCPY(dest + l, str2);
5147 }
5148 return dest;
5149}
Bram Moolenaard6754642005-01-17 22:18:45 +00005150
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151/*
5152 * Add a path separator to a file name, unless it already ends in a path
5153 * separator.
5154 */
5155 void
5156add_pathsep(p)
5157 char_u *p;
5158{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005159 if (*p != NUL && !after_pathsep(p, p + STRLEN(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 STRCAT(p, PATHSEPSTR);
5161}
5162
5163/*
5164 * FullName_save - Make an allocated copy of a full file name.
5165 * Returns NULL when out of memory.
5166 */
5167 char_u *
5168FullName_save(fname, force)
5169 char_u *fname;
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005170 int force; /* force expansion, even when it already looks
5171 * like a full path name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172{
5173 char_u *buf;
5174 char_u *new_fname = NULL;
5175
5176 if (fname == NULL)
5177 return NULL;
5178
5179 buf = alloc((unsigned)MAXPATHL);
5180 if (buf != NULL)
5181 {
5182 if (vim_FullName(fname, buf, MAXPATHL, force) != FAIL)
5183 new_fname = vim_strsave(buf);
5184 else
5185 new_fname = vim_strsave(fname);
5186 vim_free(buf);
5187 }
5188 return new_fname;
5189}
5190
5191#if defined(FEAT_CINDENT) || defined(FEAT_SYN_HL)
5192
5193static char_u *skip_string __ARGS((char_u *p));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005194static pos_T *ind_find_start_comment __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195
5196/*
5197 * Find the start of a comment, not knowing if we are in a comment right now.
5198 * Search starts at w_cursor.lnum and goes backwards.
5199 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005200 static pos_T *
5201ind_find_start_comment() /* XXX */
5202{
5203 return find_start_comment(curbuf->b_ind_maxcomment);
5204}
5205
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 pos_T *
5207find_start_comment(ind_maxcomment) /* XXX */
5208 int ind_maxcomment;
5209{
5210 pos_T *pos;
5211 char_u *line;
5212 char_u *p;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005213 int cur_maxcomment = ind_maxcomment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005215 for (;;)
5216 {
5217 pos = findmatchlimit(NULL, '*', FM_BACKWARD, cur_maxcomment);
5218 if (pos == NULL)
5219 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005221 /*
5222 * Check if the comment start we found is inside a string.
5223 * If it is then restrict the search to below this line and try again.
5224 */
5225 line = ml_get(pos->lnum);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005226 for (p = line; *p && (colnr_T)(p - line) < pos->col; ++p)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005227 p = skip_string(p);
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00005228 if ((colnr_T)(p - line) <= pos->col)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005229 break;
5230 cur_maxcomment = curwin->w_cursor.lnum - pos->lnum - 1;
5231 if (cur_maxcomment <= 0)
5232 {
5233 pos = NULL;
5234 break;
5235 }
5236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 return pos;
5238}
5239
5240/*
5241 * Skip to the end of a "string" and a 'c' character.
5242 * If there is no string or character, return argument unmodified.
5243 */
5244 static char_u *
5245skip_string(p)
5246 char_u *p;
5247{
5248 int i;
5249
5250 /*
5251 * We loop, because strings may be concatenated: "date""time".
5252 */
5253 for ( ; ; ++p)
5254 {
5255 if (p[0] == '\'') /* 'c' or '\n' or '\000' */
5256 {
5257 if (!p[1]) /* ' at end of line */
5258 break;
5259 i = 2;
5260 if (p[1] == '\\') /* '\n' or '\000' */
5261 {
5262 ++i;
5263 while (vim_isdigit(p[i - 1])) /* '\000' */
5264 ++i;
5265 }
5266 if (p[i] == '\'') /* check for trailing ' */
5267 {
5268 p += i;
5269 continue;
5270 }
5271 }
5272 else if (p[0] == '"') /* start of string */
5273 {
5274 for (++p; p[0]; ++p)
5275 {
5276 if (p[0] == '\\' && p[1] != NUL)
5277 ++p;
5278 else if (p[0] == '"') /* end of string */
5279 break;
5280 }
5281 if (p[0] == '"')
5282 continue;
5283 }
5284 break; /* no string found */
5285 }
5286 if (!*p)
5287 --p; /* backup from NUL */
5288 return p;
5289}
5290#endif /* FEAT_CINDENT || FEAT_SYN_HL */
5291
5292#if defined(FEAT_CINDENT) || defined(PROTO)
5293
5294/*
5295 * Do C or expression indenting on the current line.
5296 */
5297 void
5298do_c_expr_indent()
5299{
5300# ifdef FEAT_EVAL
5301 if (*curbuf->b_p_inde != NUL)
5302 fixthisline(get_expr_indent);
5303 else
5304# endif
5305 fixthisline(get_c_indent);
5306}
5307
5308/*
5309 * Functions for C-indenting.
5310 * Most of this originally comes from Eric Fischer.
5311 */
5312/*
5313 * Below "XXX" means that this function may unlock the current line.
5314 */
5315
5316static char_u *cin_skipcomment __ARGS((char_u *));
5317static int cin_nocode __ARGS((char_u *));
5318static pos_T *find_line_comment __ARGS((void));
5319static int cin_islabel_skip __ARGS((char_u **));
5320static int cin_isdefault __ARGS((char_u *));
5321static char_u *after_label __ARGS((char_u *l));
5322static int get_indent_nolabel __ARGS((linenr_T lnum));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005323static int skip_label __ARGS((linenr_T, char_u **pp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324static int cin_first_id_amount __ARGS((void));
5325static int cin_get_equal_amount __ARGS((linenr_T lnum));
5326static int cin_ispreproc __ARGS((char_u *));
5327static int cin_ispreproc_cont __ARGS((char_u **pp, linenr_T *lnump));
5328static int cin_iscomment __ARGS((char_u *));
5329static int cin_islinecomment __ARGS((char_u *));
5330static int cin_isterminated __ARGS((char_u *, int, int));
5331static int cin_isinit __ARGS((void));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005332static int cin_isfuncdecl __ARGS((char_u **, linenr_T, linenr_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333static int cin_isif __ARGS((char_u *));
5334static int cin_iselse __ARGS((char_u *));
5335static int cin_isdo __ARGS((char_u *));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005336static int cin_iswhileofdo __ARGS((char_u *, linenr_T));
Bram Moolenaarb345d492012-04-09 20:42:26 +02005337static int cin_is_if_for_while_before_offset __ARGS((char_u *line, int *poffset));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005338static int cin_iswhileofdo_end __ARGS((int terminated));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339static int cin_isbreak __ARGS((char_u *));
Bram Moolenaare7c56862007-08-04 10:14:52 +00005340static int cin_is_cpp_baseclass __ARGS((colnr_T *col));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005341static int get_baseclass_amount __ARGS((int col));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342static int cin_ends_in __ARGS((char_u *, char_u *, char_u *));
Bram Moolenaar75342212013-03-07 13:13:52 +01005343static int cin_starts_with __ARGS((char_u *s, char *word));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344static int cin_skip2pos __ARGS((pos_T *trypos));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005345static pos_T *find_start_brace __ARGS((void));
5346static pos_T *find_match_paren __ARGS((int));
5347static int corr_ind_maxparen __ARGS((pos_T *startpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348static int find_last_paren __ARGS((char_u *l, int start, int end));
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005349static int find_match __ARGS((int lookfor, linenr_T ourscope));
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005350static int cin_is_cpp_namespace __ARGS((char_u *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351
5352/*
5353 * Skip over white space and C comments within the line.
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005354 * Also skip over Perl/shell comments if desired.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 */
5356 static char_u *
5357cin_skipcomment(s)
5358 char_u *s;
5359{
5360 while (*s)
5361 {
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005362 char_u *prev_s = s;
5363
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 s = skipwhite(s);
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005365
5366 /* Perl/shell # comment comment continues until eol. Require a space
5367 * before # to avoid recognizing $#array. */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01005368 if (curbuf->b_ind_hash_comment != 0 && s != prev_s && *s == '#')
Bram Moolenaar39353fd2007-03-27 09:02:11 +00005369 {
5370 s += STRLEN(s);
5371 break;
5372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 if (*s != '/')
5374 break;
5375 ++s;
5376 if (*s == '/') /* slash-slash comment continues till eol */
5377 {
5378 s += STRLEN(s);
5379 break;
5380 }
5381 if (*s != '*')
5382 break;
5383 for (++s; *s; ++s) /* skip slash-star comment */
5384 if (s[0] == '*' && s[1] == '/')
5385 {
5386 s += 2;
5387 break;
5388 }
5389 }
5390 return s;
5391}
5392
5393/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005394 * Return TRUE if there is no code at *s. White space and comments are
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 * not considered code.
5396 */
5397 static int
5398cin_nocode(s)
5399 char_u *s;
5400{
5401 return *cin_skipcomment(s) == NUL;
5402}
5403
5404/*
5405 * Check previous lines for a "//" line comment, skipping over blank lines.
5406 */
5407 static pos_T *
5408find_line_comment() /* XXX */
5409{
5410 static pos_T pos;
5411 char_u *line;
5412 char_u *p;
5413
5414 pos = curwin->w_cursor;
5415 while (--pos.lnum > 0)
5416 {
5417 line = ml_get(pos.lnum);
5418 p = skipwhite(line);
5419 if (cin_islinecomment(p))
5420 {
5421 pos.col = (int)(p - line);
5422 return &pos;
5423 }
5424 if (*p != NUL)
5425 break;
5426 }
5427 return NULL;
5428}
5429
5430/*
5431 * Check if string matches "label:"; move to character after ':' if true.
5432 */
5433 static int
5434cin_islabel_skip(s)
5435 char_u **s;
5436{
5437 if (!vim_isIDc(**s)) /* need at least one ID character */
5438 return FALSE;
5439
5440 while (vim_isIDc(**s))
5441 (*s)++;
5442
5443 *s = cin_skipcomment(*s);
5444
5445 /* "::" is not a label, it's C++ */
5446 return (**s == ':' && *++*s != ':');
5447}
5448
5449/*
5450 * Recognize a label: "label:".
5451 * Note: curwin->w_cursor must be where we are looking for the label.
5452 */
5453 int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005454cin_islabel() /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455{
5456 char_u *s;
5457
5458 s = cin_skipcomment(ml_get_curline());
5459
5460 /*
5461 * Exclude "default" from labels, since it should be indented
5462 * like a switch label. Same for C++ scope declarations.
5463 */
5464 if (cin_isdefault(s))
5465 return FALSE;
5466 if (cin_isscopedecl(s))
5467 return FALSE;
5468
5469 if (cin_islabel_skip(&s))
5470 {
5471 /*
5472 * Only accept a label if the previous line is terminated or is a case
5473 * label.
5474 */
5475 pos_T cursor_save;
5476 pos_T *trypos;
5477 char_u *line;
5478
5479 cursor_save = curwin->w_cursor;
5480 while (curwin->w_cursor.lnum > 1)
5481 {
5482 --curwin->w_cursor.lnum;
5483
5484 /*
5485 * If we're in a comment now, skip to the start of the comment.
5486 */
5487 curwin->w_cursor.col = 0;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005488 if ((trypos = ind_find_start_comment()) != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 curwin->w_cursor = *trypos;
5490
5491 line = ml_get_curline();
5492 if (cin_ispreproc(line)) /* ignore #defines, #if, etc. */
5493 continue;
5494 if (*(line = cin_skipcomment(line)) == NUL)
5495 continue;
5496
5497 curwin->w_cursor = cursor_save;
5498 if (cin_isterminated(line, TRUE, FALSE)
5499 || cin_isscopedecl(line)
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005500 || cin_iscase(line, TRUE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 || (cin_islabel_skip(&line) && cin_nocode(line)))
5502 return TRUE;
5503 return FALSE;
5504 }
5505 curwin->w_cursor = cursor_save;
5506 return TRUE; /* label at start of file??? */
5507 }
5508 return FALSE;
5509}
5510
5511/*
Bram Moolenaar75342212013-03-07 13:13:52 +01005512 * Recognize structure initialization and enumerations:
5513 * "[typedef] [static|public|protected|private] enum"
5514 * "[typedef] [static|public|protected|private] = {"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 */
5516 static int
5517cin_isinit(void)
5518{
5519 char_u *s;
Bram Moolenaar75342212013-03-07 13:13:52 +01005520 static char *skip[] = {"static", "public", "protected", "private"};
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521
5522 s = cin_skipcomment(ml_get_curline());
5523
Bram Moolenaar75342212013-03-07 13:13:52 +01005524 if (cin_starts_with(s, "typedef"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 s = cin_skipcomment(s + 7);
5526
Bram Moolenaar75342212013-03-07 13:13:52 +01005527 for (;;)
5528 {
5529 int i, l;
Bram Moolenaara5285652011-12-14 20:05:21 +01005530
Bram Moolenaar75342212013-03-07 13:13:52 +01005531 for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
5532 {
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01005533 l = (int)strlen(skip[i]);
Bram Moolenaar75342212013-03-07 13:13:52 +01005534 if (cin_starts_with(s, skip[i]))
5535 {
5536 s = cin_skipcomment(s + l);
5537 l = 0;
5538 break;
5539 }
5540 }
5541 if (l != 0)
5542 break;
5543 }
5544
5545 if (cin_starts_with(s, "enum"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 return TRUE;
5547
5548 if (cin_ends_in(s, (char_u *)"=", (char_u *)"{"))
5549 return TRUE;
5550
5551 return FALSE;
5552}
5553
5554/*
5555 * Recognize a switch label: "case .*:" or "default:".
5556 */
5557 int
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005558cin_iscase(s, strict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 char_u *s;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005560 int strict; /* Allow relaxed check of case statement for JS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561{
5562 s = cin_skipcomment(s);
Bram Moolenaar75342212013-03-07 13:13:52 +01005563 if (cin_starts_with(s, "case"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 {
5565 for (s += 4; *s; ++s)
5566 {
5567 s = cin_skipcomment(s);
5568 if (*s == ':')
5569 {
5570 if (s[1] == ':') /* skip over "::" for C++ */
5571 ++s;
5572 else
5573 return TRUE;
5574 }
5575 if (*s == '\'' && s[1] && s[2] == '\'')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005576 s += 2; /* skip over ':' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 else if (*s == '/' && (s[1] == '*' || s[1] == '/'))
5578 return FALSE; /* stop at comment */
5579 else if (*s == '"')
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005580 {
5581 /* JS etc. */
5582 if (strict)
5583 return FALSE; /* stop at string */
5584 else
5585 return TRUE;
5586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 }
5588 return FALSE;
5589 }
5590
5591 if (cin_isdefault(s))
5592 return TRUE;
5593 return FALSE;
5594}
5595
5596/*
5597 * Recognize a "default" switch label.
5598 */
5599 static int
5600cin_isdefault(s)
5601 char_u *s;
5602{
5603 return (STRNCMP(s, "default", 7) == 0
5604 && *(s = cin_skipcomment(s + 7)) == ':'
5605 && s[1] != ':');
5606}
5607
5608/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +02005609 * Recognize a "public/private/protected" scope declaration label.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 */
5611 int
5612cin_isscopedecl(s)
5613 char_u *s;
5614{
5615 int i;
5616
5617 s = cin_skipcomment(s);
5618 if (STRNCMP(s, "public", 6) == 0)
5619 i = 6;
5620 else if (STRNCMP(s, "protected", 9) == 0)
5621 i = 9;
5622 else if (STRNCMP(s, "private", 7) == 0)
5623 i = 7;
5624 else
5625 return FALSE;
5626 return (*(s = cin_skipcomment(s + i)) == ':' && s[1] != ':');
5627}
5628
Bram Moolenaared38b0a2011-05-25 15:16:18 +02005629/* Maximum number of lines to search back for a "namespace" line. */
5630#define FIND_NAMESPACE_LIM 20
5631
5632/*
5633 * Recognize a "namespace" scope declaration.
5634 */
5635 static int
5636cin_is_cpp_namespace(s)
5637 char_u *s;
5638{
5639 char_u *p;
5640 int has_name = FALSE;
5641
5642 s = cin_skipcomment(s);
5643 if (STRNCMP(s, "namespace", 9) == 0 && (s[9] == NUL || !vim_iswordc(s[9])))
5644 {
5645 p = cin_skipcomment(skipwhite(s + 9));
5646 while (*p != NUL)
5647 {
5648 if (vim_iswhite(*p))
5649 {
5650 has_name = TRUE; /* found end of a name */
5651 p = cin_skipcomment(skipwhite(p));
5652 }
5653 else if (*p == '{')
5654 {
5655 break;
5656 }
5657 else if (vim_iswordc(*p))
5658 {
5659 if (has_name)
5660 return FALSE; /* word character after skipping past name */
5661 ++p;
5662 }
5663 else
5664 {
5665 return FALSE;
5666 }
5667 }
5668 return TRUE;
5669 }
5670 return FALSE;
5671}
5672
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673/*
5674 * Return a pointer to the first non-empty non-comment character after a ':'.
5675 * Return NULL if not found.
5676 * case 234: a = b;
5677 * ^
5678 */
5679 static char_u *
5680after_label(l)
5681 char_u *l;
5682{
5683 for ( ; *l; ++l)
5684 {
5685 if (*l == ':')
5686 {
5687 if (l[1] == ':') /* skip over "::" for C++ */
5688 ++l;
Bram Moolenaar3acfc302010-07-11 17:23:02 +02005689 else if (!cin_iscase(l + 1, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 break;
5691 }
5692 else if (*l == '\'' && l[1] && l[2] == '\'')
5693 l += 2; /* skip over 'x' */
5694 }
5695 if (*l == NUL)
5696 return NULL;
5697 l = cin_skipcomment(l + 1);
5698 if (*l == NUL)
5699 return NULL;
5700 return l;
5701}
5702
5703/*
5704 * Get indent of line "lnum", skipping a label.
5705 * Return 0 if there is nothing after the label.
5706 */
5707 static int
5708get_indent_nolabel(lnum) /* XXX */
5709 linenr_T lnum;
5710{
5711 char_u *l;
5712 pos_T fp;
5713 colnr_T col;
5714 char_u *p;
5715
5716 l = ml_get(lnum);
5717 p = after_label(l);
5718 if (p == NULL)
5719 return 0;
5720
5721 fp.col = (colnr_T)(p - l);
5722 fp.lnum = lnum;
5723 getvcol(curwin, &fp, &col, NULL, NULL);
5724 return (int)col;
5725}
5726
5727/*
5728 * Find indent for line "lnum", ignoring any case or jump label.
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005729 * Also return a pointer to the text (after the label) in "pp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 * label: if (asdf && asdfasdf)
5731 * ^
5732 */
5733 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005734skip_label(lnum, pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 linenr_T lnum;
5736 char_u **pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737{
5738 char_u *l;
5739 int amount;
5740 pos_T cursor_save;
5741
5742 cursor_save = curwin->w_cursor;
5743 curwin->w_cursor.lnum = lnum;
5744 l = ml_get_curline();
5745 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005746 if (cin_iscase(l, FALSE) || cin_isscopedecl(l) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 {
5748 amount = get_indent_nolabel(lnum);
5749 l = after_label(ml_get_curline());
5750 if (l == NULL) /* just in case */
5751 l = ml_get_curline();
5752 }
5753 else
5754 {
5755 amount = get_indent();
5756 l = ml_get_curline();
5757 }
5758 *pp = l;
5759
5760 curwin->w_cursor = cursor_save;
5761 return amount;
5762}
5763
5764/*
5765 * Return the indent of the first variable name after a type in a declaration.
5766 * int a, indent of "a"
5767 * static struct foo b, indent of "b"
5768 * enum bla c, indent of "c"
5769 * Returns zero when it doesn't look like a declaration.
5770 */
5771 static int
5772cin_first_id_amount()
5773{
5774 char_u *line, *p, *s;
5775 int len;
5776 pos_T fp;
5777 colnr_T col;
5778
5779 line = ml_get_curline();
5780 p = skipwhite(line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005781 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 if (len == 6 && STRNCMP(p, "static", 6) == 0)
5783 {
5784 p = skipwhite(p + 6);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005785 len = (int)(skiptowhite(p) - p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 }
5787 if (len == 6 && STRNCMP(p, "struct", 6) == 0)
5788 p = skipwhite(p + 6);
5789 else if (len == 4 && STRNCMP(p, "enum", 4) == 0)
5790 p = skipwhite(p + 4);
5791 else if ((len == 8 && STRNCMP(p, "unsigned", 8) == 0)
5792 || (len == 6 && STRNCMP(p, "signed", 6) == 0))
5793 {
5794 s = skipwhite(p + len);
5795 if ((STRNCMP(s, "int", 3) == 0 && vim_iswhite(s[3]))
5796 || (STRNCMP(s, "long", 4) == 0 && vim_iswhite(s[4]))
5797 || (STRNCMP(s, "short", 5) == 0 && vim_iswhite(s[5]))
5798 || (STRNCMP(s, "char", 4) == 0 && vim_iswhite(s[4])))
5799 p = s;
5800 }
5801 for (len = 0; vim_isIDc(p[len]); ++len)
5802 ;
5803 if (len == 0 || !vim_iswhite(p[len]) || cin_nocode(p))
5804 return 0;
5805
5806 p = skipwhite(p + len);
5807 fp.lnum = curwin->w_cursor.lnum;
5808 fp.col = (colnr_T)(p - line);
5809 getvcol(curwin, &fp, &col, NULL, NULL);
5810 return (int)col;
5811}
5812
5813/*
5814 * Return the indent of the first non-blank after an equal sign.
5815 * char *foo = "here";
5816 * Return zero if no (useful) equal sign found.
5817 * Return -1 if the line above "lnum" ends in a backslash.
5818 * foo = "asdf\
5819 * asdf\
5820 * here";
5821 */
5822 static int
5823cin_get_equal_amount(lnum)
5824 linenr_T lnum;
5825{
5826 char_u *line;
5827 char_u *s;
5828 colnr_T col;
5829 pos_T fp;
5830
5831 if (lnum > 1)
5832 {
5833 line = ml_get(lnum - 1);
5834 if (*line != NUL && line[STRLEN(line) - 1] == '\\')
5835 return -1;
5836 }
5837
5838 line = s = ml_get(lnum);
5839 while (*s != NUL && vim_strchr((char_u *)"=;{}\"'", *s) == NULL)
5840 {
5841 if (cin_iscomment(s)) /* ignore comments */
5842 s = cin_skipcomment(s);
5843 else
5844 ++s;
5845 }
5846 if (*s != '=')
5847 return 0;
5848
5849 s = skipwhite(s + 1);
5850 if (cin_nocode(s))
5851 return 0;
5852
5853 if (*s == '"') /* nice alignment for continued strings */
5854 ++s;
5855
5856 fp.lnum = lnum;
5857 fp.col = (colnr_T)(s - line);
5858 getvcol(curwin, &fp, &col, NULL, NULL);
5859 return (int)col;
5860}
5861
5862/*
5863 * Recognize a preprocessor statement: Any line that starts with '#'.
5864 */
5865 static int
5866cin_ispreproc(s)
5867 char_u *s;
5868{
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02005869 if (*skipwhite(s) == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 return TRUE;
5871 return FALSE;
5872}
5873
5874/*
5875 * Return TRUE if line "*pp" at "*lnump" is a preprocessor statement or a
5876 * continuation line of a preprocessor statement. Decrease "*lnump" to the
5877 * start and return the line in "*pp".
5878 */
5879 static int
5880cin_ispreproc_cont(pp, lnump)
5881 char_u **pp;
5882 linenr_T *lnump;
5883{
5884 char_u *line = *pp;
5885 linenr_T lnum = *lnump;
5886 int retval = FALSE;
5887
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00005888 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 {
5890 if (cin_ispreproc(line))
5891 {
5892 retval = TRUE;
5893 *lnump = lnum;
5894 break;
5895 }
5896 if (lnum == 1)
5897 break;
5898 line = ml_get(--lnum);
5899 if (*line == NUL || line[STRLEN(line) - 1] != '\\')
5900 break;
5901 }
5902
5903 if (lnum != *lnump)
5904 *pp = ml_get(*lnump);
5905 return retval;
5906}
5907
5908/*
5909 * Recognize the start of a C or C++ comment.
5910 */
5911 static int
5912cin_iscomment(p)
5913 char_u *p;
5914{
5915 return (p[0] == '/' && (p[1] == '*' || p[1] == '/'));
5916}
5917
5918/*
5919 * Recognize the start of a "//" comment.
5920 */
5921 static int
5922cin_islinecomment(p)
5923 char_u *p;
5924{
5925 return (p[0] == '/' && p[1] == '/');
5926}
5927
5928/*
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005929 * Recognize a line that starts with '{' or '}', or ends with ';', ',', '{' or
5930 * '}'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 * Don't consider "} else" a terminated line.
Bram Moolenaar496f9512011-05-19 16:35:09 +02005932 * If a line begins with an "else", only consider it terminated if no unmatched
5933 * opening braces follow (handle "else { foo();" correctly).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 * Return the character terminating the line (ending char's have precedence if
5935 * both apply in order to determine initializations).
5936 */
5937 static int
5938cin_isterminated(s, incl_open, incl_comma)
5939 char_u *s;
5940 int incl_open; /* include '{' at the end as terminator */
5941 int incl_comma; /* recognize a trailing comma */
5942{
Bram Moolenaar496f9512011-05-19 16:35:09 +02005943 char_u found_start = 0;
5944 unsigned n_open = 0;
5945 int is_else = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946
5947 s = cin_skipcomment(s);
5948
5949 if (*s == '{' || (*s == '}' && !cin_iselse(s)))
5950 found_start = *s;
5951
Bram Moolenaar496f9512011-05-19 16:35:09 +02005952 if (!found_start)
5953 is_else = cin_iselse(s);
5954
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 while (*s)
5956 {
5957 /* skip over comments, "" strings and 'c'haracters */
5958 s = skip_string(cin_skipcomment(s));
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005959 if (*s == '}' && n_open > 0)
5960 --n_open;
Bram Moolenaar496f9512011-05-19 16:35:09 +02005961 if ((!is_else || n_open == 0)
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005962 && (*s == ';' || *s == '}' || (incl_comma && *s == ','))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 && cin_nocode(s + 1))
5964 return *s;
Bram Moolenaar4ae06c12011-05-10 11:39:19 +02005965 else if (*s == '{')
5966 {
5967 if (incl_open && cin_nocode(s + 1))
5968 return *s;
5969 else
5970 ++n_open;
5971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972
5973 if (*s)
5974 s++;
5975 }
5976 return found_start;
5977}
5978
5979/*
5980 * Recognize the basic picture of a function declaration -- it needs to
5981 * have an open paren somewhere and a close paren at the end of the line and
5982 * no semicolons anywhere.
5983 * When a line ends in a comma we continue looking in the next line.
5984 * "sp" points to a string with the line. When looking at other lines it must
5985 * be restored to the line. When it's NULL fetch lines here.
5986 * "lnum" is where we start looking.
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005987 * "min_lnum" is the line before which we will not be looking.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 */
5989 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01005990cin_isfuncdecl(sp, first_lnum, min_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 char_u **sp;
5992 linenr_T first_lnum;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005993 linenr_T min_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994{
5995 char_u *s;
5996 linenr_T lnum = first_lnum;
5997 int retval = FALSE;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01005998 pos_T *trypos;
5999 int just_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000
6001 if (sp == NULL)
6002 s = ml_get(lnum);
6003 else
6004 s = *sp;
6005
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006006 if (find_last_paren(s, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006007 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006008 {
6009 lnum = trypos->lnum;
6010 if (lnum < min_lnum)
6011 return FALSE;
6012
6013 s = ml_get(lnum);
6014 }
6015
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006016 /* Ignore line starting with #. */
6017 if (cin_ispreproc(s))
6018 return FALSE;
6019
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 while (*s && *s != '(' && *s != ';' && *s != '\'' && *s != '"')
6021 {
6022 if (cin_iscomment(s)) /* ignore comments */
6023 s = cin_skipcomment(s);
6024 else
6025 ++s;
6026 }
6027 if (*s != '(')
6028 return FALSE; /* ';', ' or " before any () or no '(' */
6029
6030 while (*s && *s != ';' && *s != '\'' && *s != '"')
6031 {
6032 if (*s == ')' && cin_nocode(s + 1))
6033 {
6034 /* ')' at the end: may have found a match
6035 * Check for he previous line not to end in a backslash:
6036 * #if defined(x) && \
6037 * defined(y)
6038 */
6039 lnum = first_lnum - 1;
6040 s = ml_get(lnum);
6041 if (*s == NUL || s[STRLEN(s) - 1] != '\\')
6042 retval = TRUE;
6043 goto done;
6044 }
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006045 if ((*s == ',' && cin_nocode(s + 1)) || s[1] == NUL || cin_nocode(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 {
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006047 int comma = (*s == ',');
6048
6049 /* ',' at the end: continue looking in the next line.
6050 * At the end: check for ',' in the next line, for this style:
6051 * func(arg1
6052 * , arg2) */
6053 for (;;)
6054 {
6055 if (lnum >= curbuf->b_ml.ml_line_count)
6056 break;
6057 s = ml_get(++lnum);
6058 if (!cin_ispreproc(s))
6059 break;
6060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 if (lnum >= curbuf->b_ml.ml_line_count)
6062 break;
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006063 /* Require a comma at end of the line or a comma or ')' at the
6064 * start of next line. */
6065 s = skipwhite(s);
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006066 if (!just_started && (!comma && *s != ',' && *s != ')'))
Bram Moolenaar8d2d71d2011-04-28 13:02:09 +02006067 break;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006068 just_started = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 }
6070 else if (cin_iscomment(s)) /* ignore comments */
6071 s = cin_skipcomment(s);
6072 else
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006073 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 ++s;
Bram Moolenaarc367faa2011-12-14 20:21:35 +01006075 just_started = FALSE;
6076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 }
6078
6079done:
6080 if (lnum != first_lnum && sp != NULL)
6081 *sp = ml_get(first_lnum);
6082
6083 return retval;
6084}
6085
6086 static int
6087cin_isif(p)
6088 char_u *p;
6089{
6090 return (STRNCMP(p, "if", 2) == 0 && !vim_isIDc(p[2]));
6091}
6092
6093 static int
6094cin_iselse(p)
6095 char_u *p;
6096{
6097 if (*p == '}') /* accept "} else" */
6098 p = cin_skipcomment(p + 1);
6099 return (STRNCMP(p, "else", 4) == 0 && !vim_isIDc(p[4]));
6100}
6101
6102 static int
6103cin_isdo(p)
6104 char_u *p;
6105{
6106 return (STRNCMP(p, "do", 2) == 0 && !vim_isIDc(p[2]));
6107}
6108
6109/*
6110 * Check if this is a "while" that should have a matching "do".
6111 * We only accept a "while (condition) ;", with only white space between the
6112 * ')' and ';'. The condition may be spread over several lines.
6113 */
6114 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006115cin_iswhileofdo(p, lnum) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 char_u *p;
6117 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118{
6119 pos_T cursor_save;
6120 pos_T *trypos;
6121 int retval = FALSE;
6122
6123 p = cin_skipcomment(p);
6124 if (*p == '}') /* accept "} while (cond);" */
6125 p = cin_skipcomment(p + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006126 if (cin_starts_with(p, "while"))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 {
6128 cursor_save = curwin->w_cursor;
6129 curwin->w_cursor.lnum = lnum;
6130 curwin->w_cursor.col = 0;
6131 p = ml_get_curline();
6132 while (*p && *p != 'w') /* skip any '}', until the 'w' of the "while" */
6133 {
6134 ++p;
6135 ++curwin->w_cursor.col;
6136 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006137 if ((trypos = findmatchlimit(NULL, 0, 0,
6138 curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 && *cin_skipcomment(ml_get_pos(trypos) + 1) == ';')
6140 retval = TRUE;
6141 curwin->w_cursor = cursor_save;
6142 }
6143 return retval;
6144}
6145
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006146/*
Bram Moolenaarb345d492012-04-09 20:42:26 +02006147 * Check whether in "p" there is an "if", "for" or "while" before "*poffset".
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006148 * Return 0 if there is none.
6149 * Otherwise return !0 and update "*poffset" to point to the place where the
6150 * string was found.
6151 */
6152 static int
Bram Moolenaarb345d492012-04-09 20:42:26 +02006153cin_is_if_for_while_before_offset(line, poffset)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006154 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006155 int *poffset;
6156{
Bram Moolenaarb345d492012-04-09 20:42:26 +02006157 int offset = *poffset;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006158
6159 if (offset-- < 2)
6160 return 0;
6161 while (offset > 2 && vim_iswhite(line[offset]))
6162 --offset;
6163
6164 offset -= 1;
6165 if (!STRNCMP(line + offset, "if", 2))
6166 goto probablyFound;
6167
6168 if (offset >= 1)
6169 {
6170 offset -= 1;
6171 if (!STRNCMP(line + offset, "for", 3))
6172 goto probablyFound;
6173
6174 if (offset >= 2)
6175 {
6176 offset -= 2;
6177 if (!STRNCMP(line + offset, "while", 5))
6178 goto probablyFound;
6179 }
6180 }
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006181 return 0;
Bram Moolenaarb345d492012-04-09 20:42:26 +02006182
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006183probablyFound:
6184 if (!offset || !vim_isIDc(line[offset - 1]))
6185 {
6186 *poffset = offset;
6187 return 1;
6188 }
6189 return 0;
6190}
6191
6192/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006193 * Return TRUE if we are at the end of a do-while.
6194 * do
6195 * nothing;
6196 * while (foo
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006197 * && bar); <-- here
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006198 * Adjust the cursor to the line with "while".
6199 */
6200 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006201cin_iswhileofdo_end(terminated)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006202 int terminated;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006203{
6204 char_u *line;
6205 char_u *p;
6206 char_u *s;
6207 pos_T *trypos;
6208 int i;
6209
6210 if (terminated != ';') /* there must be a ';' at the end */
6211 return FALSE;
6212
6213 p = line = ml_get_curline();
6214 while (*p != NUL)
6215 {
6216 p = cin_skipcomment(p);
6217 if (*p == ')')
6218 {
6219 s = skipwhite(p + 1);
6220 if (*s == ';' && cin_nocode(s + 1))
6221 {
6222 /* Found ");" at end of the line, now check there is "while"
6223 * before the matching '('. XXX */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006224 i = (int)(p - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006225 curwin->w_cursor.col = i;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006226 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006227 if (trypos != NULL)
6228 {
6229 s = cin_skipcomment(ml_get(trypos->lnum));
6230 if (*s == '}') /* accept "} while (cond);" */
6231 s = cin_skipcomment(s + 1);
Bram Moolenaar75342212013-03-07 13:13:52 +01006232 if (cin_starts_with(s, "while"))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006233 {
6234 curwin->w_cursor.lnum = trypos->lnum;
6235 return TRUE;
6236 }
6237 }
6238
6239 /* Searching may have made "line" invalid, get it again. */
6240 line = ml_get_curline();
6241 p = line + i;
6242 }
6243 }
6244 if (*p != NUL)
6245 ++p;
6246 }
6247 return FALSE;
6248}
6249
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 static int
6251cin_isbreak(p)
6252 char_u *p;
6253{
6254 return (STRNCMP(p, "break", 5) == 0 && !vim_isIDc(p[5]));
6255}
6256
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006257/*
6258 * Find the position of a C++ base-class declaration or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 * constructor-initialization. eg:
6260 *
6261 * class MyClass :
6262 * baseClass <-- here
6263 * class MyClass : public baseClass,
6264 * anotherBaseClass <-- here (should probably lineup ??)
6265 * MyClass::MyClass(...) :
6266 * baseClass(...) <-- here (constructor-initialization)
Bram Moolenaar18144c82006-04-12 21:52:12 +00006267 *
6268 * This is a lot of guessing. Watch out for "cond ? func() : foo".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 */
6270 static int
Bram Moolenaare7c56862007-08-04 10:14:52 +00006271cin_is_cpp_baseclass(col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006272 colnr_T *col; /* return: column to align with */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273{
6274 char_u *s;
6275 int class_or_struct, lookfor_ctor_init, cpp_base_class;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006276 linenr_T lnum = curwin->w_cursor.lnum;
Bram Moolenaare7c56862007-08-04 10:14:52 +00006277 char_u *line = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278
6279 *col = 0;
6280
Bram Moolenaar21cf8232004-07-16 20:18:37 +00006281 s = skipwhite(line);
6282 if (*s == '#') /* skip #define FOO x ? (x) : x */
6283 return FALSE;
6284 s = cin_skipcomment(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 if (*s == NUL)
6286 return FALSE;
6287
6288 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6289
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006290 /* Search for a line starting with '#', empty, ending in ';' or containing
6291 * '{' or '}' and start below it. This handles the following situations:
6292 * a = cond ?
6293 * func() :
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006294 * asdf;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006295 * func::foo()
6296 * : something
6297 * {}
6298 * Foo::Foo (int one, int two)
6299 * : something(4),
6300 * somethingelse(3)
6301 * {}
6302 */
6303 while (lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00006305 line = ml_get(lnum - 1);
6306 s = skipwhite(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006307 if (*s == '#' || *s == NUL)
6308 break;
6309 while (*s != NUL)
6310 {
6311 s = cin_skipcomment(s);
6312 if (*s == '{' || *s == '}'
6313 || (*s == ';' && cin_nocode(s + 1)))
6314 break;
6315 if (*s != NUL)
6316 ++s;
6317 }
6318 if (*s != NUL)
6319 break;
6320 --lnum;
6321 }
6322
Bram Moolenaare7c56862007-08-04 10:14:52 +00006323 line = ml_get(lnum);
6324 s = cin_skipcomment(line);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006325 for (;;)
6326 {
6327 if (*s == NUL)
6328 {
6329 if (lnum == curwin->w_cursor.lnum)
6330 break;
6331 /* Continue in the cursor line. */
Bram Moolenaare7c56862007-08-04 10:14:52 +00006332 line = ml_get(++lnum);
6333 s = cin_skipcomment(line);
6334 if (*s == NUL)
6335 continue;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006336 }
6337
Bram Moolenaaraede6ce2011-05-10 11:56:30 +02006338 if (s[0] == '"')
6339 s = skip_string(s) + 1;
6340 else if (s[0] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 {
6342 if (s[1] == ':')
6343 {
6344 /* skip double colon. It can't be a constructor
6345 * initialization any more */
6346 lookfor_ctor_init = FALSE;
6347 s = cin_skipcomment(s + 2);
6348 }
6349 else if (lookfor_ctor_init || class_or_struct)
6350 {
6351 /* we have something found, that looks like the start of
Bram Moolenaare21877a2008-02-13 09:58:14 +00006352 * cpp-base-class-declaration or constructor-initialization */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 cpp_base_class = TRUE;
6354 lookfor_ctor_init = class_or_struct = FALSE;
6355 *col = 0;
6356 s = cin_skipcomment(s + 1);
6357 }
6358 else
6359 s = cin_skipcomment(s + 1);
6360 }
6361 else if ((STRNCMP(s, "class", 5) == 0 && !vim_isIDc(s[5]))
6362 || (STRNCMP(s, "struct", 6) == 0 && !vim_isIDc(s[6])))
6363 {
6364 class_or_struct = TRUE;
6365 lookfor_ctor_init = FALSE;
6366
6367 if (*s == 'c')
6368 s = cin_skipcomment(s + 5);
6369 else
6370 s = cin_skipcomment(s + 6);
6371 }
6372 else
6373 {
6374 if (s[0] == '{' || s[0] == '}' || s[0] == ';')
6375 {
6376 cpp_base_class = lookfor_ctor_init = class_or_struct = FALSE;
6377 }
6378 else if (s[0] == ')')
6379 {
6380 /* Constructor-initialization is assumed if we come across
6381 * something like "):" */
6382 class_or_struct = FALSE;
6383 lookfor_ctor_init = TRUE;
6384 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00006385 else if (s[0] == '?')
6386 {
6387 /* Avoid seeing '() :' after '?' as constructor init. */
6388 return FALSE;
6389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 else if (!vim_isIDc(s[0]))
6391 {
6392 /* if it is not an identifier, we are wrong */
6393 class_or_struct = FALSE;
6394 lookfor_ctor_init = FALSE;
6395 }
6396 else if (*col == 0)
6397 {
6398 /* it can't be a constructor-initialization any more */
6399 lookfor_ctor_init = FALSE;
6400
6401 /* the first statement starts here: lineup with this one... */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006402 if (cpp_base_class)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 *col = (colnr_T)(s - line);
6404 }
6405
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006406 /* When the line ends in a comma don't align with it. */
6407 if (lnum == curwin->w_cursor.lnum && *s == ',' && cin_nocode(s + 1))
6408 *col = 0;
6409
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 s = cin_skipcomment(s + 1);
6411 }
6412 }
6413
6414 return cpp_base_class;
6415}
6416
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006417 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006418get_baseclass_amount(col)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006419 int col;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006420{
6421 int amount;
6422 colnr_T vcol;
6423 pos_T *trypos;
6424
6425 if (col == 0)
6426 {
6427 amount = get_indent();
6428 if (find_last_paren(ml_get_curline(), '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006429 && (trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006430 amount = get_indent_lnum(trypos->lnum); /* XXX */
6431 if (!cin_ends_in(ml_get_curline(), (char_u *)",", NULL))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006432 amount += curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006433 }
6434 else
6435 {
6436 curwin->w_cursor.col = col;
6437 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
6438 amount = (int)vcol;
6439 }
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006440 if (amount < curbuf->b_ind_cpp_baseclass)
6441 amount = curbuf->b_ind_cpp_baseclass;
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00006442 return amount;
6443}
6444
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445/*
6446 * Return TRUE if string "s" ends with the string "find", possibly followed by
6447 * white space and comments. Skip strings and comments.
6448 * Ignore "ignore" after "find" if it's not NULL.
6449 */
6450 static int
6451cin_ends_in(s, find, ignore)
6452 char_u *s;
6453 char_u *find;
6454 char_u *ignore;
6455{
6456 char_u *p = s;
6457 char_u *r;
6458 int len = (int)STRLEN(find);
6459
6460 while (*p != NUL)
6461 {
6462 p = cin_skipcomment(p);
6463 if (STRNCMP(p, find, len) == 0)
6464 {
6465 r = skipwhite(p + len);
6466 if (ignore != NULL && STRNCMP(r, ignore, STRLEN(ignore)) == 0)
6467 r = skipwhite(r + STRLEN(ignore));
6468 if (cin_nocode(r))
6469 return TRUE;
6470 }
6471 if (*p != NUL)
6472 ++p;
6473 }
6474 return FALSE;
6475}
6476
6477/*
Bram Moolenaar75342212013-03-07 13:13:52 +01006478 * Return TRUE when "s" starts with "word" and then a non-ID character.
6479 */
6480 static int
6481cin_starts_with(s, word)
6482 char_u *s;
6483 char *word;
6484{
Bram Moolenaarb3cb9822013-03-13 17:01:52 +01006485 int l = (int)STRLEN(word);
Bram Moolenaar75342212013-03-07 13:13:52 +01006486
6487 return (STRNCMP(s, word, l) == 0 && !vim_isIDc(s[l]));
6488}
6489
6490/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 * Skip strings, chars and comments until at or past "trypos".
6492 * Return the column found.
6493 */
6494 static int
6495cin_skip2pos(trypos)
6496 pos_T *trypos;
6497{
6498 char_u *line;
6499 char_u *p;
6500
6501 p = line = ml_get(trypos->lnum);
6502 while (*p && (colnr_T)(p - line) < trypos->col)
6503 {
6504 if (cin_iscomment(p))
6505 p = cin_skipcomment(p);
6506 else
6507 {
6508 p = skip_string(p);
6509 ++p;
6510 }
6511 }
6512 return (int)(p - line);
6513}
6514
6515/*
6516 * Find the '{' at the start of the block we are in.
6517 * Return NULL if no match found.
6518 * Ignore a '{' that is in a comment, makes indenting the next three lines
6519 * work. */
6520/* foo() */
6521/* { */
6522/* } */
6523
6524 static pos_T *
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006525find_start_brace() /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526{
6527 pos_T cursor_save;
6528 pos_T *trypos;
6529 pos_T *pos;
6530 static pos_T pos_copy;
6531
6532 cursor_save = curwin->w_cursor;
6533 while ((trypos = findmatchlimit(NULL, '{', FM_BLOCKSTOP, 0)) != NULL)
6534 {
6535 pos_copy = *trypos; /* copy pos_T, next findmatch will change it */
6536 trypos = &pos_copy;
6537 curwin->w_cursor = *trypos;
6538 pos = NULL;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006539 /* ignore the { if it's in a // or / * * / comment */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 if ((colnr_T)cin_skip2pos(trypos) == trypos->col
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006541 && (pos = ind_find_start_comment()) == NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 break;
6543 if (pos != NULL)
6544 curwin->w_cursor.lnum = pos->lnum;
6545 }
6546 curwin->w_cursor = cursor_save;
6547 return trypos;
6548}
6549
6550/*
6551 * Find the matching '(', failing if it is in a comment.
Bram Moolenaar3675fa02012-04-05 17:17:42 +02006552 * Return NULL if no match found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 */
6554 static pos_T *
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006555find_match_paren(ind_maxparen) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 int ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557{
6558 pos_T cursor_save;
6559 pos_T *trypos;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00006560 static pos_T pos_copy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561
6562 cursor_save = curwin->w_cursor;
6563 if ((trypos = findmatchlimit(NULL, '(', 0, ind_maxparen)) != NULL)
6564 {
6565 /* check if the ( is in a // comment */
6566 if ((colnr_T)cin_skip2pos(trypos) > trypos->col)
6567 trypos = NULL;
6568 else
6569 {
6570 pos_copy = *trypos; /* copy trypos, findmatch will change it */
6571 trypos = &pos_copy;
6572 curwin->w_cursor = *trypos;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006573 if (ind_find_start_comment() != NULL) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 trypos = NULL;
6575 }
6576 }
6577 curwin->w_cursor = cursor_save;
6578 return trypos;
6579}
6580
6581/*
6582 * Return ind_maxparen corrected for the difference in line number between the
6583 * cursor position and "startpos". This makes sure that searching for a
6584 * matching paren above the cursor line doesn't find a match because of
6585 * looking a few lines further.
6586 */
6587 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006588corr_ind_maxparen(startpos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 pos_T *startpos;
6590{
6591 long n = (long)startpos->lnum - (long)curwin->w_cursor.lnum;
6592
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006593 if (n > 0 && n < curbuf->b_ind_maxparen / 2)
6594 return curbuf->b_ind_maxparen - (int)n;
6595 return curbuf->b_ind_maxparen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596}
6597
6598/*
6599 * Set w_cursor.col to the column number of the last unmatched ')' or '{' in
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006600 * line "l". "l" must point to the start of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 */
6602 static int
6603find_last_paren(l, start, end)
6604 char_u *l;
6605 int start, end;
6606{
6607 int i;
6608 int retval = FALSE;
6609 int open_count = 0;
6610
6611 curwin->w_cursor.col = 0; /* default is start of line */
6612
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01006613 for (i = 0; l[i] != NUL; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 {
6615 i = (int)(cin_skipcomment(l + i) - l); /* ignore parens in comments */
6616 i = (int)(skip_string(l + i) - l); /* ignore parens in quotes */
6617 if (l[i] == start)
6618 ++open_count;
6619 else if (l[i] == end)
6620 {
6621 if (open_count > 0)
6622 --open_count;
6623 else
6624 {
6625 curwin->w_cursor.col = i;
6626 retval = TRUE;
6627 }
6628 }
6629 }
6630 return retval;
6631}
6632
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006633/*
6634 * Parse 'cinoptions' and set the values in "curbuf".
6635 * Must be called when 'cinoptions', 'shiftwidth' and/or 'tabstop' changes.
6636 */
6637 void
6638parse_cino(buf)
6639 buf_T *buf;
6640{
6641 char_u *p;
6642 char_u *l;
6643 char_u *digits;
6644 int n;
6645 int divider;
6646 int fraction = 0;
6647 int sw = (int)get_sw_value(buf);
6648
6649 /*
6650 * Set the default values.
6651 */
6652 /* Spaces from a block's opening brace the prevailing indent for that
6653 * block should be. */
6654 buf->b_ind_level = sw;
6655
6656 /* Spaces from the edge of the line an open brace that's at the end of a
6657 * line is imagined to be. */
6658 buf->b_ind_open_imag = 0;
6659
6660 /* Spaces from the prevailing indent for a line that is not preceded by
6661 * an opening brace. */
6662 buf->b_ind_no_brace = 0;
6663
6664 /* Column where the first { of a function should be located }. */
6665 buf->b_ind_first_open = 0;
6666
6667 /* Spaces from the prevailing indent a leftmost open brace should be
6668 * located. */
6669 buf->b_ind_open_extra = 0;
6670
6671 /* Spaces from the matching open brace (real location for one at the left
6672 * edge; imaginary location from one that ends a line) the matching close
6673 * brace should be located. */
6674 buf->b_ind_close_extra = 0;
6675
6676 /* Spaces from the edge of the line an open brace sitting in the leftmost
6677 * column is imagined to be. */
6678 buf->b_ind_open_left_imag = 0;
6679
6680 /* Spaces jump labels should be shifted to the left if N is non-negative,
6681 * otherwise the jump label will be put to column 1. */
6682 buf->b_ind_jump_label = -1;
6683
6684 /* Spaces from the switch() indent a "case xx" label should be located. */
6685 buf->b_ind_case = sw;
6686
6687 /* Spaces from the "case xx:" code after a switch() should be located. */
6688 buf->b_ind_case_code = sw;
6689
6690 /* Lineup break at end of case in switch() with case label. */
6691 buf->b_ind_case_break = 0;
6692
6693 /* Spaces from the class declaration indent a scope declaration label
6694 * should be located. */
6695 buf->b_ind_scopedecl = sw;
6696
6697 /* Spaces from the scope declaration label code should be located. */
6698 buf->b_ind_scopedecl_code = sw;
6699
6700 /* Amount K&R-style parameters should be indented. */
6701 buf->b_ind_param = sw;
6702
6703 /* Amount a function type spec should be indented. */
6704 buf->b_ind_func_type = sw;
6705
6706 /* Amount a cpp base class declaration or constructor initialization
6707 * should be indented. */
6708 buf->b_ind_cpp_baseclass = sw;
6709
6710 /* additional spaces beyond the prevailing indent a continuation line
6711 * should be located. */
6712 buf->b_ind_continuation = sw;
6713
6714 /* Spaces from the indent of the line with an unclosed parentheses. */
6715 buf->b_ind_unclosed = sw * 2;
6716
6717 /* Spaces from the indent of the line with an unclosed parentheses, which
6718 * itself is also unclosed. */
6719 buf->b_ind_unclosed2 = sw;
6720
6721 /* Suppress ignoring spaces from the indent of a line starting with an
6722 * unclosed parentheses. */
6723 buf->b_ind_unclosed_noignore = 0;
6724
6725 /* If the opening paren is the last nonwhite character on the line, and
6726 * b_ind_unclosed_wrapped is nonzero, use this indent relative to the outer
6727 * context (for very long lines). */
6728 buf->b_ind_unclosed_wrapped = 0;
6729
6730 /* Suppress ignoring white space when lining up with the character after
6731 * an unclosed parentheses. */
6732 buf->b_ind_unclosed_whiteok = 0;
6733
6734 /* Indent a closing parentheses under the line start of the matching
6735 * opening parentheses. */
6736 buf->b_ind_matching_paren = 0;
6737
6738 /* Indent a closing parentheses under the previous line. */
6739 buf->b_ind_paren_prev = 0;
6740
6741 /* Extra indent for comments. */
6742 buf->b_ind_comment = 0;
6743
6744 /* Spaces from the comment opener when there is nothing after it. */
6745 buf->b_ind_in_comment = 3;
6746
6747 /* Boolean: if non-zero, use b_ind_in_comment even if there is something
6748 * after the comment opener. */
6749 buf->b_ind_in_comment2 = 0;
6750
6751 /* Max lines to search for an open paren. */
6752 buf->b_ind_maxparen = 20;
6753
6754 /* Max lines to search for an open comment. */
6755 buf->b_ind_maxcomment = 70;
6756
6757 /* Handle braces for java code. */
6758 buf->b_ind_java = 0;
6759
6760 /* Not to confuse JS object properties with labels. */
6761 buf->b_ind_js = 0;
6762
6763 /* Handle blocked cases correctly. */
6764 buf->b_ind_keep_case_label = 0;
6765
6766 /* Handle C++ namespace. */
6767 buf->b_ind_cpp_namespace = 0;
6768
6769 /* Handle continuation lines containing conditions of if(), for() and
6770 * while(). */
6771 buf->b_ind_if_for_while = 0;
6772
6773 for (p = buf->b_p_cino; *p; )
6774 {
6775 l = p++;
6776 if (*p == '-')
6777 ++p;
6778 digits = p; /* remember where the digits start */
6779 n = getdigits(&p);
6780 divider = 0;
6781 if (*p == '.') /* ".5s" means a fraction */
6782 {
6783 fraction = atol((char *)++p);
6784 while (VIM_ISDIGIT(*p))
6785 {
6786 ++p;
6787 if (divider)
6788 divider *= 10;
6789 else
6790 divider = 10;
6791 }
6792 }
6793 if (*p == 's') /* "2s" means two times 'shiftwidth' */
6794 {
6795 if (p == digits)
6796 n = sw; /* just "s" is one 'shiftwidth' */
6797 else
6798 {
6799 n *= sw;
6800 if (divider)
6801 n += (sw * fraction + divider / 2) / divider;
6802 }
6803 ++p;
6804 }
6805 if (l[1] == '-')
6806 n = -n;
6807
6808 /* When adding an entry here, also update the default 'cinoptions' in
6809 * doc/indent.txt, and add explanation for it! */
6810 switch (*l)
6811 {
6812 case '>': buf->b_ind_level = n; break;
6813 case 'e': buf->b_ind_open_imag = n; break;
6814 case 'n': buf->b_ind_no_brace = n; break;
6815 case 'f': buf->b_ind_first_open = n; break;
6816 case '{': buf->b_ind_open_extra = n; break;
6817 case '}': buf->b_ind_close_extra = n; break;
6818 case '^': buf->b_ind_open_left_imag = n; break;
6819 case 'L': buf->b_ind_jump_label = n; break;
6820 case ':': buf->b_ind_case = n; break;
6821 case '=': buf->b_ind_case_code = n; break;
6822 case 'b': buf->b_ind_case_break = n; break;
6823 case 'p': buf->b_ind_param = n; break;
6824 case 't': buf->b_ind_func_type = n; break;
6825 case '/': buf->b_ind_comment = n; break;
6826 case 'c': buf->b_ind_in_comment = n; break;
6827 case 'C': buf->b_ind_in_comment2 = n; break;
6828 case 'i': buf->b_ind_cpp_baseclass = n; break;
6829 case '+': buf->b_ind_continuation = n; break;
6830 case '(': buf->b_ind_unclosed = n; break;
6831 case 'u': buf->b_ind_unclosed2 = n; break;
6832 case 'U': buf->b_ind_unclosed_noignore = n; break;
6833 case 'W': buf->b_ind_unclosed_wrapped = n; break;
6834 case 'w': buf->b_ind_unclosed_whiteok = n; break;
6835 case 'm': buf->b_ind_matching_paren = n; break;
6836 case 'M': buf->b_ind_paren_prev = n; break;
6837 case ')': buf->b_ind_maxparen = n; break;
6838 case '*': buf->b_ind_maxcomment = n; break;
6839 case 'g': buf->b_ind_scopedecl = n; break;
6840 case 'h': buf->b_ind_scopedecl_code = n; break;
6841 case 'j': buf->b_ind_java = n; break;
6842 case 'J': buf->b_ind_js = n; break;
6843 case 'l': buf->b_ind_keep_case_label = n; break;
6844 case '#': buf->b_ind_hash_comment = n; break;
6845 case 'N': buf->b_ind_cpp_namespace = n; break;
6846 case 'k': buf->b_ind_if_for_while = n; break;
6847 }
6848 if (*p == ',')
6849 ++p;
6850 }
6851}
6852
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 int
6854get_c_indent()
6855{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 pos_T cur_curpos;
6857 int amount;
6858 int scope_amount;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00006859 int cur_amount = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 colnr_T col;
6861 char_u *theline;
6862 char_u *linecopy;
6863 pos_T *trypos;
6864 pos_T *tryposBrace = NULL;
6865 pos_T our_paren_pos;
6866 char_u *start;
6867 int start_brace;
Bram Moolenaare21877a2008-02-13 09:58:14 +00006868#define BRACE_IN_COL0 1 /* '{' is in column 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869#define BRACE_AT_START 2 /* '{' is at start of line */
6870#define BRACE_AT_END 3 /* '{' is at end of line */
6871 linenr_T ourscope;
6872 char_u *l;
6873 char_u *look;
6874 char_u terminated;
6875 int lookfor;
6876#define LOOKFOR_INITIAL 0
6877#define LOOKFOR_IF 1
6878#define LOOKFOR_DO 2
6879#define LOOKFOR_CASE 3
6880#define LOOKFOR_ANY 4
6881#define LOOKFOR_TERM 5
6882#define LOOKFOR_UNTERM 6
6883#define LOOKFOR_SCOPEDECL 7
6884#define LOOKFOR_NOBREAK 8
6885#define LOOKFOR_CPP_BASECLASS 9
6886#define LOOKFOR_ENUM_OR_INIT 10
6887
6888 int whilelevel;
6889 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 int n;
6891 int iscase;
6892 int lookfor_break;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02006893 int lookfor_cpp_namespace = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 int cont_amount = 0; /* amount for continuation line */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006895 int original_line_islabel;
Bram Moolenaare79d1532011-10-04 18:03:47 +02006896 int added_to_amount = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006898 /* make a copy, value is changed below */
6899 int ind_continuation = curbuf->b_ind_continuation;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900
6901 /* remember where the cursor was when we started */
6902 cur_curpos = curwin->w_cursor;
6903
Bram Moolenaar3acfc302010-07-11 17:23:02 +02006904 /* if we are at line 1 0 is fine, right? */
6905 if (cur_curpos.lnum == 1)
6906 return 0;
6907
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 /* Get a copy of the current contents of the line.
6909 * This is required, because only the most recent line obtained with
6910 * ml_get is valid! */
6911 linecopy = vim_strsave(ml_get(cur_curpos.lnum));
6912 if (linecopy == NULL)
6913 return 0;
6914
6915 /*
6916 * In insert mode and the cursor is on a ')' truncate the line at the
6917 * cursor position. We don't want to line up with the matching '(' when
6918 * inserting new stuff.
6919 * For unknown reasons the cursor might be past the end of the line, thus
6920 * check for that.
6921 */
6922 if ((State & INSERT)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00006923 && curwin->w_cursor.col < (colnr_T)STRLEN(linecopy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 && linecopy[curwin->w_cursor.col] == ')')
6925 linecopy[curwin->w_cursor.col] = NUL;
6926
6927 theline = skipwhite(linecopy);
6928
6929 /* move the cursor to the start of the line */
6930
6931 curwin->w_cursor.col = 0;
6932
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006933 original_line_islabel = cin_islabel(); /* XXX */
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006934
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 /*
6936 * #defines and so on always go at the left when included in 'cinkeys'.
6937 */
6938 if (*theline == '#' && (*linecopy == '#' || in_cinkeys('#', ' ', TRUE)))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006939 amount = curbuf->b_ind_hash_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940
6941 /*
Bram Moolenaar02c707a2010-07-17 17:12:06 +02006942 * Is it a non-case label? Then that goes at the left margin too unless:
6943 * - JS flag is set.
6944 * - 'L' item has a positive value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006946 else if (original_line_islabel && !curbuf->b_ind_js
6947 && curbuf->b_ind_jump_label < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 {
6949 amount = 0;
6950 }
6951
6952 /*
6953 * If we're inside a "//" comment and there is a "//" comment in a
6954 * previous line, lineup with that one.
6955 */
6956 else if (cin_islinecomment(theline)
6957 && (trypos = find_line_comment()) != NULL) /* XXX */
6958 {
6959 /* find how indented the line beginning the comment is */
6960 getvcol(curwin, trypos, &col, NULL, NULL);
6961 amount = col;
6962 }
6963
6964 /*
6965 * If we're inside a comment and not looking at the start of the
6966 * comment, try using the 'comments' option.
6967 */
6968 else if (!cin_iscomment(theline)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01006969 && (trypos = ind_find_start_comment()) != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01006970 /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 {
6972 int lead_start_len = 2;
6973 int lead_middle_len = 1;
6974 char_u lead_start[COM_MAX_LEN]; /* start-comment string */
6975 char_u lead_middle[COM_MAX_LEN]; /* middle-comment string */
6976 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6977 char_u *p;
6978 int start_align = 0;
6979 int start_off = 0;
6980 int done = FALSE;
6981
6982 /* find how indented the line beginning the comment is */
6983 getvcol(curwin, trypos, &col, NULL, NULL);
6984 amount = col;
Bram Moolenaar4aa97422011-04-11 14:27:38 +02006985 *lead_start = NUL;
6986 *lead_middle = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987
6988 p = curbuf->b_p_com;
6989 while (*p != NUL)
6990 {
6991 int align = 0;
6992 int off = 0;
6993 int what = 0;
6994
6995 while (*p != NUL && *p != ':')
6996 {
6997 if (*p == COM_START || *p == COM_END || *p == COM_MIDDLE)
6998 what = *p++;
6999 else if (*p == COM_LEFT || *p == COM_RIGHT)
7000 align = *p++;
7001 else if (VIM_ISDIGIT(*p) || *p == '-')
7002 off = getdigits(&p);
7003 else
7004 ++p;
7005 }
7006
7007 if (*p == ':')
7008 ++p;
7009 (void)copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
7010 if (what == COM_START)
7011 {
7012 STRCPY(lead_start, lead_end);
7013 lead_start_len = (int)STRLEN(lead_start);
7014 start_off = off;
7015 start_align = align;
7016 }
7017 else if (what == COM_MIDDLE)
7018 {
7019 STRCPY(lead_middle, lead_end);
7020 lead_middle_len = (int)STRLEN(lead_middle);
7021 }
7022 else if (what == COM_END)
7023 {
7024 /* If our line starts with the middle comment string, line it
7025 * up with the comment opener per the 'comments' option. */
7026 if (STRNCMP(theline, lead_middle, lead_middle_len) == 0
7027 && STRNCMP(theline, lead_end, STRLEN(lead_end)) != 0)
7028 {
7029 done = TRUE;
7030 if (curwin->w_cursor.lnum > 1)
7031 {
7032 /* If the start comment string matches in the previous
Bram Moolenaare21877a2008-02-13 09:58:14 +00007033 * line, use the indent of that line plus offset. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 * the middle comment string matches in the previous
7035 * line, use the indent of that line. XXX */
7036 look = skipwhite(ml_get(curwin->w_cursor.lnum - 1));
7037 if (STRNCMP(look, lead_start, lead_start_len) == 0)
7038 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7039 else if (STRNCMP(look, lead_middle,
7040 lead_middle_len) == 0)
7041 {
7042 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7043 break;
7044 }
7045 /* If the start comment string doesn't match with the
7046 * start of the comment, skip this entry. XXX */
7047 else if (STRNCMP(ml_get(trypos->lnum) + trypos->col,
7048 lead_start, lead_start_len) != 0)
7049 continue;
7050 }
7051 if (start_off != 0)
7052 amount += start_off;
7053 else if (start_align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007054 amount += vim_strsize(lead_start)
7055 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 break;
7057 }
7058
7059 /* If our line starts with the end comment string, line it up
7060 * with the middle comment */
7061 if (STRNCMP(theline, lead_middle, lead_middle_len) != 0
7062 && STRNCMP(theline, lead_end, STRLEN(lead_end)) == 0)
7063 {
7064 amount = get_indent_lnum(curwin->w_cursor.lnum - 1);
7065 /* XXX */
7066 if (off != 0)
7067 amount += off;
7068 else if (align == COM_RIGHT)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00007069 amount += vim_strsize(lead_start)
7070 - vim_strsize(lead_middle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 done = TRUE;
7072 break;
7073 }
7074 }
7075 }
7076
7077 /* If our line starts with an asterisk, line up with the
7078 * asterisk in the comment opener; otherwise, line up
7079 * with the first character of the comment text.
7080 */
7081 if (done)
7082 ;
7083 else if (theline[0] == '*')
7084 amount += 1;
7085 else
7086 {
7087 /*
7088 * If we are more than one line away from the comment opener, take
7089 * the indent of the previous non-empty line. If 'cino' has "CO"
7090 * and we are just below the comment opener and there are any
7091 * white characters after it line up with the text after it;
7092 * otherwise, add the amount specified by "c" in 'cino'
7093 */
7094 amount = -1;
7095 for (lnum = cur_curpos.lnum - 1; lnum > trypos->lnum; --lnum)
7096 {
7097 if (linewhite(lnum)) /* skip blank lines */
7098 continue;
7099 amount = get_indent_lnum(lnum); /* XXX */
7100 break;
7101 }
7102 if (amount == -1) /* use the comment opener */
7103 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007104 if (!curbuf->b_ind_in_comment2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 {
7106 start = ml_get(trypos->lnum);
7107 look = start + trypos->col + 2; /* skip / and * */
7108 if (*look != NUL) /* if something after it */
7109 trypos->col = (colnr_T)(skipwhite(look) - start);
7110 }
7111 getvcol(curwin, trypos, &col, NULL, NULL);
7112 amount = col;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007113 if (curbuf->b_ind_in_comment2 || *look == NUL)
7114 amount += curbuf->b_ind_in_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 }
7116 }
7117 }
7118
7119 /*
7120 * Are we inside parentheses or braces?
7121 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007122 else if (((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007123 && curbuf->b_ind_java == 0)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007124 || (tryposBrace = find_start_brace()) != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 || trypos != NULL)
7126 {
7127 if (trypos != NULL && tryposBrace != NULL)
7128 {
7129 /* Both an unmatched '(' and '{' is found. Use the one which is
7130 * closer to the current cursor position, set the other to NULL. */
7131 if (trypos->lnum != tryposBrace->lnum
7132 ? trypos->lnum < tryposBrace->lnum
7133 : trypos->col < tryposBrace->col)
7134 trypos = NULL;
7135 else
7136 tryposBrace = NULL;
7137 }
7138
7139 if (trypos != NULL)
7140 {
7141 /*
7142 * If the matching paren is more than one line away, use the indent of
7143 * a previous non-empty line that matches the same paren.
7144 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007145 if (theline[0] == ')' && curbuf->b_ind_paren_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007147 /* Line up with the start of the matching paren line. */
7148 amount = get_indent_lnum(curwin->w_cursor.lnum - 1); /* XXX */
7149 }
7150 else
7151 {
7152 amount = -1;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007153 our_paren_pos = *trypos;
7154 for (lnum = cur_curpos.lnum - 1; lnum > our_paren_pos.lnum; --lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007156 l = skipwhite(ml_get(lnum));
7157 if (cin_nocode(l)) /* skip comment lines */
7158 continue;
7159 if (cin_ispreproc_cont(&l, &lnum))
7160 continue; /* ignore #define, #if, etc. */
7161 curwin->w_cursor.lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007163 /* Skip a comment. XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007164 if ((trypos = ind_find_start_comment()) != NULL)
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007165 {
7166 lnum = trypos->lnum + 1;
7167 continue;
7168 }
7169
7170 /* XXX */
7171 if ((trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007172 corr_ind_maxparen(&cur_curpos))) != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007173 && trypos->lnum == our_paren_pos.lnum
7174 && trypos->col == our_paren_pos.col)
7175 {
7176 amount = get_indent_lnum(lnum); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007178 if (theline[0] == ')')
7179 {
7180 if (our_paren_pos.lnum != lnum
7181 && cur_amount > amount)
7182 cur_amount = amount;
7183 amount = -1;
7184 }
7185 break;
7186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 }
7188 }
7189
7190 /*
7191 * Line up with line where the matching paren is. XXX
7192 * If the line starts with a '(' or the indent for unclosed
7193 * parentheses is zero, line up with the unclosed parentheses.
7194 */
7195 if (amount == -1)
7196 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007197 int ignore_paren_col = 0;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007198 int is_if_for_while = 0;
7199
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007200 if (curbuf->b_ind_if_for_while)
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007201 {
7202 /* Look for the outermost opening parenthesis on this line
7203 * and check whether it belongs to an "if", "for" or "while". */
7204
7205 pos_T cursor_save = curwin->w_cursor;
7206 pos_T outermost;
7207 char_u *line;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007208
7209 trypos = &our_paren_pos;
7210 do {
7211 outermost = *trypos;
7212 curwin->w_cursor.lnum = outermost.lnum;
7213 curwin->w_cursor.col = outermost.col;
7214
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007215 trypos = find_match_paren(curbuf->b_ind_maxparen);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007216 } while (trypos && trypos->lnum == outermost.lnum);
7217
7218 curwin->w_cursor = cursor_save;
7219
7220 line = ml_get(outermost.lnum);
7221
7222 is_if_for_while =
Bram Moolenaarb345d492012-04-09 20:42:26 +02007223 cin_is_if_for_while_before_offset(line, &outermost.col);
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007224 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007225
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007226 amount = skip_label(our_paren_pos.lnum, &look);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007227 look = skipwhite(look);
7228 if (*look == '(')
7229 {
7230 linenr_T save_lnum = curwin->w_cursor.lnum;
7231 char_u *line;
7232 int look_col;
7233
7234 /* Ignore a '(' in front of the line that has a match before
7235 * our matching '('. */
7236 curwin->w_cursor.lnum = our_paren_pos.lnum;
7237 line = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007238 look_col = (int)(look - line);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007239 curwin->w_cursor.col = look_col + 1;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007240 if ((trypos = findmatchlimit(NULL, ')', 0,
7241 curbuf->b_ind_maxparen))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007242 != NULL
7243 && trypos->lnum == our_paren_pos.lnum
7244 && trypos->col < our_paren_pos.col)
7245 ignore_paren_col = trypos->col + 1;
7246
7247 curwin->w_cursor.lnum = save_lnum;
7248 look = ml_get(our_paren_pos.lnum) + look_col;
7249 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007250 if (theline[0] == ')' || (curbuf->b_ind_unclosed == 0
7251 && is_if_for_while == 0)
7252 || (!curbuf->b_ind_unclosed_noignore && *look == '('
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007253 && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 {
7255 /*
7256 * If we're looking at a close paren, line up right there;
7257 * otherwise, line up with the next (non-white) character.
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007258 * When b_ind_unclosed_wrapped is set and the matching paren is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 * the last nonwhite character of the line, use either the
7260 * indent of the current line or the indentation of the next
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007261 * outer paren and add b_ind_unclosed_wrapped (for very long
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 * lines).
7263 */
7264 if (theline[0] != ')')
7265 {
7266 cur_amount = MAXCOL;
7267 l = ml_get(our_paren_pos.lnum);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007268 if (curbuf->b_ind_unclosed_wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 && cin_ends_in(l, (char_u *)"(", NULL))
7270 {
7271 /* look for opening unmatched paren, indent one level
7272 * for each additional level */
7273 n = 1;
7274 for (col = 0; col < our_paren_pos.col; ++col)
7275 {
7276 switch (l[col])
7277 {
7278 case '(':
7279 case '{': ++n;
7280 break;
7281
7282 case ')':
7283 case '}': if (n > 1)
7284 --n;
7285 break;
7286 }
7287 }
7288
7289 our_paren_pos.col = 0;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007290 amount += n * curbuf->b_ind_unclosed_wrapped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007292 else if (curbuf->b_ind_unclosed_whiteok)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 our_paren_pos.col++;
7294 else
7295 {
7296 col = our_paren_pos.col + 1;
7297 while (vim_iswhite(l[col]))
7298 col++;
7299 if (l[col] != NUL) /* In case of trailing space */
7300 our_paren_pos.col = col;
7301 else
7302 our_paren_pos.col++;
7303 }
7304 }
7305
7306 /*
7307 * Find how indented the paren is, or the character after it
7308 * if we did the above "if".
7309 */
7310 if (our_paren_pos.col > 0)
7311 {
7312 getvcol(curwin, &our_paren_pos, &col, NULL, NULL);
7313 if (cur_amount > (int)col)
7314 cur_amount = col;
7315 }
7316 }
7317
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007318 if (theline[0] == ')' && curbuf->b_ind_matching_paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 {
7320 /* Line up with the start of the matching paren line. */
7321 }
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007322 else if ((curbuf->b_ind_unclosed == 0 && is_if_for_while == 0)
7323 || (!curbuf->b_ind_unclosed_noignore
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007324 && *look == '(' && ignore_paren_col == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 {
7326 if (cur_amount != MAXCOL)
7327 amount = cur_amount;
7328 }
7329 else
7330 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007331 /* Add b_ind_unclosed2 for each '(' before our matching one,
7332 * but ignore (void) before the line (ignore_paren_col). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 col = our_paren_pos.col;
Bram Moolenaarb21e5842006-04-16 18:30:08 +00007334 while ((int)our_paren_pos.col > ignore_paren_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 {
7336 --our_paren_pos.col;
7337 switch (*ml_get_pos(&our_paren_pos))
7338 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007339 case '(': amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 col = our_paren_pos.col;
7341 break;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007342 case ')': amount -= curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 col = MAXCOL;
7344 break;
7345 }
7346 }
7347
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007348 /* Use b_ind_unclosed once, when the first '(' is not inside
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 * braces */
7350 if (col == MAXCOL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007351 amount += curbuf->b_ind_unclosed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 else
7353 {
7354 curwin->w_cursor.lnum = our_paren_pos.lnum;
7355 curwin->w_cursor.col = col;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007356 if (find_match_paren(curbuf->b_ind_maxparen) != NULL)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007357 amount += curbuf->b_ind_unclosed2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 else
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007359 {
7360 if (is_if_for_while)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007361 amount += curbuf->b_ind_if_for_while;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007362 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007363 amount += curbuf->b_ind_unclosed;
Bram Moolenaar3675fa02012-04-05 17:17:42 +02007364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 }
7366 /*
7367 * For a line starting with ')' use the minimum of the two
7368 * positions, to avoid giving it more indent than the previous
7369 * lines:
7370 * func_long_name( if (x
7371 * arg && yy
7372 * ) ^ not here ) ^ not here
7373 */
7374 if (cur_amount < amount)
7375 amount = cur_amount;
7376 }
7377 }
7378
7379 /* add extra indent for a comment */
7380 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007381 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 }
7383
7384 /*
7385 * Are we at least inside braces, then?
7386 */
7387 else
7388 {
7389 trypos = tryposBrace;
7390
7391 ourscope = trypos->lnum;
7392 start = ml_get(ourscope);
7393
7394 /*
7395 * Now figure out how indented the line is in general.
7396 * If the brace was at the start of the line, we use that;
7397 * otherwise, check out the indentation of the line as
7398 * a whole and then add the "imaginary indent" to that.
7399 */
7400 look = skipwhite(start);
7401 if (*look == '{')
7402 {
7403 getvcol(curwin, trypos, &col, NULL, NULL);
7404 amount = col;
7405 if (*start == '{')
7406 start_brace = BRACE_IN_COL0;
7407 else
7408 start_brace = BRACE_AT_START;
7409 }
7410 else
7411 {
7412 /*
7413 * that opening brace might have been on a continuation
7414 * line. if so, find the start of the line.
7415 */
7416 curwin->w_cursor.lnum = ourscope;
7417
7418 /*
7419 * position the cursor over the rightmost paren, so that
7420 * matching it will take us back to the start of the line.
7421 */
7422 lnum = ourscope;
7423 if (find_last_paren(start, '(', ')')
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007424 && (trypos = find_match_paren(curbuf->b_ind_maxparen))
7425 != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 lnum = trypos->lnum;
7427
7428 /*
7429 * It could have been something like
7430 * case 1: if (asdf &&
7431 * ldfd) {
7432 * }
7433 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007434 if (curbuf->b_ind_js || (curbuf->b_ind_keep_case_label
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007435 && cin_iscase(skipwhite(ml_get_curline()), FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 amount = get_indent();
7437 else
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007438 amount = skip_label(lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439
7440 start_brace = BRACE_AT_END;
7441 }
7442
7443 /*
7444 * if we're looking at a closing brace, that's where
7445 * we want to be. otherwise, add the amount of room
7446 * that an indent is supposed to be.
7447 */
7448 if (theline[0] == '}')
7449 {
7450 /*
7451 * they may want closing braces to line up with something
7452 * other than the open brace. indulge them, if so.
7453 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007454 amount += curbuf->b_ind_close_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 }
7456 else
7457 {
7458 /*
7459 * If we're looking at an "else", try to find an "if"
7460 * to match it with.
7461 * If we're looking at a "while", try to find a "do"
7462 * to match it with.
7463 */
7464 lookfor = LOOKFOR_INITIAL;
7465 if (cin_iselse(theline))
7466 lookfor = LOOKFOR_IF;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007467 else if (cin_iswhileofdo(theline, cur_curpos.lnum)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 lookfor = LOOKFOR_DO;
7469 if (lookfor != LOOKFOR_INITIAL)
7470 {
7471 curwin->w_cursor.lnum = cur_curpos.lnum;
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007472 if (find_match(lookfor, ourscope) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 {
7474 amount = get_indent(); /* XXX */
7475 goto theend;
7476 }
7477 }
7478
7479 /*
7480 * We get here if we are not on an "while-of-do" or "else" (or
7481 * failed to find a matching "if").
7482 * Search backwards for something to line up with.
7483 * First set amount for when we don't find anything.
7484 */
7485
7486 /*
7487 * if the '{' is _really_ at the left margin, use the imaginary
7488 * location of a left-margin brace. Otherwise, correct the
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007489 * location for b_ind_open_extra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 */
7491
7492 if (start_brace == BRACE_IN_COL0) /* '{' is in column 0 */
7493 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007494 amount = curbuf->b_ind_open_left_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007495 lookfor_cpp_namespace = TRUE;
7496 }
7497 else if (start_brace == BRACE_AT_START &&
7498 lookfor_cpp_namespace) /* '{' is at start */
7499 {
7500
7501 lookfor_cpp_namespace = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 }
7503 else
7504 {
7505 if (start_brace == BRACE_AT_END) /* '{' is at end of line */
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007506 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007507 amount += curbuf->b_ind_open_imag;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007508
7509 l = skipwhite(ml_get_curline());
7510 if (cin_is_cpp_namespace(l))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007511 amount += curbuf->b_ind_cpp_namespace;
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 else
7514 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007515 /* Compensate for adding b_ind_open_extra later. */
7516 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 if (amount < 0)
7518 amount = 0;
7519 }
7520 }
7521
7522 lookfor_break = FALSE;
7523
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007524 if (cin_iscase(theline, FALSE)) /* it's a switch() label */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 {
7526 lookfor = LOOKFOR_CASE; /* find a previous switch() label */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007527 amount += curbuf->b_ind_case;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 }
7529 else if (cin_isscopedecl(theline)) /* private:, ... */
7530 {
7531 lookfor = LOOKFOR_SCOPEDECL; /* class decl is this block */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007532 amount += curbuf->b_ind_scopedecl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 }
7534 else
7535 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007536 if (curbuf->b_ind_case_break && cin_isbreak(theline))
7537 /* break; ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 lookfor_break = TRUE;
7539
7540 lookfor = LOOKFOR_INITIAL;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007541 /* b_ind_level from start of block */
7542 amount += curbuf->b_ind_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 }
7544 scope_amount = amount;
7545 whilelevel = 0;
7546
7547 /*
7548 * Search backwards. If we find something we recognize, line up
7549 * with that.
7550 *
7551 * if we're looking at an open brace, indent
7552 * the usual amount relative to the conditional
7553 * that opens the block.
7554 */
7555 curwin->w_cursor = cur_curpos;
7556 for (;;)
7557 {
7558 curwin->w_cursor.lnum--;
7559 curwin->w_cursor.col = 0;
7560
7561 /*
7562 * If we went all the way back to the start of our scope, line
7563 * up with it.
7564 */
7565 if (curwin->w_cursor.lnum <= ourscope)
7566 {
7567 /* we reached end of scope:
7568 * if looking for a enum or structure initialization
7569 * go further back:
7570 * if it is an initializer (enum xxx or xxx =), then
7571 * don't add ind_continuation, otherwise it is a variable
7572 * declaration:
7573 * int x,
7574 * here; <-- add ind_continuation
7575 */
7576 if (lookfor == LOOKFOR_ENUM_OR_INIT)
7577 {
7578 if (curwin->w_cursor.lnum == 0
7579 || curwin->w_cursor.lnum
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007580 < ourscope - curbuf->b_ind_maxparen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007582 /* nothing found (abuse curbuf->b_ind_maxparen as
7583 * limit) assume terminated line (i.e. a variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 * initialization) */
7585 if (cont_amount > 0)
7586 amount = cont_amount;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007587 else if (!curbuf->b_ind_js)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 amount += ind_continuation;
7589 break;
7590 }
7591
7592 l = ml_get_curline();
7593
7594 /*
7595 * If we're in a comment now, skip to the start of the
7596 * comment.
7597 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007598 trypos = ind_find_start_comment();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 if (trypos != NULL)
7600 {
7601 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007602 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 continue;
7604 }
7605
7606 /*
7607 * Skip preprocessor directives and blank lines.
7608 */
7609 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7610 continue;
7611
7612 if (cin_nocode(l))
7613 continue;
7614
7615 terminated = cin_isterminated(l, FALSE, TRUE);
7616
7617 /*
7618 * If we are at top level and the line looks like a
7619 * function declaration, we are done
7620 * (it's a variable declaration).
7621 */
7622 if (start_brace != BRACE_IN_COL0
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007623 || !cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 {
7625 /* if the line is terminated with another ','
7626 * it is a continued variable initialization.
7627 * don't add extra indent.
7628 * TODO: does not work, if a function
7629 * declaration is split over multiple lines:
7630 * cin_isfuncdecl returns FALSE then.
7631 */
7632 if (terminated == ',')
7633 break;
7634
7635 /* if it es a enum declaration or an assignment,
7636 * we are done.
7637 */
7638 if (terminated != ';' && cin_isinit())
7639 break;
7640
7641 /* nothing useful found */
7642 if (terminated == 0 || terminated == '{')
7643 continue;
7644 }
7645
7646 if (terminated != ';')
7647 {
7648 /* Skip parens and braces. Position the cursor
7649 * over the rightmost paren, so that matching it
7650 * will take us back to the start of the line.
7651 */ /* XXX */
7652 trypos = NULL;
7653 if (find_last_paren(l, '(', ')'))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007654 trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007655 curbuf->b_ind_maxparen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656
7657 if (trypos == NULL && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007658 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659
7660 if (trypos != NULL)
7661 {
7662 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007663 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 continue;
7665 }
7666 }
7667
7668 /* it's a variable declaration, add indentation
7669 * like in
7670 * int a,
7671 * b;
7672 */
7673 if (cont_amount > 0)
7674 amount = cont_amount;
7675 else
7676 amount += ind_continuation;
7677 }
7678 else if (lookfor == LOOKFOR_UNTERM)
7679 {
7680 if (cont_amount > 0)
7681 amount = cont_amount;
7682 else
7683 amount += ind_continuation;
7684 }
Bram Moolenaare79d1532011-10-04 18:03:47 +02007685 else
Bram Moolenaared38b0a2011-05-25 15:16:18 +02007686 {
Bram Moolenaare79d1532011-10-04 18:03:47 +02007687 if (lookfor != LOOKFOR_TERM
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 && lookfor != LOOKFOR_CPP_BASECLASS)
Bram Moolenaare79d1532011-10-04 18:03:47 +02007689 {
7690 amount = scope_amount;
7691 if (theline[0] == '{')
7692 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007693 amount += curbuf->b_ind_open_extra;
7694 added_to_amount = curbuf->b_ind_open_extra;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007695 }
7696 }
7697
7698 if (lookfor_cpp_namespace)
7699 {
7700 /*
7701 * Looking for C++ namespace, need to look further
7702 * back.
7703 */
7704 if (curwin->w_cursor.lnum == ourscope)
7705 continue;
7706
7707 if (curwin->w_cursor.lnum == 0
7708 || curwin->w_cursor.lnum
7709 < ourscope - FIND_NAMESPACE_LIM)
7710 break;
7711
7712 l = ml_get_curline();
7713
7714 /* If we're in a comment now, skip to the start of
7715 * the comment. */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007716 trypos = ind_find_start_comment();
Bram Moolenaare79d1532011-10-04 18:03:47 +02007717 if (trypos != NULL)
7718 {
7719 curwin->w_cursor.lnum = trypos->lnum + 1;
7720 curwin->w_cursor.col = 0;
7721 continue;
7722 }
7723
7724 /* Skip preprocessor directives and blank lines. */
7725 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
7726 continue;
7727
7728 /* Finally the actual check for "namespace". */
7729 if (cin_is_cpp_namespace(l))
7730 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007731 amount += curbuf->b_ind_cpp_namespace
7732 - added_to_amount;
Bram Moolenaare79d1532011-10-04 18:03:47 +02007733 break;
7734 }
7735
7736 if (cin_nocode(l))
7737 continue;
7738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 }
7740 break;
7741 }
7742
7743 /*
7744 * If we're in a comment now, skip to the start of the comment.
7745 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007746 if ((trypos = ind_find_start_comment()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 {
7748 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007749 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 continue;
7751 }
7752
7753 l = ml_get_curline();
7754
7755 /*
7756 * If this is a switch() label, may line up relative to that.
Bram Moolenaar18144c82006-04-12 21:52:12 +00007757 * If this is a C++ scope declaration, do the same.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 */
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007759 iscase = cin_iscase(l, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 if (iscase || cin_isscopedecl(l))
7761 {
7762 /* we are only looking for cpp base class
7763 * declaration/initialization any longer */
7764 if (lookfor == LOOKFOR_CPP_BASECLASS)
7765 break;
7766
7767 /* When looking for a "do" we are not interested in
7768 * labels. */
7769 if (whilelevel > 0)
7770 continue;
7771
7772 /*
7773 * case xx:
7774 * c = 99 + <- this indent plus continuation
7775 *-> here;
7776 */
7777 if (lookfor == LOOKFOR_UNTERM
7778 || lookfor == LOOKFOR_ENUM_OR_INIT)
7779 {
7780 if (cont_amount > 0)
7781 amount = cont_amount;
7782 else
7783 amount += ind_continuation;
7784 break;
7785 }
7786
7787 /*
7788 * case xx: <- line up with this case
7789 * x = 333;
7790 * case yy:
7791 */
7792 if ( (iscase && lookfor == LOOKFOR_CASE)
7793 || (iscase && lookfor_break)
7794 || (!iscase && lookfor == LOOKFOR_SCOPEDECL))
7795 {
7796 /*
7797 * Check that this case label is not for another
7798 * switch()
7799 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007800 if ((trypos = find_start_brace()) == NULL
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007801 || trypos->lnum == ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 {
7803 amount = get_indent(); /* XXX */
7804 break;
7805 }
7806 continue;
7807 }
7808
7809 n = get_indent_nolabel(curwin->w_cursor.lnum); /* XXX */
7810
7811 /*
7812 * case xx: if (cond) <- line up with this if
7813 * y = y + 1;
7814 * -> s = 99;
7815 *
7816 * case xx:
7817 * if (cond) <- line up with this line
7818 * y = y + 1;
7819 * -> s = 99;
7820 */
7821 if (lookfor == LOOKFOR_TERM)
7822 {
7823 if (n)
7824 amount = n;
7825
7826 if (!lookfor_break)
7827 break;
7828 }
7829
7830 /*
7831 * case xx: x = x + 1; <- line up with this x
7832 * -> y = y + 1;
7833 *
7834 * case xx: if (cond) <- line up with this if
7835 * -> y = y + 1;
7836 */
7837 if (n)
7838 {
7839 amount = n;
7840 l = after_label(ml_get_curline());
7841 if (l != NULL && cin_is_cinword(l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007842 {
7843 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007844 amount += curbuf->b_ind_open_extra;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007845 else
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007846 amount += curbuf->b_ind_level
7847 + curbuf->b_ind_no_brace;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00007848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 break;
7850 }
7851
7852 /*
7853 * Try to get the indent of a statement before the switch
7854 * label. If nothing is found, line up relative to the
7855 * switch label.
7856 * break; <- may line up with this line
7857 * case xx:
7858 * -> y = 1;
7859 */
7860 scope_amount = get_indent() + (iscase /* XXX */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007861 ? curbuf->b_ind_case_code
7862 : curbuf->b_ind_scopedecl_code);
7863 lookfor = curbuf->b_ind_case_break
7864 ? LOOKFOR_NOBREAK : LOOKFOR_ANY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 continue;
7866 }
7867
7868 /*
7869 * Looking for a switch() label or C++ scope declaration,
7870 * ignore other lines, skip {}-blocks.
7871 */
7872 if (lookfor == LOOKFOR_CASE || lookfor == LOOKFOR_SCOPEDECL)
7873 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007874 if (find_last_paren(l, '{', '}')
7875 && (trypos = find_start_brace()) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007876 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007878 curwin->w_cursor.col = 0;
7879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 continue;
7881 }
7882
7883 /*
7884 * Ignore jump labels with nothing after them.
7885 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007886 if (!curbuf->b_ind_js && cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 {
7888 l = after_label(ml_get_curline());
7889 if (l == NULL || cin_nocode(l))
7890 continue;
7891 }
7892
7893 /*
7894 * Ignore #defines, #if, etc.
7895 * Ignore comment and empty lines.
7896 * (need to get the line again, cin_islabel() may have
7897 * unlocked it)
7898 */
7899 l = ml_get_curline();
7900 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum)
7901 || cin_nocode(l))
7902 continue;
7903
7904 /*
7905 * Are we at the start of a cpp base class declaration or
7906 * constructor initialization?
7907 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00007908 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01007909 if (lookfor != LOOKFOR_TERM && curbuf->b_ind_cpp_baseclass > 0)
Bram Moolenaar18144c82006-04-12 21:52:12 +00007910 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00007911 n = cin_is_cpp_baseclass(&col);
Bram Moolenaar18144c82006-04-12 21:52:12 +00007912 l = ml_get_curline();
7913 }
7914 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 {
7916 if (lookfor == LOOKFOR_UNTERM)
7917 {
7918 if (cont_amount > 0)
7919 amount = cont_amount;
7920 else
7921 amount += ind_continuation;
7922 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00007923 else if (theline[0] == '{')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00007925 /* Need to find start of the declaration. */
7926 lookfor = LOOKFOR_UNTERM;
7927 ind_continuation = 0;
7928 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 }
7930 else
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00007931 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007932 amount = get_baseclass_amount(col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 break;
7934 }
7935 else if (lookfor == LOOKFOR_CPP_BASECLASS)
7936 {
7937 /* only look, whether there is a cpp base class
Bram Moolenaar18144c82006-04-12 21:52:12 +00007938 * declaration or initialization before the opening brace.
7939 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 if (cin_isterminated(l, TRUE, FALSE))
7941 break;
7942 else
7943 continue;
7944 }
7945
7946 /*
7947 * What happens next depends on the line being terminated.
7948 * If terminated with a ',' only consider it terminating if
Bram Moolenaar25394022007-05-10 19:06:20 +00007949 * there is another unterminated statement behind, eg:
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 * 123,
7951 * sizeof
7952 * here
7953 * Otherwise check whether it is a enumeration or structure
7954 * initialisation (not indented) or a variable declaration
7955 * (indented).
7956 */
7957 terminated = cin_isterminated(l, FALSE, TRUE);
7958
7959 if (terminated == 0 || (lookfor != LOOKFOR_UNTERM
7960 && terminated == ','))
7961 {
7962 /*
7963 * if we're in the middle of a paren thing,
7964 * go back to the line that starts it so
7965 * we can get the right prevailing indent
7966 * if ( foo &&
7967 * bar )
7968 */
7969 /*
7970 * position the cursor over the rightmost paren, so that
7971 * matching it will take us back to the start of the line.
7972 */
7973 (void)find_last_paren(l, '(', ')');
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007974 trypos = find_match_paren(corr_ind_maxparen(&cur_curpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975
7976 /*
7977 * If we are looking for ',', we also look for matching
7978 * braces.
7979 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00007980 if (trypos == NULL && terminated == ','
7981 && find_last_paren(l, '{', '}'))
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007982 trypos = find_start_brace();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983
7984 if (trypos != NULL)
7985 {
7986 /*
7987 * Check if we are on a case label now. This is
7988 * handled above.
7989 * case xx: if ( asdf &&
7990 * asdf)
7991 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007992 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007994 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 {
7996 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00007997 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 continue;
7999 }
8000 }
8001
8002 /*
8003 * Skip over continuation lines to find the one to get the
8004 * indent from
8005 * char *usethis = "bla\
8006 * bla",
8007 * here;
8008 */
8009 if (terminated == ',')
8010 {
8011 while (curwin->w_cursor.lnum > 1)
8012 {
8013 l = ml_get(curwin->w_cursor.lnum - 1);
8014 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8015 break;
8016 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008017 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 }
8019 }
8020
8021 /*
8022 * Get indent and pointer to text for current line,
8023 * ignoring any jump label. XXX
8024 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008025 if (!curbuf->b_ind_js)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008026 cur_amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008027 else
8028 cur_amount = get_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 /*
8030 * If this is just above the line we are indenting, and it
8031 * starts with a '{', line it up with this line.
8032 * while (not)
8033 * -> {
8034 * }
8035 */
8036 if (terminated != ',' && lookfor != LOOKFOR_TERM
8037 && theline[0] == '{')
8038 {
8039 amount = cur_amount;
8040 /*
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008041 * Only add b_ind_open_extra when the current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 * doesn't start with a '{', which must have a match
8043 * in the same line (scope is the same). Probably:
8044 * { 1, 2 },
8045 * -> { 3, 4 }
8046 */
8047 if (*skipwhite(l) != '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008048 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008050 if (curbuf->b_ind_cpp_baseclass)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 {
8052 /* have to look back, whether it is a cpp base
8053 * class declaration or initialization */
8054 lookfor = LOOKFOR_CPP_BASECLASS;
8055 continue;
8056 }
8057 break;
8058 }
8059
8060 /*
8061 * Check if we are after an "if", "while", etc.
8062 * Also allow " } else".
8063 */
8064 if (cin_is_cinword(l) || cin_iselse(skipwhite(l)))
8065 {
8066 /*
8067 * Found an unterminated line after an if (), line up
8068 * with the last one.
8069 * if (cond)
8070 * 100 +
8071 * -> here;
8072 */
8073 if (lookfor == LOOKFOR_UNTERM
8074 || lookfor == LOOKFOR_ENUM_OR_INIT)
8075 {
8076 if (cont_amount > 0)
8077 amount = cont_amount;
8078 else
8079 amount += ind_continuation;
8080 break;
8081 }
8082
8083 /*
8084 * If this is just above the line we are indenting, we
8085 * are finished.
8086 * while (not)
8087 * -> here;
8088 * Otherwise this indent can be used when the line
8089 * before this is terminated.
8090 * yyy;
8091 * if (stat)
8092 * while (not)
8093 * xxx;
8094 * -> here;
8095 */
8096 amount = cur_amount;
8097 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008098 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 if (lookfor != LOOKFOR_TERM)
8100 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008101 amount += curbuf->b_ind_level
8102 + curbuf->b_ind_no_brace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 break;
8104 }
8105
8106 /*
8107 * Special trick: when expecting the while () after a
8108 * do, line up with the while()
8109 * do
8110 * x = 1;
8111 * -> here
8112 */
8113 l = skipwhite(ml_get_curline());
8114 if (cin_isdo(l))
8115 {
8116 if (whilelevel == 0)
8117 break;
8118 --whilelevel;
8119 }
8120
8121 /*
8122 * When searching for a terminated line, don't use the
Bram Moolenaar334adf02011-05-25 13:34:04 +02008123 * one between the "if" and the matching "else".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 * Need to use the scope of this "else". XXX
8125 * If whilelevel != 0 continue looking for a "do {".
8126 */
Bram Moolenaar334adf02011-05-25 13:34:04 +02008127 if (cin_iselse(l) && whilelevel == 0)
8128 {
8129 /* If we're looking at "} else", let's make sure we
8130 * find the opening brace of the enclosing scope,
8131 * not the one from "if () {". */
8132 if (*l == '}')
8133 curwin->w_cursor.col =
Bram Moolenaar9b83c2f2011-05-25 17:29:44 +02008134 (colnr_T)(l - ml_get_curline()) + 1;
Bram Moolenaar334adf02011-05-25 13:34:04 +02008135
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008136 if ((trypos = find_start_brace()) == NULL
8137 || find_match(LOOKFOR_IF, trypos->lnum)
8138 == FAIL)
Bram Moolenaar334adf02011-05-25 13:34:04 +02008139 break;
8140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 }
8142
8143 /*
8144 * If we're below an unterminated line that is not an
8145 * "if" or something, we may line up with this line or
Bram Moolenaar25394022007-05-10 19:06:20 +00008146 * add something for a continuation line, depending on
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 * the line before this one.
8148 */
8149 else
8150 {
8151 /*
8152 * Found two unterminated lines on a row, line up with
8153 * the last one.
8154 * c = 99 +
8155 * 100 +
8156 * -> here;
8157 */
8158 if (lookfor == LOOKFOR_UNTERM)
8159 {
8160 /* When line ends in a comma add extra indent */
8161 if (terminated == ',')
8162 amount += ind_continuation;
8163 break;
8164 }
8165
8166 if (lookfor == LOOKFOR_ENUM_OR_INIT)
8167 {
8168 /* Found two lines ending in ',', lineup with the
8169 * lowest one, but check for cpp base class
8170 * declaration/initialization, if it is an
8171 * opening brace or we are looking just for
8172 * enumerations/initializations. */
8173 if (terminated == ',')
8174 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008175 if (curbuf->b_ind_cpp_baseclass == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 break;
8177
8178 lookfor = LOOKFOR_CPP_BASECLASS;
8179 continue;
8180 }
8181
8182 /* Ignore unterminated lines in between, but
8183 * reduce indent. */
8184 if (amount > cur_amount)
8185 amount = cur_amount;
8186 }
8187 else
8188 {
8189 /*
8190 * Found first unterminated line on a row, may
8191 * line up with this line, remember its indent
8192 * 100 +
8193 * -> here;
8194 */
8195 amount = cur_amount;
8196
8197 /*
8198 * If previous line ends in ',', check whether we
8199 * are in an initialization or enum
8200 * struct xxx =
8201 * {
8202 * sizeof a,
8203 * 124 };
8204 * or a normal possible continuation line.
8205 * but only, of no other statement has been found
8206 * yet.
8207 */
8208 if (lookfor == LOOKFOR_INITIAL && terminated == ',')
8209 {
8210 lookfor = LOOKFOR_ENUM_OR_INIT;
8211 cont_amount = cin_first_id_amount();
8212 }
8213 else
8214 {
8215 if (lookfor == LOOKFOR_INITIAL
8216 && *l != NUL
8217 && l[STRLEN(l) - 1] == '\\')
8218 /* XXX */
8219 cont_amount = cin_get_equal_amount(
8220 curwin->w_cursor.lnum);
8221 if (lookfor != LOOKFOR_TERM)
8222 lookfor = LOOKFOR_UNTERM;
8223 }
8224 }
8225 }
8226 }
8227
8228 /*
8229 * Check if we are after a while (cond);
8230 * If so: Ignore until the matching "do".
8231 */
8232 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008233 else if (cin_iswhileofdo_end(terminated))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 {
8235 /*
8236 * Found an unterminated line after a while ();, line up
8237 * with the last one.
8238 * while (cond);
8239 * 100 + <- line up with this one
8240 * -> here;
8241 */
8242 if (lookfor == LOOKFOR_UNTERM
8243 || lookfor == LOOKFOR_ENUM_OR_INIT)
8244 {
8245 if (cont_amount > 0)
8246 amount = cont_amount;
8247 else
8248 amount += ind_continuation;
8249 break;
8250 }
8251
8252 if (whilelevel == 0)
8253 {
8254 lookfor = LOOKFOR_TERM;
8255 amount = get_indent(); /* XXX */
8256 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008257 amount += curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 }
8259 ++whilelevel;
8260 }
8261
8262 /*
8263 * We are after a "normal" statement.
8264 * If we had another statement we can stop now and use the
8265 * indent of that other statement.
8266 * Otherwise the indent of the current statement may be used,
8267 * search backwards for the next "normal" statement.
8268 */
8269 else
8270 {
8271 /*
8272 * Skip single break line, if before a switch label. It
8273 * may be lined up with the case label.
8274 */
8275 if (lookfor == LOOKFOR_NOBREAK
8276 && cin_isbreak(skipwhite(ml_get_curline())))
8277 {
8278 lookfor = LOOKFOR_ANY;
8279 continue;
8280 }
8281
8282 /*
8283 * Handle "do {" line.
8284 */
8285 if (whilelevel > 0)
8286 {
8287 l = cin_skipcomment(ml_get_curline());
8288 if (cin_isdo(l))
8289 {
8290 amount = get_indent(); /* XXX */
8291 --whilelevel;
8292 continue;
8293 }
8294 }
8295
8296 /*
8297 * Found a terminated line above an unterminated line. Add
8298 * the amount for a continuation line.
8299 * x = 1;
8300 * y = foo +
8301 * -> here;
8302 * or
8303 * int x = 1;
8304 * int foo,
8305 * -> here;
8306 */
8307 if (lookfor == LOOKFOR_UNTERM
8308 || lookfor == LOOKFOR_ENUM_OR_INIT)
8309 {
8310 if (cont_amount > 0)
8311 amount = cont_amount;
8312 else
8313 amount += ind_continuation;
8314 break;
8315 }
8316
8317 /*
8318 * Found a terminated line above a terminated line or "if"
8319 * etc. line. Use the amount of the line below us.
8320 * x = 1; x = 1;
8321 * if (asdf) y = 2;
8322 * while (asdf) ->here;
8323 * here;
8324 * ->foo;
8325 */
8326 if (lookfor == LOOKFOR_TERM)
8327 {
8328 if (!lookfor_break && whilelevel == 0)
8329 break;
8330 }
8331
8332 /*
8333 * First line above the one we're indenting is terminated.
8334 * To know what needs to be done look further backward for
8335 * a terminated line.
8336 */
8337 else
8338 {
8339 /*
8340 * position the cursor over the rightmost paren, so
8341 * that matching it will take us back to the start of
8342 * the line. Helps for:
8343 * func(asdr,
8344 * asdfasdf);
8345 * here;
8346 */
8347term_again:
8348 l = ml_get_curline();
8349 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008350 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008351 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 {
8353 /*
8354 * Check if we are on a case label now. This is
8355 * handled above.
8356 * case xx: if ( asdf &&
8357 * asdf)
8358 */
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008359 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 l = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008361 if (cin_iscase(l, FALSE) || cin_isscopedecl(l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 {
8363 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008364 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 continue;
8366 }
8367 }
8368
8369 /* When aligning with the case statement, don't align
8370 * with a statement after it.
8371 * case 1: { <-- don't use this { position
8372 * stat;
8373 * }
8374 * case 2:
8375 * stat;
8376 * }
8377 */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008378 iscase = (curbuf->b_ind_keep_case_label
8379 && cin_iscase(l, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380
8381 /*
8382 * Get indent and pointer to text for current line,
8383 * ignoring any jump label.
8384 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008385 amount = skip_label(curwin->w_cursor.lnum, &l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386
8387 if (theline[0] == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008388 amount += curbuf->b_ind_open_extra;
8389 /* See remark above: "Only add b_ind_open_extra.." */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008390 l = skipwhite(l);
8391 if (*l == '{')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008392 amount -= curbuf->b_ind_open_extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 lookfor = iscase ? LOOKFOR_ANY : LOOKFOR_TERM;
8394
8395 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008396 * When a terminated line starts with "else" skip to
8397 * the matching "if":
8398 * else 3;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008399 * indent this;
Bram Moolenaar18144c82006-04-12 21:52:12 +00008400 * Need to use the scope of this "else". XXX
8401 * If whilelevel != 0 continue looking for a "do {".
8402 */
8403 if (lookfor == LOOKFOR_TERM
8404 && *l != '}'
8405 && cin_iselse(l)
8406 && whilelevel == 0)
8407 {
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008408 if ((trypos = find_start_brace()) == NULL
8409 || find_match(LOOKFOR_IF, trypos->lnum)
8410 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00008411 break;
8412 continue;
8413 }
8414
8415 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 * If we're at the end of a block, skip to the start of
8417 * that block.
8418 */
Bram Moolenaar6d8f9c62011-11-30 13:03:28 +01008419 l = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008420 if (find_last_paren(l, '{', '}') /* XXX */
8421 && (trypos = find_start_brace()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 {
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008423 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 /* if not "else {" check for terminated again */
8425 /* but skip block for "} else {" */
8426 l = cin_skipcomment(ml_get_curline());
8427 if (*l == '}' || !cin_iselse(l))
8428 goto term_again;
8429 ++curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008430 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 }
8432 }
8433 }
8434 }
8435 }
8436 }
8437
8438 /* add extra indent for a comment */
8439 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008440 amount += curbuf->b_ind_comment;
Bram Moolenaar02c707a2010-07-17 17:12:06 +02008441
8442 /* subtract extra left-shift for jump labels */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008443 if (curbuf->b_ind_jump_label > 0 && original_line_islabel)
8444 amount -= curbuf->b_ind_jump_label;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 }
8446
8447 /*
8448 * ok -- we're not inside any sort of structure at all!
8449 *
8450 * this means we're at the top level, and everything should
8451 * basically just match where the previous line is, except
8452 * for the lines immediately following a function declaration,
8453 * which are K&R-style parameters and need to be indented.
8454 */
8455 else
8456 {
8457 /*
8458 * if our line starts with an open brace, forget about any
8459 * prevailing indent and make sure it looks like the start
8460 * of a function
8461 */
8462
8463 if (theline[0] == '{')
8464 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008465 amount = curbuf->b_ind_first_open;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 }
8467
8468 /*
8469 * If the NEXT line is a function declaration, the current
8470 * line needs to be indented as a function type spec.
Bram Moolenaar1a89bbe2010-03-02 12:38:22 +01008471 * Don't do this if the current line looks like a comment or if the
8472 * current line is terminated, ie. ends in ';', or if the current line
8473 * contains { or }: "void f() {\n if (1)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 */
8475 else if (cur_curpos.lnum < curbuf->b_ml.ml_line_count
8476 && !cin_nocode(theline)
Bram Moolenaar1a89bbe2010-03-02 12:38:22 +01008477 && vim_strchr(theline, '{') == NULL
8478 && vim_strchr(theline, '}') == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 && !cin_ends_in(theline, (char_u *)":", NULL)
8480 && !cin_ends_in(theline, (char_u *)",", NULL)
Bram Moolenaarc367faa2011-12-14 20:21:35 +01008481 && cin_isfuncdecl(NULL, cur_curpos.lnum + 1,
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008482 cur_curpos.lnum + 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 && !cin_isterminated(theline, FALSE, TRUE))
8484 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008485 amount = curbuf->b_ind_func_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 }
8487 else
8488 {
8489 amount = 0;
8490 curwin->w_cursor = cur_curpos;
8491
8492 /* search backwards until we find something we recognize */
8493
8494 while (curwin->w_cursor.lnum > 1)
8495 {
8496 curwin->w_cursor.lnum--;
8497 curwin->w_cursor.col = 0;
8498
8499 l = ml_get_curline();
8500
8501 /*
8502 * If we're in a comment now, skip to the start of the comment.
8503 */ /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008504 if ((trypos = ind_find_start_comment()) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 {
8506 curwin->w_cursor.lnum = trypos->lnum + 1;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008507 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 continue;
8509 }
8510
8511 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00008512 * Are we at the start of a cpp base class declaration or
8513 * constructor initialization?
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 */ /* XXX */
Bram Moolenaar18144c82006-04-12 21:52:12 +00008515 n = FALSE;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008516 if (curbuf->b_ind_cpp_baseclass != 0 && theline[0] != '{')
Bram Moolenaar18144c82006-04-12 21:52:12 +00008517 {
Bram Moolenaare7c56862007-08-04 10:14:52 +00008518 n = cin_is_cpp_baseclass(&col);
Bram Moolenaar18144c82006-04-12 21:52:12 +00008519 l = ml_get_curline();
8520 }
8521 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 {
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008523 /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008524 amount = get_baseclass_amount(col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 break;
8526 }
8527
8528 /*
8529 * Skip preprocessor directives and blank lines.
8530 */
8531 if (cin_ispreproc_cont(&l, &curwin->w_cursor.lnum))
8532 continue;
8533
8534 if (cin_nocode(l))
8535 continue;
8536
8537 /*
8538 * If the previous line ends in ',', use one level of
8539 * indentation:
8540 * int foo,
8541 * bar;
8542 * do this before checking for '}' in case of eg.
8543 * enum foobar
8544 * {
8545 * ...
8546 * } foo,
8547 * bar;
8548 */
8549 n = 0;
8550 if (cin_ends_in(l, (char_u *)",", NULL)
8551 || (*l != NUL && (n = l[STRLEN(l) - 1]) == '\\'))
8552 {
8553 /* take us back to opening paren */
8554 if (find_last_paren(l, '(', ')')
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008555 && (trypos = find_match_paren(
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008556 curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008557 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558
8559 /* For a line ending in ',' that is a continuation line go
8560 * back to the first line with a backslash:
8561 * char *foo = "bla\
8562 * bla",
8563 * here;
8564 */
8565 while (n == 0 && curwin->w_cursor.lnum > 1)
8566 {
8567 l = ml_get(curwin->w_cursor.lnum - 1);
8568 if (*l == NUL || l[STRLEN(l) - 1] != '\\')
8569 break;
8570 --curwin->w_cursor.lnum;
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008571 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 }
8573
8574 amount = get_indent(); /* XXX */
8575
8576 if (amount == 0)
8577 amount = cin_first_id_amount();
8578 if (amount == 0)
8579 amount = ind_continuation;
8580 break;
8581 }
8582
8583 /*
8584 * If the line looks like a function declaration, and we're
8585 * not in a comment, put it the left margin.
8586 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008587 if (cin_isfuncdecl(NULL, cur_curpos.lnum, 0)) /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 break;
8589 l = ml_get_curline();
8590
8591 /*
8592 * Finding the closing '}' of a previous function. Put
8593 * current line at the left margin. For when 'cino' has "fs".
8594 */
8595 if (*skipwhite(l) == '}')
8596 break;
8597
8598 /* (matching {)
8599 * If the previous line ends on '};' (maybe followed by
8600 * comments) align at column 0. For example:
8601 * char *string_array[] = { "foo",
8602 * / * x * / "b};ar" }; / * foobar * /
8603 */
8604 if (cin_ends_in(l, (char_u *)"};", NULL))
8605 break;
8606
8607 /*
Bram Moolenaar3388bb42011-11-30 17:20:23 +01008608 * Find a line only has a semicolon that belongs to a previous
8609 * line ending in '}', e.g. before an #endif. Don't increase
8610 * indent then.
8611 */
8612 if (*(look = skipwhite(l)) == ';' && cin_nocode(look + 1))
8613 {
8614 pos_T curpos_save = curwin->w_cursor;
8615
8616 while (curwin->w_cursor.lnum > 1)
8617 {
8618 look = ml_get(--curwin->w_cursor.lnum);
8619 if (!(cin_nocode(look) || cin_ispreproc_cont(
8620 &look, &curwin->w_cursor.lnum)))
8621 break;
8622 }
8623 if (curwin->w_cursor.lnum > 0
8624 && cin_ends_in(look, (char_u *)"}", NULL))
8625 break;
8626
8627 curwin->w_cursor = curpos_save;
8628 }
8629
8630 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 * If the PREVIOUS line is a function declaration, the current
8632 * line (and the ones that follow) needs to be indented as
8633 * parameters.
8634 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008635 if (cin_isfuncdecl(&l, curwin->w_cursor.lnum, 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008637 amount = curbuf->b_ind_param;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 break;
8639 }
8640
8641 /*
8642 * If the previous line ends in ';' and the line before the
8643 * previous line ends in ',' or '\', ident to column zero:
8644 * int foo,
8645 * bar;
8646 * indent_to_0 here;
8647 */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00008648 if (cin_ends_in(l, (char_u *)";", NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 {
8650 l = ml_get(curwin->w_cursor.lnum - 1);
8651 if (cin_ends_in(l, (char_u *)",", NULL)
8652 || (*l != NUL && l[STRLEN(l) - 1] == '\\'))
8653 break;
8654 l = ml_get_curline();
8655 }
8656
8657 /*
8658 * Doesn't look like anything interesting -- so just
8659 * use the indent of this line.
8660 *
8661 * Position the cursor over the rightmost paren, so that
8662 * matching it will take us back to the start of the line.
8663 */
8664 find_last_paren(l, '(', ')');
8665
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008666 if ((trypos = find_match_paren(curbuf->b_ind_maxparen)) != NULL)
Bram Moolenaarddfc9782008-02-25 20:55:22 +00008667 curwin->w_cursor = *trypos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 amount = get_indent(); /* XXX */
8669 break;
8670 }
8671
8672 /* add extra indent for a comment */
8673 if (cin_iscomment(theline))
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008674 amount += curbuf->b_ind_comment;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675
8676 /* add extra indent if the previous line ended in a backslash:
8677 * "asdfasdf\
8678 * here";
8679 * char *foo = "asdf\
8680 * here";
8681 */
8682 if (cur_curpos.lnum > 1)
8683 {
8684 l = ml_get(cur_curpos.lnum - 1);
8685 if (*l != NUL && l[STRLEN(l) - 1] == '\\')
8686 {
8687 cur_amount = cin_get_equal_amount(cur_curpos.lnum - 1);
8688 if (cur_amount > 0)
8689 amount = cur_amount;
8690 else if (cur_amount == 0)
8691 amount += ind_continuation;
8692 }
8693 }
8694 }
8695 }
8696
8697theend:
8698 /* put the cursor back where it belongs */
8699 curwin->w_cursor = cur_curpos;
8700
8701 vim_free(linecopy);
8702
8703 if (amount < 0)
8704 return 0;
8705 return amount;
8706}
8707
8708 static int
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008709find_match(lookfor, ourscope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 int lookfor;
8711 linenr_T ourscope;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712{
8713 char_u *look;
8714 pos_T *theirscope;
8715 char_u *mightbeif;
8716 int elselevel;
8717 int whilelevel;
8718
8719 if (lookfor == LOOKFOR_IF)
8720 {
8721 elselevel = 1;
8722 whilelevel = 0;
8723 }
8724 else
8725 {
8726 elselevel = 0;
8727 whilelevel = 1;
8728 }
8729
8730 curwin->w_cursor.col = 0;
8731
8732 while (curwin->w_cursor.lnum > ourscope + 1)
8733 {
8734 curwin->w_cursor.lnum--;
8735 curwin->w_cursor.col = 0;
8736
8737 look = cin_skipcomment(ml_get_curline());
8738 if (cin_iselse(look)
8739 || cin_isif(look)
8740 || cin_isdo(look) /* XXX */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008741 || cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 {
8743 /*
8744 * if we've gone outside the braces entirely,
8745 * we must be out of scope...
8746 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008747 theirscope = find_start_brace(); /* XXX */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 if (theirscope == NULL)
8749 break;
8750
8751 /*
8752 * and if the brace enclosing this is further
8753 * back than the one enclosing the else, we're
8754 * out of luck too.
8755 */
8756 if (theirscope->lnum < ourscope)
8757 break;
8758
8759 /*
8760 * and if they're enclosed in a *deeper* brace,
8761 * then we can ignore it because it's in a
8762 * different scope...
8763 */
8764 if (theirscope->lnum > ourscope)
8765 continue;
8766
8767 /*
8768 * if it was an "else" (that's not an "else if")
8769 * then we need to go back to another if, so
8770 * increment elselevel
8771 */
8772 look = cin_skipcomment(ml_get_curline());
8773 if (cin_iselse(look))
8774 {
8775 mightbeif = cin_skipcomment(look + 4);
8776 if (!cin_isif(mightbeif))
8777 ++elselevel;
8778 continue;
8779 }
8780
8781 /*
8782 * if it was a "while" then we need to go back to
8783 * another "do", so increment whilelevel. XXX
8784 */
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008785 if (cin_iswhileofdo(look, curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 {
8787 ++whilelevel;
8788 continue;
8789 }
8790
8791 /* If it's an "if" decrement elselevel */
8792 look = cin_skipcomment(ml_get_curline());
8793 if (cin_isif(look))
8794 {
8795 elselevel--;
8796 /*
8797 * When looking for an "if" ignore "while"s that
8798 * get in the way.
8799 */
8800 if (elselevel == 0 && lookfor == LOOKFOR_IF)
8801 whilelevel = 0;
8802 }
8803
8804 /* If it's a "do" decrement whilelevel */
8805 if (cin_isdo(look))
8806 whilelevel--;
8807
8808 /*
8809 * if we've used up all the elses, then
8810 * this must be the if that we want!
8811 * match the indent level of that if.
8812 */
8813 if (elselevel <= 0 && whilelevel <= 0)
8814 {
8815 return OK;
8816 }
8817 }
8818 }
8819 return FAIL;
8820}
8821
8822# if defined(FEAT_EVAL) || defined(PROTO)
8823/*
8824 * Get indent level from 'indentexpr'.
8825 */
8826 int
8827get_expr_indent()
8828{
8829 int indent;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008830 pos_T save_pos;
8831 colnr_T save_curswant;
8832 int save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 int save_State;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008834 int use_sandbox = was_set_insecurely((char_u *)"indentexpr",
8835 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008837 /* Save and restore cursor position and curswant, in case it was changed
8838 * via :normal commands */
8839 save_pos = curwin->w_cursor;
8840 save_curswant = curwin->w_curswant;
8841 save_set_curswant = curwin->w_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 set_vim_var_nr(VV_LNUM, curwin->w_cursor.lnum);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008843 if (use_sandbox)
8844 ++sandbox;
8845 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 indent = eval_to_number(curbuf->b_p_inde);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008847 if (use_sandbox)
8848 --sandbox;
8849 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850
8851 /* Restore the cursor position so that 'indentexpr' doesn't need to.
8852 * Pretend to be in Insert mode, allow cursor past end of line for "o"
8853 * command. */
8854 save_State = State;
8855 State = INSERT;
Bram Moolenaar8fe8d9e2013-02-13 16:10:17 +01008856 curwin->w_cursor = save_pos;
8857 curwin->w_curswant = save_curswant;
8858 curwin->w_set_curswant = save_set_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 check_cursor();
8860 State = save_State;
8861
8862 /* If there is an error, just keep the current indent. */
8863 if (indent < 0)
8864 indent = get_indent();
8865
8866 return indent;
8867}
8868# endif
8869
8870#endif /* FEAT_CINDENT */
8871
8872#if defined(FEAT_LISP) || defined(PROTO)
8873
8874static int lisp_match __ARGS((char_u *p));
8875
8876 static int
8877lisp_match(p)
8878 char_u *p;
8879{
8880 char_u buf[LSIZE];
8881 int len;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01008882 char_u *word = *curbuf->b_p_lw != NUL ? curbuf->b_p_lw : p_lispwords;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883
8884 while (*word != NUL)
8885 {
8886 (void)copy_option_part(&word, buf, LSIZE, ",");
8887 len = (int)STRLEN(buf);
8888 if (STRNCMP(buf, p, len) == 0 && p[len] == ' ')
8889 return TRUE;
8890 }
8891 return FALSE;
8892}
8893
8894/*
8895 * When 'p' is present in 'cpoptions, a Vi compatible method is used.
8896 * The incompatible newer method is quite a bit better at indenting
8897 * code in lisp-like languages than the traditional one; it's still
8898 * mostly heuristics however -- Dirk van Deun, dirk@rave.org
8899 *
8900 * TODO:
8901 * Findmatch() should be adapted for lisp, also to make showmatch
8902 * work correctly: now (v5.3) it seems all C/C++ oriented:
8903 * - it does not recognize the #\( and #\) notations as character literals
8904 * - it doesn't know about comments starting with a semicolon
8905 * - it incorrectly interprets '(' as a character literal
8906 * All this messes up get_lisp_indent in some rare cases.
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008907 * Update from Sergey Khorev:
8908 * I tried to fix the first two issues.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 */
8910 int
8911get_lisp_indent()
8912{
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008913 pos_T *pos, realpos, paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 int amount;
8915 char_u *that;
8916 colnr_T col;
8917 colnr_T firsttry;
8918 int parencount, quotecount;
8919 int vi_lisp;
8920
8921 /* Set vi_lisp to use the vi-compatible method */
8922 vi_lisp = (vim_strchr(p_cpo, CPO_LISP) != NULL);
8923
8924 realpos = curwin->w_cursor;
8925 curwin->w_cursor.col = 0;
8926
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008927 if ((pos = findmatch(NULL, '(')) == NULL)
8928 pos = findmatch(NULL, '[');
8929 else
8930 {
8931 paren = *pos;
8932 pos = findmatch(NULL, '[');
8933 if (pos == NULL || ltp(pos, &paren))
8934 pos = &paren;
8935 }
8936 if (pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 {
8938 /* Extra trick: Take the indent of the first previous non-white
8939 * line that is at the same () level. */
8940 amount = -1;
8941 parencount = 0;
8942
8943 while (--curwin->w_cursor.lnum >= pos->lnum)
8944 {
8945 if (linewhite(curwin->w_cursor.lnum))
8946 continue;
8947 for (that = ml_get_curline(); *that != NUL; ++that)
8948 {
8949 if (*that == ';')
8950 {
8951 while (*(that + 1) != NUL)
8952 ++that;
8953 continue;
8954 }
8955 if (*that == '\\')
8956 {
8957 if (*(that + 1) != NUL)
8958 ++that;
8959 continue;
8960 }
8961 if (*that == '"' && *(that + 1) != NUL)
8962 {
Bram Moolenaar15ff6c12006-09-15 18:18:09 +00008963 while (*++that && *that != '"')
8964 {
8965 /* skipping escaped characters in the string */
8966 if (*that == '\\')
8967 {
8968 if (*++that == NUL)
8969 break;
8970 if (that[1] == NUL)
8971 {
8972 ++that;
8973 break;
8974 }
8975 }
8976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 }
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008978 if (*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008980 else if (*that == ')' || *that == ']')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 --parencount;
8982 }
8983 if (parencount == 0)
8984 {
8985 amount = get_indent();
8986 break;
8987 }
8988 }
8989
8990 if (amount == -1)
8991 {
8992 curwin->w_cursor.lnum = pos->lnum;
8993 curwin->w_cursor.col = pos->col;
8994 col = pos->col;
8995
8996 that = ml_get_curline();
8997
8998 if (vi_lisp && get_indent() == 0)
8999 amount = 2;
9000 else
9001 {
9002 amount = 0;
9003 while (*that && col)
9004 {
9005 amount += lbr_chartabsize_adv(&that, (colnr_T)amount);
9006 col--;
9007 }
9008
9009 /*
9010 * Some keywords require "body" indenting rules (the
9011 * non-standard-lisp ones are Scheme special forms):
9012 *
9013 * (let ((a 1)) instead (let ((a 1))
9014 * (...)) of (...))
9015 */
9016
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009017 if (!vi_lisp && (*that == '(' || *that == '[')
9018 && lisp_match(that + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 amount += 2;
9020 else
9021 {
9022 that++;
9023 amount++;
9024 firsttry = amount;
9025
9026 while (vim_iswhite(*that))
9027 {
9028 amount += lbr_chartabsize(that, (colnr_T)amount);
9029 ++that;
9030 }
9031
9032 if (*that && *that != ';') /* not a comment line */
9033 {
Bram Moolenaare21877a2008-02-13 09:58:14 +00009034 /* test *that != '(' to accommodate first let/do
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 * argument if it is more than one line */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009036 if (!vi_lisp && *that != '(' && *that != '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 firsttry++;
9038
9039 parencount = 0;
9040 quotecount = 0;
9041
9042 if (vi_lisp
9043 || (*that != '"'
9044 && *that != '\''
9045 && *that != '#'
9046 && (*that < '0' || *that > '9')))
9047 {
9048 while (*that
9049 && (!vim_iswhite(*that)
9050 || quotecount
9051 || parencount)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009052 && (!((*that == '(' || *that == '[')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 && !quotecount
9054 && !parencount
9055 && vi_lisp)))
9056 {
9057 if (*that == '"')
9058 quotecount = !quotecount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009059 if ((*that == '(' || *that == '[')
9060 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061 ++parencount;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009062 if ((*that == ')' || *that == ']')
9063 && !quotecount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 --parencount;
9065 if (*that == '\\' && *(that+1) != NUL)
9066 amount += lbr_chartabsize_adv(&that,
9067 (colnr_T)amount);
9068 amount += lbr_chartabsize_adv(&that,
9069 (colnr_T)amount);
9070 }
9071 }
9072 while (vim_iswhite(*that))
9073 {
9074 amount += lbr_chartabsize(that, (colnr_T)amount);
9075 that++;
9076 }
9077 if (!*that || *that == ';')
9078 amount = firsttry;
9079 }
9080 }
9081 }
9082 }
9083 }
9084 else
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009085 amount = 0; /* no matching '(' or '[' found, use zero indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086
9087 curwin->w_cursor = realpos;
9088
9089 return amount;
9090}
9091#endif /* FEAT_LISP */
9092
9093 void
9094prepare_to_exit()
9095{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009096#if defined(SIGHUP) && defined(SIG_IGN)
9097 /* Ignore SIGHUP, because a dropped connection causes a read error, which
9098 * makes Vim exit and then handling SIGHUP causes various reentrance
9099 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009100 signal(SIGHUP, SIG_IGN);
9101#endif
9102
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103#ifdef FEAT_GUI
9104 if (gui.in_use)
9105 {
9106 gui.dying = TRUE;
9107 out_trash(); /* trash any pending output */
9108 }
9109 else
9110#endif
9111 {
9112 windgoto((int)Rows - 1, 0);
9113
9114 /*
9115 * Switch terminal mode back now, so messages end up on the "normal"
9116 * screen (if there are two screens).
9117 */
9118 settmode(TMODE_COOK);
9119#ifdef WIN3264
9120 if (can_end_termcap_mode(FALSE) == TRUE)
9121#endif
9122 stoptermcap();
9123 out_flush();
9124 }
9125}
9126
9127/*
9128 * Preserve files and exit.
9129 * When called IObuff must contain a message.
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009130 * NOTE: This may be called from deathtrap() in a signal handler, avoid unsafe
9131 * functions, such as allocating memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 */
9133 void
9134preserve_exit()
9135{
9136 buf_T *buf;
9137
9138 prepare_to_exit();
9139
Bram Moolenaar4770d092006-01-12 23:22:24 +00009140 /* Setting this will prevent free() calls. That avoids calling free()
9141 * recursively when free() was invoked with a bad pointer. */
9142 really_exiting = TRUE;
9143
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 out_str(IObuff);
9145 screen_start(); /* don't know where cursor is now */
9146 out_flush();
9147
9148 ml_close_notmod(); /* close all not-modified buffers */
9149
9150 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9151 {
9152 if (buf->b_ml.ml_mfp != NULL && buf->b_ml.ml_mfp->mf_fname != NULL)
9153 {
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009154 OUT_STR("Vim: preserving files...\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 screen_start(); /* don't know where cursor is now */
9156 out_flush();
9157 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
9158 break;
9159 }
9160 }
9161
9162 ml_close_all(FALSE); /* close all memfiles, without deleting */
9163
Bram Moolenaarbec9c202013-09-05 21:41:39 +02009164 OUT_STR("Vim: Finished.\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165
9166 getout(1);
9167}
9168
9169/*
9170 * return TRUE if "fname" exists.
9171 */
9172 int
9173vim_fexists(fname)
9174 char_u *fname;
9175{
9176 struct stat st;
9177
9178 if (mch_stat((char *)fname, &st))
9179 return FALSE;
9180 return TRUE;
9181}
9182
9183/*
9184 * Check for CTRL-C pressed, but only once in a while.
9185 * Should be used instead of ui_breakcheck() for functions that check for
9186 * each line in the file. Calling ui_breakcheck() each time takes too much
9187 * time, because it can be a system call.
9188 */
9189
9190#ifndef BREAKCHECK_SKIP
9191# ifdef FEAT_GUI /* assume the GUI only runs on fast computers */
9192# define BREAKCHECK_SKIP 200
9193# else
9194# define BREAKCHECK_SKIP 32
9195# endif
9196#endif
9197
9198static int breakcheck_count = 0;
9199
9200 void
9201line_breakcheck()
9202{
9203 if (++breakcheck_count >= BREAKCHECK_SKIP)
9204 {
9205 breakcheck_count = 0;
9206 ui_breakcheck();
9207 }
9208}
9209
9210/*
9211 * Like line_breakcheck() but check 10 times less often.
9212 */
9213 void
9214fast_breakcheck()
9215{
9216 if (++breakcheck_count >= BREAKCHECK_SKIP * 10)
9217 {
9218 breakcheck_count = 0;
9219 ui_breakcheck();
9220 }
9221}
9222
9223/*
Bram Moolenaard7834d32009-12-02 16:14:36 +00009224 * Invoke expand_wildcards() for one pattern.
9225 * Expand items like "%:h" before the expansion.
9226 * Returns OK or FAIL.
9227 */
9228 int
9229expand_wildcards_eval(pat, num_file, file, flags)
9230 char_u **pat; /* pointer to input pattern */
9231 int *num_file; /* resulting number of files */
9232 char_u ***file; /* array of resulting files */
9233 int flags; /* EW_DIR, etc. */
9234{
9235 int ret = FAIL;
9236 char_u *eval_pat = NULL;
9237 char_u *exp_pat = *pat;
9238 char_u *ignored_msg;
9239 int usedlen;
9240
9241 if (*exp_pat == '%' || *exp_pat == '#' || *exp_pat == '<')
9242 {
9243 ++emsg_off;
9244 eval_pat = eval_vars(exp_pat, exp_pat, &usedlen,
9245 NULL, &ignored_msg, NULL);
9246 --emsg_off;
9247 if (eval_pat != NULL)
9248 exp_pat = concat_str(eval_pat, exp_pat + usedlen);
9249 }
9250
9251 if (exp_pat != NULL)
9252 ret = expand_wildcards(1, &exp_pat, num_file, file, flags);
9253
9254 if (eval_pat != NULL)
9255 {
9256 vim_free(exp_pat);
9257 vim_free(eval_pat);
9258 }
9259
9260 return ret;
9261}
9262
9263/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 * Expand wildcards. Calls gen_expand_wildcards() and removes files matching
9265 * 'wildignore'.
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009266 * Returns OK or FAIL. When FAIL then "num_file" won't be set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 */
9268 int
9269expand_wildcards(num_pat, pat, num_file, file, flags)
9270 int num_pat; /* number of input patterns */
9271 char_u **pat; /* array of input patterns */
9272 int *num_file; /* resulting number of files */
9273 char_u ***file; /* array of resulting files */
9274 int flags; /* EW_DIR, etc. */
9275{
9276 int retval;
9277 int i, j;
9278 char_u *p;
9279 int non_suf_match; /* number without matching suffix */
9280
9281 retval = gen_expand_wildcards(num_pat, pat, num_file, file, flags);
9282
9283 /* When keeping all matches, return here */
Bram Moolenaar9e193ac2010-07-19 23:11:27 +02009284 if ((flags & EW_KEEPALL) || retval == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285 return retval;
9286
9287#ifdef FEAT_WILDIGN
9288 /*
9289 * Remove names that match 'wildignore'.
9290 */
9291 if (*p_wig)
9292 {
9293 char_u *ffname;
9294
9295 /* check all files in (*file)[] */
9296 for (i = 0; i < *num_file; ++i)
9297 {
9298 ffname = FullName_save((*file)[i], FALSE);
9299 if (ffname == NULL) /* out of memory */
9300 break;
9301# ifdef VMS
9302 vms_remove_version(ffname);
9303# endif
9304 if (match_file_list(p_wig, (*file)[i], ffname))
9305 {
9306 /* remove this matching file from the list */
9307 vim_free((*file)[i]);
9308 for (j = i; j + 1 < *num_file; ++j)
9309 (*file)[j] = (*file)[j + 1];
9310 --*num_file;
9311 --i;
9312 }
9313 vim_free(ffname);
9314 }
9315 }
9316#endif
9317
9318 /*
9319 * Move the names where 'suffixes' match to the end.
9320 */
9321 if (*num_file > 1)
9322 {
9323 non_suf_match = 0;
9324 for (i = 0; i < *num_file; ++i)
9325 {
9326 if (!match_suffix((*file)[i]))
9327 {
9328 /*
9329 * Move the name without matching suffix to the front
9330 * of the list.
9331 */
9332 p = (*file)[i];
9333 for (j = i; j > non_suf_match; --j)
9334 (*file)[j] = (*file)[j - 1];
9335 (*file)[non_suf_match++] = p;
9336 }
9337 }
9338 }
9339
9340 return retval;
9341}
9342
9343/*
9344 * Return TRUE if "fname" matches with an entry in 'suffixes'.
9345 */
9346 int
9347match_suffix(fname)
9348 char_u *fname;
9349{
9350 int fnamelen, setsuflen;
9351 char_u *setsuf;
9352#define MAXSUFLEN 30 /* maximum length of a file suffix */
9353 char_u suf_buf[MAXSUFLEN];
9354
9355 fnamelen = (int)STRLEN(fname);
9356 setsuflen = 0;
9357 for (setsuf = p_su; *setsuf; )
9358 {
9359 setsuflen = copy_option_part(&setsuf, suf_buf, MAXSUFLEN, ".,");
Bram Moolenaar055a2ba2009-07-14 19:40:21 +00009360 if (setsuflen == 0)
9361 {
9362 char_u *tail = gettail(fname);
9363
9364 /* empty entry: match name without a '.' */
9365 if (vim_strchr(tail, '.') == NULL)
9366 {
9367 setsuflen = 1;
9368 break;
9369 }
9370 }
9371 else
9372 {
9373 if (fnamelen >= setsuflen
9374 && fnamencmp(suf_buf, fname + fnamelen - setsuflen,
9375 (size_t)setsuflen) == 0)
9376 break;
9377 setsuflen = 0;
9378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379 }
9380 return (setsuflen != 0);
9381}
9382
9383#if !defined(NO_EXPANDPATH) || defined(PROTO)
9384
9385# ifdef VIM_BACKTICK
9386static int vim_backtick __ARGS((char_u *p));
9387static int expand_backtick __ARGS((garray_T *gap, char_u *pat, int flags));
9388# endif
9389
9390# if defined(MSDOS) || defined(FEAT_GUI_W16) || defined(WIN3264)
9391/*
9392 * File name expansion code for MS-DOS, Win16 and Win32. It's here because
9393 * it's shared between these systems.
9394 */
9395# if defined(DJGPP) || defined(PROTO)
9396# define _cdecl /* DJGPP doesn't have this */
9397# else
9398# ifdef __BORLANDC__
9399# define _cdecl _RTLENTRYF
9400# endif
9401# endif
9402
9403/*
9404 * comparison function for qsort in dos_expandpath()
9405 */
9406 static int _cdecl
9407pstrcmp(const void *a, const void *b)
9408{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009409 return (pathcmp(*(char **)a, *(char **)b, -1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410}
9411
9412# ifndef WIN3264
9413 static void
9414namelowcpy(
9415 char_u *d,
9416 char_u *s)
9417{
9418# ifdef DJGPP
9419 if (USE_LONG_FNAME) /* don't lower case on Windows 95/NT systems */
9420 while (*s)
9421 *d++ = *s++;
9422 else
9423# endif
9424 while (*s)
9425 *d++ = TOLOWER_LOC(*s++);
9426 *d = NUL;
9427}
9428# endif
9429
9430/*
Bram Moolenaar231334e2005-07-25 20:46:57 +00009431 * Recursively expand one path component into all matching files and/or
9432 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 * Return the number of matches found.
9434 * "path" has backslashes before chars that are not to be expanded, starting
9435 * at "path[wildoff]".
Bram Moolenaar231334e2005-07-25 20:46:57 +00009436 * Return the number of matches found.
9437 * NOTE: much of this is identical to unix_expandpath(), keep in sync!
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438 */
9439 static int
9440dos_expandpath(
9441 garray_T *gap,
9442 char_u *path,
9443 int wildoff,
Bram Moolenaar231334e2005-07-25 20:46:57 +00009444 int flags, /* EW_* flags */
Bram Moolenaar25394022007-05-10 19:06:20 +00009445 int didstar) /* expanded "**" once already */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009447 char_u *buf;
9448 char_u *path_end;
9449 char_u *p, *s, *e;
9450 int start_len = gap->ga_len;
9451 char_u *pat;
9452 regmatch_T regmatch;
9453 int starts_with_dot;
9454 int matches;
9455 int len;
9456 int starstar = FALSE;
9457 static int stardepth = 0; /* depth for "**" expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458#ifdef WIN3264
9459 WIN32_FIND_DATA fb;
9460 HANDLE hFind = (HANDLE)0;
9461# ifdef FEAT_MBYTE
9462 WIN32_FIND_DATAW wfb;
9463 WCHAR *wn = NULL; /* UCS-2 name, NULL when not used. */
9464# endif
9465#else
9466 struct ffblk fb;
9467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 char_u *matchname;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009469 int ok;
9470
9471 /* Expanding "**" may take a long time, check for CTRL-C. */
9472 if (stardepth > 0)
9473 {
9474 ui_breakcheck();
9475 if (got_int)
9476 return 0;
9477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478
9479 /* make room for file name */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009480 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 if (buf == NULL)
9482 return 0;
9483
9484 /*
9485 * Find the first part in the path name that contains a wildcard or a ~1.
9486 * Copy it into buf, including the preceding characters.
9487 */
9488 p = buf;
9489 s = buf;
9490 e = NULL;
9491 path_end = path;
9492 while (*path_end != NUL)
9493 {
9494 /* May ignore a wildcard that has a backslash before it; it will
9495 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9496 if (path_end >= path + wildoff && rem_backslash(path_end))
9497 *p++ = *path_end++;
9498 else if (*path_end == '\\' || *path_end == ':' || *path_end == '/')
9499 {
9500 if (e != NULL)
9501 break;
9502 s = p + 1;
9503 }
9504 else if (path_end >= path + wildoff
9505 && vim_strchr((char_u *)"*?[~", *path_end) != NULL)
9506 e = p;
9507#ifdef FEAT_MBYTE
9508 if (has_mbyte)
9509 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009510 len = (*mb_ptr2len)(path_end);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 STRNCPY(p, path_end, len);
9512 p += len;
9513 path_end += len;
9514 }
9515 else
9516#endif
9517 *p++ = *path_end++;
9518 }
9519 e = p;
9520 *e = NUL;
9521
9522 /* now we have one wildcard component between s and e */
9523 /* Remove backslashes between "wildoff" and the start of the wildcard
9524 * component. */
9525 for (p = buf + wildoff; p < s; ++p)
9526 if (rem_backslash(p))
9527 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009528 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 --e;
9530 --s;
9531 }
9532
Bram Moolenaar231334e2005-07-25 20:46:57 +00009533 /* Check for "**" between "s" and "e". */
9534 for (p = s; p < e; ++p)
9535 if (p[0] == '*' && p[1] == '*')
9536 starstar = TRUE;
9537
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 starts_with_dot = (*s == '.');
9539 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9540 if (pat == NULL)
9541 {
9542 vim_free(buf);
9543 return 0;
9544 }
9545
9546 /* compile the regexp into a program */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009547 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009548 ++emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549 regmatch.rm_ic = TRUE; /* Always ignore case */
9550 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009551 if (flags & (EW_NOERROR | EW_NOTWILD))
Bram Moolenaarb5609832011-07-20 15:04:58 +02009552 --emsg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 vim_free(pat);
9554
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009555 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 {
9557 vim_free(buf);
9558 return 0;
9559 }
9560
9561 /* remember the pattern or file name being looked for */
9562 matchname = vim_strsave(s);
9563
Bram Moolenaar231334e2005-07-25 20:46:57 +00009564 /* If "**" is by itself, this is the first time we encounter it and more
9565 * is following then find matches without any directory. */
9566 if (!didstar && stardepth < 100 && starstar && e - s == 2
9567 && *path_end == '/')
9568 {
9569 STRCPY(s, path_end + 1);
9570 ++stardepth;
9571 (void)dos_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9572 --stardepth;
9573 }
9574
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 /* Scan all files in the directory with "dir/ *.*" */
9576 STRCPY(s, "*.*");
9577#ifdef WIN3264
9578# ifdef FEAT_MBYTE
9579 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
9580 {
9581 /* The active codepage differs from 'encoding'. Attempt using the
9582 * wide function. If it fails because it is not implemented fall back
9583 * to the non-wide version (for Windows 98) */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009584 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 if (wn != NULL)
9586 {
9587 hFind = FindFirstFileW(wn, &wfb);
9588 if (hFind == INVALID_HANDLE_VALUE
9589 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
9590 {
9591 vim_free(wn);
9592 wn = NULL;
9593 }
9594 }
9595 }
9596
9597 if (wn == NULL)
9598# endif
9599 hFind = FindFirstFile(buf, &fb);
9600 ok = (hFind != INVALID_HANDLE_VALUE);
9601#else
9602 /* If we are expanding wildcards we try both files and directories */
9603 ok = (findfirst((char *)buf, &fb,
9604 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
9605#endif
9606
9607 while (ok)
9608 {
9609#ifdef WIN3264
9610# ifdef FEAT_MBYTE
9611 if (wn != NULL)
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009612 p = utf16_to_enc(wfb.cFileName, NULL); /* p is allocated here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 else
9614# endif
9615 p = (char_u *)fb.cFileName;
9616#else
9617 p = (char_u *)fb.ff_name;
9618#endif
9619 /* Ignore entries starting with a dot, unless when asked for. Accept
9620 * all entries found with "matchname". */
9621 if ((p[0] != '.' || starts_with_dot)
9622 && (matchname == NULL
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009623 || (regmatch.regprog != NULL
9624 && vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009625 || ((flags & EW_NOTWILD)
9626 && fnamencmp(path + (s - buf), p, e - s) == 0)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627 {
9628#ifdef WIN3264
9629 STRCPY(s, p);
9630#else
9631 namelowcpy(s, p);
9632#endif
9633 len = (int)STRLEN(buf);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009634
9635 if (starstar && stardepth < 100)
9636 {
9637 /* For "**" in the pattern first go deeper in the tree to
9638 * find matches. */
9639 STRCPY(buf + len, "/**");
9640 STRCPY(buf + len + 3, path_end);
9641 ++stardepth;
9642 (void)dos_expandpath(gap, buf, len + 1, flags, TRUE);
9643 --stardepth;
9644 }
9645
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 STRCPY(buf + len, path_end);
9647 if (mch_has_exp_wildcard(path_end))
9648 {
9649 /* need to expand another component of the path */
9650 /* remove backslashes for the remaining components only */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009651 (void)dos_expandpath(gap, buf, len + 1, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652 }
9653 else
9654 {
9655 /* no more wildcards, check if there is a match */
9656 /* remove backslashes for the remaining components only */
9657 if (*path_end != 0)
9658 backslash_halve(buf + len + 1);
9659 if (mch_getperm(buf) >= 0) /* add existing file */
9660 addfile(gap, buf, flags);
9661 }
9662 }
9663
9664#ifdef WIN3264
9665# ifdef FEAT_MBYTE
9666 if (wn != NULL)
9667 {
9668 vim_free(p);
9669 ok = FindNextFileW(hFind, &wfb);
9670 }
9671 else
9672# endif
9673 ok = FindNextFile(hFind, &fb);
9674#else
9675 ok = (findnext(&fb) == 0);
9676#endif
9677
9678 /* If no more matches and no match was used, try expanding the name
9679 * itself. Finds the long name of a short filename. */
9680 if (!ok && matchname != NULL && gap->ga_len == start_len)
9681 {
9682 STRCPY(s, matchname);
9683#ifdef WIN3264
9684 FindClose(hFind);
9685# ifdef FEAT_MBYTE
9686 if (wn != NULL)
9687 {
9688 vim_free(wn);
Bram Moolenaar36f692d2008-11-20 16:10:17 +00009689 wn = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 if (wn != NULL)
9691 hFind = FindFirstFileW(wn, &wfb);
9692 }
9693 if (wn == NULL)
9694# endif
9695 hFind = FindFirstFile(buf, &fb);
9696 ok = (hFind != INVALID_HANDLE_VALUE);
9697#else
9698 ok = (findfirst((char *)buf, &fb,
9699 (*path_end != NUL || (flags & EW_DIR)) ? FA_DIREC : 0) == 0);
9700#endif
9701 vim_free(matchname);
9702 matchname = NULL;
9703 }
9704 }
9705
9706#ifdef WIN3264
9707 FindClose(hFind);
9708# ifdef FEAT_MBYTE
9709 vim_free(wn);
9710# endif
9711#endif
9712 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +02009713 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 vim_free(matchname);
9715
9716 matches = gap->ga_len - start_len;
9717 if (matches > 0)
9718 qsort(((char_u **)gap->ga_data) + start_len, (size_t)matches,
9719 sizeof(char_u *), pstrcmp);
9720 return matches;
9721}
9722
9723 int
9724mch_expandpath(
9725 garray_T *gap,
9726 char_u *path,
9727 int flags) /* EW_* flags */
9728{
Bram Moolenaar231334e2005-07-25 20:46:57 +00009729 return dos_expandpath(gap, path, 0, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730}
9731# endif /* MSDOS || FEAT_GUI_W16 || WIN3264 */
9732
Bram Moolenaar231334e2005-07-25 20:46:57 +00009733#if (defined(UNIX) && !defined(VMS)) || defined(USE_UNIXFILENAME) \
9734 || defined(PROTO)
9735/*
9736 * Unix style wildcard expansion code.
9737 * It's here because it's used both for Unix and Mac.
9738 */
9739static int pstrcmp __ARGS((const void *, const void *));
9740
9741 static int
9742pstrcmp(a, b)
9743 const void *a, *b;
9744{
9745 return (pathcmp(*(char **)a, *(char **)b, -1));
9746}
9747
9748/*
9749 * Recursively expand one path component into all matching files and/or
9750 * directories. Adds matches to "gap". Handles "*", "?", "[a-z]", "**", etc.
9751 * "path" has backslashes before chars that are not to be expanded, starting
9752 * at "path + wildoff".
9753 * Return the number of matches found.
9754 * NOTE: much of this is identical to dos_expandpath(), keep in sync!
9755 */
9756 int
9757unix_expandpath(gap, path, wildoff, flags, didstar)
9758 garray_T *gap;
9759 char_u *path;
9760 int wildoff;
9761 int flags; /* EW_* flags */
9762 int didstar; /* expanded "**" once already */
9763{
9764 char_u *buf;
9765 char_u *path_end;
9766 char_u *p, *s, *e;
9767 int start_len = gap->ga_len;
9768 char_u *pat;
9769 regmatch_T regmatch;
9770 int starts_with_dot;
9771 int matches;
9772 int len;
9773 int starstar = FALSE;
9774 static int stardepth = 0; /* depth for "**" expansion */
9775
9776 DIR *dirp;
9777 struct dirent *dp;
9778
9779 /* Expanding "**" may take a long time, check for CTRL-C. */
9780 if (stardepth > 0)
9781 {
9782 ui_breakcheck();
9783 if (got_int)
9784 return 0;
9785 }
9786
9787 /* make room for file name */
9788 buf = alloc((int)STRLEN(path) + BASENAMELEN + 5);
9789 if (buf == NULL)
9790 return 0;
9791
9792 /*
9793 * Find the first part in the path name that contains a wildcard.
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +02009794 * When EW_ICASE is set every letter is considered to be a wildcard.
Bram Moolenaar231334e2005-07-25 20:46:57 +00009795 * Copy it into "buf", including the preceding characters.
9796 */
9797 p = buf;
9798 s = buf;
9799 e = NULL;
9800 path_end = path;
9801 while (*path_end != NUL)
9802 {
9803 /* May ignore a wildcard that has a backslash before it; it will
9804 * be removed by rem_backslash() or file_pat_to_reg_pat() below. */
9805 if (path_end >= path + wildoff && rem_backslash(path_end))
9806 *p++ = *path_end++;
9807 else if (*path_end == '/')
9808 {
9809 if (e != NULL)
9810 break;
9811 s = p + 1;
9812 }
9813 else if (path_end >= path + wildoff
Bram Moolenaar2d0b92f2012-04-30 21:09:43 +02009814 && (vim_strchr((char_u *)"*?[{~$", *path_end) != NULL
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01009815 || (!p_fic && (flags & EW_ICASE)
9816 && isalpha(PTR2CHAR(path_end)))))
Bram Moolenaar231334e2005-07-25 20:46:57 +00009817 e = p;
9818#ifdef FEAT_MBYTE
9819 if (has_mbyte)
9820 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009821 len = (*mb_ptr2len)(path_end);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009822 STRNCPY(p, path_end, len);
9823 p += len;
9824 path_end += len;
9825 }
9826 else
9827#endif
9828 *p++ = *path_end++;
9829 }
9830 e = p;
9831 *e = NUL;
9832
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009833 /* Now we have one wildcard component between "s" and "e". */
Bram Moolenaar231334e2005-07-25 20:46:57 +00009834 /* Remove backslashes between "wildoff" and the start of the wildcard
9835 * component. */
9836 for (p = buf + wildoff; p < s; ++p)
9837 if (rem_backslash(p))
9838 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009839 STRMOVE(p, p + 1);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009840 --e;
9841 --s;
9842 }
9843
9844 /* Check for "**" between "s" and "e". */
9845 for (p = s; p < e; ++p)
9846 if (p[0] == '*' && p[1] == '*')
9847 starstar = TRUE;
9848
9849 /* convert the file pattern to a regexp pattern */
9850 starts_with_dot = (*s == '.');
9851 pat = file_pat_to_reg_pat(s, e, NULL, FALSE);
9852 if (pat == NULL)
9853 {
9854 vim_free(buf);
9855 return 0;
9856 }
9857
9858 /* compile the regexp into a program */
Bram Moolenaar94950a92010-12-02 16:01:29 +01009859 if (flags & EW_ICASE)
9860 regmatch.rm_ic = TRUE; /* 'wildignorecase' set */
9861 else
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01009862 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009863 if (flags & (EW_NOERROR | EW_NOTWILD))
9864 ++emsg_silent;
Bram Moolenaar231334e2005-07-25 20:46:57 +00009865 regmatch.regprog = vim_regcomp(pat, RE_MAGIC);
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 vim_free(pat);
9869
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009870 if (regmatch.regprog == NULL && (flags & EW_NOTWILD) == 0)
Bram Moolenaar231334e2005-07-25 20:46:57 +00009871 {
9872 vim_free(buf);
9873 return 0;
9874 }
9875
9876 /* If "**" is by itself, this is the first time we encounter it and more
9877 * is following then find matches without any directory. */
9878 if (!didstar && stardepth < 100 && starstar && e - s == 2
9879 && *path_end == '/')
9880 {
9881 STRCPY(s, path_end + 1);
9882 ++stardepth;
9883 (void)unix_expandpath(gap, buf, (int)(s - buf), flags, TRUE);
9884 --stardepth;
9885 }
9886
9887 /* open the directory for scanning */
9888 *s = NUL;
9889 dirp = opendir(*buf == NUL ? "." : (char *)buf);
9890
9891 /* Find all matching entries */
9892 if (dirp != NULL)
9893 {
9894 for (;;)
9895 {
9896 dp = readdir(dirp);
9897 if (dp == NULL)
9898 break;
9899 if ((dp->d_name[0] != '.' || starts_with_dot)
Bram Moolenaar1f5965b2012-01-10 18:37:58 +01009900 && ((regmatch.regprog != NULL && vim_regexec(&regmatch,
9901 (char_u *)dp->d_name, (colnr_T)0))
Bram Moolenaar0b573a52011-07-27 17:31:47 +02009902 || ((flags & EW_NOTWILD)
9903 && fnamencmp(path + (s - buf), dp->d_name, e - s) == 0)))
Bram Moolenaar231334e2005-07-25 20:46:57 +00009904 {
9905 STRCPY(s, dp->d_name);
9906 len = STRLEN(buf);
9907
9908 if (starstar && stardepth < 100)
9909 {
9910 /* For "**" in the pattern first go deeper in the tree to
9911 * find matches. */
9912 STRCPY(buf + len, "/**");
9913 STRCPY(buf + len + 3, path_end);
9914 ++stardepth;
9915 (void)unix_expandpath(gap, buf, len + 1, flags, TRUE);
9916 --stardepth;
9917 }
9918
9919 STRCPY(buf + len, path_end);
9920 if (mch_has_exp_wildcard(path_end)) /* handle more wildcards */
9921 {
9922 /* need to expand another component of the path */
9923 /* remove backslashes for the remaining components only */
9924 (void)unix_expandpath(gap, buf, len + 1, flags, FALSE);
9925 }
9926 else
9927 {
9928 /* no more wildcards, check if there is a match */
9929 /* remove backslashes for the remaining components only */
9930 if (*path_end != NUL)
9931 backslash_halve(buf + len + 1);
9932 if (mch_getperm(buf) >= 0) /* add existing file */
9933 {
Bram Moolenaar95e9b492006-03-15 23:04:43 +00009934#ifdef MACOS_CONVERT
Bram Moolenaar231334e2005-07-25 20:46:57 +00009935 size_t precomp_len = STRLEN(buf)+1;
9936 char_u *precomp_buf =
9937 mac_precompose_path(buf, precomp_len, &precomp_len);
Bram Moolenaar95e9b492006-03-15 23:04:43 +00009938
Bram Moolenaar231334e2005-07-25 20:46:57 +00009939 if (precomp_buf)
9940 {
9941 mch_memmove(buf, precomp_buf, precomp_len);
9942 vim_free(precomp_buf);
9943 }
9944#endif
9945 addfile(gap, buf, flags);
9946 }
9947 }
9948 }
9949 }
9950
9951 closedir(dirp);
9952 }
9953
9954 vim_free(buf);
Bram Moolenaar473de612013-06-08 18:19:48 +02009955 vim_regfree(regmatch.regprog);
Bram Moolenaar231334e2005-07-25 20:46:57 +00009956
9957 matches = gap->ga_len - start_len;
9958 if (matches > 0)
9959 qsort(((char_u **)gap->ga_data) + start_len, matches,
9960 sizeof(char_u *), pstrcmp);
9961 return matches;
9962}
9963#endif
9964
Bram Moolenaarcc448b32010-07-14 16:52:17 +02009965#if defined(FEAT_SEARCHPATH)
9966static int find_previous_pathsep __ARGS((char_u *path, char_u **psep));
9967static int is_unique __ARGS((char_u *maybe_unique, garray_T *gap, int i));
Bram Moolenaar162bd912010-07-28 22:29:10 +02009968static void expand_path_option __ARGS((char_u *curdir, garray_T *gap));
9969static char_u *get_path_cutoff __ARGS((char_u *fname, garray_T *gap));
Bram Moolenaarcc448b32010-07-14 16:52:17 +02009970static void uniquefy_paths __ARGS((garray_T *gap, char_u *pattern));
9971static int expand_in_path __ARGS((garray_T *gap, char_u *pattern, int flags));
9972
9973/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +02009974 * Moves "*psep" back to the previous path separator in "path".
9975 * Returns FAIL is "*psep" ends up at the beginning of "path".
Bram Moolenaarcc448b32010-07-14 16:52:17 +02009976 */
9977 static int
9978find_previous_pathsep(path, psep)
9979 char_u *path;
9980 char_u **psep;
9981{
9982 /* skip the current separator */
9983 if (*psep > path && vim_ispathsep(**psep))
Bram Moolenaardc685ab2010-08-13 21:16:49 +02009984 --*psep;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02009985
9986 /* find the previous separator */
Bram Moolenaardc685ab2010-08-13 21:16:49 +02009987 while (*psep > path)
9988 {
9989 if (vim_ispathsep(**psep))
9990 return OK;
9991 mb_ptr_back(path, *psep);
9992 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +02009993
9994 return FAIL;
9995}
9996
9997/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +02009998 * Returns TRUE if "maybe_unique" is unique wrt other_paths in "gap".
9999 * "maybe_unique" is the end portion of "((char_u **)gap->ga_data)[i]".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010000 */
10001 static int
10002is_unique(maybe_unique, gap, i)
10003 char_u *maybe_unique;
10004 garray_T *gap;
10005 int i;
10006{
10007 int j;
10008 int candidate_len;
10009 int other_path_len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010010 char_u **other_paths = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010011 char_u *rival;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010012
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010013 for (j = 0; j < gap->ga_len; j++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010014 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010015 if (j == i)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010016 continue; /* don't compare it with itself */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010017
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010018 candidate_len = (int)STRLEN(maybe_unique);
10019 other_path_len = (int)STRLEN(other_paths[j]);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010020 if (other_path_len < candidate_len)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010021 continue; /* it's different when it's shorter */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010022
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010023 rival = other_paths[j] + other_path_len - candidate_len;
Bram Moolenaarda9836c2010-08-16 21:53:27 +020010024 if (fnamecmp(maybe_unique, rival) == 0
10025 && (rival == other_paths[j] || vim_ispathsep(*(rival - 1))))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010026 return FALSE; /* match */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010027 }
10028
Bram Moolenaar162bd912010-07-28 22:29:10 +020010029 return TRUE; /* no match found */
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010030}
10031
10032/*
Bram Moolenaar1a509df2010-08-01 17:59:57 +020010033 * Split the 'path' option into an array of strings in garray_T. Relative
Bram Moolenaar162bd912010-07-28 22:29:10 +020010034 * paths are expanded to their equivalent fullpath. This includes the "."
10035 * (relative to current buffer directory) and empty path (relative to current
10036 * directory) notations.
10037 *
10038 * TODO: handle upward search (;) and path limiter (**N) notations by
10039 * expanding each into their equivalent path(s).
10040 */
10041 static void
10042expand_path_option(curdir, gap)
10043 char_u *curdir;
10044 garray_T *gap;
10045{
10046 char_u *path_option = *curbuf->b_p_path == NUL
10047 ? p_path : curbuf->b_p_path;
10048 char_u *buf;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010049 char_u *p;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010050 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010051
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010052 if ((buf = alloc((int)MAXPATHL)) == NULL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010053 return;
10054
10055 while (*path_option != NUL)
10056 {
10057 copy_option_part(&path_option, buf, MAXPATHL, " ,");
10058
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010059 if (buf[0] == '.' && (buf[1] == NUL || vim_ispathsep(buf[1])))
Bram Moolenaar162bd912010-07-28 22:29:10 +020010060 {
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010061 /* Relative to current buffer:
10062 * "/path/file" + "." -> "/path/"
10063 * "/path/file" + "./subdir" -> "/path/subdir" */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010064 if (curbuf->b_ffname == NULL)
10065 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010066 p = gettail(curbuf->b_ffname);
10067 len = (int)(p - curbuf->b_ffname);
10068 if (len + (int)STRLEN(buf) >= MAXPATHL)
10069 continue;
10070 if (buf[1] == NUL)
10071 buf[len] = NUL;
10072 else
10073 STRMOVE(buf + len, buf + 2);
10074 mch_memmove(buf, curbuf->b_ffname, len);
10075 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010076 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010077 else if (buf[0] == NUL)
10078 /* relative to current directory */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010079 STRCPY(buf, curdir);
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010080 else if (path_with_url(buf))
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010081 /* URL can't be used here */
Bram Moolenaar84f888a2010-08-05 21:40:16 +020010082 continue;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010083 else if (!mch_isFullName(buf))
10084 {
10085 /* Expand relative path to their full path equivalent */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010086 len = (int)STRLEN(curdir);
10087 if (len + (int)STRLEN(buf) + 3 > MAXPATHL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010088 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010089 STRMOVE(buf + len + 1, buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010090 STRCPY(buf, curdir);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010091 buf[len] = PATHSEP;
Bram Moolenaar57adda12010-08-03 22:11:29 +020010092 simplify_filename(buf);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010093 }
10094
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010095 if (ga_grow(gap, 1) == FAIL)
10096 break;
Bram Moolenaar811fe632013-04-24 17:34:20 +020010097
10098# if defined(MSWIN) || defined(MSDOS)
10099 /* Avoid the path ending in a backslash, it fails when a comma is
10100 * appended. */
Bram Moolenaar4e0d9742013-05-04 03:40:27 +020010101 len = (int)STRLEN(buf);
Bram Moolenaar811fe632013-04-24 17:34:20 +020010102 if (buf[len - 1] == '\\')
10103 buf[len - 1] = '/';
10104# endif
10105
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010106 p = vim_strsave(buf);
10107 if (p == NULL)
10108 break;
10109 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010110 }
10111
10112 vim_free(buf);
10113}
10114
10115/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010116 * Returns a pointer to the file or directory name in "fname" that matches the
10117 * longest path in "ga"p, or NULL if there is no match. For example:
Bram Moolenaar162bd912010-07-28 22:29:10 +020010118 *
10119 * path: /foo/bar/baz
10120 * fname: /foo/bar/baz/quux.txt
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010121 * returns: ^this
Bram Moolenaar162bd912010-07-28 22:29:10 +020010122 */
10123 static char_u *
10124get_path_cutoff(fname, gap)
10125 char_u *fname;
10126 garray_T *gap;
10127{
10128 int i;
10129 int maxlen = 0;
10130 char_u **path_part = (char_u **)gap->ga_data;
10131 char_u *cutoff = NULL;
10132
10133 for (i = 0; i < gap->ga_len; i++)
10134 {
10135 int j = 0;
10136
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010137 while ((fname[j] == path_part[i][j]
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +020010138# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010139 || (vim_ispathsep(fname[j]) && vim_ispathsep(path_part[i][j]))
10140#endif
10141 ) && fname[j] != NUL && path_part[i][j] != NUL)
Bram Moolenaar162bd912010-07-28 22:29:10 +020010142 j++;
10143 if (j > maxlen)
10144 {
10145 maxlen = j;
10146 cutoff = &fname[j];
10147 }
10148 }
10149
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010150 /* skip to the file or directory name */
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010151 if (cutoff != NULL)
Bram Moolenaar31710262010-08-13 13:36:15 +020010152 while (vim_ispathsep(*cutoff))
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010153 mb_ptr_adv(cutoff);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010154
10155 return cutoff;
10156}
10157
10158/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010159 * Sorts, removes duplicates and modifies all the fullpath names in "gap" so
10160 * that they are unique with respect to each other while conserving the part
10161 * that matches the pattern. Beware, this is at least O(n^2) wrt "gap->ga_len".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010162 */
10163 static void
10164uniquefy_paths(gap, pattern)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010165 garray_T *gap;
10166 char_u *pattern;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010167{
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010168 int i;
10169 int len;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010170 char_u **fnames = (char_u **)gap->ga_data;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010171 int sort_again = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010172 char_u *pat;
10173 char_u *file_pattern;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010174 char_u *curdir;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010175 regmatch_T regmatch;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010176 garray_T path_ga;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010177 char_u **in_curdir = NULL;
10178 char_u *short_name;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010179
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010180 remove_duplicates(gap);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010181 ga_init2(&path_ga, (int)sizeof(char_u *), 1);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010182
10183 /*
10184 * We need to prepend a '*' at the beginning of file_pattern so that the
10185 * regex matches anywhere in the path. FIXME: is this valid for all
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010186 * possible patterns?
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010187 */
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010188 len = (int)STRLEN(pattern);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010189 file_pattern = alloc(len + 2);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010190 if (file_pattern == NULL)
10191 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010192 file_pattern[0] = '*';
Bram Moolenaar162bd912010-07-28 22:29:10 +020010193 file_pattern[1] = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010194 STRCAT(file_pattern, pattern);
10195 pat = file_pat_to_reg_pat(file_pattern, NULL, NULL, TRUE);
10196 vim_free(file_pattern);
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010197 if (pat == NULL)
10198 return;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010199
Bram Moolenaarb31e4382010-07-24 16:01:56 +020010200 regmatch.rm_ic = TRUE; /* always ignore case */
10201 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10202 vim_free(pat);
10203 if (regmatch.regprog == NULL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010204 return;
10205
Bram Moolenaar162bd912010-07-28 22:29:10 +020010206 if ((curdir = alloc((int)(MAXPATHL))) == NULL)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010207 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010208 mch_dirname(curdir, MAXPATHL);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010209 expand_path_option(curdir, &path_ga);
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010210
10211 in_curdir = (char_u **)alloc_clear(gap->ga_len * sizeof(char_u *));
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010212 if (in_curdir == NULL)
10213 goto theend;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010214
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010215 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010216 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010217 char_u *path = fnames[i];
10218 int is_in_curdir;
Bram Moolenaar31710262010-08-13 13:36:15 +020010219 char_u *dir_end = gettail_dir(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010220 char_u *pathsep_p;
10221 char_u *path_cutoff;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010222
Bram Moolenaar624c7aa2010-07-16 20:38:52 +020010223 len = (int)STRLEN(path);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010224 is_in_curdir = fnamencmp(curdir, path, dir_end - path) == 0
Bram Moolenaar162bd912010-07-28 22:29:10 +020010225 && curdir[dir_end - path] == NUL;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010226 if (is_in_curdir)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010227 in_curdir[i] = vim_strsave(path);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010228
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010229 /* Shorten the filename while maintaining its uniqueness */
10230 path_cutoff = get_path_cutoff(path, &path_ga);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010231
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010232 /* we start at the end of the path */
10233 pathsep_p = path + len - 1;
10234
10235 while (find_previous_pathsep(path, &pathsep_p))
10236 if (vim_regexec(&regmatch, pathsep_p + 1, (colnr_T)0)
10237 && is_unique(pathsep_p + 1, gap, i)
10238 && path_cutoff != NULL && pathsep_p + 1 >= path_cutoff)
10239 {
10240 sort_again = TRUE;
10241 mch_memmove(path, pathsep_p + 1, STRLEN(pathsep_p));
10242 break;
10243 }
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010244
10245 if (mch_isFullName(path))
10246 {
10247 /*
10248 * Last resort: shorten relative to curdir if possible.
10249 * 'possible' means:
10250 * 1. It is under the current directory.
10251 * 2. The result is actually shorter than the original.
10252 *
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010253 * Before curdir After
10254 * /foo/bar/file.txt /foo/bar ./file.txt
10255 * c:\foo\bar\file.txt c:\foo\bar .\file.txt
10256 * /file.txt / /file.txt
10257 * c:\file.txt c:\ .\file.txt
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010258 */
10259 short_name = shorten_fname(path, curdir);
Bram Moolenaar31710262010-08-13 13:36:15 +020010260 if (short_name != NULL && short_name > path + 1
10261#if defined(MSWIN) || defined(MSDOS)
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010262 /* On windows,
Bram Moolenaar31710262010-08-13 13:36:15 +020010263 * shorten_fname("c:\a\a.txt", "c:\a\b")
Bram Moolenaar31710262010-08-13 13:36:15 +020010264 * returns "\a\a.txt", which is not really the short
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010265 * name, hence: */
Bram Moolenaar31710262010-08-13 13:36:15 +020010266 && !vim_ispathsep(*short_name)
10267#endif
10268 )
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010269 {
10270 STRCPY(path, ".");
10271 add_pathsep(path);
Bram Moolenaarcda000e2010-08-14 13:34:39 +020010272 STRMOVE(path + STRLEN(path), short_name);
Bram Moolenaar9bc040c2010-08-11 22:05:57 +020010273 }
10274 }
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010275 ui_breakcheck();
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010276 }
10277
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010278 /* Shorten filenames in /in/current/directory/{filename} */
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010279 for (i = 0; i < gap->ga_len && !got_int; i++)
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010280 {
10281 char_u *rel_path;
10282 char_u *path = in_curdir[i];
10283
10284 if (path == NULL)
10285 continue;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010286
10287 /* If the {filename} is not unique, change it to ./{filename}.
10288 * Else reduce it to {filename} */
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010289 short_name = shorten_fname(path, curdir);
10290 if (short_name == NULL)
10291 short_name = path;
10292 if (is_unique(short_name, gap, i))
10293 {
10294 STRCPY(fnames[i], short_name);
10295 continue;
10296 }
10297
10298 rel_path = alloc((int)(STRLEN(short_name) + STRLEN(PATHSEPSTR) + 2));
10299 if (rel_path == NULL)
10300 goto theend;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010301 STRCPY(rel_path, ".");
10302 add_pathsep(rel_path);
10303 STRCAT(rel_path, short_name);
10304
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010305 vim_free(fnames[i]);
10306 fnames[i] = rel_path;
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010307 sort_again = TRUE;
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010308 ui_breakcheck();
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010309 }
10310
Bram Moolenaar162bd912010-07-28 22:29:10 +020010311theend:
10312 vim_free(curdir);
Bram Moolenaar0be992e2010-08-12 21:50:51 +020010313 if (in_curdir != NULL)
10314 {
10315 for (i = 0; i < gap->ga_len; i++)
10316 vim_free(in_curdir[i]);
10317 vim_free(in_curdir);
10318 }
Bram Moolenaar162bd912010-07-28 22:29:10 +020010319 ga_clear_strings(&path_ga);
Bram Moolenaar473de612013-06-08 18:19:48 +020010320 vim_regfree(regmatch.regprog);
Bram Moolenaar162bd912010-07-28 22:29:10 +020010321
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010322 if (sort_again)
Bram Moolenaarcb9d45c2010-07-20 18:10:15 +020010323 remove_duplicates(gap);
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010324}
10325
10326/*
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010327 * Calls globpath() with 'path' values for the given pattern and stores the
10328 * result in "gap".
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010329 * Returns the total number of matches.
10330 */
10331 static int
10332expand_in_path(gap, pattern, flags)
10333 garray_T *gap;
10334 char_u *pattern;
10335 int flags; /* EW_* flags */
10336{
Bram Moolenaar162bd912010-07-28 22:29:10 +020010337 char_u *curdir;
10338 garray_T path_ga;
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010339 char_u *files = NULL;
10340 char_u *s; /* start */
10341 char_u *e; /* end */
10342 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
10354 paths = ga_concat_strings(&path_ga);
10355 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 Moolenaar94950a92010-12-02 16:01:29 +010010359 files = globpath(paths, pattern, (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 if (files == NULL)
10362 return 0;
10363
10364 /* Copy each path in files into gap */
10365 s = e = files;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010366 while (*s != NUL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010367 {
Bram Moolenaar162bd912010-07-28 22:29:10 +020010368 while (*e != '\n' && *e != NUL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010369 e++;
Bram Moolenaar162bd912010-07-28 22:29:10 +020010370 if (*e == NUL)
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010371 {
10372 addfile(gap, s, flags);
10373 break;
10374 }
10375 else
10376 {
10377 /* *e is '\n' */
Bram Moolenaar162bd912010-07-28 22:29:10 +020010378 *e = NUL;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010379 addfile(gap, s, flags);
10380 e++;
10381 s = e;
10382 }
10383 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010384 vim_free(files);
10385
Bram Moolenaarbdc975c2010-08-02 21:33:37 +020010386 return gap->ga_len;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010387}
10388#endif
10389
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010390#if defined(FEAT_SEARCHPATH) || defined(FEAT_CMDL_COMPL) || defined(PROTO)
10391/*
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010392 * Sort "gap" and remove duplicate entries. "gap" is expected to contain a
10393 * list of file names in allocated memory.
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010394 */
10395 void
10396remove_duplicates(gap)
10397 garray_T *gap;
10398{
10399 int i;
10400 int j;
10401 char_u **fnames = (char_u **)gap->ga_data;
10402
Bram Moolenaardc685ab2010-08-13 21:16:49 +020010403 sort_strings(fnames, gap->ga_len);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +020010404 for (i = gap->ga_len - 1; i > 0; --i)
10405 if (fnamecmp(fnames[i - 1], fnames[i]) == 0)
10406 {
10407 vim_free(fnames[i]);
10408 for (j = i + 1; j < gap->ga_len; ++j)
10409 fnames[j - 1] = fnames[j];
10410 --gap->ga_len;
10411 }
10412}
10413#endif
10414
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010415static int has_env_var __ARGS((char_u *p));
10416
10417/*
10418 * Return TRUE if "p" contains what looks like an environment variable.
10419 * Allowing for escaping.
10420 */
10421 static int
10422has_env_var(p)
10423 char_u *p;
10424{
10425 for ( ; *p; mb_ptr_adv(p))
10426 {
10427 if (*p == '\\' && p[1] != NUL)
10428 ++p;
10429 else if (vim_strchr((char_u *)
10430#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
10431 "$%"
10432#else
10433 "$"
10434#endif
10435 , *p) != NULL)
10436 return TRUE;
10437 }
10438 return FALSE;
10439}
10440
10441#ifdef SPECIAL_WILDCHAR
10442static int has_special_wildchar __ARGS((char_u *p));
10443
10444/*
10445 * Return TRUE if "p" contains a special wildcard character.
10446 * Allowing for escaping.
10447 */
10448 static int
10449has_special_wildchar(p)
10450 char_u *p;
10451{
10452 for ( ; *p; mb_ptr_adv(p))
10453 {
10454 if (*p == '\\' && p[1] != NUL)
10455 ++p;
10456 else if (vim_strchr((char_u *)SPECIAL_WILDCHAR, *p) != NULL)
10457 return TRUE;
10458 }
10459 return FALSE;
10460}
10461#endif
10462
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463/*
10464 * Generic wildcard expansion code.
10465 *
10466 * Characters in "pat" that should not be expanded must be preceded with a
10467 * backslash. E.g., "/path\ with\ spaces/my\*star*"
10468 *
10469 * Return FAIL when no single file was found. In this case "num_file" is not
10470 * set, and "file" may contain an error message.
10471 * Return OK when some files found. "num_file" is set to the number of
10472 * matches, "file" to the array of matches. Call FreeWild() later.
10473 */
10474 int
10475gen_expand_wildcards(num_pat, pat, num_file, file, flags)
10476 int num_pat; /* number of input patterns */
10477 char_u **pat; /* array of input patterns */
10478 int *num_file; /* resulting number of files */
10479 char_u ***file; /* array of resulting files */
10480 int flags; /* EW_* flags */
10481{
10482 int i;
10483 garray_T ga;
10484 char_u *p;
10485 static int recursive = FALSE;
10486 int add_pat;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010487#if defined(FEAT_SEARCHPATH)
10488 int did_expand_in_path = FALSE;
10489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490
10491 /*
10492 * expand_env() is called to expand things like "~user". If this fails,
10493 * it calls ExpandOne(), which brings us back here. In this case, always
10494 * call the machine specific expansion function, if possible. Otherwise,
10495 * return FAIL.
10496 */
10497 if (recursive)
10498#ifdef SPECIAL_WILDCHAR
10499 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10500#else
10501 return FAIL;
10502#endif
10503
10504#ifdef SPECIAL_WILDCHAR
10505 /*
10506 * If there are any special wildcard characters which we cannot handle
10507 * here, call machine specific function for all the expansion. This
10508 * avoids starting the shell for each argument separately.
10509 * For `=expr` do use the internal function.
10510 */
10511 for (i = 0; i < num_pat; i++)
10512 {
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010513 if (has_special_wildchar(pat[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514# ifdef VIM_BACKTICK
10515 && !(vim_backtick(pat[i]) && pat[i][1] == '=')
10516# endif
10517 )
10518 return mch_expand_wildcards(num_pat, pat, num_file, file, flags);
10519 }
10520#endif
10521
10522 recursive = TRUE;
10523
10524 /*
10525 * The matching file names are stored in a growarray. Init it empty.
10526 */
10527 ga_init2(&ga, (int)sizeof(char_u *), 30);
10528
10529 for (i = 0; i < num_pat; ++i)
10530 {
10531 add_pat = -1;
10532 p = pat[i];
10533
10534#ifdef VIM_BACKTICK
10535 if (vim_backtick(p))
10536 add_pat = expand_backtick(&ga, p, flags);
10537 else
10538#endif
10539 {
10540 /*
10541 * First expand environment variables, "~/" and "~user/".
10542 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010543 if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 {
Bram Moolenaar9f0545d2007-09-26 20:36:32 +000010545 p = expand_env_save_opt(p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546 if (p == NULL)
10547 p = pat[i];
10548#ifdef UNIX
10549 /*
10550 * On Unix, if expand_env() can't expand an environment
10551 * variable, use the shell to do that. Discard previously
10552 * found file names and start all over again.
10553 */
Bram Moolenaarf4e11432013-07-03 16:53:03 +020010554 else if (has_env_var(p) || *p == '~')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555 {
10556 vim_free(p);
Bram Moolenaar782027e2009-06-24 14:25:49 +000010557 ga_clear_strings(&ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558 i = mch_expand_wildcards(num_pat, pat, num_file, file,
10559 flags);
10560 recursive = FALSE;
10561 return i;
10562 }
10563#endif
10564 }
10565
10566 /*
10567 * If there are wildcards: Expand file names and add each match to
10568 * the list. If there is no match, and EW_NOTFOUND is given, add
10569 * the pattern.
10570 * If there are no wildcards: Add the file name if it exists or
10571 * when EW_NOTFOUND is given.
10572 */
10573 if (mch_has_exp_wildcard(p))
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010574 {
10575#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010576 if ((flags & EW_PATH)
10577 && !mch_isFullName(p)
10578 && !(p[0] == '.'
10579 && (vim_ispathsep(p[1])
10580 || (p[1] == '.' && vim_ispathsep(p[2]))))
10581 )
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010582 {
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010583 /* :find completion where 'path' is used.
10584 * Recursiveness is OK here. */
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010585 recursive = FALSE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010586 add_pat = expand_in_path(&ga, p, flags);
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010587 recursive = TRUE;
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010588 did_expand_in_path = TRUE;
Bram Moolenaar80a7dcf2010-08-04 17:07:20 +020010589 }
Bram Moolenaarcc448b32010-07-14 16:52:17 +020010590 else
10591#endif
10592 add_pat = mch_expandpath(&ga, p, flags);
10593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594 }
10595
10596 if (add_pat == -1 || (add_pat == 0 && (flags & EW_NOTFOUND)))
10597 {
10598 char_u *t = backslash_halve_save(p);
10599
10600#if defined(MACOS_CLASSIC)
10601 slash_to_colon(t);
10602#endif
10603 /* When EW_NOTFOUND is used, always add files and dirs. Makes
10604 * "vim c:/" work. */
10605 if (flags & EW_NOTFOUND)
10606 addfile(&ga, t, flags | EW_DIR | EW_FILE);
10607 else if (mch_getperm(t) >= 0)
10608 addfile(&ga, t, flags);
10609 vim_free(t);
10610 }
10611
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010612#if defined(FEAT_SEARCHPATH)
Bram Moolenaard732f9a2010-08-15 13:29:11 +020010613 if (did_expand_in_path && ga.ga_len > 0 && (flags & EW_PATH))
Bram Moolenaarb28ebbc2010-07-14 16:59:57 +020010614 uniquefy_paths(&ga, p);
10615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616 if (p != pat[i])
10617 vim_free(p);
10618 }
10619
10620 *num_file = ga.ga_len;
10621 *file = (ga.ga_data != NULL) ? (char_u **)ga.ga_data : (char_u **)"";
10622
10623 recursive = FALSE;
10624
10625 return (ga.ga_data != NULL) ? OK : FAIL;
10626}
10627
10628# ifdef VIM_BACKTICK
10629
10630/*
10631 * Return TRUE if we can expand this backtick thing here.
10632 */
10633 static int
10634vim_backtick(p)
10635 char_u *p;
10636{
10637 return (*p == '`' && *(p + 1) != NUL && *(p + STRLEN(p) - 1) == '`');
10638}
10639
10640/*
10641 * Expand an item in `backticks` by executing it as a command.
10642 * Currently only works when pat[] starts and ends with a `.
10643 * Returns number of file names found.
10644 */
10645 static int
10646expand_backtick(gap, pat, flags)
10647 garray_T *gap;
10648 char_u *pat;
10649 int flags; /* EW_* flags */
10650{
10651 char_u *p;
10652 char_u *cmd;
10653 char_u *buffer;
10654 int cnt = 0;
10655 int i;
10656
10657 /* Create the command: lop off the backticks. */
10658 cmd = vim_strnsave(pat + 1, (int)STRLEN(pat) - 2);
10659 if (cmd == NULL)
10660 return 0;
10661
10662#ifdef FEAT_EVAL
10663 if (*cmd == '=') /* `={expr}`: Expand expression */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000010664 buffer = eval_to_string(cmd + 1, &p, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665 else
10666#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010667 buffer = get_cmd_output(cmd, NULL,
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010668 (flags & EW_SILENT) ? SHELL_SILENT : 0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669 vim_free(cmd);
10670 if (buffer == NULL)
10671 return 0;
10672
10673 cmd = buffer;
10674 while (*cmd != NUL)
10675 {
10676 cmd = skipwhite(cmd); /* skip over white space */
10677 p = cmd;
10678 while (*p != NUL && *p != '\r' && *p != '\n') /* skip over entry */
10679 ++p;
10680 /* add an entry if it is not empty */
10681 if (p > cmd)
10682 {
10683 i = *p;
10684 *p = NUL;
10685 addfile(gap, cmd, flags);
10686 *p = i;
10687 ++cnt;
10688 }
10689 cmd = p;
10690 while (*cmd != NUL && (*cmd == '\r' || *cmd == '\n'))
10691 ++cmd;
10692 }
10693
10694 vim_free(buffer);
10695 return cnt;
10696}
10697# endif /* VIM_BACKTICK */
10698
10699/*
10700 * Add a file to a file list. Accepted flags:
10701 * EW_DIR add directories
10702 * EW_FILE add files
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010703 * EW_EXEC add executable files
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704 * EW_NOTFOUND add even when it doesn't exist
10705 * EW_ADDSLASH add slash after directory name
10706 */
10707 void
10708addfile(gap, f, flags)
10709 garray_T *gap;
10710 char_u *f; /* filename */
10711 int flags;
10712{
10713 char_u *p;
10714 int isdir;
10715
10716 /* if the file/dir doesn't exist, may not add it */
10717 if (!(flags & EW_NOTFOUND) && mch_getperm(f) < 0)
10718 return;
10719
10720#ifdef FNAME_ILLEGAL
10721 /* if the file/dir contains illegal characters, don't add it */
10722 if (vim_strpbrk(f, (char_u *)FNAME_ILLEGAL) != NULL)
10723 return;
10724#endif
10725
10726 isdir = mch_isdir(f);
10727 if ((isdir && !(flags & EW_DIR)) || (!isdir && !(flags & EW_FILE)))
10728 return;
10729
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010730 /* If the file isn't executable, may not add it. Do accept directories. */
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010731 if (!isdir && (flags & EW_EXEC) && !mch_can_exe(f, NULL))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010732 return;
10733
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734 /* Make room for another item in the file list. */
10735 if (ga_grow(gap, 1) == FAIL)
10736 return;
10737
10738 p = alloc((unsigned)(STRLEN(f) + 1 + isdir));
10739 if (p == NULL)
10740 return;
10741
10742 STRCPY(p, f);
10743#ifdef BACKSLASH_IN_FILENAME
10744 slash_adjust(p);
10745#endif
10746 /*
10747 * Append a slash or backslash after directory names if none is present.
10748 */
10749#ifndef DONT_ADD_PATHSEP_TO_DIR
10750 if (isdir && (flags & EW_ADDSLASH))
10751 add_pathsep(p);
10752#endif
10753 ((char_u **)gap->ga_data)[gap->ga_len++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754}
10755#endif /* !NO_EXPANDPATH */
10756
10757#if defined(VIM_BACKTICK) || defined(FEAT_EVAL) || defined(PROTO)
10758
10759#ifndef SEEK_SET
10760# define SEEK_SET 0
10761#endif
10762#ifndef SEEK_END
10763# define SEEK_END 2
10764#endif
10765
10766/*
10767 * Get the stdout of an external command.
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010768 * If "ret_len" is NULL replace NUL characters with NL. When "ret_len" is not
10769 * NULL store the length there.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770 * Returns an allocated string, or NULL for error.
10771 */
10772 char_u *
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010773get_cmd_output(cmd, infile, flags, ret_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774 char_u *cmd;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010775 char_u *infile; /* optional input file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 int flags; /* can be SHELL_SILENT */
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010777 int *ret_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778{
10779 char_u *tempname;
10780 char_u *command;
10781 char_u *buffer = NULL;
10782 int len;
10783 int i = 0;
10784 FILE *fd;
10785
10786 if (check_restricted() || check_secure())
10787 return NULL;
10788
10789 /* get a name for the temp file */
10790 if ((tempname = vim_tempname('o')) == NULL)
10791 {
10792 EMSG(_(e_notmp));
10793 return NULL;
10794 }
10795
10796 /* Add the redirection stuff */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010797 command = make_filter_cmd(cmd, infile, tempname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798 if (command == NULL)
10799 goto done;
10800
10801 /*
10802 * Call the shell to execute the command (errors are ignored).
10803 * Don't check timestamps here.
10804 */
10805 ++no_check_timestamps;
10806 call_shell(command, SHELL_DOOUT | SHELL_EXPAND | flags);
10807 --no_check_timestamps;
10808
10809 vim_free(command);
10810
10811 /*
10812 * read the names from the file into memory
10813 */
10814# ifdef VMS
Bram Moolenaar25394022007-05-10 19:06:20 +000010815 /* created temporary file is not always readable as binary */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816 fd = mch_fopen((char *)tempname, "r");
10817# else
10818 fd = mch_fopen((char *)tempname, READBIN);
10819# endif
10820
10821 if (fd == NULL)
10822 {
10823 EMSG2(_(e_notopen), tempname);
10824 goto done;
10825 }
10826
10827 fseek(fd, 0L, SEEK_END);
10828 len = ftell(fd); /* get size of temp file */
10829 fseek(fd, 0L, SEEK_SET);
10830
10831 buffer = alloc(len + 1);
10832 if (buffer != NULL)
10833 i = (int)fread((char *)buffer, (size_t)1, (size_t)len, fd);
10834 fclose(fd);
10835 mch_remove(tempname);
10836 if (buffer == NULL)
10837 goto done;
10838#ifdef VMS
10839 len = i; /* VMS doesn't give us what we asked for... */
10840#endif
10841 if (i != len)
10842 {
10843 EMSG2(_(e_notread), tempname);
10844 vim_free(buffer);
10845 buffer = NULL;
10846 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010847 else if (ret_len == NULL)
Bram Moolenaarfb332a22013-08-03 14:10:50 +020010848 {
10849 /* Change NUL into SOH, otherwise the string is truncated. */
10850 for (i = 0; i < len; ++i)
Bram Moolenaarf40f4ab2013-08-03 17:31:28 +020010851 if (buffer[i] == NUL)
10852 buffer[i] = 1;
Bram Moolenaarfb332a22013-08-03 14:10:50 +020010853
Bram Moolenaar162bd912010-07-28 22:29:10 +020010854 buffer[len] = NUL; /* make sure the buffer is terminated */
Bram Moolenaarfb332a22013-08-03 14:10:50 +020010855 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020010856 else
10857 *ret_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858
10859done:
10860 vim_free(tempname);
10861 return buffer;
10862}
10863#endif
10864
10865/*
10866 * Free the list of files returned by expand_wildcards() or other expansion
10867 * functions.
10868 */
10869 void
10870FreeWild(count, files)
10871 int count;
10872 char_u **files;
10873{
Bram Moolenaarfc1421e2006-04-20 22:17:20 +000010874 if (count <= 0 || files == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875 return;
10876#if defined(__EMX__) && defined(__ALWAYS_HAS_TRAILING_NULL_POINTER) /* XXX */
10877 /*
10878 * Is this still OK for when other functions than expand_wildcards() have
10879 * been used???
10880 */
10881 _fnexplodefree((char **)files);
10882#else
10883 while (count--)
10884 vim_free(files[count]);
10885 vim_free(files);
10886#endif
10887}
10888
10889/*
Bram Moolenaara9dc3752010-07-11 20:46:53 +020010890 * Return TRUE when need to go to Insert mode because of 'insertmode'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891 * Don't do this when still processing a command or a mapping.
10892 * Don't do this when inside a ":normal" command.
10893 */
10894 int
10895goto_im()
10896{
10897 return (p_im && stuff_empty() && typebuf_typed());
10898}
Bram Moolenaar75a8d742014-05-07 15:10:21 +020010899
10900/*
10901 * Returns the isolated name of the shell:
10902 * - Skip beyond any path. E.g., "/usr/bin/csh -f" -> "csh -f".
10903 * - Remove any argument. E.g., "csh -f" -> "csh".
10904 * But don't allow a space in the path, so that this works:
10905 * "/usr/bin/csh --rcfile ~/.cshrc"
10906 * But don't do that for Windows, it's common to have a space in the path.
10907 */
10908 char_u *
10909get_isolated_shell_name()
10910{
10911 char_u *p;
10912
10913#ifdef WIN3264
10914 p = gettail(p_sh);
10915 p = vim_strnsave(p, (int)(skiptowhite(p) - p));
10916#else
10917 p = skiptowhite(p_sh);
10918 if (*p == NUL)
10919 {
10920 /* No white space, use the tail. */
10921 p = vim_strsave(gettail(p_sh));
10922 }
10923 else
10924 {
10925 char_u *p1, *p2;
10926
10927 /* Find the last path separator before the space. */
10928 p1 = p_sh;
10929 for (p2 = p_sh; p2 < p; mb_ptr_adv(p2))
10930 if (vim_ispathsep(*p2))
10931 p1 = p2 + 1;
10932 p = vim_strnsave(p1, (int)(p - p1));
10933 }
10934#endif
10935 return p;
10936}